You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.3 KiB

import six
from six.moves import filter, map
from collections import defaultdict
from UserDict import IterableUserDict
class HashMap(IterableUserDict):
def items(self):
return six.iteritems(self.data)
def keys(self):
return six.iterkeys(self.data)
def values(self):
return six.itervalues(self.data)
def find(self, predicate):
if not callable(predicate):
raise TypeError('predicate must be callable')
for obj in self.values():
if predicate(obj):
yield obj
def find_one(self, predicate):
return next(self.find(predicate), None)
def select(self, **kwargs):
for obj in self.values():
for k, v in six.iteritems(kwargs):
if getattr(obj, k) != v:
continue
yield obj
def select_one(self, **kwargs):
return next(self.select(**kwargs), None)
def filter(self, predicate):
if not callable(predicate):
raise TypeError('predicate must be callable')
return filter(self.values(), predicate)
def map(self, predicate):
if not callable(predicate):
raise TypeError('predicate must be callable')
return map(self.values(), predicate)
class DefaultHashMap(defaultdict, HashMap):
pass