mango.managers module

class mango.managers.Manager(instance, name, rev_name, rev=False, cls=None, typed=True, unique=True)

Bases: mango.managers.ManagerBase

The manager class can be used to manage a set of models. It will be possible to add and more objects from the list and filter using keys and values. It is possible to manage only direct instances of a cls or using any of its subclasses.

add(*models)

Loop over the provided models and add them to models list if they pass the validator checks.

Parameters:models (models.Model) –
Returns:Added models
Return type:tuple[models.Model]
add_attribute_to_instance(instance, **kwargs)

Add a connection attribute to the instance. The connection attribute is a maya message attribute, depending on the array and hidden settings the connection will be hidden and is determined to only be a source or a destination. When a relation is set to be hidden the attribute is hidden. The attribute is controlled by a manager. This manager gets stored on the instance and will be returned. It is possible to specify if the attribute to be added is the source or destination attribute.

Parameters:instance (models.Model) –
all_iter()
Returns:Models
Return type:generator[models.Model]
clear()

Remove all models from the manager.

create(**kwargs)

Create and add an instance of the cls model using the provided keyword arguments. It is possible to override the class by added in cls keyword argument if you are with a typed manager. This cls will be part of the validation process to ensure that actually is matching the typed manager.

Parameters:kwargs
Returns:Instance
Return type:models.Model
remove(*models, **options)

Loop over the provided models and remove them from the models list if they are part of the list.

Parameters:models (models.Model) –
set(*models)

Loop over the provided models and add them to models list if they pass the validator checks. Before this happens the original list is cleared.

Parameters:models (tuple[models.Model]) –
Returns:Set models
Return type:tuple[models.Model]
class mango.managers.ManagerBase(cls, typed=True, unique=True)

Bases: object

The manager class can be used to manage a set of models. It will be possible to add and more objects from the list and filter using keys and values. It is possible to manage only direct instances of a cls or using any of its subclasses.

all()
Returns:All models
Return type:list[models.Model]
all_iter()

Abstract method that needs to be implemented to make sure that all models are found that belong to the manager.

Returns:Models
Return type:generator[models.Model]
create(**kwargs)

Create and add an instance of the cls model using the provided keyword arguments. It is possible to override the class by added in cls keyword argument if you are with a typed manager. This cls will be part of the validation process to ensure that actually is matching the typed manager.

Parameters:kwargs
Returns:Instance
Return type:models.Model
filter(**kwargs)

The filter allows for operators to be attached to the keys. For example ‘name_contains’ or ‘index_ge’ it’ll require getting used to the name of the rich operators. The operators package is used to do these comparisons.

Cheat sheet:
lt: a < b le: a <= b eq: a == b ne: a != b gt: a > b ge: a >= b
Returns:Filtered models
Return type:list[models.Model]
Raises:RuntimeError – When the provided operator cannot be found.
filter_iter(**kwargs)
Returns:Filtered models
Return type:generator[models.Model]
Raises:RuntimeError – When the provided operator cannot be found.
first()
Returns:First instance of the models list
Return type:models.Model/None
get(**kwargs)
Returns:First instance of a match in the model list
Return type:models.Model/None
get_or_create(**kwargs)

Return the an existing model matching the keyword arguments. If it doesn’t the keyword arguments will be used to create the model.

Returns:Model
Return type:models.Model
length()
Returns:Length
Return type:int
validate_obj_is_instance(obj)
Parameters:obj (model.Model) –
Raises:TypeError – When the provided model is not an instance of the class.
validate_obj_is_subclass(obj)
Parameters:obj (model.Model) –
Raises:TypeError – When the provided model is not a subclass of the class.
validate_obj_is_unique(obj, **kwargs)

For every object checked the unique fields of that objects are used and added to the unique fields set. This needs to be done as there might be additional unique fields for types of the original class. Any additional arguments will be used to update the search keywords dictionary to determine of an object is unique.

Parameters:
  • obj (model.Model) –
  • kwargs (dict) –
Raises:

ValueError – When the provided model is not unique.

class mango.managers.ManagerDefault(cls)

Bases: mango.managers.ManagerBase

The default manager is used by the types themselves. It will help manage the instances that belong to the type. It will not only manage any of the instances of the type but also any instances that have inherited from the type.

all_iter()
Returns:All models
Return type:generator[models.Model]