Problem Bank Helpers

problem_bank_helpers.problem_bank_helpers.create_data2() defaultdict[source]
problem_bank_helpers.problem_bank_helpers.sigfigs(x: str) int[source]

Returns the number of significant digits in a number represented as a string.

This takes into account strings formatted in 1.23e+3 format and even strings such as 123.450. This has a limit of 16 sigfigs, which can be increased but doesn’t seem practical

Parameters:

x (str) – The number as a string

Returns:

The number of significant figures in the number

Return type:

int

Examples

>>> sigfigs("1.23e+3")
3
>>> sigfigs("123.450")
6
problem_bank_helpers.problem_bank_helpers.round_sig(x: float, sig: int) float[source]

Round a number to a specified number of significant digits.

Parameters:
  • x (float or int) – The number to be rounded.

  • sig (int) – The number of significant digits.

Returns:

The rounded number retaining the type of the input.

Return type:

float or int

problem_bank_helpers.problem_bank_helpers.sigFigCheck(subVariable, LaTeXstr, unitString)[source]
problem_bank_helpers.problem_bank_helpers.roundp(*args, **kwargs)[source]

Wrapper function for the sigfig.round package. Also deals with case if requested sig figs is more than provided.

Parameters:

num (number) – Number to round or format.

Returns:

Rounded number output to correct significant figures.

Return type:

float | str

problem_bank_helpers.problem_bank_helpers.round_str(*args, **kwargs)[source]
problem_bank_helpers.problem_bank_helpers.num_as_str(num, digits_after_decimal=2)[source]

Rounds numbers properly to specified digits after decimal place

Parameters:
  • num (float) – Number that is to be rounded

  • digits_after_decimal (int, optional) – Number of digits to round to. Defaults to 2.

Returns:

A string that is correctly rounded (you know why it’s not a float!)

Return type:

str

problem_bank_helpers.problem_bank_helpers.sign_str(number)[source]

Returns the sign of the input number as a string.

Parameters:

sign (number) – A number, float, etc…

Returns:

Either ‘+’ or ‘-’

Return type:

str

problem_bank_helpers.problem_bank_helpers.automatic_feedback(data, string_rep=None, rtol=None)[source]
problem_bank_helpers.problem_bank_helpers.ErrorCheck(errorCheck, subVariable, Variable, LaTeXstr, tolerance)[source]
problem_bank_helpers.problem_bank_helpers.backticks_to_code_tags(data: dict) None[source]

Converts backticks to <code> tags, and code fences to <pl-code> tags for a filled PrairieLearn question data dictionary. Note: this only makes replacements multiple choice (and other similar question) answer options.

Parameters:

data (dict) – The filled PrairieLearn question data dictionary

problem_bank_helpers.problem_bank_helpers.base64_encode(s: str) str[source]

Encode a regular string into a base64 representation to act as a file for prarielearn to store

Parameters:

s (str) – The string containing the file contents to encode

Returns:

A string containing the base64 encoded contents of the file

Return type:

str

problem_bank_helpers.problem_bank_helpers.base64_decode(f: str) str[source]

Decode a base64 string (which is a file) from prairielearn into a useable string

Parameters:

f (str) – The string representation of a base64 encoded file

Returns:

The decoded contents of the file

Return type:

str

problem_bank_helpers.problem_bank_helpers.string_to_pl_user_file(string: str, data: dict, name: str = 'user_code.py') None[source]

Encode a string to base64 and add it as the user submitted code file

Parameters:
  • string (str) – The string to encode and add as the user submitted code file

  • data (dict) – The data dictionary to add the file to

  • name (str, optional) – The name of the file to add. Defaults to “user_code.py”.

problem_bank_helpers.problem_bank_helpers.create_html_table(table: list[list[str]], width: str = '100%', first_row_is_header: bool = True, first_col_is_header: bool = True, wrap_header_latex: bool = False, wrap_nonheader_latex: bool = False) str[source]

Convert a python table to HTML

Example usage:

server.py: data[“params”][“table1”] = pbh.convert_markdown_table([[“a”, “b”, “c”], [“x”, “1”]], wrap_nonheader_latex=True)

markdown: {{{ params.table1 }}}

Parameters:
  • table (list) – A list of lists representing the table

  • width (str, optional) – The width of the table. Ex. “100%”, “500px”, etc.

  • first_row_is_header (bool, optional) – Whether the first row is a header. Defaults to True.

  • first_col_is_header (bool, optional) – Whether the first column is a header. Defaults to True.

  • wrap_nonheader_latex (bool, optional) – Whether to wrap all non-header table cells in $ for LaTeX. Defaults to False.

  • wrap_header_latex (bool, optional) – Whether to wrap all header table cells in $ for LaTeX. Defaults to False.

Returns:

The HTML representation of the table

Return type:

str

problem_bank_helpers.problem_bank_helpers.template_mc(data: dict, part_num: int, choices: dict) None[source]

Adds multiple choice to data from dictionary

Parameters:
  • data (dict) – the data dictionary

  • part_num (int) – the part number

  • choices (dict) – the multiple-choice dictionary

Example

>>> options = {
...     "option1 goes here": ["correct", "Nice work!"],
...     "option2 goes here": ["Incorrect", "Incorrect, try again!"],
...     ...
... }
>>> template_mc(data2, 1, options)