colour.utilities.data_structures Module¶
Data Structures¶
Defines various data structures classes:
ArbitraryPrecisionMapping
: A mutable mapping / dict like object where numeric keys are stored with an arbitrary precision.Structure
: An object similar to C/C++ structured type.Lookup
: A dict sub-class acting as a lookup to retrieve keys by values.CaseInsensitiveMapping
: A case insensitive mapping allowing values retrieving from keys while ignoring the key case.
-
class
colour.utilities.data_structures.
ArbitraryPrecisionMapping
(data=None, key_decimals=0, **kwargs)[source]¶ Bases:
_abcoll.MutableMapping
Implements a mutable mapping / dict like object where numeric keys are stored with an arbitrary precision.
Parameters: - data (dict, optional) – dict of data to store into the mapping at initialisation.
- key_decimals (int, optional) – Decimals count the keys will be rounded at
- **kwargs (dict, optional) – Key / Value pairs to store into the mapping at initialisation.
-
key_decimals
¶
Examples
>>> data1 = {0.1999999998: 'Nemo', 0.2000000000: 'John'} >>> apm1 = ArbitraryPrecisionMapping(data1, key_decimals=10) >>> # Doctests skip for Python 2.x compatibility. >>> tuple(apm1.keys()) (0.1999999998, 0.2) >>> apm2 = ArbitraryPrecisionMapping(data1, key_decimals=7) >>> # Doctests skip for Python 2.x compatibility. >>> tuple(apm2.keys()) (0.2,)
-
__contains__
(item)[source] Returns if the mapping contains given item (rounded if numeric).
Parameters: item (unicode) – Item (rounded if numeric) name. Returns: Is item in mapping. Return type: bool Notes
- Reimplements the
MutableMapping.__contains__()
method.
- Reimplements the
-
__delitem__
(item)[source] Deletes the item (rounded if numeric) with given value.
Parameters: item (unicode) – Item (rounded if numeric) name. Notes
- Reimplements the
MutableMapping.__delitem__()
method.
- Reimplements the
-
__getitem__
(item)[source] Returns the value of given item (rounded if numeric).
Parameters: item (unicode) – Item (rounded if numeric) name. Returns: Item value. Return type: object Notes
- Reimplements the
MutableMapping.__getitem__()
method.
- Reimplements the
-
__iter__
()[source] Iterates over the items (rounded if numeric) names in the mapping.
Returns: Item names. Return type: generator Notes
- Reimplements the
MutableMapping.__iter__()
method.
- Reimplements the
-
__len__
()[source] Returns the items count.
Returns: Items count. Return type: int Notes
- Reimplements the
MutableMapping.__iter__()
method.
- Reimplements the
-
__setitem__
(item, value)[source] Sets given item (rounded if numeric) with given value.
Parameters: Returns: Item value (rounded if numeric).
Return type: Notes
- Reimplements the
MutableMapping.__setitem__()
method.
- Reimplements the
-
key_decimals
Property for self.__key_decimals private attribute.
Returns: self.__key_decimals. Return type: unicode
-
class
colour.utilities.data_structures.
Structure
(*args, **kwargs)[source]¶ Bases:
dict
Defines an object similar to C/C++ structured type.
Parameters: - *args (list, optional) – Arguments.
- **kwargs (dict, optional) – Key / Value pairs.
References
[1] Mansencal, T. (n.d.). Structure. Retrieved from https://github.com/KelSolaar/Foundations/blob/develop/foundations/data_structures.py Examples
>>> person = Structure(first_name='Doe', last_name='John', gender='male') >>> # Doctests skip for Python 2.x compatibility. >>> person.first_name 'Doe' >>> sorted(person.keys()) ['first_name', 'gender', 'last_name'] >>> # Doctests skip for Python 2.x compatibility. >>> person['gender'] 'male'
-
__delattr__
(attribute)[source] Deletes both key and sibling attribute.
Parameters: attribute (object) – Attribute. Notes
- Reimplements the
dict.__delattr__()
method.
- Reimplements the
-
__delitem__
(attribute)¶ Deletes both key and sibling attribute.
Parameters: attribute (object) – Attribute. Notes
- Reimplements the
dict.__delattr__()
method.
- Reimplements the
-
__getattr__
(attribute)[source] Returns given attribute value.
Parameters: attribute (unicode) – Attribute name. Notes
- Reimplements the
dict.__getattr__()
method.
Returns: Attribute value. Return type: object Raises: AttributeError
– If the attribute is not defined.- Reimplements the
-
__setattr__
(attribute, value)[source] Sets both key and sibling attribute with given value.
Parameters: Notes
- Reimplements the
dict.__setattr__()
method.
- Reimplements the
-
__setitem__
(attribute, value)¶ Sets both key and sibling attribute with given value.
Parameters: Notes
- Reimplements the
dict.__setattr__()
method.
- Reimplements the
-
update
(*args, **kwargs)[source] Updates both keys and sibling attributes.
Parameters: - *args (list, optional) – Arguments.
- **kwargs (dict, optional) – Keywords arguments.
Notes
- Reimplements the
dict.update()
method.
-
class
colour.utilities.data_structures.
Lookup
[source]¶ Bases:
dict
Extends dict type to provide a lookup by value(s).
References
[2] Mansencal, T. (n.d.). Lookup. Retrieved from https://github.com/KelSolaar/Foundations/blob/develop/foundations/data_structures.py Examples
>>> person = Lookup(first_name='Doe', last_name='John', gender='male') >>> person.first_key_from_value('Doe') 'first_name' >>> persons = Lookup(John='Doe', Jane='Doe', Luke='Skywalker') >>> sorted(persons.keys_from_value('Doe')) ['Jane', 'John']
-
class
colour.utilities.data_structures.
CaseInsensitiveMapping
(data=None, **kwargs)[source]¶ Bases:
_abcoll.MutableMapping
Implements a case-insensitive mutable mapping / dict object.
Allows values retrieving from keys while ignoring the key case. The keys are expected to be unicode or string-like objects supporting the
str.lower()
method.Parameters: - data (dict) – dict of data to store into the mapping at initialisation.
- **kwargs (dict, optional) – Key / Value pairs to store into the mapping at initialisation.
Warning
The keys are expected to be unicode or string-like objects.
References
[3] Reitz, K. (n.d.). CaseInsensitiveDict. Retrieved from https://github.com/kennethreitz/requests/blob/v1.2.3/requests/structures.py#L37 Examples
>>> methods = CaseInsensitiveMapping({'McCamy': 1, 'Hernandez': 2}) >>> methods['mccamy'] 1
-
__contains__
(item)[source] Returns if the mapping contains given item.
Parameters: item (unicode) – Item name. Returns: Is item in mapping. Return type: bool Notes
- Reimplements the
MutableMapping.__contains__()
method.
- Reimplements the
-
__delitem__
(item)[source] Deletes the item with given name.
The item is deleted from the mapping using its lower name.
Parameters: item (unicode) – Item name. Notes
- Reimplements the
MutableMapping.__delitem__()
method.
- Reimplements the
-
__eq__
(item)[source] Returns the equality with given object.
Parameters: item – Object item. Returns: Equality. Return type: bool Notes
- Reimplements the
MutableMapping.__eq__()
method.
- Reimplements the
-
__getitem__
(item)[source] Returns the value of given item.
The item value is retrieved using its lower name in the mapping.
Parameters: item (unicode) – Item name. Returns: Item value. Return type: object Notes
- Reimplements the
MutableMapping.__getitem__()
method.
- Reimplements the
-
__iter__
()[source] Iterates over the items names in the mapping.
The item names returned are the original input ones.
Returns: Item names. Return type: generator Notes
- Reimplements the
MutableMapping.__iter__()
method.
- Reimplements the
-
__len__
()[source] Returns the items count.
Returns: Items count. Return type: int Notes
- Reimplements the
MutableMapping.__iter__()
method.
- Reimplements the
-
__ne__
(item)[source] Returns the inequality with given object.
Parameters: item – Object item. Returns: Inequality. Return type: bool Notes
- Reimplements the
MutableMapping.__ne__()
method.
- Reimplements the
-
__repr__
()[source] Returns the mapping representation with the original item names.
Returns: Mapping representation. Return type: unicode Notes
- Reimplements the
MutableMapping.__repr__()
method.
- Reimplements the
-
__setitem__
(item, value)[source] Sets given item with given value.
The item is stored as lower in the mapping while the original name and its value are stored together as the value in a tuple:
{“item.lower()”: (“item”, value)}
Parameters: Notes
- Reimplements the
MutableMapping.__setitem__()
method.
- Reimplements the
-
copy
()[source] Returns a copy of the mapping.
Returns: Mapping copy. Return type: CaseInsensitiveMapping Notes
- The
CaseInsensitiveMapping
class copy returned is a simple copy not a deepcopy.
- The
-
lower_items
()[source] Iterates over the lower items names.
Returns: Lower item names. Return type: generator