mathmaker.lib.tools package

Submodules

mathmaker.lib.tools.config module

Read configuration files.

mathmaker.lib.tools.config.load_config(file_tag, settingsdir)[source]

Will load the values from the yaml config file, named file_tag.yaml.

The default configuration values are loaded from mathmaker/settings/default/.yaml, then load_config will update with values found successively in /etc/mathmaker/.yaml, then in ~/.config/mathmaker/.yaml, finally in mathmaker/settings/dev/.yaml.

mathmaker.lib.tools.db module

class mathmaker.lib.tools.db.source(table_name, cols, **kwargs)[source]

Bases: object

next(**kwargs)[source]

mathmaker.lib.tools.ext_dict module

Extend dict.

class mathmaker.lib.tools.ext_dict.ext_dict[source]

Bases: dict

A dict with more methods.

flat(sep='.')[source]

Return a recursively flattened dict.

If the dictionary contains nested dictionaries, this function will return a one-level (“flat”) dictionary.

Example:
>>> d = ext_dict({'a': {'a1': 3, 'a2': {'z': 5}}, 'b': 'data'})
>>> d.flat() == {'a.a1': 3, 'a.a2.z': 5, 'b': 'data'}
True
recursive_update(d2)[source]

Update self with d2 key/values, recursively update nested dicts.

Example:
>>> d = ext_dict({'a': 1, 'b': {'a': 7, 'c': 10}})
>>> d.recursive_update({'a': 24, 'd': 13, 'b': {'c': 100}})
>>> print(d == {'a': 24, 'd': 13, 'b': {'a': 7, 'c': 100}})
True

mathmaker.lib.tools.header_comment module

Provide the function that builds the header comment from software infos.

mathmaker.lib.tools.header_comment.generate(document_format, comment_symbol='% ')[source]

Return the header comment for output text files.

mathmaker.lib.tools.po_file module

mathmaker.lib.tools.po_file.get_list_of(what, language, arg)[source]
mathmaker.lib.tools.po_file.retrieve(language, po_filename)[source]

mathmaker.lib.tools.tag module

mathmaker.lib.tools.tag.classify_tag(tag)[source]
mathmaker.lib.tools.tag.translate_int_pairs_tag(tag)[source]
mathmaker.lib.tools.tag.translate_single_nb_tag(tag)[source]

From single..._mintomax, get and return min and max in a dictionary.

mathmaker.lib.tools.wording module

Use these functions to process sentences or objects containing a wording.

mathmaker.lib.tools.wording.cut_off_hint_from(sentence: str) → tuple[source]

Return the sentence and the possible hint separated.

Only one hint will be taken into account.

Parameters:sentence (str) – the sentence to inspect
Return type:tuple
Examples:
>>> cut_off_hint_from("This sentence has no hint.")
('This sentence has no hint.', '')
>>> cut_off_hint_from("This sentence has a hint: |hint:length_unit|")
('This sentence has a hint:', 'length_unit')
>>> cut_off_hint_from("Malformed hint:|hint:length_unit|")
('Malformed hint:|hint:length_unit|', '')
>>> cut_off_hint_from("Malformed hint: |hint0:length_unit|")
('Malformed hint: |hint0:length_unit|', '')
>>> cut_off_hint_from("Two hints: |hint:unit| |hint:something_else|")
('Two hints: |hint:unit|', 'something_else')
mathmaker.lib.tools.wording.extract_formatting_tags_from(s: str)[source]

Return all tags found wrapped in {}. Punctuation has no effect.

Parameters:s – the sentence where to look for {tags}.
mathmaker.lib.tools.wording.handle_valueless_names_tags(arg: object, sentence: str)[source]

Each {name} tag found triggers an arg.name attribute to be randomly set.

All concerned tags are: {name}, {nameN}, {masculine_name}, {masculine_nameN}, {feminine_name}, {feminine_nameN}.

If the tag embbeds a value, like in {name=John}, then it’s ignored by this function. If arg already has an attribute matching the tag, then it’s also ignored by this function.

Now, say arg has no attributes like name, name1, etc. then, if sentence contains:

  • “{name}” then arg.name will receive a random name.
  • “{name1}”, then arg.name1 will receive a random name.
  • “{name=Michael}”, then this function ignores it.
  • “{feminine_name}”, then arg.feminine_name will get a random feminine name.
Parameters:
  • arg – the object that attributes must be checked and possibly set
  • sentence – the sentence where to look for “name” tags.
mathmaker.lib.tools.wording.handle_valueless_unit_tags(arg: object, sentence: str)[source]

Each {*_unit} tag triggers an arg.*_unit attribute to be randomly set.

For instance, if {length_unit} is found, then arg.length_unit will get a random length unit. Moreover, if {area_unit} or {volume_unit} are found, arg.length_unit is set accordingly too. If arg.length_unit does already exist, then arg.area_unit will be set accordingly (and not randomly any more).

{*_unitN}, <*_unit> and <*_unitN> tags will be handled the same way by this function.

If the tag embbeds a value, like in {capacity_unit=dL}, then it’s ignored by this function. If arg already has an attribute matching the tag, then it’s also ignored by this function.

Parameters:
  • arg (object) – the object that attributes must be checked and possibly set
  • sentence (str) – the sentence where to look for “unit” tags.
Return type:

None

mathmaker.lib.tools.wording.insert_nonbreaking_spaces(sentence: str)[source]

Replace spaces by nonbreaking ones between a number and the next word.

Parameters:sentence – the sentence to process
mathmaker.lib.tools.wording.is_unit(word: str) → bool[source]

Return True if word is a “unit” tag (e.g. ends with _unit}).

Punctuation has no effect.

Parameters:word – the word to inspect
mathmaker.lib.tools.wording.is_unitN(word)[source]

Return True if word is a “unitN” tag (e.g. ends with _unitN}).

Punctuation has no effect.

Parameters:word – the word to inspect
mathmaker.lib.tools.wording.is_wrapped(word: str, braces='{}', extra_braces='') → bool[source]

Return True if word is wrapped between braces.

Parameters:
  • word – the word to inspect
  • braces – to change the default {} braces to something else,

like [] or <> :param extra_braces: to add extra braces around the usual ones. Like in ({tag}) or [{tag}] :Examples:

>>> is_wrapped('{word}')
True
>>> is_wrapped('{word},')
False
>>> is_wrapped('<word>')
False
>>> is_wrapped('<word>', braces='<>')
True
>>> is_wrapped('({word})')
False
>>> is_wrapped('({word})', extra_braces='()')
True
>>> is_wrapped('[{word}]', extra_braces='()')
False
mathmaker.lib.tools.wording.is_wrapped_P(word: str, braces='{}', extra_braces='') → bool[source]

Return True if word is wrapped between braces & followed by a punctuation.

Parameters:
  • word – the word to inspect
  • braces – to change the default {} braces to something else,

like [] or <> :param extra_braces: to add extra braces around the usual ones. Like in ({tag}) or [{tag}]

Examples:
>>> is_wrapped_P('{word}')
False
>>> is_wrapped_P('{word},')
True
>>> is_wrapped_P('<word>')
False
>>> is_wrapped_P('<word>', braces='<>')
False
>>> is_wrapped_P('<word>:', braces='<>')
True
>>> is_wrapped_P('({word})', extra_braces='()')
False
>>> is_wrapped_P('({word}).', extra_braces='()')
True
>>> is_wrapped_P('[{word}]?', extra_braces='[]')
True
mathmaker.lib.tools.wording.is_wrapped_p(word: str, braces='{}', extra_braces='') → bool[source]

Return True if word is wrapped between braces. Punctuation has no effect.

Parameters:
  • word – the word to inspect
  • braces – to change the default {} braces to something else,

like [] or <> :param extra_braces: to add extra braces around the usual ones. Like in ({tag}) or [{tag}]

Examples:
>>> is_wrapped_p('{word}')
True
>>> is_wrapped_p('{word},')
True
>>> is_wrapped_p('<word>')
False
>>> is_wrapped_p('<word>', braces='<>')
True
>>> is_wrapped_p('<word>:', braces='<>')
True
>>> is_wrapped_p('({word}).')
False
>>> is_wrapped_p('({word}).', extra_braces='()')
True
>>> is_wrapped_p('[{word}]?', extra_braces='[]')
True
mathmaker.lib.tools.wording.merge_nb_unit_pairs(arg: object, w_prefix='')[source]

Merge all occurences of {nbN} {*_unit} in arg.wording into {nbN_*_unit}.

In the same time, the matching attribute arg.nbN_*_unit is set with Value(nbN, unit=Unit(arg.*_unit)).into_str(display_SI_unit=True) (the possible exponent is taken into account too).

Parameters:arg (object) – the object whose attribute wording will be processed. It must have a wording attribute as well as nbN and *_unit attributes.
Return type:None
Example:
>>> class Object(object): pass
...
>>> arg = Object()
>>> arg.wording = 'I have {nb1} {capacity_unit} of water.'
>>> arg.nb1 = 2
>>> arg.capacity_unit = 'L'
>>> merge_nb_unit_pairs(arg)
>>> arg.wording
'I have {nb1_capacity_unit} of water.'
>>> arg.nb1_capacity_unit
'\\SI{2}{L}'
mathmaker.lib.tools.wording.post_process(sentence: str)[source]

Apply all desired post processes to a sentence without {tags}.

So far, this is only the replacement of spaces following a number and preceding a word by nonbreaking spaces.

Parameters:sentence – the sentence to post process
mathmaker.lib.tools.wording.process_attr_values(sentence: str) → tuple[source]

Build a dict with all {key=val} occurrences in sentence. Update such tags.

All {key=val} occurrences will be replaced by {key}.

Parameters:sentence – the sentence to process
Returns:this couple: (transformed_sentence, {key:val, ...})
mathmaker.lib.tools.wording.setup_wording_format_of(w_object: object, w_prefix='')[source]

Set w_object’s attributes according to the tags found in w_object.wording.

This is the complete process of the wording. w_object.wording will also be modified in the process.

For instance, if w_object.wording is: “Here are one {name}, {nb1} {length_unit1} of roads, and a cube of {nb2} {volume_unit=cm}. What is the side’s length of the cube? |hint:length_unit|”

Then w_object.wording becomes: “Here are one {name}, {nb1_length_unit1} of roads, and a cube of {nb2_volume_unit}. What is the side’s length of the cube?”

w_object.name will be set with a random name,

w_object.nb1_length_unit1 will be set with: ‘\SI{<value of nb1>}{<random length unit>}’

w_object.length_unit will be set to centimeters

w_object.nb2_volume_unit will be set with: ‘\SI{<value of nb2>}{cm^{3}}’

w_object.hint will be set with: ‘cm’

If w_prefix is set, the “wording” processed attributes will be w_object.<prefix>wording and w_object.<prefix>wording_format. This allows to process several different wordings.

Parameters:
  • w_object – The object having a ‘wording’ attribute to process.
  • w_prefix – The possible prefix of the “wording” attributes to

process.

mathmaker.lib.tools.wording.unwrapped(word: str) → str[source]

Remove first and last char plus possible punctuation of word.

Examples:
>>> unwrapped('{word}')
'word'
>>> unwrapped('{word},')
'word'
>>> unwrapped('{word}:')
'word'
mathmaker.lib.tools.wording.wrap(word: str, braces='{}', o_str=None, e_str=None) → str[source]

Return the word wrapped between the two given strings.

Using o_str and e_str (e.g. opening str and ending str) will override braces content. It’s interesting when one want to wrap the word with something longer than a char.

Parameters:
  • word (str) – the chunk of text to be wrapped
  • braces (str) – the pair of braces that will wrap the word
  • o_str (str) – prefix the word.
  • e_str (str) – suffix the word
Return type:

str

Examples:
>>> wrap('wonderful')
'{wonderful}'
>>> wrap('wonderful', braces='<>')
'<wonderful>'
>>> wrap('wonderful', o_str='<<', e_str='>>')
'<<wonderful>>'
>>> wrap('wonderful', e_str='}*')
'{wonderful}*'
mathmaker.lib.tools.wording.wrap_latex_keywords(s: str) → str[source]

Replace some {kw} by {{kw}}, to prevent format() from using them as keys.

mathmaker.lib.tools.xml_sheet module

mathmaker.lib.tools.xml_sheet.check_q_consistency(q_attrib, sources)[source]

(Unfinished) Check the consistency of question’s kind, subkind and source.

mathmaker.lib.tools.xml_sheet.get_attributes(filename, tag)[source]

Gathers the attributes of all filename‘s ‘node‘s matching tag.

Parameters:
  • filename (str) – The XML file name.
  • tag (str) – The tag we’re looking for.
Return type:

list

mathmaker.lib.tools.xml_sheet.get_exercises_list(file_name)[source]

Retrieves the exercises’ list from file_name.

Parameters:file_name (str) – The XML file name.
Return type:list
mathmaker.lib.tools.xml_sheet.get_q_kinds_from(exercise_node)[source]

Retrieves the exercise kind and the questions from one exercise section.

Parameters:exercise_node – The XML node of the exercise.
Return type:tuple
mathmaker.lib.tools.xml_sheet.get_sheet_config(file_name)[source]

Retrieves the sheet configuration values from file_name.

Parameters:file_name (str) – The XML file name.
Return type:tuple
mathmaker.lib.tools.xml_sheet.get_xml_schema_path()[source]
mathmaker.lib.tools.xml_sheet.get_xml_sheets_paths()[source]

Returns all paths to default xml frameworks.

They are returned as a dictionary like: {id: path_to_matching_file.xml, ...} the id being the filename without its extension.

Return type:dict

Module contents