Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mapping-like interface #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

turicas
Copy link

@turicas turicas commented Jul 18, 2012

With these little changes we can use any BaseCache child classes as a Python dict (using collections.MutableMapping), with some little limitations compared to dict:

  • iteritems(), keys(), values() and len() and other methods related to getting all keys/values will not work unless you implement __iter__ and __len__ in the child class.
  • If my cache doesn't have the key test and I execute my_cache['test'] it will not raise KeyError like in a dict; it'll return None (as the current BaseCache.get API).

This modification is very handy in cases when we are using dicts to store and then need to persist or cache them in some way, since only the line that creates the dict needs to be replaced.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant