toolrack.aio

toolrack.certificate

toolrack.collect

toolrack.config

toolrack.convert

Utilities for unit conversion.

toolrack.convert.BYTE_SUFFIXES = (None, 'kib', 'mib', 'gib', 'tib', 'pib', 'eib', 'zib', 'yib')

Binary byte multipliers

toolrack.convert.convert_bbyte(value, suffix=None, to=None)

Convert a binary byte value across multipliers.

Parameters:
  • value (int) – the current value.
  • suffix (str) – the current multiplier for the value (None for bytes).
  • to (str) – the target multiplier (None for bytes).

toolrack.fsmap

Access the filesystem in a dict-like fashion.

This module provides a Directory class which provides access to the filesystem subtree below its path, allow accessing files and sub-directories as elements of a dict (e.g. directory['foo'] or directory['foo/bar']).

toolrack.fsmap.DIR = <object object>

Marker for creating directories.

class toolrack.fsmap.Directory(path)

Provide access to the sub-tree of a directrory.

It represents a directory in the filesystem:

directory = Directory('/base/path')

The object is iterable and yields names of contained elements:

for elem in directory:
    do_something(directory[elem])

Sub-directories and files below the base path can be accessed as items of a dict. For instance:

directory['a-dir']['a-file']

or even with a single access, using OS path format:

directory['a-dir/a-file']

Path elements can be removed with del:

del directory['a-file']
del directory['a-dir']  # this will delete the whole sub-tree

Files are created/overwritten by assiging content:

directory['a-file'] = 'some content'

and directories are created using the DIR marker:

directory['a-new-dir'] = DIR

toolrack.iterate

Utility functions for iterables.

toolrack.iterate.flatten_dict(data, join_char='.', prefix='')

Flatten a nested dict to (key, value) tuples.

A neted dict like:

{'foo': {'bar': 3, 'baz': 4},
 'bza': 'something'}

is flattened in a sequence of tuples like:

('foo.bar': 3), ('foo.baz': 4), ('bza': 'something')
Parameters:
  • data (dict) – a dict to flatten.
  • join_char (str) – the character to use to join key tokens.
  • prefix (str) – an optional prefix to prepend to keys.

toolrack.json_util

Utilities for dealing with JSON data.

toolrack.json_util.indent(in_fd, out_fd, indent=4, ensure_ascii=False)

Indent JSON data.

It reads text in JSON format from in_fd and writes the formatted output to out_fd, using the specified amount of indent spaces.

Parameters:
  • in_fd – input file descriptor.
  • out_fd – output file descriptor.
  • indent (int) – number of spaces used for indentation.
  • ensure_ascii (bool) – passed to the JSON serializer, if specified, non-ASCII characters are escaped.

toolrack.log

Logging helpers.

The Loggable mixin provides a logger attribute with a configured logger using the class name (including the module path) as logger name.

toolrack.log.LOG_FORMAT = '%(asctime)s - %(levelname)s - %(name)s - %(message)s'

Default format for logging to a stream

class toolrack.log.Loggable

Mixin class providing a logger attribute.

logger

Decorator to a class method a cached property.

The property method is called just the first time for an instance, and its result cached.

toolrack.log.setup_logger(name=None, stream=None, level=None, format='%(asctime)s - %(levelname)s - %(name)s - %(message)s')

Helper to setup a logger with a default handler.

Parameters:
  • name (str) – the name of the logger. If not specified, the root logger is used.
  • stream – an output stream for the log handler. If not specified, the null handler is installed.
  • level – the minimum log level for the logger.

toolrack.password

Password generation functions.

PasswordProfile defines a set of characters to use for generating password. It’s creating by passing a string with characters or character definitions encosed in curly braces (such as {alnum}, {num}, {alpha}), which are expanded to the corresponding set of characters.

For instance:

profile = PasswordProfile('{alpha}-_')
profile.generate(length=5)

yields a 5-chars password composed of letters, dashes and underscores.

toolrack.password.DEFAULT_CHARS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'

Default character set: letters, numbers and punctuation

toolrack.password.DEFAULT_LENGTH = 10

Default password length

class toolrack.password.PasswordProfile(definition)

A password profile, specifying how to generate a random password.

CHAR_DEFS = {'alnum': 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', 'alpha': 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 'num': '0123456789', 'punct': '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~', 'space': ' \t\n\r\x0b\x0c'}
chars

Return the set of characters used in generation.

generate(length=10)

Generate a random password.

toolrack.password.generate_password(chars='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!"#$%&\'()*+, -./:;<=>?@[\\]^_`{|}~', length=10)

Generate a random password using the supplied characters.

Parameters:
  • chars (str) – a string with chars to choose from.
  • length (int) – number of chars for the password.

toolrack.path

Functions for paths handling.

toolrack.path.match_files(dirpaths, patterns, ignorecase=False)

Search files by name based on shell patterns.

Parameters:
  • dirpaths (list) – a list of paths to search from.
  • patterns (list) – a list of name patterns to match.
Returns:

an iterator yielding matched files.

toolrack.property

Property decorators.

class toolrack.property.cachedproperty(func)

Decorator to a class method a cached property.

The property method is called just the first time for an instance, and its result cached.

toolrack.script

toolrack.testing

toolrack.testing.fixtures

toolrack.threading

Thread-related utilities.

class toolrack.threading.ThreadLocalAttribute(name)

Descriptor to proxy access to a class attribute, making it thread-local.

This descriptor can be used to make a class attribute thread-local in a trasparent way:

class MyClass:

    attr = ThreadLocalAttribute('attr')


instance = MyClass()

The attribute will be normally accessible as instance.attr, but it’s stored in a threading.local() context.

toolrack.threading.thread_local_attrs(*attrs)

Class decorator to make attributes storage thread-local.

It should be passed names of attributes in the decorated class to make local:

@thread_local_attrs('foo', 'bar')
class MyClass:

    foo = 3
    bar = None

ToolRack

Latest Version Build Status Coverage Status Documentation

A collection of utility functions and classes, and a few scripts too.

Documentation

API docs are available on ReadTheDocs.

Installation

ToolRack can be installed from PyPI.

As a user run:

$ pip install toolrack

Development installation

The source tree is available available at https://github.com/albertodonato/toolrack.

Development setup can be done via Virtualenv.

As a user run:

$ python3 -m venv <target-dir>
$ . <target-dir>/bin/activate
$ git clone https://github.com/albertodonato/toolrack
$ cd toolrack
$ pip install -e .

Indices and tables