Exemplo n.º 1
0
def valid_duration(representation):
    try:
        Duration(representation)
    except ScaleFormatError:
        return False

    return True
Exemplo n.º 2
0
def timefromstring(s):
    """
    :param s : String to parse time from
    :returns : Time in seconds
    """
    s = s.removeprefix("for")
    try:
        return int(Duration(s).to_seconds())
    except InvalidTokenError:
        return None
Exemplo n.º 3
0
 def setUp(self):
     self.test_duration = Duration('1d')
     self.test_duration2 = Duration('1D')
Exemplo n.º 4
0
class TestDuration(unittest.TestCase):
    def setUp(self):
        self.test_duration = Duration('1d')
        self.test_duration2 = Duration('1D')

    def tearDown(self):
        pass

    def test_conversions(self):
        self.assertEqual(self.test_duration2.to_centuries(), 0.1)
        self.assertEqual(self.test_duration2.to_decades(), 1)
        self.assertEqual(self.test_duration2.to_years(), 10)
        self.assertEqual(self.test_duration2.to_months(), 120)
        self.assertEqual(self.test_duration2.to_weeks(), 522.86)
        self.assertEqual(self.test_duration2.to_days(), 3660)
        self.assertEqual(self.test_duration2.to_hours(), 87840)
        self.assertEqual(self.test_duration2.to_minutes(), 5270400)
        self.assertEqual(self.test_duration2.to_seconds(), 316224000)
        self.assertEqual(self.test_duration2.to_miliseconds(), 316224000000)

    def test_repr_has_valid_representation(self):
        self.assertEqual(self.test_duration.__repr__(), '<Duration 1d>')

    def test_parse_simple_valid_scale(self):
        duration_representation = self.test_duration.parse('1d')
        self.assertTrue(isinstance(duration_representation, list))

        duration_representation = duration_representation[0]
        self.assertEqual(duration_representation.value, 1.0)
        self.assertTrue(isinstance(duration_representation.scale, Scale))
        self.assertEqual(duration_representation.scale.representation.short,
                         'd')

    def test_parse_composed_valid_scale(self):
        duration_representation = self.test_duration.parse(
            '1d, 24h and 36 minutes')  # noqa: E501

        self.assertTrue(isinstance(duration_representation, list))
        self.assertEqual(len(duration_representation), 3)

        first, second, third = duration_representation

        self.assertEqual(first.value, 1.0)
        self.assertTrue(isinstance(first.scale, Scale))
        self.assertEqual(first.scale.representation.short, 'd')

        self.assertEqual(second.value, 24.0)
        self.assertTrue(isinstance(second.scale, Scale))
        self.assertEqual(second.scale.representation.short, 'h')

        self.assertEqual(third.value, 36.0)
        self.assertTrue(isinstance(third.scale, Scale))
        self.assertEqual(third.scale.representation.short, 'm')

    def test_parse_simple_malformed_scale_raises(self):
        self.assertRaises(ScaleFormatError, Duration, 'd1')

    def test_parse_composed_malformed_scale_raises(self):
        self.assertRaises(ScaleFormatError, Duration, '1d h23')
Exemplo n.º 5
0
 async def convert(self, ctx, arg):
     duration = Duration(arg)
     return timedelta(seconds=duration.to_seconds())
Exemplo n.º 6
0
    def __init__(self, filename):
        with open(filename, "r") as f:
            timeline_data = yaml.full_load(f)

        self.data = dict()
        for index, time_spec in timeline_data.items():
            try:
                # if bare int treat as tick value
                tick_index = int(index)
            except:
                # otherwise treat as natural language duration
                tick_index = CLOCK.seconds_to_ticks(
                    Duration(index).to_seconds())
            self.data[tick_index] = defaultdict_rec()

            for note, note_spec in time_spec.items():
                try:
                    note = int(note)
                except:
                    pass

                self.data[tick_index][note]["channel"] = note_spec.get(
                    "channel", 0)

                self.data[tick_index][note]["sticky"] = note_spec.get(
                    "sticky", False)

                action_spec = note_spec.get("action", None)
                if not action_spec:
                    self.data[tick_index][note]["action"] = None
                else:
                    tokens = action_spec.split()
                    if tokens[0] == "print":
                        self.data[tick_index][note]["action"] = PrintAction(
                            " ".join(tokens[1:]))

                # Note Action
                note_output_spec = note_spec.get("note", None)
                if not note_output_spec:
                    self.data[tick_index][note]["note_output"] = note
                else:
                    if isinstance(note_output_spec, str):
                        note_output = []
                        for _note in note_output_spec.split():
                            note_output.append(NOTE_DB.get(_note))
                        note_output = tuple(note_output)
                    else:
                        note_output = note_output_spec
                    self.data[tick_index][note]["note_output"] = note_output

                # LED State
                led_spec = note_spec.get("led", None)

                if not led_spec:
                    led_state_active = LedState(color="orange", state="bright")
                    led_state_inactive = LedState(color="orange", state="dim")
                else:
                    led_spec_active = led_spec.get("active", {})
                    led_spec_inactive = led_spec.get("inactive", {})
                    led_state_active = LedState(
                        color=led_spec_active.get("color", "orange"),
                        state=led_spec_active.get("state", "bright"))
                    led_state_inactive = LedState(
                        color=led_spec_inactive.get("color", "orange"),
                        state=led_spec_inactive.get("state", "dim"))

                self.data[tick_index][note]["led_state"][
                    "active"] = led_state_active
                self.data[tick_index][note]["led_state"][
                    "inactive"] = led_state_inactive
Exemplo n.º 7
0
def to_timedelta(arg):
    duration = Duration(arg)
    return timedelta(seconds=duration.to_seconds())
Exemplo n.º 8
0
 async def convert(self, ctx, arg):
     seconds = Duration(arg).to_seconds()
     if seconds <= 0:
         raise commands.BadArgument("Invalid duration.")
     return timedelta(seconds=seconds)