Skip to content

Commit

Permalink
Somewhat unsatisfying/halfway solution to an issue with initializing …
Browse files Browse the repository at this point in the history
…a frequency band with hz instances. Ideally, no raw floats or ints are ever used to represent hz, the same way time is represented wiht a semantically meaningful type
  • Loading branch information
JohnVinyard committed Mar 8, 2019
1 parent dc781e1 commit 1fed45a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
19 changes: 14 additions & 5 deletions zounds/spectral/frequencyscale.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,26 @@

class Hertz(float):
def __init__(self, hz):
self.hz = hz
try:
self.hz = hz.hz
except AttributeError:
self.hz = hz

def __neg__(self):
return Hertz(-self.hz)

def __add__(self, other):
return Hertz(self.hz + other.hz)
try:
other = other.hz
except AttributeError:
pass
return Hertz(self.hz + other)

def __float__(self):
return self.hz



class Hz(Hertz):
pass
Hz = Hertz


# TODO: What commonalities can be factored out of this class and TimeSlice?
Expand Down
7 changes: 7 additions & 0 deletions zounds/spectral/test_frequencyscale.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ def test_can_subtract_hertz(self):


class FrequencyBandTests(unittest2.TestCase):

def test_can_create_frequency_band_with_hertz_instances(self):
fb = FrequencyBand(Hz(20), Hz(20000))
self.assertIsInstance(fb.center_frequency, float)
self.assertEqual(20, fb.start_hz)
self.assertEqual(20000, fb.stop_hz)

def test_can_create_from_center_frequency(self):
fb = FrequencyBand.from_center(1000, 50)
self.assertEqual(FrequencyBand(975, 1025), fb)
Expand Down

0 comments on commit 1fed45a

Please sign in to comment.