Introduction
Overview
Abstract data structures.
Motivation
estruttura was born out of the need for a similar interface between the datta and objetto packages.
Relationship
estruttura provides a Relationship class that contains information about the values stored in the Structures, such as typing (with support for runtime type checking), serialization, validation, conversion, etc.
>>> from six import string_types
>>> from estruttura import Relationship, exceptions
>>> def converter(value):
... if isinstance(value, string_types) and "." in value:
... return float(value)
... elif not isinstance(value, float):
... return int(value)
...
>>> def validator(value):
... if value > 10:
... raise exceptions.ValidationError(value)
...
>>> relationship = Relationship(types=(int, float), converter=converter, validator=validator)
>>> relationship.process_value("3.3")
3.3
Structures
estruttura offers all the class combinations of the concepts described below.
Private Structure
Holds data internally and only allow for changes privately.
User Structure
Allows for changing of the data by external clients (public).
Immutable Structure
Only allows data changes through copying. Immutable structures are hashable.
>>> from estruttura.examples import ImmutableList
>>> l_a = ImmutableList()
>>> l_b = l_a.extend(range(3))
>>> list(l_b) == [0, 1, 2]
True
Mutable Structure
Allows in-place data changes. Mutable structures are not hashable.
>>> from estruttura.examples import MutableList
>>> l = MutableList()
>>> l.extend(range(3))
>>> list(l) == [0, 1, 2]
True
Dict Structure
Dictionary-like data structure class.
>>> from estruttura import Relationship
>>> from estruttura.examples import MutableDict
>>> class StrIntDict(MutableDict):
... relationship = Relationship(converter=str)
... value_relationship = Relationship(converter=int)
...
>>> StrIntDict({1: "1"})
StrIntDict({'1': 1})
List Structure
List-like data structure class.
>>> from estruttura import Relationship
>>> from estruttura.examples import MutableList
>>> class IntList(MutableList):
... relationship = Relationship(converter=int)
...
>>> IntList(["1", 1, 1.0])
IntList([1, 1, 1])
Set Structure
Set-like data structure class.
>>> from estruttura import Relationship
>>> from estruttura.examples import MutableSet
>>> class IntSet(MutableSet):
... relationship = Relationship(converter=int)
...
>>> IntSet({"1", 1, 1.0})
IntSet({1})
Structure
Dataclass-like structure class that has a schema defined by attributes.
>>> import math
>>> from estruttura import Attribute, Relationship, getter
>>> from estruttura.examples import ImmutableClass
>>> class Point(ImmutableClass):
... x = Attribute()
... y = Attribute()
... d = Attribute(serializable=True, repr=True)
... @getter(d, dependencies=(x, y))
... def _(self):
... return math.sqrt(self.x**2 + self.y**2)
...
>>> Point(3, 4)
Point(3, 4, <d=5.0>)
>>> Point(3, 4).serialize() == {"x": 3, "y": 4, "d": 5.0}
True
>>> Point.deserialize({"x": 3, "y": 4})
Point(3, 4, <d=5.0>)
Documentation
- estruttura package
Attribute
Attribute.to_items()
Attribute.get_default_value()
Attribute.process_value()
Attribute.getter()
Attribute.setter()
Attribute.deleter()
Attribute.update()
Attribute.name
Attribute.owner
Attribute.default
Attribute.factory
Attribute.relationship
Attribute.required
Attribute.init
Attribute.init_as
Attribute.settable
Attribute.deletable
Attribute.serializable
Attribute.serialize_as
Attribute.serialize_default
Attribute.constant
Attribute.repr
Attribute.eq
Attribute.order
Attribute.hash
Attribute.doc
Attribute.metadata
Attribute.namespace
Attribute.extra_paths
Attribute.builtin_paths
Attribute.constant_value
Attribute.dependencies
Attribute.recursive_dependencies
Attribute.dependents
Attribute.recursive_dependents
Attribute.fget
Attribute.fset
Attribute.fdel
Attribute.count
Attribute.owned
Attribute.named
Attribute.delegated
Attribute.has_default
MutableAttribute
getter()
setter()
deleter()
get_global_attribute_count()
BaseStructureMeta
BaseStructure
BaseUserStructure
BaseImmutableStructure
BaseUserImmutableStructure
BaseMutableStructure
BaseUserMutableStructure
BaseCollectionStructure
BaseUserCollectionStructure
BaseImmutableCollectionStructure
BaseUserImmutableCollectionStructure
BaseMutableCollectionStructure
BaseUserMutableCollectionStructure
AttributeMap
StructureMeta
Structure
UserStructure
ImmutableStructure
UserImmutableStructure
MutableStructure
UserMutableStructure
DictStructure
UserDictStructure
ImmutableDictStructure
UserImmutableDictStructure
MutableDictStructure
UserMutableDictStructure
ListStructure
UserListStructure
ImmutableListStructure
UserImmutableListStructure
UserImmutableListStructure.append()
UserImmutableListStructure.extend()
UserImmutableListStructure.remove()
UserImmutableListStructure.reverse()
UserImmutableListStructure.sort()
UserImmutableListStructure.insert()
UserImmutableListStructure.move()
UserImmutableListStructure.delete()
UserImmutableListStructure.set()
UserImmutableListStructure.update()
MutableListStructure
UserMutableListStructure
UserMutableListStructure.pop()
UserMutableListStructure.insert()
UserMutableListStructure.append()
UserMutableListStructure.extend()
UserMutableListStructure.remove()
UserMutableListStructure.reverse()
UserMutableListStructure.sort()
UserMutableListStructure.move()
UserMutableListStructure.delete()
UserMutableListStructure.set()
UserMutableListStructure.update()
resolve_index()
resolve_continuous_slice()
pre_move()
Relationship
Relationship.convert_value()
Relationship.validate_value()
Relationship.check_value_type()
Relationship.process_value()
Relationship.accepts_type()
Relationship.serialize_value()
Relationship.deserialize_value()
Relationship.to_items()
Relationship.update()
Relationship.converter
Relationship.validator
Relationship.types
Relationship.subtypes
Relationship.serializer
Relationship.extra_paths
Relationship.builtin_paths
Relationship.types_info
Relationship.will_process
RelationshipTypesInfo
SetStructure
UserSetStructure
ImmutableSetStructure
UserImmutableSetStructure
MutableSetStructure
UserMutableSetStructure
UserMutableSetStructure.pop()
UserMutableSetStructure.intersection_update()
UserMutableSetStructure.symmetric_difference_update()
UserMutableSetStructure.difference_update()
UserMutableSetStructure.add()
UserMutableSetStructure.discard()
UserMutableSetStructure.remove()
UserMutableSetStructure.update()
dict_cls()
list_cls()
set_cls()
attribute()
dict_attribute()
list_attribute()
set_attribute()
- Subpackages
- Submodules