Notebook

Functions to use py2gift from a Jupyter notebook.

Convenience functions

A class to act as a container. This will be useful to simulate a Python module.


source

ClassesContainer

 ClassesContainer ()

Initialize self. See help(type(self)) for accurate signature.

classes_container = ClassesContainer()
class DummyClass:
    
    n = 3
classes_container.add(DummyClass)
classes_container.DummyClass
__main__.DummyClass
classes_container.DummyClass.n
3

Magics


source

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, AND

  • Use 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,

m = MyMagics()

to test the parser,

m.location_parser.parse_args('settings -c myclass -C mycategory'.split())
Namespace(settings='settings', cls='myclass', category=['mycategory'])

When no category nor class are passed:

m.location_parser.parse_args('settings'.split())
Namespace(settings='settings', cls=None, category=None)

Parsed category is always a list

m.location_parser.parse_args('settings -c myc -C cat 1'.split())
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.

category = 'foo'
line = m.location_parser.parse_args(f'settings -c myc -C {json.dumps(category)}'.split())
json.loads(' '.join(line.category))
'foo'
category = 'foo foo'
line = m.location_parser.parse_args(f'settings -c myc -C {json.dumps(category)}'.split())
json.loads(' '.join(line.category))
'foo foo'
category = ['cat', 'subcat']
line = m.location_parser.parse_args(f'settings -c myc -C {json.dumps(category)}'.split())
json.loads(' '.join(line.category))
['cat', 'subcat']

Notice that a json-dumped string is not equal to the string,

json.dumps('Category 1') == 'Category 1'
False

meaning that, if in a variable, the category must always be dumped

Actual use

Magics must be somehow registered

# %lmagic -n foo
settings = py2gift.input_file.Settings()
category_name = settings.add_category('Category 1')
settings.add_or_update_class(category_name=category_name, class_name='question name', question_base_name='base name', n_instances=2)
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'}
cls = 'ClassA'
category = 'Category 1'
settings.add_or_update_class(category_name=category, class_name=cls, question_base_name='base name', n_instances=2)
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'
category = 'foo foo'

settings = py2gift.input_file.Settings()
category_name = settings.add_category(category)
settings.add_or_update_class(category_name=category_name, class_name=cls, question_base_name='base name', n_instances=2)
more
'statement recorded'
cls = 'cls'
category = 'wap wap'
base_category = 'oh'

settings = py2gift.input_file.Settings()
category_name = settings.add_category(category_name=category, base_category=base_category)
settings.add_or_update_class(category_name=category_name, class_name=cls, question_base_name='base name', n_instances=2)
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'}