pyorg.util module

Misc. utility code.

class pyorg.util.Namespace(_map=None, **kwargs)[source]

Bases: object

A simple collection of attribute values, that supports inheritance.

Meant to be used to pass large sets of arguments down through recursive function calls in a way that they can be overridden easily.

Public attributes and methods start with an underscore so as not to interfere with the namespace.

_map

Stores the underlying data.

Type:collections.ChainMap
class pyorg.util.SingleDispatch(default, registry=None, doc=None)[source]

Bases: pyorg.util.SingleDispatchBase

Generic function which dispatches on the type of its first argument.

iter_keys(arg)[source]
validate_key(key)[source]

Validate and possibly replace a key before an implementation is registered under it.

Default implementation simply returns the argument. Subclasses may wish to override this. An error should be raised for invalid keys.

Parameters:key – Key passed to register().
Returns:
Return type:Key to use for registration, which may be different than argument.
class pyorg.util.SingleDispatchBase(default, registry=None, doc=None)[source]

Bases: abc.ABC

ABC for a generic function which dispatches on some trait of its first argument.

May be bound to an object or class as a method.

Concrete subclasses must implement one of the get_key() or iter_keys() method.

default

Default implementation.

Type:callable
registry

Stores the specialized implementation functions by key.

Type:dict
bind(instance, owner=None)[source]

Get a version of the function bound to the given instance as a method.

Parameters:
  • instance – Object instance to bind to.
  • owner
copy()[source]
dispatch(arg)[source]

Get the actual function implementation for the given argument.

get_key(arg)[source]

Get the key to look up the correct implementation for the given argument.

iter_keys(arg)[source]
register(key, impl=None)[source]

Register an implementation for the given key.

Parameters:
  • key – Key to register method under. May also be a list of keys.
  • impl (callable) – Implementation to register under the given key(s). If None will return a decorator function that completes the registration.
Returns:

None if method is given. Otherwise returns a decorator that will register the function it is applied to.

Return type:

function or None

validate_key(key)[source]

Validate and possibly replace a key before an implementation is registered under it.

Default implementation simply returns the argument. Subclasses may wish to override this. An error should be raised for invalid keys.

Parameters:key – Key passed to register().
Returns:
Return type:Key to use for registration, which may be different than argument.
class pyorg.util.SingleDispatchMethod(func, instance, owner=None)[source]

Bases: object

Version of a SingleDispatchBase which acts as a method.

func
Type:SingleDispatchBase
instance

Instance the function is bound to, or None.

owner
default
dispatch(arg)[source]
class pyorg.util.TreeNamespace(_map=None, _path=(), **kwargs)[source]

Bases: pyorg.util.Namespace

Namespace with a _path attribute that marks its location in a tree structure.

_path
Type:tuple
pyorg.util.parse_iso_date(string)[source]

Parse date or datetime from an ISO 8601 date string.

Parameters:string
Returns:Return time varies based on whether the string includes a time component.
Return type:datetime.date or datetime.datetime