Creating a new kind of mapping
Python has a built-in mapping called dict
, and numerous library mappings. In addition to the collections
module extensions to dict
(defaultdict
, Counter
, OrderedDict
, and ChainMap
), there are several other library modules that contain mapping-like structures.
The shelve
module is an important example of another mapping. We'll look at this in Chapter 10, Storing and Retrieving Objects via Shelve. The dbm
module is similar to shelve
, in that it also maps a key to a value.
The mailbox
module and email.message
modules both have classes that provide an interface that is similar to dict
for the mailbox structure used to manage local e-mails.
As far as design strategies go, we can extend or wrap one of the existing mappings to add even more features.
We could upgrade Counter
to add mean and standard deviation to data stored as a frequency distribution. Indeed, we can also calculate median and mode very easily from this class.
Here's a StatsCounter
extension to Counter
...