utils package

Module contents

utils.at_exit(f, *args, **kwargs)[source]

Diaper-protected atexit handler registering. Same syntax as atexit.register()

utils.classproperty(f)[source]

Enables properties for whole classes:

Usage:

>>> class Foo(object):
...     @classproperty
...     def bar(cls):
...         return "bar"
...
>>> print Foo.bar
baz
utils.deferred_verpick(version_d)[source]

This turns a dictionary for verpick to a class property.

Useful for verpicked constants.

class utils.kwargify(function)[source]

Bases: object

utils.lazycache(wrapped_method)[source]

method decorator to create a lazily-evaluated and cached property

lazycache‘d properties are complete object descriptors, supporting get, set, and del, though del will clear a property’s cache rather than destroy the property entirely

Usage:

>>> from utils import lazycache
>>> class Example(object):
...     @lazycache
...     def lazyprop(self):
...             return '42'
...
>>> ex = Example()
>>> value = ex.lazyprop
>>> print value
42
>>> print value is ex.lazyprop
# lazyprop guarantees this to be True, normal properties do not.
True
>>> ex.lazyprop = '99'
>>> print ex.lazyprop
# setting works!
99
>>> del(ex.lazyprop)
>>> print ex.lazyprop
# deleting clears the cache, so the value is recomputed on the next call
42

Values are stored in a private attribute of the same name as the method being decorated, e.g. a decorated method named lazyprop will store its cached value in an attr called _lazyprop

utils.normalize_text(text)[source]
utils.property_or_none([fget[, fset[, fdel[, doc]]]])[source]

Property decorator that turns AttributeErrors into None returns

Useful for chained attr lookups where some links in the chain are None

Note

This delegates back to the property builtin and inherits its signature; thus it can be used interchangeably with property.

utils.read_env(file)[source]

Given a py.path.Local file name, return a dict of exported shell vars and their values

Note

This will only include shell variables that are exported from the file being parsed

Returns a dict of varname: value pairs. If the file does not exist or bash could not parse the file, this dict will be empty.

utils.tries(num_tries, exceptions, f, *args, **kwargs)[source]

Tries to call the function multiple times if specific exceptions occur.

Parameters:
  • num_tries – How many times to try if exception is raised
  • exceptions – Tuple (or just single one) of exceptions that should be treated as repeat.
  • f – Callable to be called.
  • *args – Arguments to be passed through to the callable
  • **kwargs – Keyword arguments to be passed through to the callable
Returns:

What f returns.

Raises:

What f raises if the try count is exceeded.