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

Bugfix for clear_meta and expanded has_meta in DataDictBase. #271

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
8 changes: 6 additions & 2 deletions plottr/data/datadict.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ def has_meta(self, key: str) -> bool:
if k in self:
return True
else:
for key, field_dict in self.data_items():
if k in field_dict:
return True
return False

def meta_val(self, key: str, data: Union[str, None] = None) -> Any:
Expand Down Expand Up @@ -244,7 +247,7 @@ def clear_meta(self, data: Union[str, None] = None) -> None:
"""
Delete meta information.

:param data: if this is not None, delete onlymeta information from data
:param data: if this is not None, delete only meta information from data
field `data`. Else, delete all top-level meta, as well as
meta for all data fields.

Expand All @@ -260,7 +263,8 @@ def clear_meta(self, data: Union[str, None] = None) -> None:
self.delete_meta(m, d)

else:
for m, _ in self.meta_items(data):
data_meta_list = [m for m, _ in self.meta_items(data)]
for m in data_meta_list:
self.delete_meta(m, data)

def extract(self: T, data: List[str], include_meta: bool = True,
Expand Down
14 changes: 14 additions & 0 deletions test/pytest/test_data_dict_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,26 @@ def test_meta():
assert dd.meta_val('info')(1) == 0
assert dd.meta_val('@^&', 'x') == 0

assert dd.has_meta('meta1')
assert dd.has_meta('meta3')

for k in ['meta1', 'meta2', '@^&']:
assert dd.meta_val(k, data='x') == dd['x'][f'__{k}__']
assert f'__{k}__' in dd['x']
assert k in [n for n, _ in dd.meta_items('x')]

# test stripping of meta information

# test stripping specific data field

dd.clear_meta('x')
assert dd.validate()
x_nmeta = 0
for k, _ in dd['x'].items():
if k[:2] == '__' and k[-2:] == '__':
x_nmeta += 1
assert x_nmeta == 0

dd.clear_meta()
assert dd.validate()

Expand Down