49 lines
1.1 KiB
Python
49 lines
1.1 KiB
Python
from __future__ import absolute_import
|
|
|
|
import collections
|
|
from abc import abstractmethod
|
|
|
|
|
|
class DefaultMapping(collections.MutableMapping):
|
|
|
|
__slots__ = ()
|
|
|
|
@abstractmethod
|
|
def __contains__(self, key): # pragma: nocover
|
|
return False
|
|
|
|
@abstractmethod
|
|
def __getitem__(self, key): # pragma: nocover
|
|
if hasattr(self.__class__, '__missing__'):
|
|
return self.__class__.__missing__(self, key)
|
|
else:
|
|
raise KeyError(key)
|
|
|
|
def get(self, key, default=None):
|
|
if key in self:
|
|
return self[key]
|
|
else:
|
|
return default
|
|
|
|
__marker = object()
|
|
|
|
def pop(self, key, default=__marker):
|
|
if key in self:
|
|
value = self[key]
|
|
del self[key]
|
|
elif default is self.__marker:
|
|
raise KeyError(key)
|
|
else:
|
|
value = default
|
|
return value
|
|
|
|
def setdefault(self, key, default=None):
|
|
if key in self:
|
|
value = self[key]
|
|
else:
|
|
self[key] = value = default
|
|
return value
|
|
|
|
|
|
DefaultMapping.register(dict)
|