Skip to content

Instantly share code, notes, and snippets.

@dtheodor
Created January 27, 2014 13:54
Show Gist options
  • Save dtheodor/8648940 to your computer and use it in GitHub Desktop.
Save dtheodor/8648940 to your computer and use it in GitHub Desktop.
GaTransactionConf = namedtuple("GaTransactionConf", ["sku", "name", "category"])
class GaTransactionsDict(dict):
def __getitem__(self, key):
if isinstance(key, GaTransactionConf):
for mapping in self.keys():
if cmp_ga_conf(key, mapping):
key = mapping #update the key
return dict.__getitem__(self, key)
def cmp_ga_conf(record, goal_mapping):
if ((goal_mapping.sku is None or record.sku in goal_mapping.sku) and
(goal_mapping.name is None or record.name in goal_mapping.name) and
(goal_mapping.category is None or record.category in goal_mapping.category)):
return True
else:
return False
a = GaTransactionConf(sku=(1,2), name=(3,4), category=None)
goal_dict = GaTransactionsDict({a:1})
print goal_dict
b = GaTransactionConf(sku=(1,2), name=(3,4), category=None)
assert goal_dict[b] is goal_dict[a]
c = GaTransactionConf(sku=1,name=3,category=None)
assert goal_dict[c] == goal_dict[a]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment