mirror of
https://github.com/openmc-dev/openmc.git
synced 2026-07-27 21:55:41 -04:00
Improve repr() for Python classes exposing C API
This commit is contained in:
parent
d4c7b58a4b
commit
a1eb39d8fc
6 changed files with 33 additions and 26 deletions
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue