From 0feacc4a8f0d906fb032c8d36d451d0eb3be0aef Mon Sep 17 00:00:00 2001 From: wpbonelli Date: Thu, 25 Jul 2024 10:25:06 -0400 Subject: [PATCH] misc cleanup --- flopy4/block.py | 39 +++++++++++++++++++++------------------ flopy4/package.py | 20 ++++++++++---------- flopy4/param.py | 6 +++++- 3 files changed, 36 insertions(+), 29 deletions(-) diff --git a/flopy4/block.py b/flopy4/block.py index 70bdf19..683740a 100644 --- a/flopy4/block.py +++ b/flopy4/block.py @@ -16,25 +16,24 @@ def single_keystring(members): return len(params) == 1 and isinstance(params[0], MFKeystring) -def get_param(members, key, block): +def get_param(members, name, block): ks = [m for m in members.values() if isinstance(m, MFKeystring)] if len(ks) == 1: param = ks[0] else: - param = members.get(key) + param = members.get(name) if param is not None: - param.name = key + param.name = name else: - raise ValueError(f"Invalid parameter: {key.upper()}") + raise ValueError(f"Invalid parameter: {name.upper()}") param.block = block return param class MFBlockMeta(type): def __new__(cls, clsname, bases, attrs): - new = super().__new__(cls, clsname, bases, attrs) if clsname == "MFBlock": - return new + return super().__new__(cls, clsname, bases, attrs) # detect block name block_name = ( @@ -45,18 +44,17 @@ def __new__(cls, clsname, bases, attrs): # add parameter specification as class attribute. # dynamically set the parameters' name and block. - params = { - k: v.with_name(k).with_block(block_name) - for k, v in attrs.items() - if issubclass(type(v), MFParam) - } - if len([p for p in params if isinstance(p, MFKeystring)]) > 1: - raise ValueError("Only one keystring allowed per block") - for key, param in params.items(): - setattr(new, key, param) - new.params = MFParams(params) + params = dict() + for attr_name, attr in attrs.items(): + if issubclass(type(attr), MFParam): + attr.__doc__ = attr.description + attr.name = attr_name + attr.block = block_name + attrs[attr_name] = attr + params[attr_name] = attr + attrs["params"] = MFParams(params) - return new + return super().__new__(cls, clsname, bases, attrs) class MFBlockMappingMeta(MFBlockMeta, ABCMeta): @@ -86,7 +84,7 @@ def __init__(self, name=None, index=None, params=None): super().__init__(params) def __getattribute__(self, name: str) -> Any: - if name == "data": + if name in ["data", "params"]: return super().__getattribute__(name) param = self.data.get(name) @@ -169,3 +167,8 @@ def __init__(self, blocks=None): def __repr__(self): return pformat({k: repr(v) for k, v in self.data.items()}) + + def write(self, f): + """Write the blocks to file.""" + for block in self.data.values(): + block.write(f) diff --git a/flopy4/package.py b/flopy4/package.py index c73596e..1aea7ea 100644 --- a/flopy4/package.py +++ b/flopy4/package.py @@ -26,13 +26,14 @@ def __new__(cls, clsname, bases, attrs): # add parameter and block specification as class # attributes. subclass mfblock dynamically based # on each block parameter specification. - params = MFParams( - { - k: v.with_name(k) - for k, v in attrs.items() - if issubclass(type(v), MFParam) - } - ) + params = dict() + for attr_name, attr in attrs.items(): + if issubclass(type(attr), MFParam): + attr.__doc__ = attr.description + attr.name = attr_name + attrs[attr_name] = attr + params[attr_name] = attr + params = MFParams(params) blocks = MFBlocks( { block_name: get_block( @@ -78,7 +79,7 @@ def __str__(self): return buffer.getvalue() def __getattribute__(self, name: str) -> Any: - if name == "data": + if name in ["data", "params", "blocks"]: return super().__getattribute__(name) block = self.data.get(name) @@ -128,5 +129,4 @@ def load(cls, f): def write(self, f): """Write the package to file.""" - for block in self.data.values(): - block.write(f) + super().write(f) diff --git a/flopy4/param.py b/flopy4/param.py index 2c2e463..ef263a7 100644 --- a/flopy4/param.py +++ b/flopy4/param.py @@ -177,6 +177,10 @@ def __init__(self, params=None): for key, param in self.items(): setattr(self, key, param) + def __repr__(self): + return pformat({k: repr(v) for k, v in self.data.items()}) + def write(self, f): - for param in self.values(): + """Write the parameters to file.""" + for param in self.data.values(): param.write(f)