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

Work to refactor amuse.rfi #1078

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
274 changes: 169 additions & 105 deletions src/amuse/community/fractalcluster/interface.py

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/amuse/community/galactics/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ endif
-include ${AMUSE_DIR}/config.mk

CC ?= gcc
CFLAGS ?= -O3
CFLAGS ?= -O3

FC ?= gfortran
FORTRAN ?= $(FC)
Expand Down
2 changes: 1 addition & 1 deletion src/amuse/community/galactics/src/src/genhalo.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// random skip factor, must be larger than the number of random draws per particle
#define SKIP 100000

main(argc,argv)
int main(argc,argv)
int argc;
char **argv;
{
Expand Down
148 changes: 66 additions & 82 deletions src/amuse/datamodel/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import warnings


class KeyGenerator(object):
class KeyGenerator:
def __next__(self):
pass

Expand Down Expand Up @@ -100,7 +100,7 @@ def set_random_key_generator(
UniqueKeyGenerator = RandomNumberUniqueKeyGenerator(number_of_bits=number_of_bits)


class AttributeStorage(object):
class AttributeStorage:
"""
Abstract base class of particle storage objects. Implemented by the
storage classes and the particle sets
Expand Down Expand Up @@ -184,7 +184,7 @@ def get_defined_settable_attribute_names(self):
return self.get_defined_attribute_names()


class DerivedAttribute(object):
class DerivedAttribute:
"""
Abstract base class for calculated properties and
methods on sets.
Expand All @@ -203,7 +203,7 @@ def set_value_for_entity(self, particles, key, value):
raise exceptions.AmuseException("cannot set value of a DerivedAttribute")


class AbstractAttributeValue(object):
class AbstractAttributeValue:
def __str__(self):
return self._values.__str__()

Expand Down Expand Up @@ -509,11 +509,13 @@ def get_value_for_entity(self, particles, particle, index):


def new_particles_function_attribute_with_doc(function):
class BoundParticlesFunctionAttribute(object):
class BoundParticlesFunctionAttribute:
if function.__doc__:
__doc__ = (
"\n Documentation on '{0}' particles function attribute:"
"\n\n".format(function.__name__) + function.__doc__
f"\n"
f" Documentation on '{function.__name__}' particles function "
f"attribute:\n\n"
f"{function.__doc__}"
)
_function = staticmethod(function)

Expand All @@ -527,11 +529,13 @@ def __call__(self, *list_arguments, **keyword_arguments):


def new_particle_function_attribute_with_doc(function):
class BoundParticleFunctionAttribute(object):
class BoundParticleFunctionAttribute:
if function.__doc__:
__doc__ = (
"\n Documentation on '{0}' particle function attribute:"
"\n\n".format(function.__name__) + function.__doc__
f"\n"
f" Documentation on '{function.__name__}' particle function "
f"attribute:\n\n"
f"{function.__doc__}"
)
_function = staticmethod(function)

Expand All @@ -549,11 +553,13 @@ def __call__(self, *list_arguments, **keyword_arguments):


def new_caching_particles_function_attribute_with_doc(name, function):
class CachingBoundParticlesFunctionAttribute(object):
class CachingBoundParticlesFunctionAttribute:
if function.__doc__:
__doc__ = (
"\n Documentation on '{0}' particles function attribute:"
"\n\n".format(function.__name__) + function.__doc__
f"\n"
f" Documentation on '{function.__name__}' particles function "
f"attribute:\n\n"
f"{function.__doc__}"
)
_function = staticmethod(function)
_name = name
Expand Down Expand Up @@ -655,7 +661,7 @@ def __setstate__(self, state):
)


class CollectionAttributes(object):
class CollectionAttributes:
"""
Objects of this class store attributes for
a particles collection or a grid.
Expand All @@ -676,7 +682,7 @@ def __getattr__(self, name):
try:
return self._attributes[name]
except KeyError:
raise AttributeError("uknown attribute: {0!r}".format(name))
raise AttributeError(f"unknown attribute: {name!r}")

def __setattr__(self, name, value):
self._attributes[name] = value
Expand All @@ -690,7 +696,7 @@ def __setstate__(self, data):
def __str__(self):
lines = []
for name, value in self._attributes.items():
lines.append("{0}: {1}".format(name, value))
lines.append(f"{name}: {value}")
return "\n".join(lines)

def _copy_for_collection(self, newcollection):
Expand All @@ -700,7 +706,7 @@ def iteritems(self):
return iter(self._attributes.items())


class CachedResults(object):
class CachedResults:
"""
Stores results for functions that only need to be called once
"""
Expand All @@ -712,7 +718,7 @@ def __getstate__(self):
return {"results": {}}


class PrivateProperties(object):
class PrivateProperties:
"""
Defined for superclasses to store private properties.
Every set has :meth:`__setattr__` defined.
Expand Down Expand Up @@ -751,27 +757,24 @@ def __str__(self):
return output


class UndefinedAttribute(object):
class UndefinedAttribute:
def __get__(self, obj, type=None):
raise AttributeError()


class AsynchronuousAccessToSet(object):
class AsynchronuousAccessToSet:
"""
Helper object to get asynchronuous acces to sets
Helper object to get asynchronous access to sets
"""

def __init__(self, store):
object.__setattr__(self, "_store", store)

def __getattr__(self, name_of_the_attribute):
# ~ if name_of_the_attribute == '__setstate__':
# ~ raise AttributeError('type object {0!r} has no attribute {1!r}'.format(type(self._store), name_of_the_attribute))
if name_of_the_attribute in self._store._derived_attributes:
raise AttributeError(
"type object {0!r} cannot asynchronuously access attribute {1!r}".format(
type(self._store), name_of_the_attribute
)
f"type object {type(self._store)!r} cannot asynchronously access "
f"attribute {name_of_the_attribute!r}"
)
try:
return self._store._convert_to_entities_or_quantities(
Expand All @@ -785,34 +788,29 @@ def __getattr__(self, name_of_the_attribute):
in self._store.get_attribute_names_defined_in_store()
):
raise
else:
raise AttributeError(
"You tried to access attribute '{0}'"
" but this attribute is not defined for this set.".format(
name_of_the_attribute
)
)
raise AttributeError(
f"You tried to access attribute '{name_of_the_attribute}'"
f" but this attribute is not defined for this set."
) from ex

def __setattr__(self, name_of_the_attribute, value):
value = self._store.check_attribute(value)
if name_of_the_attribute in self._store._derived_attributes:
raise AttributeError(
"type object {0!r} cannot asynchronuously access attribute {1!r}".format(
type(self._store), name_of_the_attribute
)
)
else:
request = self._store.set_values_in_store_async(
self._store.get_all_indices_in_store(),
[name_of_the_attribute],
[self._store._convert_from_entities_or_quantities(value)],
f"type object {type(self._store)!r} cannot asynchronously access "
f"attribute {name_of_the_attribute!r}"
)
request = self._store.set_values_in_store_async(
self._store.get_all_indices_in_store(),
[name_of_the_attribute],
[self._store._convert_from_entities_or_quantities(value)],
)

def __setstate__(self, arg):
self.__dict__.update(arg)


class AbstractSet(object):
class AbstractSet:
"""
Abstract superclass of all sets of particles and grids.
"""
Expand Down Expand Up @@ -861,42 +859,33 @@ def __getattr__(self, name_of_the_attribute):
return self._request
if name_of_the_attribute == "__setstate__":
raise AttributeError(
"type object {0!r} has no attribute {1!r}".format(
type(self), name_of_the_attribute
)
f"type object {type(self)!r} has no attribute {name_of_the_attribute!r}"
)
if name_of_the_attribute in self._derived_attributes:
return self._derived_attributes[
name_of_the_attribute
].get_values_for_entities(self)
else:
try:
return self._convert_to_entities_or_quantities(
self.get_all_values_of_attribute_in_store(name_of_the_attribute)
)
except Exception as ex:
if name_of_the_attribute in self.get_attribute_names_defined_in_store():
raise
else:
raise AttributeError(
"You tried to access attribute '{0}'"
" but this attribute is not defined for this set.".format(
name_of_the_attribute
)
)
try:
return self._convert_to_entities_or_quantities(
self.get_all_values_of_attribute_in_store(name_of_the_attribute)
)
except Exception as ex:
if name_of_the_attribute in self.get_attribute_names_defined_in_store():
raise
raise AttributeError(
f"You tried to access attribute '{name_of_the_attribute}'"
f" but this attribute is not defined for this set."
) from ex

def _get_derived_attribute_value(self, name_of_the_attribute):
if name_of_the_attribute in self._derived_attributes:
return self._derived_attributes[
name_of_the_attribute
].get_values_for_entities(self)
else:
raise AttributeError(
"You tried to access attribute '{0}'"
" but this attribute is not defined for this set.".format(
name_of_the_attribute
)
)
raise AttributeError(
f"You tried to access attribute '{name_of_the_attribute}'"
f" but this attribute is not defined for this set."
)

def check_attribute(self, value):
if not (is_quantity(value) or hasattr(value, "as_set")):
Expand Down Expand Up @@ -928,8 +917,7 @@ def _get_value_of_attribute(self, particle, index, attribute):
return self._derived_attributes[attribute].get_value_for_entity(
self, particle, index
)
else:
return self.get_value_in_store(index, attribute)
return self.get_value_in_store(index, attribute)

def _get_values_for_entity(self, index, attributes):
return [x[0] for x in self.get_values_in_store([index], attributes)]
Expand All @@ -942,16 +930,14 @@ def _set_value_of_attribute(self, index, attribute, value):
return self._derived_attributes[attribute].set_value_for_entity(
self, index, value
)
else:
return self.set_values_in_store(
numpy.asarray([index]), [attribute], [value]
)
return self.set_values_in_store(
numpy.asarray([index]), [attribute], [value]
)

def _convert_to_entities_or_quantities(self, x):
if hasattr(x, "unit") and x.unit.iskey():
return self._subset(x.number)
else:
return x
return x

def _convert_to_entities_or_quantities_async(self, x):
def handler(inner):
Expand Down Expand Up @@ -1756,7 +1742,7 @@ def copy(
result[index] = copy_of_container
else:
raise exceptions.AmuseException(
"unkown type in link {0}, copy not implemented".format(type(x))
f"unknown type in link {type(x)}, copy not implemented"
)
index += 1

Expand All @@ -1774,7 +1760,7 @@ def copy_with_link_transfer(
from amuse.datamodel.grids import GridPoint

if memento is None:
memento = dict()
memento = {}

result = LinkedArray(numpy.empty_like(self))
index = 0
Expand Down Expand Up @@ -1811,9 +1797,7 @@ def copy_with_link_transfer(
result[index] = x
else:
raise exceptions.AmuseException(
"unkown type in link {0}, transfer link not implemented".format(
type(x)
)
f"unknown type in link {type(x)}, transfer link not implemented"
)

return result
Expand Down Expand Up @@ -1917,7 +1901,7 @@ def copy_with_link_transfer(
filter_attributes=lambda particle_set, x: True,
):
if memento is None:
memento = dict()
memento = {}

if must_copy:
raise Exception("unfixed syntax error")
Expand Down
Loading
Loading