Skip to content

Commit df54004

Browse files
committed
ConfigurationView: Improvements from ChainMap
1 parent 70543df commit df54004

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

celery/datastructures.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,8 @@ def items(self):
416416
class ConfigurationView(AttributeDictMixin):
417417
"""A view over an applications configuration dicts.
418418
419+
Custom (but older) version of :class:`collections.ChainMap`.
420+
419421
If the key does not exist in ``changes``, the ``defaults`` dicts
420422
are consulted.
421423
@@ -457,6 +459,10 @@ def get(self, key, default=None):
457459
except KeyError:
458460
return default
459461

462+
def clear(self):
463+
"""Removes all changes, but keeps defaults."""
464+
self.changes.clear()
465+
460466
def setdefault(self, key, default):
461467
try:
462468
return self[key]
@@ -468,10 +474,10 @@ def update(self, *args, **kwargs):
468474
return self.changes.update(*args, **kwargs)
469475

470476
def __contains__(self, key):
471-
for d in self._order:
472-
if key in d:
473-
return True
474-
return False
477+
return any(key in m for m in self._order)
478+
479+
def __bool__(self):
480+
return any(self._order)
475481

476482
def __repr__(self):
477483
return repr(dict(items(self)))
@@ -482,7 +488,7 @@ def __iter__(self):
482488
def __len__(self):
483489
# The logic for iterating keys includes uniq(),
484490
# so to be safe we count by explicitly iterating
485-
return len(self.keys())
491+
return len(set().union(*self._order))
486492

487493
def _iter(self, op):
488494
# defaults must be first in the stream, so values in

0 commit comments

Comments
 (0)