Entity module

Quick Start

For a quick start and a high level description of the usage of this module, refer to Creating your Entity.

Module Documentation

Base entity for modeling of API’s entities

class nova_api.entity.Entity(id_: str = <factory>, creation_datetime: datetime.datetime = <factory>, last_modified_datetime: datetime.datetime = <factory>)[source]

Base Entity implementation

This class is the base implementation of the entity for modeling API resources. The fields included in this class will be inherited for all subclasses.

Examples

@dataclass
class Person(Entity):
    name: str = None
    age: int = None
    birthday: date = None
__iter__()[source]

Iteration through the Entity receiving the tuple (field_name, field_value_serialized)

Iterates through the fields in the entity and yields a tuple with the field_name and the serialized field_value. For fields that are instances of Entities, the key will have _id_ appended and the value will be the id_, as stated in _serialize_field.

Return key, value:
 The tuple with the field_name and field_value
static __new__(cls, *args, **kwargs)[source]

Prevents the instantiation of the abstract class Entity

Raises:NotImplementedError – When trying to instantiate Entity directly.
__setattr__(key: str, value) → None[source]

Sets and validates Entity attributes. Parsing then when necessary.

Fields that are subclasses of Entity will be instantiated (but only with id_ set). Datetime formats also will be cast if received as strings. Strings will have trailing and leading white spaces removed.

Parameters:
  • key – The attribute name
  • value – The attribute value
Returns:

None

__weakref__

list of weak references to the object (if defined)

get_db_values(field_serializer=None) → list[source]

Returns all attributes to save in database with formatted values.

Goes through the fields in the entity and converts them to the expected value to save in the database. For example: datetime values are converted to string with the specific sql format.

Also verifies if a field contains in metadata database=False and excludes it from the list if so. Default for database is True.

Parameters:field_serializer – Custom function to define serialization of

fields :return: Serialized values to save in database

static serialize_field(field_value)[source]

Returns the value of a field formatted to be saved in the database.

For fields that are subclasses of Entity, returns only the id_ to save in the database. For datetime and date fields, values are transformed into strings with strftime function from datetime.

Parameters:field_value – Value of the field in the entity being serialized.
Returns:The serialized value of the field for database insertion.
nova_api.entity.generate_id() → str[source]

Generates an uuid v4.

Returns:Hexadecimal string representation of the uuid.
nova_api.entity.get_time() → datetime.datetime[source]

Get current time without microseconds

Returns:Current datetime with no microseconds.