Struct
- class mosaic.types.struct.Struct(content=None, extensible=False, mutable=True)[source]
Bases:
mosaic.types.immutable.ImmutableObject
Structs represent dictionary-like objects that provide some extra features over Python’s
OrderedDict
.Their internal representation is an
OrderedDict
, a dictionary that maintains the order in which members were added to it. Unlike traditional Python dicts, however, both square-bracket and dot notation are allowed to access the members of the Struct.Structs can be
extensible
andmutable
, which is determined when they are instantiated and remains unchanged for the life of the object.When a Struct is not
mutable
, its members can only be assigned once, any further attempts at modifying the value of a member will result in anAttributeError
. Formutable
Structs the values of its members can be changed as many times as needed.Whether or not a Struct is
extensible
affects how its members are accessed. When trying to find a member of the Struct, a naive search is performed first, assuming that the item exists within its internal dictionary.If the search fails, and the Struct is not
extensible
anAttributeError
is raised. Otherwise, a new search starts for members in the dictionary with a similar signature to the requested item.If this search fails, an
AttributeError
is raised. If if does not fail and a match is found, the match is returned wrapped in a function that will evaluate whether the variant exists and is callable.- Parameters
content (dict-like, optional) – Dict-like object to initialise the Struct, defaults to empty.
extensible (bool, optional) – Whether or not the Struct should be extensible, defaults to
False
.mutable (bool, optional) – Whether or not the Struct should be mutable, defaults to
True
.
Examples
Let’s create an empty, immutable Struct:
>>> struct = Struct(mutable=False) >>> struct.member = 10 >>> struct.member 10 >>> struct['member'] 10 >>> struct.member = 20 AttributeError('The attribute member already exists in the container, this container is not mutable')
If the container was
mutable
instead:>>> struct = Struct() >>> struct.member = 10 >>> struct.member 10 >>> struct.member = 20 >>> struct.member 20
The same way mutability affects member assignment, extensibility affects how the members of the Struct are accessed:
>>> def function_variant_1(): >>> print('Executed variant_1') >>> >>> def function_variant_2(): >>> print('Executed variant_2') >>> >>> class KlassVariant1: >>> def __init__(self): >>> print('Instantiated Variant1') >>> >>> class KlassVariant2: >>> def __init__(self): >>> print('Instantiated Variant2') >>> >>> struct = Struct(extensible=True) >>> struct.function_variant_1 = function_variant_1 >>> struct.function_variant_2 = function_variant_2 >>> struct.KlassVariant1 = KlassVariant1 >>> struct.KlassVariant2 = KlassVariant2 >>> >>> struct.function(use='variant_1') Executed variant_1 >>> struct.Klass('Variant2')() Instantiated Variant2 >>> struct.Klass('variant_1')() Instantiated Variant1
- delete(item)[source]
Delete an item from the container using its key.
- Parameters
item (str) – Name of the item to delete.
- get(item, default=None)[source]
Returns an item from the Struct or a default value if it is not found.
- Parameters
item (str) – Name of the item to find
default (object, optional) – Default value to be returned in case the item is not found, defaults to
None
- items()[source]
Returns the list of keys and values in the Struct.
- Returns
List of keys and values in the Struct.
- Return type
odict_items
- keys()[source]
Returns the list of keys in the Struct.
- Returns
List of keys in the Struct.
- Return type
odict_keys
- pop(item, default=None)[source]
Returns an item from the Struct and deletes it, or returns a default value if it is not found.
- Parameters
item (str) – Name of the item to find
default (object, optional) – Default value to be returned in case the item is not found