Skip to content

Instantly share code, notes, and snippets.

@numpde
Last active June 20, 2021 19:51
Show Gist options
  • Save numpde/82c9016d1a8799ff30a213befb750e1f to your computer and use it in GitHub Desktop.
Save numpde/82c9016d1a8799ff30a213befb750e1f to your computer and use it in GitHub Desktop.
Parameter logger with sorcery
# RA, 2021-06-20
import executing
import inspect
import sorcery
class Memo(dict):
"""
Example:
memo = Memo()
(name, surname) = memo("X", "Y")
print(memo) # {'name': 'X', 'surname': 'Y'}
Modified from `sorcery.spell` and `sorcery.assigned_names`.
"""
def __call__(self, value0, *values):
exe = executing.Source.executing(inspect.currentframe().f_back)
names = sorcery.core.FrameInfo(exe).assigned_names(allow_one=True)[0]
assert len(names) == (1 + len(values))
for (name, value) in zip(names, (value0, *values)):
self[name] = value
if values:
return (value0, *values)
else:
return value0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment