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
Other Parameters:
 

**kwargs (dict, optional) – Key / Value pairs to store into the mapping at initialisation.

key_decimals
__setitem__()[source]
__getitem__()[source]
__delitem__()[source]
__contains__()[source]
__iter__()[source]
__len__()[source]

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,)
data

Property for self.data attribute.

Returns:ArbitraryPrecisionMapping data structure.
Return type:dict

Warning

ArbitraryPrecisionMapping.data is read only.

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.

Other Parameters:
 
  • *args (list, optional) – Arguments.
  • **kwargs (dict, optional) – Key / Value pairs.
__getattr__()[source]
__setattr__()[source]
__delattr__()[source]
update()[source]

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'
update(*args, **kwargs)[source]

Updates both keys and sibling attributes.

Other Parameters:
 
  • *args (list, optional) – Arguments.
  • **kwargs (dict, optional) – Keywords arguments.

Notes

class colour.utilities.data_structures.Lookup[source]

Bases: dict

Extends dict type to provide a lookup by value(s).

first_key_from_value()[source]
keys_from_value()[source]

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']
first_key_from_value(value)[source]

Gets the first key with given value.

Parameters:value (object) – Value.
Returns:Key.
Return type:object
keys_from_value(value)[source]

Gets the keys with given value.

Parameters:value (object) – Value.
Returns:Keys.
Return type:object
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.
Other Parameters:
 **kwargs (dict, optional) – Key / Value pairs to store into the mapping at initialisation.
__setitem__()[source]
__getitem__()[source]
__delitem__()[source]
__contains__()[source]
__iter__()[source]
__len__()[source]
__eq__()[source]
__ne__()[source]
__repr__()[source]
copy()[source]
lower_items()[source]

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

Returns a copy of the mapping.

Returns:Mapping copy.
Return type:CaseInsensitiveMapping

Notes

data

Property for self.data attribute.

Returns:ArbitraryPrecisionMapping data structure.
Return type:dict

Warning

ArbitraryPrecisionMapping.data is read only.

lower_items()[source]

Iterates over the lower items names.

Returns:Lower item names.
Return type:generator