From 284caf03c310a1b58603b600c6204a681e232876 Mon Sep 17 00:00:00 2001 From: Will Thompson Date: Mon, 7 Mar 2016 18:52:56 +0000 Subject: [PATCH] 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. --- pandora/item/utils.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pandora/item/utils.py b/pandora/item/utils.py index 238b5564..ba272104 100644 --- a/pandora/item/utils.py +++ b/pandora/item/utils.py @@ -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)