forked from 0x2620/pandora
get_by_key: short-circuit
This is about 30% faster, presumably because it avoids allocation and/or closing over variables is slow(?). It's not hugely significant (I misread a line_profile report) but why not.
This commit is contained in:
parent
7ac68697d4
commit
284caf03c3
1 changed files with 5 additions and 2 deletions
|
@ -75,8 +75,11 @@ def get_positions(ids, pos):
|
|||
return positions
|
||||
|
||||
def get_by_key(objects, key, value):
|
||||
obj = filter(lambda o: o.get(key) == value, objects)
|
||||
return obj and obj[0] or None
|
||||
for o in objects:
|
||||
if o.get(key) == value:
|
||||
return o
|
||||
|
||||
return None
|
||||
|
||||
def get_by_id(objects, id):
|
||||
return get_by_key(objects, 'id', id)
|
||||
|
|
Loading…
Reference in a new issue