util

Convenience functions.

In order to avoid tqdm’s experimental warning

warnings.filterwarnings("ignore", message='Using `tqdm.autonotebook.tqdm` in notebook mode')

Rendering latex in a notebook

Convenience function to print \(\LaTeX\) text in Jupyter Notebook code (as opposed to markdown) cells. The purpose is to print \(\LaTeX\) content that is only known at run time. Font size of every formula is increased (to \Large).


source

render_latex

 render_latex (text:str)

Returns latex-aware markdown text

Type Details
text str Input text
Returns str Markdown text
render_latex('Bla blah...')

Bla blah…

render_latex('Variable $A$')

Variable \(\Large A\)

Accessors

Pandas‘s accessors mechanishm is leveraged here. In order to do that, first Pandas’ _register_accessor method was aliased above as simply register_accessor. Then a new base class, AccessorEndowedClass, from which any class exposing an accessor must inherit, is created. The only thing it does is adding a class attribute that is expected by Pandas’ _register_accessor method.


source

AccessorEndowedClass

 AccessorEndowedClass ()

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

For instance

class Car(AccessorEndowedClass):
    
    def __init__(self, weight):
        
        self.weight = weight

@register_accessor('seat', Car)
class SeatAccessor:
    
    def __init__(self, car: Car):
        
        self._confortable: bool = True
    
    @property
    def confortable(self):
        
        return self._confortable

Accessing the class itself

car = Car(1600)
car.weight
1600

Accessing the appended accessor

car.seat.confortable
True

Assorted

A function to turn an integer into a roman number. Adapted from here.


source

int_to_roman

 int_to_roman (num:int)

Returns an integer number in roman format

Type Details
num int Input
Returns str Roman number for the input
print(int_to_roman(53))
LIII

Files

Writing a dictionary into a YAML file

A function to write a dictionary to a YAML file with some formatting.


source

dict_to_yaml

 dict_to_yaml (d:dict, output_file:str|pathlib.Path)

Writes a dictionary in a YAML file

Type Details
d dict Input
output_file str | pathlib.Path Ouput
Returns None
yaml_file = '_test.yaml'
d = {'foo': 1, 'ouch': 'psss'}
dict_to_yaml(d, yaml_file)
foo: 1
ouch: psss

Extracting a dictionary from a YAML file


source

yaml_to_dict

 yaml_to_dict (file:str|pathlib.Path)

Reads a dictionary from a YAML file

Type Details
file str | pathlib.Path Input file
Returns dict Output
yaml_to_dict(yaml_file)
{'foo': 1, 'ouch': 'psss'}

Writing an input file for gift-wrapper


source

write_multiple_categories

 write_multiple_categories (category_questions:dict[str,list[dict]],
                            pictures_base_directory:str,
                            output_file:str|None='out.yaml')

Writes a file suitable as input to gift-wrapper

Type Default Details
category_questions dict Every key is the name of a category, and every value is a list of questions (every question is itself a dictionary)
pictures_base_directory str The “pictures base directory” parameter that must be passed to gift-wrapper
output_file str | None out.yaml Output file
Returns None

We build a dictionary with questions belonging different categories. This might be read from a YAML file.

category_questions_example = {
    ('Category A', 'Category B'): [
        {
            'class': 'MultipleChoice',
            'statement': 'Compute the entropy....\n',
            'feedback': 'We just need to...\n',
            'time': '3',
            'answers': {
                'perfect': '1',
                'wrong': ['0', '2']
            },
            'name': 'Entropy of a random variable I'
        }
    ],
    'Category C': [
        {
            'class': 'Numerical',
            'statement': 'Compute the average....\n',
            'feedback': 'In order to...\n',
            'time': '3',
            'solution': {
                'value': 3.1,
                'error': '10%'
            }, 
            'name': 'Mean energy I'
        }
    ]
}
category_questions_example
{('Category A',
  'Category B'): [{'class': 'MultipleChoice',
   'statement': 'Compute the entropy....\n',
   'feedback': 'We just need to...\n',
   'time': '3',
   'answers': {'perfect': '1', 'wrong': ['0', '2']},
   'name': 'Entropy of a random variable I'}],
 'Category C': [{'class': 'Numerical',
   'statement': 'Compute the average....\n',
   'feedback': 'In order to...\n',
   'time': '3',
   'solution': {'value': 3.1, 'error': '10%'},
   'name': 'Mean energy I'}]}
output_file = '_output.yaml'
write_multiple_categories(category_questions_example, 'pics', output_file)
pictures base directory: pics
categories:
  - name:
      - Category A
      - Category B
    questions:
      - class: MultipleChoice
        statement: "Compute the entropy....\n"
        feedback: "We just need to...\n"
        time: '3'
        answers:
          perfect: '1'
          wrong:
            - '0'
            - '2'
        name: Entropy of a random variable I
  - name: Category C
    questions:
      - class: Numerical
        statement: "Compute the average....\n"
        feedback: "In order to...\n"
        time: '3'
        solution:
          value: 3.1
          error: 10%
        name: Mean energy I
!rm {output_file}