Last active
June 20, 2021 19:51
-
-
Save numpde/82c9016d1a8799ff30a213befb750e1f to your computer and use it in GitHub Desktop.
Parameter logger with sorcery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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