Andrei
7 years ago
No known key found for this signature in database
GPG Key ID: 4D2A02C7D500E9D9
2 changed files with
16 additions and
9 deletions
-
disco/util/hashmap.py
-
tests/util/hashmap.py
|
|
@ -27,16 +27,12 @@ class HashMap(dict): |
|
|
|
def find_one(self, predicate): |
|
|
|
return next(self.find(predicate), None) |
|
|
|
|
|
|
|
def select(self, *args, **kwargs): |
|
|
|
if kwargs: |
|
|
|
args += tuple(kwargs) |
|
|
|
|
|
|
|
def select(self, **kwargs): |
|
|
|
for obj in self.values(): |
|
|
|
for check in args: |
|
|
|
for k, v in six.iteritems(check): |
|
|
|
if getattr(obj, k) != v: |
|
|
|
break |
|
|
|
yield obj |
|
|
|
for k, v in six.iteritems(kwargs): |
|
|
|
if getattr(obj, k) != v: |
|
|
|
break |
|
|
|
yield obj |
|
|
|
|
|
|
|
def select_one(self, **kwargs): |
|
|
|
return next(self.select(**kwargs), None) |
|
|
|
|
|
@ -44,3 +44,14 @@ def test_hashmap_items(hashmap): |
|
|
|
assert len(list(hashmap.items())) == 100000 |
|
|
|
assert list(hashmap.items())[0][0] == 0 |
|
|
|
assert list(hashmap.items())[0][1] == hashmap[0] |
|
|
|
|
|
|
|
|
|
|
|
def test_hashmap_select_one(): |
|
|
|
class Test(object): |
|
|
|
x = 1 |
|
|
|
y = 2 |
|
|
|
hashmap = HashMap() |
|
|
|
hashmap['x'] = Test() |
|
|
|
|
|
|
|
assert hashmap.select_one(x=1) == hashmap['x'] |
|
|
|
assert hashmap.select_one(x=2) == None |
|
|
|