Skip to content

Commit

Permalink
Merge pull request #20 from yuma-m/feature/chord-eq
Browse files Browse the repository at this point in the history
Fix __eq__ of Chord and support __eq__ for Quality
  • Loading branch information
yuma-m authored Feb 25, 2018
2 parents 5a00d1d + 9d23aea commit e143f2f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 12 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ on=None
['A', 'C', 'E', 'G']
```

### Compare Chords

```python
>>> Chord("C") == Chord("D")
False
>>> Chord("C#") == Chord("Db")
True
>>> c = Chord("C")
>>> c.transpose(2)
>>> c == Chord("D")
True
```

### Find Chords

```python
Expand Down
13 changes: 11 additions & 2 deletions pychord/chord.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


from .parser import parse
from .utils import transpose_note, display_appended, display_on
from .utils import transpose_note, display_appended, display_on, note_to_val


class Chord(object):
Expand Down Expand Up @@ -33,7 +33,16 @@ def __repr__(self):
return "<Chord: {}>".format(self._chord)

def __eq__(self, other):
return self._chord == other.chord
if note_to_val(self._root) != note_to_val(other.root):
return False
if self._quality != other.quality:
return False
if self._appended != other.appended:
return False
if self._on and other.on:
if note_to_val(self._on) != note_to_val(other.on):
return False
return True

def __ne__(self, other):
return not self.__eq__(other)
Expand Down
10 changes: 8 additions & 2 deletions pychord/quality.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ def __unicode__(self):
def __str__(self):
return self._quality

def __eq__(self, other):
return self._quality == other.quality

def __ne__(self, other):
return not self.__eq__(other)

@property
def quality(self):
""" Get name of quality """
Expand Down Expand Up @@ -59,8 +65,8 @@ def append_on_chord(self, on_chord, root):
root_val = note_to_val(root)
on_chord_val = note_to_val(on_chord) - root_val

lista = list(self.components)
for idx, val in enumerate(lista):
list_ = list(self.components)
for idx, val in enumerate(list_):
if val % 12 == on_chord_val:
self.components.remove(val)
break
Expand Down
19 changes: 11 additions & 8 deletions test/test_transpose.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,41 +13,44 @@ def test_transpose_zero(self):
c.transpose(0)
self.assertEqual(c.root, "A")
self.assertEqual(c.quality.quality, "m")
self.assertEqual(c, Chord("Am"))

def test_transpose_positive(self):
c = Chord("Am")
c.transpose(3)
self.assertEqual(c.root, "C")
self.assertEqual(c.quality.quality, "m")
self.assertEqual(c, Chord("Cm"))

def test_transpose_negative(self):
c = Chord("Am")
c.transpose(-4)
self.assertEqual(c.root, "F")
self.assertEqual(c.quality.quality, "m")
self.assertEqual(c, Chord("Fm"))

def test_transpose_slash(self):
c = Chord("Am7/G")
c.transpose(3)
self.assertEqual(c.root, "C")
self.assertEqual(c.quality.quality, "m7")
self.assertEqual(c.on, "Bb")
self.assertEqual(c, Chord("Cm7/Bb"))

def test_invalid_transpose_type(self):
c = Chord("Am")
self.assertRaises(TypeError, c.transpose, "A")

def test_transpose_eq1(self):
c1 = Chord("C")
c1.transpose(1)
c2 = Chord("Db")
self.assertEqual(c1, c2)
c = Chord("C")
c.transpose(1)
self.assertEqual(c, Chord("C#"))
self.assertEqual(c, Chord("Db"))

def test_transpose_eq2(self):
c1 = Chord("C")
c1.transpose(2)
c2 = Chord("D")
self.assertEqual(c1, c2)
c = Chord("C")
c.transpose(2)
self.assertEqual(c, Chord("D"))


if __name__ == '__main__':
Expand Down

0 comments on commit e143f2f

Please sign in to comment.