-
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
Save particle state in a dict (Issue #3916) #4059
Conversation
Your pull request does not meet our code formatting rules. Specifically, I suggest you make the following changes: diff --git a/src/python/espressomd/particle_data.pyx b/src/python/espressomd/particle_data.pyx
index 281a2d5ed..d6afb6b02 100644
--- a/src/python/espressomd/particle_data.pyx
+++ b/src/python/espressomd/particle_data.pyx
@@ -55,9 +55,8 @@ cdef class ParticleHandle:
cdef int update_particle_data(self) except -1:
self.particle_data = &get_particle_data(self._id)
-
- def to_dict(self):
+ def to_dict(self):
"""
Returns the particle's attributes as a dictionary.
It includes the content of \`\`particle_attributes\`\`, minus a few exceptions:
@@ -179,7 +178,7 @@ cdef class ParticleHandle:
def __get__(self):
self.update_particle_data()
- return make_array_locked(unfolded_position(< Vector3d > self.particle_data.r.p, < Vector3i > self.particle_data.l.i, box_geo.length()))
+ return make_array_locked(unfolded_position( < Vector3d > self.particle_data.r.p, < Vector3i > self.particle_data.l.i, box_geo.length()))
property pos_folded:
"""
@@ -1701,17 +1700,16 @@ class ParticleSlice(_ParticleSliceImpl):
raise AttributeError(
f"ParticleHandle does not have the attribute {name}.")
super().__setattr__(name, value)
-
- def to_dict(self):
+ def to_dict(self):
"""
Returns the particles attributes as a dictionary, such that it can
be used to save the particle data and recover it easily using
-
+
>>> p = system.part.add(...)
>>> particle_dict = p.to_dict()
>>> system.part.add(particle_dict)
-
+
It includes the content of \`\`particle_attributes\`\`, minus a few exceptions:
- :attr:\`~ParticleHandle.dip\`, :attr:\`~ParticleHandle.director\`:
@@ -1723,7 +1721,7 @@ class ParticleSlice(_ParticleSliceImpl):
odict = {}
key_list = [p.id for p in self]
-
+
for particle_number in key_list:
pdict = ParticleHandle(particle_number).to_dict()
for p_key, p_value in pdict.items():
@@ -1768,7 +1766,7 @@ cdef class ParticleList:
- :attr:\`~ParticleHandle.image_box\`, :attr:\`~ParticleHandle.node\`
"""
-
+
odict = {}
for p in self:
pdict = p.to_dict()
diff --git a/testsuite/python/particle_to_dict.py b/testsuite/python/particle_to_dict.py
index ce45cdc82..1a4b60e07 100644
--- a/testsuite/python/particle_to_dict.py
+++ b/testsuite/python/particle_to_dict.py
@@ -3,17 +3,22 @@ import unittest_decorators as utx
import numpy as np
import espressomd
+
class ParticleDictionaryTest(ut.TestCase):
box_l = 30.
- system = espressomd.System(box_l = 3 * [box_l])
+ system = espressomd.System(box_l=3 * [box_l])
def test(self):
- p = self.system.part.add(pos = np.random.uniform(size = (10,3)) * self.box_l)
+ p = self.system.part.add(
+ pos=np.random.uniform(
+ size=(
+ 10, 3)) * self.box_l)
pp = str(p)
pdict = p.to_dict()
p.remove()
self.system.part.add(pdict)
self.assertEqual(str(self.system.part.select()), pp)
+
if __name__ == "__main__":
ut.main() To apply these changes, please do one of the following:
You can run Please note that there are often multiple ways to correctly format code. As I am just a robot, I sometimes fail to identify the most aesthetically pleasing way. So please look over my suggested changes and adapt them where the style does not make sense. |
Your pull request does not meet our code style rules. Pylint summary:
You can generate these warnings with |
#3916
Add functions:
ParticleHandle.to_dict()
ParticleSlice.to_dict()
Description of changes:
Returns the particles attributes as a dictionary, such that it can be used to save the particle data and recover it by using
p = system.part.add(...)
particle_dict = p.to_dict()
system.part.add(particle_dict)
Codes are mainly written by @RiccardoFrenner