signoffs.signoffs.utils#
Core objects documented here are imported from the public API in singoffs.signoffs.utils
E.g.
from signoffs.signoffs.utils import service
signoffs.core.utils#
Utility functions and classes
Module Contents#
Classes#
A string describing a path from one object to another via attributes accesses. |
|
A descriptor used to âinjectâ instances of a âserviceâ class into its ownerâs instances. |
|
A descriptor used to âinjectâ instances of a âserviceâ class onto its owner class. |
Functions#
Dynamically import the given object (class, function, constant, etc.) from the given module_path If obj_name is None, the last dotted item in the module_path will be imported |
|
Factory to return specialized service descriptors. |
|
Factory to return specialized class service descriptors. |
API#
- signoffs.core.utils.dynamic_import(abs_module_path, obj_name=None)[source]#
Dynamically import the given object (class, function, constant, etc.) from the given module_path If obj_name is None, the last dotted item in the module_path will be imported
- class signoffs.core.utils.Accessor[source]#
Bases:
strA string describing a path from one object to another via attributes accesses.
Relations are separated by a
__character.Shamelessly adapted from the amazing django_tables2 https://pypi.org/project/django-tables2/
Initialization
Initialize self. See help(type(self)) for accurate signature.
- SEPARATOR#
â__â
- ALTERS_DATA_ERROR_FMT#
âRefusing to call {method}() because
.alters_data = Trueâ
- LOOKUP_ERROR_FMT#
âFailed lookup for attribute [{attr}] in {obj}, when resolving the accessor
{accessor}â
- resolve(obj, safe=True, quiet=False)[source]#
Return an attribute described by the accessor by traversing the attributes of object
Callable objects are called, and their result is used, before proceeding with the resolving. Usage:
>>> from django.contrib.auth.models import User >>> user = User(first_name='Brad') >>> x = Accessor("user__first_name") >>> x.resolve(user) "Brad"
- Parameters:
- Returns:
resolved target object
- Raises:
TypeError, AttributeError, KeyError, ValueError â (unless
quiet==True)
- property bits#
- signoffs.core.utils.service(service_class, **kwargs)[source]#
Factory to return specialized service descriptors.
- Returns:
a
ServiceDescriptorfor a specialized subclass ofservice_class, that haskwargsas class attributes
Usage:
>>> class Service: ... service_type = 'generic' ... def __init__(self, owner, extra=None): ... self.owner = owner ... self.extra = extra ... def __str__(self): ... extra = f' with {self.extra}' if self.extra else '' ... return f'A {self.service_type} service for {self.owner}{extra}' ... class Owner: ... a_service = service(Service, service_type='special')(extra="whazoo") ... def __str__(self): ... return 'Owner' ... o = Owner() ... assert str(o.a_service) == "A special service for Owner with whazoo"
- class signoffs.core.utils.ServiceDescriptor(service_class=None, **kwargs)[source]#
A descriptor used to âinjectâ instances of a âserviceâ class into its ownerâs instances.
A âserviceâ provides services or strategies to its owner, but needs the owner instance to do its own work. Construction of the owner instance may not be under direct control, so service instantiation must be automated. Service class must expect owner instance as first positional parameter of its constructor.
Initialization
Inject
service_classinstance into the instance of the descriptorâs owner classfirst positional arg for
service_classinitializer must be an instance of owner classkwargsare passed through to theservice_classinitializer- service_class#
None
- signoffs.core.utils.class_service(service_class, **kwargs)[source]#
Factory to return specialized class service descriptors.
- Returns:
a
ClassServiceDescriptorfor a specialized subclass ofservice_class, that haskwargsas class attributes
- class signoffs.core.utils.ClassServiceDescriptor(service_class=None, **kwargs)[source]#
A descriptor used to âinjectâ instances of a âserviceâ class onto its owner class.
This is analogous to
ServiceDescriptorbut service instance is available on owner class First positional parameter ofservice_classclass must be an owner class (type not instance!)Initialization
Inject
service_classinstance, initialized with owner class, into the descriptorâs owner classfirst positional arg for
service_classinitializer must be an owner class typekwargsare passed through to theservice_classinitializer- service_class#
None