Skip to content

Units#93

Merged
da-h merged 2 commits intov5-devfrom
units
Feb 14, 2023
Merged

Units#93
da-h merged 2 commits intov5-devfrom
units

Conversation

@da-h
Copy link
Copy Markdown
Owner

@da-h da-h commented Nov 11, 2021

Units

This MR introduces Units into miniflask.
Those are custom numbers that share multiple representations.

TODO:

  • check syntax for better simplicity. it seems to me that more complicated units can be defined very good currently, but easier ones (like time, with a fixed set of conversions) are cumbersome to define. Can we do better?
  • add unit tests
  • add examples to documentation

Example

First, we define time to be a unit with four different representations.:

def convert_time_unit(src_value, src_unit, dest_unit):
    factors = {
        "second": 1,  # base unit
        "minute": 60,
        "hour": 60 * 60,
        "day": 60 * 60 * 24,
    }

    # first convert to base, then to dest unit
    return src_value * factors[src_unit] / factors[dest_unit]

time = mf.register_unit("time", convert_time_unit, [
    ["second", "s"],
    ["minute", "m"],
    ["hour", "h"],
    ["day", "d"]
])

In CLI, we can overwrite this unit using:

  • --time 1.5d (1.5 days)
  • --time 24h (24 hour)
  • --time 500m (500 minutes)

Using u in v Notation, we can specify the base unit to convert all calculations to.:

  • --time 12h in d (0.5 days)
  • --time 1.5d in h (18 hours)

To register a unit, pass it to register_defaults as follows:
Note, that the base unit is always the one that is passed last to the variable.
In the example below, time uses hours internally, time2 and time3 use days internally.

mf.register_defaults({
    "time": time(6.0, "h"),
    "time2": time(6.0, "h in d"), # in notation works here as well
    "time3": time(6.0, "d")
})

Querying other representations can be done easily, once defined:

state["time"].unit # the base unit name
state["time"].second
state["time"].minute
state["time"].hour
state["time"].day

Calculations works similarly:

state["time"].second += 10
state["time"].minute += 10
state["time"].hour += 10
state["time"].day += 10

Things done in this MR

  • added internal classes for units (Unit, UnitValue, UnitValueArgparse)
  • added Units to Argparse / CLI-Parsing
  • added mf.register_unit funciton

Check all before creating this PR:

  • Documentation adapted
  • unit tests adapted / created

@da-h da-h linked an issue Nov 11, 2021 that may be closed by this pull request
@da-h da-h force-pushed the units branch 2 times, most recently from 8ec24bf to 4e4c042 Compare November 12, 2021 23:15
@da-h da-h changed the title Draft: Units Units Nov 12, 2021
@da-h da-h requested a review from sbrodehl November 12, 2021 23:20
Comment thread src/miniflask/util.py
Comment thread tests/state/units/modules/defineunits/__init__.py Outdated
Comment thread src/miniflask/util.py Outdated
self.converter = converter_fn

def __call__(self, value, unit):
if " in " in unit:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in can be a variable so that it can be set easier. Is a regex necessary, or is the str compare sufficient?

@da-h da-h force-pushed the units branch 2 times, most recently from 86f4401 to df1b7be Compare December 20, 2022 15:07
@da-h da-h changed the base branch from master to v5-dev December 20, 2022 15:16
@da-h da-h mentioned this pull request Jan 9, 2023
@da-h da-h requested a review from sbrodehl January 10, 2023 14:50
@da-h da-h force-pushed the units branch 3 times, most recently from 3829fae to f81418a Compare February 13, 2023 21:14
tests: added conversion tests

tests: adapted unit-feature tests
@da-h
Copy link
Copy Markdown
Owner Author

da-h commented Feb 13, 2023

I am quite confident and positive towards the current design decisions, having it tried already in a testing environment of larger scale. ;)

Unfortunately, there is still one thing i am not very happy with, namely the length of the example in the documentation. Do you have possibly a easier solution using the current method-signature of the unit-definition, @sbrodehl ?

@da-h da-h requested review from sbrodehl and removed request for sbrodehl February 13, 2023 21:21
@da-h da-h merged commit ee8d3f2 into v5-dev Feb 14, 2023
@da-h da-h deleted the units branch February 14, 2023 10:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Consider adding units

2 participants