Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: Allow ureg.wraps to convert string arguments #711

Closed
lewisamarshall opened this issue Oct 6, 2018 · 5 comments · Fixed by #962
Closed

Proposal: Allow ureg.wraps to convert string arguments #711

lewisamarshall opened this issue Oct 6, 2018 · 5 comments · Fixed by #962

Comments

@lewisamarshall
Copy link

Hi! I really like pint, and I'm designing a package that uses it for scientific computations.
I'd like to propose an extension that I would find useful. This extension would automatically convert strings to Quantities on wrapping functions. Example code is below.

@ur.wraps(ur.second, ur.meter, strict=False)
def test(l):
    return l*2

test('1 mm')
# Actual result:    <Quantity(1 mm1 mm, 'second')>
# Desired result: <Quantity(0.002, 'second')>

This behavior could be placed behind another keyword argument, for example convert_strings=True.

The use case here is as follows:

  • My module is designed to be part of an scientific stack workflow, where users import objects to perform specific calculations.
  • I want all calculations to use the same unitregistry. To do this, I would need users to import my_module.ureg to make Quantities to pass into my functions. It would also require that the end user understand pint.
  • Allowing strings would mean that users could directly write their units into the calculation inputs without an intermediate layer. The example from the "Wrapping and Checking" documentation would be pendulum_period("10 cm"). This behavior seems very intuitive to me.

If you think this is a good idea, I'd be happy to submit a PR for it.

@jondoesntgit
Copy link
Contributor

jondoesntgit commented Oct 7, 2018

As far as using the same registry, I usually type from pint import _DEFAULT_REGISTRY as u

@jondoesntgit
Copy link
Contributor

Can you describe your workflow a little more? If users already understand Pint, can they use Quantity objects? Are you parsing text files?

@lewisamarshall
Copy link
Author

Sure. The module I'm talking about is ebb, which is a pipe flow calculation tool.

The tutorial is here.
https://nbviewer.jupyter.org/github/lewisamarshall/ebb/blob/tutorial_update/tutorial.ipynb

The intended audience is fluidic engineers with novice or higher python skill. Anyone interested in performing a fluid flow calculation should be able to import the module and start modeling in a jupyter notebook.

This module is a work in progress. It has the functionality that I'm interested in, but through a different mechanism. At the time I started working on it, I wasn't aware of ureg.wraps.

Typical code that would use the proposed functionality is below.

from ebb.pipes import CircularPipe; from ebb.fluids import Water
pipe = CircularPipe(radius='1 mm', length='10 cm')
pipe.fluid(Water)
print(pipe.maximum_velocity(pressure='0.1 psi'))

This code would also work if the strings were replaced by Quantities. The output of functions are all Quantities.

@lewisamarshall
Copy link
Author

Regarding _DEFAULT_REGISTRY, I appreciate this information, but it seems like it requires more knowledge from the end user rather than less.

Thanks for taking the time to consider this! ✨

@hgrecco
Copy link
Owner

hgrecco commented Oct 12, 2018

I think we can accept that PR that parses strings into quantities within wraps but only if strict=True.

@hgrecco hgrecco mentioned this issue Dec 28, 2019
5 tasks
bors bot added a commit that referenced this issue Dec 28, 2019
962: Improved wraps and check r=hgrecco a=hgrecco

- [x] Closes #711, #723
- [x] Executed ``black -t py36 . && isort -rc . && flake8`` with no errors
- [x] The change is fully covered by automated unit tests
- [x] Documented in docs/ as appropriate
- [x] Added an entry to the CHANGES file


Co-authored-by: Hernan <[email protected]>
@bors bors bot closed this as completed in 2d48e6b Dec 28, 2019
@bors bors bot closed this as completed in #962 Dec 28, 2019
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 a pull request may close this issue.

3 participants