Skip to content

Commit

Permalink
update print function for particles sets and individual particles
Browse files Browse the repository at this point in the history
  • Loading branch information
arjenve committed Jan 13, 2016
1 parent 6153459 commit 9be462d
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/amuse/datamodel/particles.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@ def to_string(self, attributes_to_show = None, split_at = 20):
attributes = sorted(self.get_attribute_names_defined_in_store())
if attributes_to_show:
attributes = [x for x in attributes if x in attributes_to_show]

if len(self) == 0:
return self.empty_particles_set_string(attributes)

format_float = '{0: >11.3e}'.format
format_str20 = '{0: >20}'.format
Expand Down Expand Up @@ -329,6 +332,7 @@ def to_string(self, attributes_to_show = None, split_at = 20):
lines = map(lambda x : ' '.join(x), rows)
return '\n'.join(lines)


def _get_particle(self, key):
if self.has_key_in_store(key):
return Particle(key, self._original_set())
Expand Down Expand Up @@ -1048,6 +1052,27 @@ def random_sample(self, number_of_particles):
return self.__getitem__(random.sample(xrange(len(self)), number_of_particles))


def empty_particles_set_string(self, attributes = ()):
result = 'empty particles set ('+self.__class__.__name__+')'
if len(attributes) > 0:
sorted_attributes = sorted(attributes)
sorted_attributes.insert(0, 'key')
header = 'attributes:'
result += '\n' + header
for x in self.divide_attributes(sorted_attributes):
result += '\n'
result += ' ' * len(header)
result += ','.join(x)
return result

def divide_attributes(self, attributes = (), n = 3):
y = []
for x in attributes:
y.append(x)
if len(y) == n:
yield y
y = []

class Particles(AbstractParticleSet):
"""
A set of particles. Attributes and values are stored in
Expand Down Expand Up @@ -3406,7 +3431,7 @@ def __str__(self):
output += ', set=<{0}>'.format(id(self.particles_set))

for name, value in self.particles_set._values_of_particle(self._set_index):
output += ', '
output += '\n , '
output += name
output += '='
if isinstance(value, Particle):
Expand All @@ -3423,6 +3448,7 @@ def __str__(self):
return output



def __dir__(self):
result = []
result.extend(dir(type(self)))
Expand Down

0 comments on commit 9be462d

Please sign in to comment.