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

misc cleanup #13

Merged
merged 1 commit into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 21 additions & 18 deletions flopy4/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand All @@ -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):
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
20 changes: 10 additions & 10 deletions flopy4/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
6 changes: 5 additions & 1 deletion flopy4/param.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Loading