utils package

Module contents

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

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


Enables properties for whole classes:


>>> class Foo(object):
...     @classproperty
...     def bar(cls):
...         return "bar"
>>> print Foo.bar

This turns a dictionary for verpick to a class property.

Useful for verpicked constants.

class utils.kwargify(function)[source]

Bases: object


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


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

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.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


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


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


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.

  • 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

What f returns.


What f raises if the try count is exceeded.