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

copy.deepcopy(UnitRegistry()) #864

Merged
merged 10 commits into from
Sep 10, 2019
Merged

copy.deepcopy(UnitRegistry()) #864

merged 10 commits into from
Sep 10, 2019

Conversation

crusaderky
Copy link
Contributor

@crusaderky crusaderky commented Aug 30, 2019

Closes #856

Allow creating an independent copy of a UnitRegistry through deepcopy().
Also introduce several speedups in UnitsContainer and ParserHelper.

@crusaderky
Copy link
Contributor Author

copy.deepcopy misbehaves in case of dynamic class definition, such as that performed by build_unit_class:

>>> def gen_C():
...     class C:
...         pass
...     return C

>>> C1 = gen_C()
>>> C2 = copy.deepcopy(C1)
>>> C2 is C1
True

@crusaderky
Copy link
Contributor Author

crusaderky commented Aug 30, 2019

...aaand done :)

This gives some performance benefit compared to re-initialising everything from the beginning:

%timeit pint.UnitRegistry()
135 ms ± 2.31 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

ureg = pint.UnitRegistry()
%timeit copy.deepcopy(ureg)
42.7 ms ± 799 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

@crusaderky crusaderky changed the title WIP: copy.deepcopy(UnitRegistry()) copy.deepcopy(UnitRegistry()) Aug 30, 2019
@crusaderky
Copy link
Contributor Author

@hgrecco this is ready for review and merge

@hgrecco
Copy link
Owner

hgrecco commented Sep 3, 2019

Great. I would like a second opinion as this is a rather large change. Maybe @andrewgsavage or @jondoesntgit can take also a look.

@crusaderky
Copy link
Contributor Author

@hgrecco @andrewgsavage @jondoesntgit ping - any observations before this can be merged?

@jondoesntgit
Copy link
Contributor

I don't have any observations. This is a bug that I've never had to worry about in my code.

@andrewgsavage
Copy link
Collaborator

Me neither

@hgrecco
Copy link
Owner

hgrecco commented Sep 10, 2019

bors r+

bors bot added a commit that referenced this pull request Sep 10, 2019
864: copy.deepcopy(UnitRegistry()) r=hgrecco a=crusaderky

Closes #856 

Allow creating an independent copy of a UnitRegistry through deepcopy().
Also introduce several speedups in UnitsContainer and ParserHelper.

Co-authored-by: Guido Imperiale <guido.imperiale@amphorainc.com>
@bors
Copy link
Contributor

bors bot commented Sep 10, 2019

Build succeeded

@bors bors bot merged commit fb888ae into hgrecco:master Sep 10, 2019
@crusaderky crusaderky deleted the deepcopy branch September 11, 2019 10:46
@crusaderky
Copy link
Contributor Author

Thank you!

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.

deepcopy(ureg) broken
4 participants