-
Notifications
You must be signed in to change notification settings - Fork 189
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
Dynamic properties for ParticleSlice #1160
Conversation
… has but that weren't explicitly defined
- Fix unittest without EXTERNAL_FORCES - Rename ParticleSliceImpl to _ParticleSliceImpl - Better unit tests - Fix for scalar quantities - Remove unneeded custom getters/setters - Fix getter for slice that doesn't start at 0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add some more in-code documentation for future developers.
@KaiSzuttor, I'm currently refactoring |
return res[:-1] | ||
res += str(pl[i]) + ", " | ||
# Remove final comma | ||
return "ParticleSlice([" + res[:-2] + "])" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for this awkward comma removal
pl = ParticleList()
return "ParticleSlice([" + ", ".join(str(pl[i]) for i in self.id_selection if pl.exists(i)) + "])"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's nicer, and it's more in line with how Python and Numpy print their own data types. Also, if I remove that space at the end, why not remove the trailing comma along with it.
return res[:-1] | ||
res += str(self[i]) + ", " | ||
# Remove final comma | ||
return "ParticleList([" + res[:-2] + "])" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for this awkward comma removal
return "ParticleList([" + ", ".join(str(self[i]) for i in range(max_seen_particle + 1) if self.exists(i)) + "])"
I was able to get rid of the injector class by switching to
Explicitly capturing the loop variable like this
didn't help either, even though that is the standard solution for this problem in pure Python. I really don't understand what is going on here, might be a Cython bug. Anyway, once Travis passes, I think this can be merged. |
Small change to the getter so it writes straight to a numpy array instead of converting a list into one at the end. Also extend unit test so it covers all code paths. |
Fixes #958.
@RudolfWeeber @fweik @hmenke
Any properties that are not defined for
ParticleSlice
are automatically drawn fromParticleHandle
, including docstrings. To the setter, one can either provide one value that is set on all particles or a list of values where the order corresponds to that of the slice members.This turned out to be surprisingly difficult:
setattr
to add stuff to Cython classes, so I ended up wrapping the Cython class into a Python class that has all the autogenerated properties.Further improvements made along the way:
ParticleSlice.__str__
, which previously didn't return anything asint
is not the same asnumpy.int64
.ParticleSlice.__str__
andParticleList.__str__
so that the representations look like Numpy arrays or Python lists. Previously there were newlines and the name was missing.utils.check_type_or_throw_except
so that it also allowsnumpy.int64
whereint
is requested.PR Checklist