Skip to content
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
34 changes: 33 additions & 1 deletion flopy4/mf6/component.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,42 @@
from abc import ABC
from collections.abc import MutableMapping

from xattree import xattree

COMPONENTS = {}
"""MF6 component registry."""


class Component(ABC):
@xattree
class Component(ABC, MutableMapping):
@classmethod
def __attrs_init_subclass__(cls):
COMPONENTS[cls.__name__.lower()] = cls

def __attrs_post_init__(self):
self._where = type(self).__xattree__["where"]

def __getitem__(self, key):
data = getattr(self, self._where)
return data.children[key]

def __setitem__(self, key, value):
data = getattr(self, self._where)
if key in data.children:
data.update({key: value})
else:
data = data.assign({key: value})
setattr(self, self._where, data)

def __delitem__(self, key):
data = getattr(self, self._where)
data = data.drop_nodes(key)
setattr(self, self._where, data)

def __iter__(self):
data = getattr(self, self._where)
return iter(data.children)

def __len__(self):
data = getattr(self, self._where)
return len(data.children)
6 changes: 6 additions & 0 deletions test/test_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,9 @@ def test_init_big_sim():
chd.head.data.todense(),
sim.models["gwf"].chd[0].data.head.data.todense(),
)

# test dictionary access/deletion
assert gwf.get("npf")
assert gwf["npf"].attrs["host"] is npf
del gwf["npf"]
assert not gwf.get("npf")
Loading