views:

78

answers:

3

Hi all,

I'm working on a music app' in Python and would like to use the fractions module to handle time signatures amongst other things. My problem is that fractions get simplified, i.e.:

>>> from fractions import Fraction
>>> x = Fraction(4, 4)
>>> x
Fraction(1, 1)

However, it is important from a musical point of view that 4/4 stays 4/4 even though it equals 1. Is there any built-in way to avoid that behaviour?

Thanks!

+3  A: 

You should use some data structure other than Fraction. Just a plain tuple would be simplest, though you could also make your own class. If you need to do calculations, do them on the individual integers that make up the time signature.

John Y
+8  A: 

Yes: make a custom class for it.

Musical time signatures are not fractions, so it doesn't make sense to represent them with a math class.

Andrew McGregor
No problem, I figured it was better to ask before reinventing the wheel. And indeed, they are no fractions, but it would have simplified things to treat them as such in other parts of the app.
Anthony Labarre
Do not even invent a class for this. Use a `namedtuple`.
S.Lott
Actually, I would... but that's a matter of music theory. It's not just a pair of numbers, there are some reasonable operations you might want to do on a time signature; add or remove beats taking say 3/4 to 4/4, do a 3-against-2 taking 4/4 to 12/8, split the bar taking 5/4 to 3/4 and 2/4, and various others. It makes sense to have methods for these.
Andrew McGregor
A: 
Owen S.