Python: avoid fractional simplification

I am working on a music application in Python and would like to use the fraction module to process the time signature by the way. My problem is that the fractions are simplified, i.e.:

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

However, from a musical point of view, it is important that 4/4 remains 4/4, even if it is 1. Is there a built-in way to avoid this behavior?

Thank!

+3
source share
3 answers

Yes: create your own class for it.

Music time signatures are not fractions, so it makes no sense to represent them using a mathematical class.

+9
source

, . , . , , .

+3

No no. GCD division is built into fraction class operations. Other reasons you probably want to create your own class, at least if you work with musical notation, are:

  • to be able to handle total time (C) and reduce time (slash-C) replacements for 4/4 and 2/4 - or even better to make it optional
  • to be able to represent composite time signatures (e.g. 2 + 3 + 2/8)
0
source

Source: https://habr.com/ru/post/1746127/


All Articles