]> asedeno.scripts.mit.edu Git - bluechips.git/blobdiff - bluechips/model/expenditure.py
little performance tweaks
[bluechips.git] / bluechips / model / expenditure.py
index 59867238bb46018fb2d0d224c181b758b83acb94..9d0fc7e0f981ad2719f2435789199a934b5c5bca 100644 (file)
@@ -65,9 +65,6 @@ class Expenditure(object):
                 del split_dict[user]
             else:
                 split_dict[user] = share / total
-        
-        for user, share in split_dict.iteritems():
-            split_dict[user] = share / total
             
         amounts_dict = dict()
         
@@ -81,12 +78,23 @@ class Expenditure(object):
                 winner = random.choice(amounts_dict.keys())
                 amounts_dict[winner] += Currency(1)
         elif difference < 0:
-            for i in xrange(difference):
+            for i in xrange(-difference):
                 winner = random.choice(amounts_dict.keys())
                 amounts_dict[winner] -= Currency(1)
         
         for user, share in amounts_dict.iteritems():
             s = Split(self, user, share)
-            meta.Session.save(s)
+            meta.Session.add(s)
+
+    def involves(self, user):
+        "Returns True if ``user`` is involved in this expenditure."
+        return any((split.user == user) and (split.share != 0)
+                   for split in self.splits)
+
+    def share(self, user):
+        "Return the share corresponding to ``user``."
+        shares = dict((split.user, split.share)
+                      for split in self.splits)
+        return shares.get(user, Currency(0))
 
 __all__ = ['Expenditure']