Struct

class mosaic.types.struct.Struct(content=None, mutable=True)[source]

Bases: object

Structs represent dictionary-like objects that provide some extra features over Python’s dict.

Their internal representation is an dict, 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.

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 an AttributeError. For mutable Structs the values of its members can be changed as many times as needed.

Parameters:
  • content (dict-like, optional) – Dict-like object to initialise the Struct, defaults to empty.

  • 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
copy()[source]

Returns a deepcopy of the Struct.

Returns:

Copied Struct

Return type:

Struct

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

update(content)[source]

Updates the Struct with the contents of a dict-like object.

Parameters:

content (dict-like) – Content with which to update the Struct

values()[source]

Returns the list of values in the Struct.

Returns:

List of values in the Struct.

Return type:

odict_values