Skip to content

Commit

Permalink
Merge pull request #10 from tacaswell/enh_add_eq
Browse files Browse the repository at this point in the history
ENH: add `__eq__`
  • Loading branch information
WeatherGod committed Aug 16, 2015
2 parents c4e26a4 + 158ab1f commit ddc6e11
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
11 changes: 11 additions & 0 deletions cycler.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,17 @@ def __imul__(self, other):
self._right = copy.copy(other)
return self

def __eq__(self, other):
"""
Check equality
"""
if len(self) != len(other):
return False
if self.keys ^ other.keys:
return False

return all(a == b for a, b in zip(self, other))

def __repr__(self):
op_map = {zip: '+', product: '*'}
if self._right is None:
Expand Down
24 changes: 23 additions & 1 deletion test_cycler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import six
from six.moves import zip, range
from cycler import cycler, Cycler
from nose.tools import assert_equal, assert_raises, assert_true
from nose.tools import (assert_equal, assert_not_equal,
assert_raises, assert_true)
from itertools import product, cycle
from operator import add, iadd, mul, imul

Expand Down Expand Up @@ -160,3 +161,24 @@ def test_call():
assert_equal(a, b)

assert_equal(j, len(c) * 2)


def _eq_test_helper(a, b, res):
if res:
assert_equal(a, b)
else:
assert_not_equal(a, b)


def test_eq():
a = cycler('c', 'rgb')
b = cycler('c', 'rgb')
yield _eq_test_helper, a, b, True
yield _eq_test_helper, a, b[::-1], False
c = cycler('lw', range(3))
yield _eq_test_helper, a+c, c+a, True
yield _eq_test_helper, a+c, c+b, True
yield _eq_test_helper, a*c, c*a, False
yield _eq_test_helper, a, c, False
d = cycler('c', 'ymk')
yield _eq_test_helper, b, d, False

0 comments on commit ddc6e11

Please sign in to comment.