I/O & System Utilities
Input/output operations and system interaction utilities.
iolib
I/O operations: CSV rendering, zipped CSV handling, iterable-to-stream conversion, JSON byteification, print suppression.
- render_csv(rows, dialect=<class 'csv.excel'>)[source]
Render rows as CSV string.
- Parameters:
rows – Iterable of rows to render.
dialect – CSV dialect to use (default: csv.excel).
- Returns:
CSV formatted string.
- Return type:
Example:
>>> render_csv([['a', 'b'], ['1', '2']]) 'a,b\r\n1,2\r\n'
- class CsvZip[source]
Bases:
ZipFileZipped CSV file that handles file permissions correctly on DOS.
Example:
>>> cz = CsvZip() >>> cz.writecsv('test', [['a', 'b'], ['1', '2']]) >>> len(cz.value) > 0 True
- property value
- writecsv(filename, data)[source]
- iterable_to_stream(iterable, buffer_size=8192)[source]
Convert an iterable that yields bytestrings to a read-only input stream.
- Parameters:
iterable – Iterable yielding bytestrings.
buffer_size (int) – Buffer size for the stream.
- Returns:
BufferedReader stream.
Example:
>>> stream = iterable_to_stream([b'hello', b' ', b'world']) >>> stream.read() b'hello world'
Note
- stream(func)[source]
Decorator that converts first streamable input param to a stream.
- Parameters:
func – Function to wrap.
- Returns:
Wrapped function.
- json_load_byteified(file_handle)[source]
Parse ASCII encoded JSON from file handle.
- Parameters:
file_handle – File handle to read from.
- Returns:
Parsed JSON data.
- json_loads_byteified(json_text)[source]
Parse ASCII encoded JSON from text string.
- Parameters:
json_text (str) – JSON text to parse.
- Returns:
Parsed JSON data.
Example:
>>> json_loads_byteified('{"foo": "bar"}') {'foo': 'bar'} >>> json_loads_byteified('{"foo": "bar", "things": [7, {"qux": "baz", "moo": {"cow": ["milk"]}}]}') {'foo': 'bar', 'things': [7, {'qux': 'baz', 'moo': {'cow': ['milk']}}]}
- suppress_print()[source]
Context manager to suppress stdout (print statements).
Useful when third-party code includes unwanted print statements.
Example:
>>> with suppress_print(): ... print("This won't appear")
- wrap_suppress_print(func)[source]
Decorator version of suppress_print context manager.
- Parameters:
func – Function to wrap.
- Returns:
Wrapped function with suppressed stdout.
Example:
>>> @wrap_suppress_print ... def noisy(): ... print("This won't appear") ... return 42 >>> noisy() 42
stream
Stream utilities: YAML/JSON conversion, binary/text handling, checksum calculation, stream decorators.
- is_tty()[source]
Check if running in a terminal.
Returns True if: - Running under pytest (always True for testing) - stdin is a TTY (interactive input) - stderr is a TTY (logger output visible in terminal)
- Returns:
True if in a terminal environment.
- Return type:
proc
Process utilities: finding processes by name/port, killing processes, process management.
- process_by_name(name)[source]
Find processes by name that are listening on a port.
- Parameters:
name (str) – Process name to search for.
- Yields:
psutil.Process objects matching the name with LISTEN connections.
See also
See
tests/test_proc.pyfor usage examples.
- process_by_name_and_port(name, port)[source]
Find a process by name listening on a specific port.
- Parameters:
- Returns:
psutil.Process if found, None otherwise.
See also
See
tests/test_proc.pyfor usage examples.
- kill_proc(name=None, version=None, dry_run=False, use_terminate=False)[source]
Kill processes matching name and/or version.
- Parameters:
- Returns:
True if matching processes were found.
- Return type:
See also
See
tests/test_proc.pyfor usage examples.
signals
Signal handling: DelayedKeyboardInterrupt context manager for
deferring Ctrl+C during critical sections, signal translation map.
- class DelayedKeyboardInterrupt(propagate_to_forked_processes=None)[source]
Bases:
objectContext manager that suppresses SIGINT & SIGTERM during a block.
Signal handlers are called on exit from the block.
Note
Inspired by https://stackoverflow.com/a/21919644
- Parameters:
propagate_to_forked_processes – Controls behavior in forked processes: - True: Same behavior as parent process - False: Use original signal handler - None: Ignore signals (default)
mime
MIME type utilities for guessing file extensions and content types.
- guess_type(url)[source]
Guess mimetype from a URL or filename.
- Parameters:
url (str) – URL or filename to examine.
- Returns:
Guessed mimetype or None.
- Return type:
Example:
>>> guess_type('document.pdf') 'application/pdf' >>> guess_type('image.jpg') 'image/jpeg'