Skip to content

Commit

Permalink
constants: Drop 'DurationType' and 'ParameterType'
Browse files Browse the repository at this point in the history
This patch helps simplifying the code-base.
Instead of 'DurationType' use 'core_parameters.abc.Duration'.
Instead of 'ParameterType' use the specific parameter abc or
'typing.Any'.
  • Loading branch information
levinericzimmermann committed Nov 16, 2023
1 parent 11d8adf commit 0f4f792
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 118 deletions.
9 changes: 0 additions & 9 deletions mutwo/core_constants/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,5 @@
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)."""

ParameterType = typing.Any
"""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
4 changes: 2 additions & 2 deletions mutwo/core_converters/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def convert(self, simple_event_to_convert: core_events.SimpleEvent) -> typing.An
)


MutwoParameterDict = dict[str, core_constants.ParameterType]
MutwoParameterDict = dict[str, typing.Any]


class MutwoParameterDictToKeywordArgument(core_converters.abc.Converter):
Expand Down Expand Up @@ -105,7 +105,7 @@ def __init__(

def convert(
self, mutwo_parameter_dict_to_convert: MutwoParameterDict
) -> typing.Optional[tuple[str, core_constants.ParameterType]]:
) -> typing.Optional[tuple[str, typing.Any]]:
try:
return (
self._keyword,
Expand Down
28 changes: 9 additions & 19 deletions mutwo/core_events/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import functools
import typing

from mutwo import core_constants
from mutwo import core_events
from mutwo import core_parameters
from mutwo import core_utilities
Expand Down Expand Up @@ -156,10 +155,7 @@ def _event_to_metrized_event(self):
def _set_parameter(
self,
parameter_name: str,
object_or_function: typing.Callable[
[core_constants.ParameterType], core_constants.ParameterType
]
| core_constants.ParameterType,
object_or_function: typing.Callable[[typing.Any], typing.Any] | typing.Any,
set_unassigned_parameter: bool,
id_set: set[int],
) -> Event:
Expand All @@ -169,7 +165,7 @@ def _set_parameter(
def _mutate_parameter(
self,
parameter_name: str,
function: typing.Callable[[core_constants.ParameterType], None] | typing.Any,
function: typing.Callable[[typing.Any], None] | typing.Any,
id_set: set[int],
) -> Event:
...
Expand Down Expand Up @@ -264,7 +260,7 @@ def set(self, attribute_name: str, value: typing.Any) -> Event:
@abc.abstractmethod
def get_parameter(
self, parameter_name: str, flat: bool = False, filter_undefined: bool = False
) -> tuple[core_constants.ParameterType, ...] | core_constants.ParameterType:
) -> tuple[typing.Any, ...] | typing.Any:
"""Return event attribute with the entered name.
:param parameter_name: The name of the attribute that shall be returned.
Expand Down Expand Up @@ -297,10 +293,7 @@ def get_parameter(
def set_parameter(
self,
parameter_name: str,
object_or_function: typing.Callable[
[core_constants.ParameterType], core_constants.ParameterType
]
| core_constants.ParameterType,
object_or_function: typing.Callable[[typing.Any], typing.Any] | typing.Any,
set_unassigned_parameter: bool = True,
) -> typing.Optional[Event]:
"""Sets parameter to new value for all children events.
Expand Down Expand Up @@ -352,7 +345,7 @@ def set_parameter(
def mutate_parameter(
self,
parameter_name: str,
function: typing.Callable[[core_constants.ParameterType], None] | typing.Any,
function: typing.Callable[[typing.Any], None] | typing.Any,
) -> typing.Optional[Event]:
"""Mutate parameter with a function.
Expand Down Expand Up @@ -765,10 +758,7 @@ def _apply_once_per_event(
def _set_parameter( # type: ignore
self,
parameter_name: str,
object_or_function: typing.Callable[
[core_constants.ParameterType], core_constants.ParameterType
]
| core_constants.ParameterType,
object_or_function: typing.Callable[[typing.Any], typing.Any] | typing.Any,
set_unassigned_parameter: bool,
id_set: set[int],
) -> ComplexEvent[T]:
Expand All @@ -783,7 +773,7 @@ def _set_parameter( # type: ignore
def _mutate_parameter( # type: ignore
self,
parameter_name: str,
function: typing.Callable[[core_constants.ParameterType], None] | typing.Any,
function: typing.Callable[[typing.Any], None] | typing.Any,
id_set: set[int],
) -> ComplexEvent[T]:
return self._apply_once_per_event(
Expand Down Expand Up @@ -884,8 +874,8 @@ def get_event_from_index_sequence(

def get_parameter(
self, parameter_name: str, flat: bool = False, filter_undefined: bool = False
) -> tuple[core_constants.ParameterType, ...]:
parameter_value_list: list[core_constants.ParameterType] = []
) -> tuple[typing.Any, ...]:
parameter_value_list: list[typing.Any] = []
for event in self:
parameter_value_or_parameter_value_tuple = event.get_parameter(
parameter_name, flat=flat
Expand Down
26 changes: 11 additions & 15 deletions mutwo/core_events/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import ranges

from mutwo import core_constants
from mutwo import core_events
from mutwo import core_parameters
from mutwo import core_utilities
Expand Down Expand Up @@ -88,10 +87,7 @@ def __repr__(self) -> str:
def _set_parameter(
self,
parameter_name: str,
object_or_function: typing.Callable[
[core_constants.ParameterType], core_constants.ParameterType
]
| core_constants.ParameterType,
object_or_function: typing.Callable[[typing.Any], typing.Any] | typing.Any,
set_unassigned_parameter: bool,
id_set: set[int],
) -> SimpleEvent:
Expand All @@ -107,7 +103,7 @@ def _set_parameter(
def _mutate_parameter(
self,
parameter_name: str,
function: typing.Callable[[core_constants.ParameterType], None] | typing.Any,
function: typing.Callable[[typing.Any], None] | typing.Any,
id_set: set[int],
) -> SimpleEvent:
parameter = self.get_parameter(parameter_name)
Expand Down Expand Up @@ -170,7 +166,7 @@ def destructive_copy(self) -> SimpleEvent:

def get_parameter(
self, parameter_name: str, flat: bool = False, filter_undefined: bool = False
) -> core_constants.ParameterType:
) -> typing.Any:
return getattr(self, parameter_name, None)

# Update docstring
Expand Down Expand Up @@ -575,8 +571,8 @@ def get_event_at(

def cut_out( # type: ignore
self,
start: core_constants.DurationType,
end: core_constants.DurationType,
start: core_parameters.abc.Duration,
end: core_parameters.abc.Duration,
) -> SequentialEvent[T]:
start, end = (
core_events.configurations.UNKNOWN_OBJECT_TO_DURATION(unknown_object)
Expand Down Expand Up @@ -619,8 +615,8 @@ def cut_out( # type: ignore

def cut_off( # type: ignore
self,
start: core_constants.DurationType,
end: core_constants.DurationType,
start: core_parameters.abc.Duration,
end: core_parameters.abc.Duration,
) -> SequentialEvent[T]:
start, end = (
core_events.configurations.UNKNOWN_OBJECT_TO_DURATION(unknown_object)
Expand Down Expand Up @@ -874,7 +870,7 @@ def _make_event_slice_tuple(
# ###################################################################### #

@core_events.abc.ComplexEvent.duration.getter
def duration(self) -> core_constants.DurationType:
def duration(self) -> core_parameters.abc.Duration:
try:
return max(event.duration for event in self)
# If SimultaneousEvent is empty
Expand All @@ -901,8 +897,8 @@ def cut_out( # type: ignore

def cut_off( # type: ignore
self,
start: core_constants.DurationType,
end: core_constants.DurationType,
start: core_parameters.abc.Duration,
end: core_parameters.abc.Duration,
) -> SimultaneousEvent[T]:
start, end = (
core_events.configurations.UNKNOWN_OBJECT_TO_DURATION(unknown_object)
Expand Down Expand Up @@ -947,7 +943,7 @@ def slide_in(
return self

def split_child_at(
self, absolute_time: core_constants.DurationType
self, absolute_time: core_parameters.abc.Duration
) -> SimultaneousEvent[T]:
for event_index, event in enumerate(self):
try:
Expand Down
Loading

0 comments on commit 0f4f792

Please sign in to comment.