craftr.utils

Various common utilities used by Craftr and its extension modules.

Transform Functions

craftr.utils.flatten(iterable)[source]

Given an iterable that in turn yields an iterable, this function flattens the nested iterables into a single iteration.

craftr.utils.uniquify(iterable)[source]

Create a list of items in iterable without duplicate, preserving the order of the elements where it first appeared.

Recordclass

class craftr.utils.recordclass_base(*args, **kwargs)[source]

Base class that provides a namedtuple like interface based on the __slots__ parameter.

class MyRecord(recordclass_base):
  __slots__ = 'foo bar ham'.split()

data = MyRecord('a foo', 42, ham="spam")
items()[source]
keys()[source]
values()[source]
craftr.utils.recordclass(__name, __fields, **defaults)[source]

Creates a new class that can represent a record with the specified fields. This is equal to a mutable namedtuple. The returned class also supports keyword arguments in its constructor.

Parameters:
  • __name – The name of the recordclass.
  • __fields – A string or list of field names.
  • defaults – Default values for fields. The defaults may list field names that haven’t been listed in fields.

Environment Variables

craftr.utils.append_path(pth)[source]

Appends pth to the PATH environment variable.

craftr.utils.prepend_path(pth)[source]

Prepends pth to the PATH environment variable.

craftr.utils.override_environ(new_environ=None)[source]

Context-manager that restores the old environ on exit.

Parameters:new_environ – A dictionary that will update the environ inside the context-manager.

craftr.utils.regex Module

Regex utility functions.

craftr.utils.regex.search_get_groups(pattern, subject, mode=0)[source]

Performs re.search() and returns a list of the captured groups, including the complete matched string as the first group. If the regex search was unsuccessful, a list with that many items containing None is returned.