Skip to content

Commit

Permalink
Set 'quicktions' to optional imports
Browse files Browse the repository at this point in the history
With b11b213 quicktions was added as a
mandatory requirement to 'mutwo.core'. This happened after the interal
representations of durations were changed from 'floats' to 'fractions':
Afterwards the performance of 'mutwo.core' significantly dropped.

Follow-up commits further improved the performance by using 'floats'
instead of 'fractions' for internal calculations [1]. So the performance
could be much more improved by simply using 'floats' instead of
'fractions' for internal calculations than by replacing 'fractions'
with 'quicktions'. Furthermore nowadays we plan for version
2.0.0 to actually revert the internal representation of durations to
float [2].

Due to these reasons it seems to be better to drop 'quicktions' again as
a mandatory requirement: then 'mutwo' has less dependencies, which
simplifies the installation and the maintenance (these are very important
long-goal terms). The first step to do so is to change the 'mutwo.core' codebase
to only optionally import 'quicktions' in case it has been installed.
The final drop of the 'quicktions' requirement can be done in version
2.0.0.

[1] see for instance fc49bb7
[2] https://github.com/mutwo-org/mutwo.core/blob/7d39b80/ROADMAP.md?plain=1#L4
  • Loading branch information
levinericzimmermann committed Sep 15, 2023
1 parent d0f2cc2 commit e3ee75a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 14 deletions.
13 changes: 11 additions & 2 deletions mutwo/core_constants/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
import fractions
import typing

import quicktions
try:
import quicktions
except ImportError:
quicktions = None

Real = float | fractions.Fraction | quicktions.Fraction | int
Real = float | fractions.Fraction | int
"""The main reason for this constant is a mypy issue with Pythons buildin
[numbers module](https://docs.python.org/3/library/numbers.html) which
is documented [here](https://github.com/python/mypy/issues/3186). Mypy
Expand All @@ -16,6 +19,9 @@
'float', but this wouldn't include `fractions.Fraction` which is often
necessary in musical contexts (as github user arseniiv also remarked)."""

if quicktions:
Real |= quicktions.Fraction

DurationType = Real
"""Type variable to arguments and return values for `duration`.
This can be any real number (float, integer, fraction)."""
Expand All @@ -24,3 +30,6 @@
"""Type variable to assign to arguments and return values
which expect objects from the :mod:`mutwo.core.parameters` module,
but could actually be anything."""

# Cleanup
del fractions, quicktions, typing
28 changes: 19 additions & 9 deletions mutwo/core_events/configurations.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,17 @@
import functools
import typing

import quicktions
try:
import quicktions
except ImportError:
quicktions = None


def _add_quicktions(type_tuple: tuple[typing.Type, ...]) -> tuple[typing.Type, ...]:
if quicktions:
type_tuple += (quicktions.Fraction,)
return type_tuple


# We can't set core_converters.UnknownObjectToObject
# directly because it would raise a circular import error.
Expand All @@ -22,7 +32,10 @@ def string_to_duration(string: str):
if "." in string:
f = float
elif "/" in string:
f = quicktions.Fraction
if quicktions:
f = quicktions.Fraction
else:
f = fractions.Fraction
else:
f = int
try:
Expand All @@ -34,7 +47,7 @@ def string_to_duration(string: str):
return core_converters.UnknownObjectToObject[core_parameters.abc.Duration](
(
(
(float, int, fractions.Fraction, quicktions.Fraction),
_add_quicktions((float, int, fractions.Fraction)),
core_parameters.DirectDuration,
),
((str,), string_to_duration),
Expand Down Expand Up @@ -110,13 +123,10 @@ def __unknown_object_to_tempo_point():
from mutwo import core_converters
from mutwo import core_parameters

type_tuple = _add_quicktions((float, int, fractions.Fraction))

return core_converters.UnknownObjectToObject[core_parameters.abc.TempoPoint](
(
(
(float, int, fractions.Fraction, quicktions.Fraction),
core_parameters.DirectTempoPoint,
),
)
((type_tuple, core_parameters.DirectTempoPoint),)
)


Expand Down
17 changes: 14 additions & 3 deletions mutwo/core_parameters/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@
import operator
import typing

import quicktions as fractions
import fractions as _fractions
try:
import quicktions as fractions
except ImportError:
import fractions
_fractions = None
else:
import fractions as _fractions

import ranges

Expand Down Expand Up @@ -251,9 +256,15 @@ class Duration(
property :attr:`duration` has to be overridden.
The attribute :attr:`duration` is stored in unit `beats`.
The ``duration`` of :mod:`mutwo` events are therefore not
related to a clear physical unit as for instance seconds.
The reason for this decision is to simplify musical usage.
"""

direct_comparison_type_tuple = (float, int, fractions.Fraction, _fractions.Fraction)
direct_comparison_type_tuple = (float, int, fractions.Fraction)
if _fractions:
direct_comparison_type_tuple += (_fractions.Fraction,)

def _math_operation(
self, other: DurationOrReal, operation: typing.Callable[[float, float], float]
Expand Down

0 comments on commit e3ee75a

Please sign in to comment.