Skip to content

Commit

Permalink
fixup implement read/write
Browse files Browse the repository at this point in the history
  • Loading branch information
nikohansen committed Dec 14, 2023
1 parent 32b9aa9 commit c36beee
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions code-experiments/build/python/src/cocoex/utilities.py
Expand Up @@ -98,12 +98,12 @@ def eval_value(value):
'sys', 'warnings', 'time', 'defaultdict', 'os', 'np', 'scipy')])))
return res

def simple_save_dict(dict_, ignore_list=('_',)):
"""return a pruned `dict` such that ``ast.literal_eval(repr(dict_))`` works.
def dict_to_eval(dict_, ignore_list=('_', 'self')):
"""return a pruned `dict` so that ``ast.literal_eval(repr(dict_))`` works.
Keys that start with entries from `ignore_list` when interpreted as
`str` are removed, by default those starting with ``_``, where
`ignore_list` must be a `str` or a `tuple` of `str`.
`str` are removed, by default those starting with ``_`` or ``self``,
where `ignore_list` must be a `str` or a `tuple` of `str`.
See also `write_setting`.
"""
Expand All @@ -125,21 +125,21 @@ def write_setting(dict_, filename, ignore_list=None):
for keeping a record or, e.g., for checking the `budget_multiplier`
later.
A typical usecase is ``write_setting(globals(), 'parameters.pydat')``.
A typical usecase is ``write_setting(locals(), 'parameters.pydat')``.
When ``ignore_list is not None`` it is passed to `simple_save_dict`
which determines which parameters are written or omitted, by default
keys starting with ``_`` are omitted and items that bail on
When ``ignore_list is not None`` it is passed to `dict_to_eval`
which determines which parameters are written or omitted. By default,
keys starting with ``_`` or ``self`` are omitted and items that bail on
`literal_eval`.
See also `simple_save_dict` and `read_setting`.
See also `dict_to_eval` and `read_setting`.
"""
if not _os.path.exists(filename):
with open(filename, 'wt') as f:
if ignore_list is None:
f.write(repr(simple_save_dict(dict_)))
f.write(repr(dict_to_eval(dict_)))
else:
f.write(repr(simple_save_dict(dict_, ignore_list)))
f.write(repr(dict_to_eval(dict_, ignore_list)))
else:
_warnings.warn('nothing written as the file "' + filename +
'"\nexists already (this should never happen as\n'
Expand Down

0 comments on commit c36beee

Please sign in to comment.