Developer Interface¶
Maps¶
-
class
maps.FrozenMap(*args, **kwargs)[source]¶ An immutable, hashable key-value mapping accessible via bracket-notation (i.e.
__getitem__).Parameters: Usage:
>>> import maps >>> fm = maps.FrozenMap({'a': 1, 'b': 2}) >>> fm['a'] 1 >>> list(fm.items()) [('a', 1), ('b', 2)] >>> len(fm) 2 >>> hash(fm) 3212389899479848432
-
class
maps.FixedKeyMap(*args, **kwargs)[source]¶ A key-value mapping with a fixed set of keys whose items are accessible via bracket-notation (i.e.
__getitem__and__setitem__). Though the set of keys is immutable, the corresponding values can be edited.Parameters: Usage:
>>> import maps >>> fkm = maps.FixedKeyMap({'a': 1, 'b': 2}) >>> fkm['a'] 1 >>> fkm['b'] += 10 >>> fkm['b'] 12 >>> list(fkm.items()) [('a', 1), ('b', 12)] >>> len(fkm) 2
Named Maps¶
-
maps.namedfrozen(typename, fields)[source]¶ Creates a new class that inherits from
maps.FrozenMapthat has the specified fields as keys. Fields are accessible via bracket-notation (i.e.__getitem__) as well as dot-notation (i.e.__getattr__). Instances of the returned class are immutable.Parameters: - typename (str) – Name of the new Map class
- fields (iterable) – Names of the fields
Raises: ValueError – if the type name or field names provided are not properly formatted
Returns: The newly created class
Return type: class
Usage:
>>> import maps >>> RGB = maps.namedfrozen('RGB', ['red', 'green', 'blue']) >>> coral = RGB(255, 127, 80) >>> coral['red'] 255 >>> coral.green 127
-
maps.namedfixedkey(typename, fields)[source]¶ Creates a new class that inherits from
maps.FixedKeyMapthat has the speciefied fields as keys. Fields are accessible via bracket-notation (i.e.__getitem__) as well as dot-notation (i.e.__getattr__). Instances of the returned class have a fixed set of keys, but the values corresponding to those keys can be edited.Parameters: - typename (str) – Name of the new Map class
- fields (iterable) – Names of the fields
Raises: ValueError – if the type name or field names provided are not properly formatted
Returns: The newly created class
Return type: class
Usage:
>>> import maps >>> Person = maps.namedfixedkey('Person', ['name', 'gender', 'age']) >>> bob = Person('bob', 'male', 40) >>> bob['name'] 'bob' >>> bob.gender 'male' >>> bob.age += 1 >>> bob.age 41
-
class
maps.NamedDict[source]¶ A subclass of
dictwhose items can be accessed via standard bracket-notation (i.e.__getitemand__setitem__) as well as dot-notation (i.e.__getattr__and__setattr__).Usage:
>>> import maps >>> nd = maps.NamedDict({'a': 1, 'b': 2}) >>> nd['a'] 1 >>> nd.b 2 >>> nd['c'] = 3 >>> nd.c 3 >>> nd.d = 4 >>> nd.d 4 >>> d = dict(nd) >>> d {'a': 1, 'b': 2, 'c': 3}
Named Map MetaClasses¶
The Named Maps section details maps.namedfrozen() and
maps.namedfixedkey(), which are the recommended way to instantiate Named Map
classes. Under the hood, those 2 functions leverage the metaclasses detailed below.
-
class
maps.NamedFrozenMapMeta(typename, fields=[])[source]¶ Returns a new
maps.FrozenMapsubclass namedtypename. The new subclass is used to createdict-like objects that have fields accessible by attribute lookup as well as being indexable by name and iterable. Instances of the subclass also have a helpful docstring (with typename and field_names) and a helpful __repr__() method which lists the mapping contents in a name=value format.field_namescan be a sequence of strings such as['x', 'y'].Any valid Python identifier may be used for a fieldname except for names starting with an underscore. Valid identifiers consist of letters, digits, and underscores but do not start with a digit or underscore and cannot be a keyword such as class, for, return, global, pass, or raise.
This metaclass injects 3 methods into the subclass:
__getattr__,__setattr__, and__repr__.1.
__getattr__attempts to retrieve attributes from an instance’s underlying_datadictionary, raisingAttributeErrorif the attribute is not found.2.
__setattr__raisesTypeErrorunless the attribute name has a leading underscore, in which case the attribute will be set normally.3.
__repr__simply replacesFrozenMapwith the name of the instantiated class.maps.namedfrozen()provides a convenient alias for calling this metaclass.Parameters: - typename (str) – Name for the new class
- fields (iterable) – Names for the fields of the new class
Raises: ValueError – if the type name or field names provided are not properly formatted
Returns: Newly created subclass of
maps.FrozenMap-
static
_getattr(name)[source]¶ Retrieves attribute by name.
Parameters: name (str) – Name of the desired attribute Raises: AttributeError – if an attribute with the specified name cannot be found Returns: Desired attribute
-
class
maps.NamedFixedKeyMapMeta(typename, fields=[])[source]¶ Returns a new
maps.FixedKeyMapsubclass namedtypename. The new subclass is used to createdict-like objects that have fields accessible by attribute lookup as well as being indexable by name and iterable. Instances of the subclass also have a helpful docstring (with typename and field_names) and a helpful __repr__() method which lists the mapping contents in a name=value format.field_namescan be a sequence of strings such as['x', 'y'].Any valid Python identifier may be used for a fieldname except for names starting with an underscore. Valid identifiers consist of letters, digits, and underscores but do not start with a digit or underscore and cannot be a keyword such as class, for, return, global, pass, or raise.
This metaclass injects 3 methods into the subclass:
__getattr__,__setattr__, and__repr__.1.
__getattr__attempts to retrieve attributes from an instance’s underlying_datadictionary, raisingAttributeErrorif the attribute is not found.2.
__setattr__attempts to set the value for the specified attribute, but raisesTypeErrorif the attribute is not part of the fixed key set. If the attribute name has a leading underscore, these checks are skipped and the value is set normally.3.
__repr__simply replacesFixedKeyMapwith the name of the instantiated class.maps.namedfixedkey()provides a convenient alias for calling this metaclass.Parameters: - typename (str) – Name for the new class
- fields (iterable) – Names for the fields of the new class
Raises: ValueError – if the type name or field names provided are not properly formatted
Returns: Newly created subclass of
maps.FixedKeyMap-
static
_getattr(name)[source]¶ Retrieves attribute by name.
Parameters: name (str) – Name of the desired attribute Raises: AttributeError – if an attribute with the specified name cannot be found Returns: Desired attribute