drf_braces.utils module

drf_braces.utils.add_base_class_to_instance(instance, base_class=None, new_name=None)[source]

Generic utility for adding a base class to an instance.

This function returns a copy of the given instance which will then include the new base_class in its __mro__.

The way that is done internally is it creates a brand new class with correct bases. Then the newly created class is instantiated. Since __init__ could be expensive operation in any of the base classes of the original instance mro, nto make it cheap, we temporarily switch __init__ with super simple implementation which does nothing but only instantiates class. Once instantiated, then we copy all of the instance attributes to the newly created instance. Finally, then we pop our mock __init__ implementation.

Parameters:
  • instance (object) – Instance of any object
  • base_class (type) – Any class which will be added as first class in the newly copied instance mro.
Returns:

Shallow copy of instance which will also inherit base_class.

drf_braces.utils.find_class_args(klass)[source]

Find all class arguments (parameters) which can be passed in __init__.

drf_braces.utils.find_function_args(func)[source]

Get the list of parameter names which function accepts.

drf_braces.utils.find_matching_class_kwargs(reference_object, klass)[source]
drf_braces.utils.get_attr_from_base_classes(bases, attrs, attr, default=None)[source]

The attribute is retrieved from the base classes if they are not already present on the object.

Parameters:
  • bases (tuple, list) – The base classes for a class.
  • attrs (dict) – The attributes of the class.
  • attr (str) – Specific attribute being looked for.
  • default (any) – Whatever default value is expected if the attr is not found.
Returns:

attribute value as found in base classes or a default when attribute is not found and default is provided.

Raises:

AttributeError – When the attribute is not present anywhere in the call chain hierarchy specified through bases and the attributes of the class itself

drf_braces.utils.get_class_name_with_new_suffix(klass, existing_suffix, new_suffix)[source]

Generates new name by replacing the existing suffix with a new one.

Parameters:
  • klass (type) – original class from which new name is generated
  • existing_suffix (str) – the suffix which needs to remain where it is
  • new_suffix (str) – the new suffix desired

Example

>>> get_class_name_with_new_suffix(FooForm, 'Form', 'NewForm')
'FooNewForm'
Returns:the name with the new suffix
Return type:new_name (str)
drf_braces.utils.initialize_class_using_reference_object(reference_object, klass, **kwargs)[source]

Utility function which instantiates klass by extracting __init__ kwargs from reference_object attributes.

Parameters:
  • reference_object (object) – Any object instance from which matching attributes will be used as klass’s __init__ kwargs.
  • klass (type) – Class which will be instantiated by using reference_object attributes.
  • **kwargs – Any additional kwargs which will be passed during instantiation.
Returns:

Instantiated klass object.

drf_braces.utils.reduce_attr_dict_from_base_classes(bases, getter, default=None)[source]
drf_braces.utils.reduce_attr_dict_from_instance(self, getter, default=None)[source]