HPInc / HP-Digital-Microfluidics

HP Digital Microfluidics Software Platform and Libraries
MIT License
3 stars 1 forks source link

Make it easier to set default units #179

Closed EvanKirshenbaum closed 8 months ago

EvanKirshenbaum commented 8 months ago

While working on the ability to specify default units to use (#167), I added, Exerciser.set_default_units(), which takes the units found on that argument (which may include several and be specified several times).

While switching to platform-dispatched Exercisers (#176), I realized that I had to specify default default units before adding args to the ArgumentParer so that help strings would have reasonable units for default values.

For the former, it would be easier if I could simply have said something like

set_default_units(*units_from_cmd_line)

and then do the latter as simply

set_default_units(ms, uL, deg_C, volts)

likely making that list a property of the Exerciser, which could be specified as a constructor parameter.

The other problem with specifying the default defaults in setup_task() is that it would be nicer if it were localized, so that you could say

with default_units(ms, uL, deg_C, volts):
    task.add_args_to(group, parser, exerciser=self)
    self.add_common_args_to(parser)

and have them reset back to what they were at the end.

Note that this won't really work right with multiple threads, since default units are a global notion. I could probably use threading.local to make a dimension's default units thread-local, but there doesn't seem to be any easy way to get the value to inherit its value from the parent thread (short of making sure that we use a subclass of Thread that copies at least threading.get_ident() into thread-local data for the new thread. This may be more mechanism than is warranted here,

Also, it will probably still be astonishing if a callback is registered to a Delayed object and invoked in a different thread. Come to think of it, with futures, this whole thing will be weird, as the scope will likely be exited before the value is posted to the future.

This clearly needs more thought.

Migrated from internal repository. Originally created by @EvanKirshenbaum on Jun 18, 2022 at 10:13 PM PDT. Closed on Jun 19, 2022 at 11:44 AM PDT.
EvanKirshenbaum commented 8 months ago

This issue was referenced by the following commits before migration:

EvanKirshenbaum commented 8 months ago

Threading issues aside (as they typically are in Python), it was all pretty straightforward. You can now say

set_default_units(s, ms, us)

with default_units(s, ms, V):
   ...

I also generalized the handling of --units (and dimensional arguments in general) to allow UnitExprs rather than just Units.

Migrated from internal repository. Originally created by @EvanKirshenbaum on Jun 19, 2022 at 11:44 AM PDT.