arjuna.tpi.data.entity module¶
- arjuna.tpi.data.entity.data_entity(entity_name, *attrs, bases=(), **attrs_with_defaults)¶
Create a new Data Entity class with provided name and attributes.
Check Data Entities documentation for various use cases.
- Parameters
entity_name – The class name for this new Data Entity type.
*attrs – Arbitrary names for Python attributes to be associated with objects of this entity.
- Keyword Arguments
bases – Base data entities for this entity. Can be a string or tuple or list.
**attrs_with_defaults – Arbitrary attributes to be associated with objects of this entity, with the defaults that are provided.
Note
You can create objects of newly created data entity just like a normal Python class.
Person = data_entity("Person", "name age") person = Person(name="SomeName", age=21)
Note
- The defaults that are provided can be any of the following:
Any Python Object
A Python callable
Arjuna generator
Arjuna composite
Note
Data entity objects behave like Python dictionaries.
So you can retrieve an attribute value as:
entity.attr # or entity['attr']
Following dict-like operations are valid too. The key difference to note is that in these operations that attributes that have None value are excluded unlike a Python dictionary.
entity.keys() entity.items() **entity # Unpacking of key-values # Iterating on keys for attr in entity: pass # Iterating on key-value pairs for attr, value in entity.items(): pass
To retain keys/attrs corresponding to None values, you can provide remove_none=False as argument:
entity.keys(remove_none=False) entity.items(remove_none=False) **entity # Unpacking of key-values # Iterating on keys for attr in entity.keys(remove_none=False): pass # Iterating on key-value pairs for attr, value in entity.items(remove_none=False): pass
Also note that because len() in Python is not flexible to allow for the above, you can use size method:
len(entity) # Will ignore attrs with None value entity.size() # Will ignore attrs with None value entity.size(remove_none=False) # Includes attrs with None value
All above mentioned methods also accept remove argument to explicitly exclude one or more attributes by name.
entity.keys(remove='some_key') entity.keys(remove={'some_key1', 'some_key2'}) entity.items(remove='some_key') entity.items(remove={'some_key1', 'some_key2'}) entity.size(remove='some_key') entity.size(remove={'some_key1', 'some_key2'})
Note
When you provide one or more bases, the overriding order is B1 -> B2 -> B3 ….. -> This Entity.
At each stage of this chain, you can
Add one or more mandatory attributes.
Add one or more optional attributes
Make an optional attribute in base entity as mandatory.
Make a mandatory attribute as optional by assigning a default value.
Change the default for an existing default attribute to something else.
Note
Delete operation is disllowed on the data entity because it corresponds to attribute deletion. Use as_dict() method for representation that has one or more keys removed.
# Raises exception del entity['some_attr']
Note
You can make an object of a data entity IMMUTABLE by passing freeze=True argument.
person = Person(name="SomeName", age=21, freeze=True) # Raises Exception person.age = 25