diff --git a/openmc/capi/cell.py b/openmc/capi/cell.py index 3e767caf3e..81bdfdc40e 100644 --- a/openmc/capi/cell.py +++ b/openmc/capi/cell.py @@ -5,6 +5,7 @@ from weakref import WeakValueDictionary import numpy as np from . import _dll +from .core import _View from .error import _error_handler __all__ = ['CellView', 'cells'] @@ -22,7 +23,7 @@ _dll.openmc_get_cell_index.restype = c_int _dll.openmc_get_cell_index.errcheck = _error_handler -class CellView(object): +class CellView(_View): """View of a cell. This class exposes a cell that is stored internally in the OpenMC solver. To @@ -48,9 +49,6 @@ class CellView(object): cls.__instances[args] = instance return cls.__instances[args] - def __init__(self, index): - self._index = index - @property def id(self): cell_id = c_int32() @@ -84,4 +82,7 @@ class _CellMapping(Mapping): def __len__(self): return c_int32.in_dll(_dll, 'n_cells').value + def __repr__(self): + return repr(dict(self)) + cells = _CellMapping() diff --git a/openmc/capi/core.py b/openmc/capi/core.py index 4335bcc46e..2234fac640 100644 --- a/openmc/capi/core.py +++ b/openmc/capi/core.py @@ -170,3 +170,11 @@ class _DLLGlobal(object): def __set__(self, instance, value): self.ctype.in_dll(_dll, self.name).value = value + + +class _View(object): + def __init__(self, index): + self._index = index + + def __repr__(self): + return "{}[{}]".format(type(self).__name__, self._index) diff --git a/openmc/capi/filter.py b/openmc/capi/filter.py index 28d95a37cb..dc59560e0d 100644 --- a/openmc/capi/filter.py +++ b/openmc/capi/filter.py @@ -7,6 +7,7 @@ import numpy as np from numpy.ctypeslib import as_array from . import _dll +from .core import _View from .error import _error_handler from .material import MaterialView @@ -56,7 +57,7 @@ _dll.openmc_mesh_filter_set_mesh.restype = c_int _dll.openmc_mesh_filter_set_mesh.errcheck = _error_handler -class FilterView(object): +class FilterView(_View): __instances = WeakValueDictionary() def __new__(cls, *args): @@ -65,9 +66,6 @@ class FilterView(object): cls.__instances[args] = instance return cls.__instances[args] - def __init__(self, index): - self._index = index - @property def id(self): filter_id = c_int32() @@ -215,4 +213,7 @@ class _FilterMapping(Mapping): def __len__(self): return c_int32.in_dll(_dll, 'n_filters').value + def __repr__(self): + return repr(dict(self)) + filters = _FilterMapping() diff --git a/openmc/capi/material.py b/openmc/capi/material.py index 728caa3561..81c47f5f9f 100644 --- a/openmc/capi/material.py +++ b/openmc/capi/material.py @@ -6,6 +6,7 @@ import numpy as np from numpy.ctypeslib import as_array from . import _dll, NuclideView +from .core import _View from .error import _error_handler @@ -36,7 +37,7 @@ _dll.openmc_material_set_densities.restype = c_int _dll.openmc_material_set_densities.errcheck = _error_handler -class MaterialView(object): +class MaterialView(_View): """View of a material. This class exposes a material that is stored internally in the OpenMC @@ -60,15 +61,6 @@ class MaterialView(object): """ __instances = WeakValueDictionary() - def __new__(cls, *args): - if args not in cls.__instances: - instance = super().__new__(cls) - cls.__instances[args] = instance - return cls.__instances[args] - - def __init__(self, index): - self._index = index - @property def id(self): mat_id = c_int32() @@ -167,4 +159,7 @@ class _MaterialMapping(Mapping): def __len__(self): return c_int32.in_dll(_dll, 'n_materials').value + def __repr__(self): + return repr(dict(self)) + materials = _MaterialMapping() diff --git a/openmc/capi/nuclide.py b/openmc/capi/nuclide.py index 6e3245e9e4..16bb71accd 100644 --- a/openmc/capi/nuclide.py +++ b/openmc/capi/nuclide.py @@ -6,6 +6,7 @@ import numpy as np from numpy.ctypeslib import as_array from . import _dll +from .core import _View from .error import _error_handler @@ -35,7 +36,7 @@ def load_nuclide(name): _dll.openmc_load_nuclide(name.encode()) -class NuclideView(object): +class NuclideView(_View): """View of a nuclide. This class exposes a nuclide that is stored internally in the OpenMC @@ -61,9 +62,6 @@ class NuclideView(object): cls.__instances[args] = instance return cls.__instances[args] - def __init__(self, index): - self._index = index - @property def name(self): name = c_char_p() @@ -90,4 +88,7 @@ class _NuclideMapping(Mapping): def __len__(self): return c_int.in_dll(_dll, 'n_nuclides').value + def __repr__(self): + return repr(dict(self)) + nuclides = _NuclideMapping() diff --git a/openmc/capi/tally.py b/openmc/capi/tally.py index 3d056c2833..db32f98f88 100644 --- a/openmc/capi/tally.py +++ b/openmc/capi/tally.py @@ -5,6 +5,7 @@ from weakref import WeakValueDictionary from numpy.ctypeslib import as_array from . import _dll, NuclideView +from .core import _View from .error import _error_handler from .filter import _get_filter @@ -50,7 +51,7 @@ _dll.openmc_tally_set_type.restype = c_int _dll.openmc_tally_set_type.errcheck = _error_handler -class TallyView(object): +class TallyView(_View): """View of a tally. This class exposes a tally that is stored internally in the OpenMC @@ -82,9 +83,6 @@ class TallyView(object): cls.__instances[args] = instance return cls.__instances[args] - def __init__(self, index): - self._index = index - @property def id(self): tally_id = c_int32() @@ -162,4 +160,7 @@ class _TallyMapping(Mapping): def __len__(self): return c_int32.in_dll(_dll, 'n_tallies').value + def __repr__(self): + return repr(dict(self)) + tallies = _TallyMapping()