add_base_class_to_instance(instance, base_class=None, new_name=None)¶
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
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
- instance (object) – Instance of any object
- base_class (type) – Any class which will be added as first class in the newly copied instance mro.
Shallow copy of
instancewhich will also inherit
Find all class arguments (parameters) which can be passed in
Get the list of parameter names which function accepts.
get_attr_from_base_classes(bases, attrs, attr, default=None)¶
The attribute is retrieved from the base classes if they are not already present on the object.
- 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.
attribute value as found in base classes or a default when attribute is not found and default is provided.
AttributeError– When the attribute is not present anywhere in the call chain hierarchy specified through bases and the attributes of the class itself
get_class_name_with_new_suffix(klass, existing_suffix, new_suffix)¶
Generates new name by replacing the existing suffix with a new one.
- 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
>>> get_class_name_with_new_suffix(FooForm, 'Form', 'NewForm') 'FooNewForm'
Returns: the name with the new suffix Return type: new_name (str)
initialize_class_using_reference_object(reference_object, klass, **kwargs)¶
Utility function which instantiates
- reference_object (object) – Any object instance from which matching
attributes will be used as
- klass (type) – Class which will be instantiated by using
- **kwargs – Any additional kwargs which will be passed during instantiation.
- reference_object (object) – Any object instance from which matching attributes will be used as
reduce_attr_dict_from_base_classes(bases, getter, default=None)¶
reduce_attr_dict_from_instance(self, getter, default=None)¶