= ClassesContainer() classes_container
Notebook
py2gift
from a Jupyter notebook.
Convenience functions
A class to act as a container. This will be useful to simulate a Python module.
ClassesContainer
ClassesContainer ()
Initialize self. See help(type(self)) for accurate signature.
class DummyClass:
= 3 n
classes_container.add(DummyClass)
classes_container.DummyClass
__main__.DummyClass
classes_container.DummyClass.n
3
Magics
MyMagics
MyMagics (shell=None, **kwargs)
Base class for implementing magic functions.
Shell functions which can be reached as %function_name. All magic functions should accept a string, which they can parse for their own needs. This can make some functions easier to type, eg %cd ../
vs. %cd("../")
Classes providing magic functions need to subclass this class, and they MUST:
Use the method decorators
@line_magic
and@cell_magic
to decorate individual methods as magic functions, ANDUse the class decorator
@magics_class
to ensure that the magic methods are properly registered at the instance level upon instance initialization.
See :mod:magic_functions
for examples of actual implementation classes.
Parser
The class can be instantiated directly,
= MyMagics() m
to test the parser,
'settings -c myclass -C mycategory'.split()) m.location_parser.parse_args(
Namespace(settings='settings', cls='myclass', category=['mycategory'])
When no category nor class are passed:
'settings'.split()) m.location_parser.parse_args(
Namespace(settings='settings', cls=None, category=None)
Parsed category is always a list
'settings -c myc -C cat 1'.split()) m.location_parser.parse_args(
Namespace(settings='settings', cls='myc', category=['cat', '1'])
json
should be used to pass the category. Then, within the magic, the category
list is joined (with a space) and parsed back using json
to whatever type it originally had.
= 'foo'
category = m.location_parser.parse_args(f'settings -c myc -C {json.dumps(category)}'.split())
line ' '.join(line.category)) json.loads(
'foo'
= 'foo foo'
category = m.location_parser.parse_args(f'settings -c myc -C {json.dumps(category)}'.split())
line ' '.join(line.category)) json.loads(
'foo foo'
= ['cat', 'subcat']
category = m.location_parser.parse_args(f'settings -c myc -C {json.dumps(category)}'.split())
line ' '.join(line.category)) json.loads(
['cat', 'subcat']
Notice that a json
-dumped string is not equal to the string,
'Category 1') == 'Category 1' json.dumps(
False
meaning that, if in a variable, the category must always be dumped
Actual use
Magics must be somehow registered
# %lmagic -n foo
= py2gift.input_file.Settings()
settings = settings.add_category('Category 1')
category_name =category_name, class_name='question name', question_base_name='base name', n_instances=2) settings.add_or_update_class(category_name
settings.to_dict()
{'output file': 'quiz.yaml',
'pictures base directory': 'quiz/pics',
'categories': [{'name': 'Category 1',
'classes': [{'name': 'question name',
'question base name': 'base name',
'number of instances': 2}]}]}
Consider
bla bla
'statement recorded'
settings
{'categories': [{'classes': [{'name': 'question name',
'number of instances': 2,
'question base name': 'base name',
'statement': 'Consider\n\nbla bla\n'}],
'name': 'Category 1'}],
'output file': 'quiz.yaml',
'pictures base directory': 'quiz/pics'}
= 'ClassA'
cls = 'Category 1'
category =category, class_name=cls, question_base_name='base name', n_instances=2) settings.add_or_update_class(category_name
settings
{'categories': [{'classes': [{'name': 'question name',
'number of instances': 2,
'question base name': 'base name',
'statement': 'Consider\n\nbla bla\n'},
{'name': 'ClassA',
'number of instances': 2,
'question base name': 'base name'}],
'name': 'Category 1'}],
'output file': 'quiz.yaml',
'pictures base directory': 'quiz/pics'}
more blah
'statement recorded'
= 'cls'
cls = 'foo foo'
category
= py2gift.input_file.Settings()
settings = settings.add_category(category)
category_name =category_name, class_name=cls, question_base_name='base name', n_instances=2) settings.add_or_update_class(category_name
more
'statement recorded'
= 'cls'
cls = 'wap wap'
category = 'oh'
base_category
= py2gift.input_file.Settings()
settings = settings.add_category(category_name=category, base_category=base_category)
category_name =category_name, class_name=cls, question_base_name='base name', n_instances=2) settings.add_or_update_class(category_name
category_name
['oh', 'oh/wap wap']
settings
{'categories': [{'classes': [{'name': 'cls',
'number of instances': 2,
'question base name': 'base name'}],
'name': ['oh', 'oh/wap wap']}],
'output file': 'quiz.yaml',
'pictures base directory': 'quiz/pics'}
We must...
'feedback recorded'
settings
{'categories': [{'classes': [{'feedback': 'We must...\n',
'name': 'cls',
'number of instances': 2,
'question base name': 'base name'}],
'name': ['oh', 'oh/wap wap']}],
'output file': 'quiz.yaml',
'pictures base directory': 'quiz/pics'}