pyorg.util module¶
Misc. utility code.
-
class
pyorg.util.SingleDispatch(default, registry=None, doc=None)[source]¶ Bases:
pyorg.util.SingleDispatchBaseGeneric function which dispatches on the type of its first argument.
-
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.ABCABC 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()oriter_keys()method.-
default¶ Default implementation.
Type: callable
-
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 –
-
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
methodis 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.
-