Skip to content
Browse files

* adds weightedpool driver

  • Loading branch information...
1 parent 79b9712 commit f407806f1e31146505c6330516b1ecdd4328fb68 @rongoro rongoro committed
Showing with 68 additions and 0 deletions.
  1. +35 −0 src/clusto/drivers/categories/pool.py
  2. +33 −0 src/clusto/test/drivers/categories/pooltests.py
View
35 src/clusto/drivers/categories/pool.py
@@ -59,3 +59,38 @@ def getPools(cls, obj, allPools=True):
return pools
+
+class WeightedPool(Pool):
+ """Weighted Pool
+
+ Just like a pool but allows its contents to have weights using the
+ set/getWeight functions.
+
+ The property 'defaultweight' can be set to set a default weight items
+ in the pool.
+ """
+
+ _driverName = "weightedpool"
+
+
+ _properties = {'defaultweight': None }
+
+ def setWeight(self, thing, weight):
+
+ if thing not in self:
+ raise LookupError("%s is not in this pool." % thing)
+
+ self.setAttr("weight", numbered=weight, value=thing)
+
+ def getWeight(self, thing):
+
+ if thing not in self:
+ raise LookupError("%s is not in this pool." % thing)
+
+ thingattr = self.attrs("weight", value=thing)
+
+ if thingattr:
+ return thingattr[0].number
+ else:
+ return self.defaultweight
+
View
33 src/clusto/test/drivers/categories/pooltests.py
@@ -157,3 +157,36 @@ def testPoolAttrsOverride(self):
+
+class WeightedPoolTests(testbase.ClustoTestBase):
+
+ def testSetWeight(self):
+
+ s = BasicServer('s')
+ p = WeightedPool('p')
+
+ p.insert(s)
+ p.setWeight(s, 45)
+
+ self.assertEqual(45, p.getWeight(s))
+
+ def testSetWeightNotInPool(self):
+
+ s = BasicServer('s')
+ p = WeightedPool('p')
+
+ self.assertRaises(LookupError, p.setWeight, s, 45)
+
+ def testDefaultWeight(self):
+
+
+ s = BasicServer('s')
+ p = WeightedPool('p')
+
+ p.insert(s)
+
+ self.assertEqual(None, p.getWeight(s))
+
+ p.defaultweight = 33
+
+ self.assertEqual(33, p.getWeight(s))

0 comments on commit f407806

Please sign in to comment.
Something went wrong with that request. Please try again.