tbm-utils

tbm-utils is a commonly-used set of utilities used by me (thebigmunch).

Getting Started

Install tbm-utils with pip.

$ pip install -U tbm-utils

API Reference

CLI

class tbm_utils.cli.SubcommandHelpFormatter(prog, indent_increment=2, max_help_position=24, width=None)[source]
class tbm_utils.cli.UsageHelpFormatter(prog, indent_increment=2, max_help_position=24, width=None)[source]

Config

Constants

tbm_utils.constants.FILEPATH_CHARACTER_REPLACEMENTS = {'"': "''", '*': 'x', '/': ',', ':': '-', '<': '[', '>': ']', '?': '', '\\': '-', '|': '!'}

Mapping of invalid filepath characters with appropriate replacements.

Type

dict

Datetime

Decorators

tbm_utils.decorators.cast_to_list(wrapped=None, *, position=0)[source]

Cast the positional argument at given position into a list if not already a list.

tbm_utils.decorators.datareader(wrapped=None, *, position=0)[source]

Cast the positional argument at given position to DataReader.

Humanize

IO

class tbm_utils.io.DataReader(data, buffer_size=8192)[source]

A buffered reader wrapper.

It includes support for filepaths, file-like objects, and bytes-like objects.

Parameters
read(size=-1)[source]

Read and return up to n bytes.

If the argument is omitted, None, or negative, reads and returns all data until EOF.

If the argument is positive, and the underlying raw stream is not ‘interactive’, multiple raw reads may be issued to satisfy the byte count (unless EOF is reached first). But for interactive raw streams (as well as sockets and pipes), at most one raw read will be issued, and a short result does not imply that EOF is imminent.

Returns an empty bytes object on EOF.

Returns None if the underlying raw stream was open in non-blocking mode and no data is available at the moment.

seek_first(sub)[source]

Seek to the first index of a subsequence in data.

seek_last(sub)[source]

Seek to the last index of a subsequence in data.

seek_next(sub)[source]

Seek to the next index of a subsequence in data.

class tbm_utils.io.DataWriter(data, buffer_size=8192)[source]

A buffered writer wrapper.

It includes support for filepaths, file-like objects, and bytes-like objects.

Parameters

Misc

Path

tbm_utils.path.UNIX_PATH_RE = re.compile('(/(cygdrive/)?)(.*)')

Regex pattern matching UNIX-style filepaths.

tbm_utils.path.convert_unix_path(filepath)[source]

Convert Unix filepath string from Unix to Windows format.

Parameters

filepath (str, os.PathLike, Path) – A filepath string.

Returns

A Windows path object.

Return type

Path

Raises

Structures

class tbm_utils.structures.LabelList(initlist=None)[source]