pint icon indicating copy to clipboard operation
pint copied to clipboard

Make ureg's non-unit attributes and methods more accessible

Open andrewgsavage opened this issue 3 years ago • 4 comments

I often use the Object. autocomplete functionality in jupyter notebooks to explore new libraries. When I do this with a UnitRegistry, I get A, A_90, A_US, .... It's near impossible to find methods like auto_reduce_dimensions, default_format, default_system, define etc because there's so many units.

A custom __dir__ could be defined so the non unit attributes and methods appear at the top of the autocomplete list.

andrewgsavage avatar Jan 11 '22 01:01 andrewgsavage

I agree with this. I am not sure that this will work because IIRC dir(obj) sorts the resulting list. So I wonder if the Jupyter (or another IDE) will sort this as well.

hgrecco avatar Jan 11 '22 23:01 hgrecco

You're right, dir(obj) returns a sorted list because that makes look-ups faster. Thus, the only way I can see to actually do this is by removing the units from __dir__, and by recommending to use ureg["unit"]: that also autocompletes, at least with ipython / jupyter. Not sure how easy that change would be, though.

Edit: or at least, it could if UnitRegistry defined _ipython_key_completions

keewis avatar Jan 12 '22 12:01 keewis

Using ureg["unit"] is a deprecated feature as you can see in the code base. https://github.com/hgrecco/pint/blob/09546b5f83fd5bc93809ee8880b69c328b699a60/pint/registry.py#L332-L337

jules-ch avatar Jan 12 '22 14:01 jules-ch

dir(obj) calls __dir__, and __dir__ returns a sorted list. We can override __dir__ to return an unsorted list. Had a play and think I've worked out how to do it.

edit: never mind, dir(obj) sorts the result from __dir__

andrewgsavage avatar Jan 12 '22 14:01 andrewgsavage