Skip to content

Commit

Permalink
Revert code for __eq__. Added magic methods for <,>,<=,>=
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsekar committed May 26, 2018
1 parent f282469 commit f2e12aa
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
20 changes: 20 additions & 0 deletions tests/test_indexer.py
Expand Up @@ -262,3 +262,23 @@ def test_indexer_eq_ne(self):
I6 = I4[I1!=[1,2]]
self.assertTrue(I6['c']=='r' and I6['d']=='s')
self.assertTrue('a' not in I6 and 'b' not in I6)

def test_indexer_lt_le_gt_ge(self):
I1 = NumericIndexer().update(dict(a=1,b=2,c=3,d=4))
I2 = NumericIndexer().update(dict(a=1,b=1,c=2,d=5))

x = I1 < I2
for key in ['a','b','c']: self.assertTrue(not x[key])
for key in ['d']: self.assertTrue(x[key])

x = I1 <= I2
for key in ['b','c']: self.assertTrue(not x[key])
for key in ['a','d']: self.assertTrue(x[key])

x = I1 > I2
for key in ['b','c']: self.assertTrue(x[key])
for key in ['a','d']: self.assertTrue(not x[key])

x = I1 >= I2
for key in ['a','b','c']: self.assertTrue(x[key])
for key in ['d']: self.assertTrue(not x[key])
41 changes: 39 additions & 2 deletions wc_rules/indexer.py
Expand Up @@ -214,8 +214,12 @@ def flush(self):

def __eq__(self,other):
if isinstance(other,Indexer):
keys =(key for key in self if self[key]==other[key])
return Slicer(default=False).add_keys(list(keys))
S = Slicer(default=False)
for val in self.value_cache:
if val in other.value_cache:
x = self.slice([val]) & other.slice([val])
S = S | x
return S
if isinstance(other,self.primitive_type):
return self.slice([other])
if isinstance(other,list):
Expand All @@ -229,7 +233,40 @@ class BooleanIndexer(Indexer):
primitive_type = bool

class NumericIndexer(Indexer):
'''
In additon to operators `==` and `!=`, NumericIndexer also supports `>`,`>=`, `<` and `<=`
I1 > I2 returns a slice for all keys in I1 whose values in I1 are greater than their values in I2
I > value returns a slice for all keys in I mapped to values greater than `value`
I > list_of_values is not supported
'''
primitive_type = (int,float,)

def __lt__(self,other):
if isinstance(other,Indexer):
keys =(key for key in self if key in other and self[key] < other[key])
return Slicer(default=False).add_keys(list(keys))
if isinstance(other,self.primitive_type):
return self.slice(lambda x: x < other)
raise utils.IndexerError('To use __le__, __lt__, __ge__,__gt__, either compare two Indexers, or an indexer and a compatible value.')

def __gt__(self,other):
if isinstance(other,Indexer):
keys =(key for key in self if key in other and self[key] > other[key])
return Slicer(default=False).add_keys(list(keys))
if isinstance(other,self.primitive_type):
return self.slice(lambda x: x > other)
raise utils.IndexerError('To use __le__, __lt__, __ge__,__gt__, either compare two Indexers, or an indexer and a compatible value.')

def __le__(self,other):
x1 = self < other
x2 = self == other
return x1|x2

def __ge__(self,other):
x1 = self > other
x2 = self == other
return x1|x2

class StringIndexer(Indexer):
primitive_type = str

0 comments on commit f2e12aa

Please sign in to comment.