class_tools package

Submodules

class_tools.decorators module

class class_tools.decorators.NotSet[source]

Bases: object

Class to indicate that an argument has not been specified.

class_tools.decorators.add_property(name, *args, abstract=False, **kwargs)[source]

Create a classdecorator that adds a property or abc.abstractproperty to the decorated class.

Parameters:
  • name (str) – the name for the property
  • abstract (bool, optional) – whether to create an abstractproperty instead of a property
  • kwargs (args,) – arguments passed to property (or abstractproperty of abstract=True)
Returns:

decorator for classes

Return type:

classdecorator

class_tools.decorators.classdecorator(decorated_fun)[source]

Decorator for other decorator functions that are supposed to only be applied to classes.

Raises:ValueError – if the resulting decorator is applied to non-classes
class_tools.decorators.conflicting_arguments(*conflicts)[source]

Create a decorator which raises a ValueError when the decorated function was called with conflicting arguments. Works for both positional and keyword arguments.

Parameters:conflicts (sequence of str) – the arguments which should not be specified together.
Returns:a decorator for callables
Return type:callable
class_tools.decorators.constant(name, value, *args, **kwargs)[source]

Create a classdecorator that adds a readonly_property returning a static value to the decorated class.

Parameters:
  • name (str) – the name for the constant
  • value (object) – the value of the constant
  • kwargs (args,) – arguments passed to readonly_property
class_tools.decorators.readonly_property(name, getter, *args, **kwargs)[source]

Create a classdecorator that adds a read-only property (i.e. without setter and deleter).

Parameters:
  • name (str) – the name for the constant
  • getter (callable) – the property.getter to use
  • kwargs (args,) – arguments passed to add_property
class_tools.decorators.with_eq_comparing_properties()[source]

Create a classdecorator that overwrites the __eq__-method so that it compares all properties with a getter for equality.

Returns:classdecorator
Return type:callable
class_tools.decorators.with_init_from_properties()[source]

Create a classdecorator that overwrites the __init__-method so that it accepts arguments according to its read- and settable properties.

Returns:classdecorator
Return type:callable
class_tools.decorators.with_repr_like_init_from_properties(indent=' ', full_path=False)[source]

Create a classdecorator that overwrites the __repr__-method so that it returns a representation according to the decorated class’ properties.

Note

The created __repr__-method assumes that the decorated class’ __init__-method accepts keyword arguments similar to its properties. The with_init_from_properties classdecorator creates such an initializer.

Parameters:
  • indent (str, optional) – the indentation string. The default is four spaces.
  • full_path (bool, optional) – whether to use the full_object_path instead of just the object names. Defaults to False.
Returns:

classdecorator

Return type:

callable

class_tools.decorators.wrapper_property(name, *args, attr=<class 'class_tools.decorators.NotSet'>, static_default=<class 'class_tools.decorators.NotSet'>, dynamic_default=<class 'class_tools.decorators.NotSet'>, set_default=False, static_type=<class 'class_tools.decorators.NotSet'>, dynamic_type=<class 'class_tools.decorators.NotSet'>, doc_default=None, doc_type=None, doc_getter=None, doc_setter=None, doc_property=None, **kwargs)[source]

Create a classdecorator that adds a property with getter, setter and deleter, wrapping an attribute.

Parameters:
  • name (str) – the name for the constant
  • attr (str, optional) – the name for the wrapped attribute. If unset, use name with an underscore (_) prepended.
  • static_default (object, optional) – value to use in the getter if the attr is not yet set
  • dynamic_default (object, optional) – the return value of this function (called with the object as argument) is used in the getter if the attr is not yet set.
  • set_default (bool, optional) – whether to set the attr to the static_default or dynamic_default (if specified) in the getter when it was not yet set. Default is False.
  • doc_type, doc_getter, doc_setter (doc_default,) – documentation strings.
  • static_type (callable, optional) – function to convert the value in the setter.
  • dynamic_type (callable, optional) – function to convert the value in the setter. Differing from static_type, this function is also handed the object reference as first argument.
  • kwargs (args,) – arguments passed to add_property

class_tools.propertyobject module

class class_tools.propertyobject.PropertyObject(**properties)[source]

Bases: object

Convenience base class decorated with with_init_from_properties, with_eq_comparing_properties and with_repr_like_init_from_properties.

class_tools.utils module

class_tools.utils.full_object_path(var)[source]

Get the full path string for a given object

Parameters:obj (object) – The variable to get the full string from
Returns:The full object path
Return type:str
class_tools.utils.get_properties(obj, getter=None, setter=None, deleter=None)[source]

Get a mapping of property names to properties from a given object’s class or a class itself.

Parameters:
  • obj (object) – either an object or a class
  • setter, deleter (getter,) – also check whether a getter/setter/deleter is defined. The default is no check.
Returns:

mapping of property names to the properties

Return type:

dict

class_tools.version module