Commit 91fcd576 authored by Ayush Tiwari's avatar Ayush Tiwari

erp5_catalog: Update the Filter and FilterDict class removing the useless methods

More comments about dict-like objects in Python:
- do not use .keys() whenever you don't need a list copy of keys
- do not implement a has_key method that would only be an alias of __contains__ (btw, there's still a lot of uses on dict.has_key in ERP5 and they should be replaced by in)
- do not implement a get method that calls __getitem__ unless it is used often; if performance is important, you may consider to duplicate the code of __getitem__
- similarly, we could remove the keymethod: the caller could still cast to list

/suggested by @jm
parent 35a352e0
...@@ -83,14 +83,16 @@ class Filter(object): ...@@ -83,14 +83,16 @@ class Filter(object):
def get(self, key, default=None): def get(self, key, default=None):
try: try:
return self.__getitem__(key) return self[key]
except KeyError: return default except KeyError:
return default
def __setitem__(self, key, value): def __setitem__(self, key, value):
self._method._setProperty(key, value) self._method._setProperty(key, value)
def __iter__(self): def __iter__(self):
return iter(('type', 'expression_cache_key', 'expression', 'filtered', 'expression_instance')) return iter(('type', 'expression_cache_key', 'expression',
'filtered', 'expression_instance'))
class FilterDict(object): class FilterDict(object):
""" """
...@@ -129,25 +131,22 @@ class FilterDict(object): ...@@ -129,25 +131,22 @@ class FilterDict(object):
def keys(self): def keys(self):
return self._catalog.getFilterDict().keys() return self._catalog.getFilterDict().keys()
def has_key(self, method_id):
return method_id in self.keys()
def __setitem__(self, key, item): def __setitem__(self, key, item):
filter_ = self.__getitem__(key) filter_ = self[key]
for k, v in item.items(): for k, v in item.iteritems():
filter_.__setitem__(k, v) filter_[k] = v
def get(self, key, default=None): def get(self, key, default=None):
if key in self: try:
return self.__getitem__(key) return self[key]
else: except KeyError:
return default return default
def __iter__(self): def __iter__(self):
return iter(self.keys()) return iter(self._catalog.getFilterDict())
def __contains__(self, item): def __contains__(self, item):
return item in self.keys() return item in self._catalog.getFilterDict()
class ERP5Catalog(Folder, Catalog): class ERP5Catalog(Folder, Catalog):
""" """
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment