colour.utilities.CanonicalMapping#

class colour.utilities.CanonicalMapping(data: Optional[Union[Generator, Mapping]] = None, **kwargs: Any)[source]#

Bases: MutableMapping

Implement a delimiter and case-insensitive dict-like object with support for slugs, i.e. SEO friendly and human-readable version of the keys but also canonical keys, i.e. slugified keys without delimiters.

The item keys are expected to be str-like objects thus supporting the str.lower() method. Setting items is done by using the given keys. Retrieving or deleting an item and testing whether an item exist is done by transforming the item’s key in a sequence as follows:

  • Original Key

  • Lowercase Key

  • Slugified Key

  • Canonical Key

For example, given the McCamy 1992 key:

  • Original Key : McCamy 1992

  • Lowercase Key : mccamy 1992

  • Slugified Key : mccamy-1992

  • Canonical Key : mccamy1992

Parameters:
  • data (Optional[Union[Generator, Mapping]]) – Data to store into the delimiter and case-insensitive dict-like object at initialisation.

  • kwargs (Any) – Key / value pairs to store into the mapping at initialisation.

Attributes

Methods

Examples

>>> methods = CanonicalMapping({"McCamy 1992": 1, "Hernandez 1999": 2})
>>> methods["mccamy 1992"]
1
>>> methods["MCCAMY 1992"]
1
>>> methods["mccamy-1992"]
1
>>> methods["mccamy1992"]
1
__init__(data: Optional[Union[Generator, Mapping]] = None, **kwargs: Any) None[source]#
Parameters:
Return type:

None

property data: Dict#

Getter property for the delimiter and case-insensitive dict-like object data.

Returns:

Data.

Return type:

dict

__repr__() str[source]#

Return an evaluable string representation of the delimiter and case-insensitive dict-like object.

Returns:

Evaluable string representation.

Return type:

str

__setitem__(item: Union[str, Any], value: Any)[source]#

Set given item with given value in the delimiter and case-insensitive dict-like object.

Parameters:
  • item (Union[str, Any]) – Item to set in the delimiter and case-insensitive dict-like object.

  • value (Any) – Value to store in the delimiter and case-insensitive dict-like object.

__getitem__(item: Union[str, Any]) Any[source]#

Return the value of given item from the delimiter and case-insensitive dict-like object.

Parameters:

item (Union[str, Any]) – Item to retrieve the value of from the delimiter and case-insensitive dict-like object.

Returns:

Item value.

Return type:

object

Notes

  • The item value can be retrieved by using either its lower-case, slugified or canonical variant.

__delitem__(item: Union[str, Any])[source]#

Delete given item from the delimiter and case-insensitive dict-like object.

Parameters:

item (Union[str, Any]) – Item to delete from the delimiter and case-insensitive dict-like object.

Notes

  • The item can be deleted by using either its lower-case, slugified or canonical variant.

__contains__(item: Union[str, Any]) bool[source]#

Return whether the delimiter and case-insensitive dict-like object contains given item.

Parameters:

item (Union[str, Any]) – Item to find whether it is in the delimiter and case-insensitive dict-like object.

Returns:

Whether given item is in the delimiter and case-insensitive dict-like object.

Return type:

bool

Notes

  • The item presence can be checked by using either its lower-case, slugified or canonical variant.

__iter__() Generator[source]#

Iterate over the items of the delimiter and case-insensitive dict-like object.

Yields:

Generator – Item generator.

Return type:

Generator

Notes

  • The iterated items are the original items.

__len__() int[source]#

Return the items count.

Returns:

Items count.

Return type:

numpy.integer

__eq__(other: Any) bool[source]#

Return whether the delimiter and case-insensitive dict-like object is equal to given other object.

Parameters:

other (Any) – Object to test whether it is equal to the delimiter and case-insensitive dict-like object

Returns:

Whether given object is equal to the delimiter and case-insensitive dict-like object.

Return type:

bool

__ne__(other: Any) bool[source]#

Return whether the delimiter and case-insensitive dict-like object is not equal to given other object.

Parameters:

other (Any) – Object to test whether it is not equal to the delimiter and case-insensitive dict-like object

Returns:

Whether given object is not equal to the delimiter and case-insensitive dict-like object.

Return type:

bool

copy() CanonicalMapping[source]#

Return a copy of the delimiter and case-insensitive dict-like object.

Returns:

Case-insensitive dict-like object copy.

Return type:

CanonicalMapping

Warning

  • The CanonicalMapping class copy returned is a copy of the object not a deepcopy!

lower_keys() Generator[source]#

Iterate over the lower-case keys of the delimiter and case-insensitive dict-like object.

Yields:

Generator – Item generator.

Return type:

Generator

lower_items() Generator[source]#

Iterate over the lower-case items of the delimiter and case-insensitive dict-like object.

Yields:

Generator – Item generator.

Return type:

Generator

slugified_keys() Generator[source]#

Iterate over the slugified keys of the delimiter and case-insensitive dict-like object.

Yields:

Generator – Item generator.

Return type:

Generator

slugified_items() Generator[source]#

Iterate over the slugified items of the delimiter and case-insensitive dict-like object.

Yields:

Generator – Item generator.

Return type:

Generator

canonical_keys() Generator[source]#

Iterate over the canonical keys of the delimiter and case-insensitive dict-like object.

Yields:

Generator – Item generator.

Return type:

Generator

canonical_items() Generator[source]#

Iterate over the canonical items of the delimiter and case-insensitive dict-like object.

Yields:

Generator – Item generator.

Return type:

Generator

__hash__ = None#
__weakref__#

list of weak references to the object (if defined)