analyst.utils package

Submodules

analyst.utils.jupyformatter module

Format Python script downloaded from Jupyter Notebook to a clearer layout according to the rules in append_valid_line.

Example usage:

import analyst as a; a.jupyformatter() #$

Or use it as a standalone file located under the same directory:

from jupyformatter import jupyformatter; jupyformatter(do_setup=True) #$

Set do_deletion to False if is used to format this script itself. do_deletion will not affect #$ delete below.

Todo

  • More logging information, integrated with -v verbose option in argparse.
  • Implement #$ delete from, #$ delete to commands using continue in edit.
  • Automatically insert current date
analyst.utils.jupyformatter.append_valid_line(output_ls, line, do_deletion)[source]

Append line to the list of lines for output according to customed rules.

Called by:
edit
Calls:
wrap_markdown
analyst.utils.jupyformatter.clean_line(line)[source]

Clean line string with customed rules.

Arg:
line (string)
Returns:line (string)
Called by:
edit
analyst.utils.jupyformatter.convert_heading(input_ls)[source]

Convert personal standard Notebook heading to script docstring.

Return:
converted_input_ls (list)
Called by:
edit
Calls:
wrap_markdown

After effect: the header part of the list of lines will have one line fewer comparing to the original list.

For example, convert the head of the script from
1 |#!/usr/bin/env python3 2 |# coding: utf-8 3 | 4 |# ## Formatting Python script downloaded from Jupyter Notebook 5 |# Ver. 02 Proto 6 |# 7 |# — 8 |# <div style=”text-align:right”>HS<br>November 21, 2017</div> 9 |

10 |# In[29]: 11 | 12 |”“” 13 |Docstring 14 |”“”

to
1 |#!/usr/bin/env python3 2 |# coding: utf-8 3 | 4 |”“” 5 | Formatting Python script downloaded from Jupyter Notebook 6 | Ver. 02 Proto 7 | ————————————————————- 8 | HS 9 | November 21, 2017

10 | 11 | 12 |Docstring 13 |”“”

Mar 23, 2018: note that scripts downloaded from the Linux platform have one extra blank line after `# In[d]:

`.

analyst.utils.jupyformatter.convert_nb_to_py()[source]
analyst.utils.jupyformatter.edit(input_ls, do_deletion)[source]

Loop over each line for editing.

Arg:
input_ls (list):
a list of lines readed from the input text file.
Returns:a list of lines going to write to the output.
Return type:output_ls (list)
Called by:
main
Calls:
convert_heading clean_line warn_line append_valid_line
analyst.utils.jupyformatter.get_notebook_name()[source]

Return the name of the current jupyter notebook.

References

https://github.com/jupyter/notebook/issues/1000#issuecomment-359875246 https://stackoverflow.com/a/13055551

analyst.utils.jupyformatter.in_ipynb()[source]

Return True if this code is running in a Jupyter Notebook environment.

analyst.utils.jupyformatter.jupyformatter(do_setup=False, do_deletion=True)[source]

Main function

analyst.utils.jupyformatter.parse_args()[source]

Parse command line arguments to get input and output directories.

Called by:
main
analyst.utils.jupyformatter.read_input(filename)[source]

Given filename (directory), return a list of lines of the input text file.

analyst.utils.jupyformatter.setup()[source]
analyst.utils.jupyformatter.warn_line(line, line_num, do_deletion)[source]

Log with customed rules.

Called by:
edit
analyst.utils.jupyformatter.wrap_markdown(line, wrap_at=79)[source]

Wrap Markdown heading/comment (starts with # `) at `wrap_at.

Args:
line (string) wrap_at (int, default to 79)
Return:
line_wrapped_ls (list): a list of line string(s) if the original
line needs to be wrapped. Every line string in the list is a line ends with a new line character `

`.

Called by:
append_valid_line
analyst.utils.jupyformatter.write_output(out_fname, output_ls)[source]

Write the formatted lines to output file.

Called by:
main

analyst.utils.logging_init module

Setup logging module’s configuration for Jupyter notebook or iPython environment.

Example usage:
import logging from utils import logging_init
analyst.utils.logging_init.config_logging(level='info', format_='%(asctime)s [%(levelname)s] %(message)s', datefmt='%H:%M')[source]
Parameters:level (str, in {‘debug’, ‘info’, ‘warning’, ‘error’, ‘critical’})
analyst.utils.logging_init.critical(*msg_ls, sep=' ')[source]
analyst.utils.logging_init.debug(*msg_ls, sep=' ')[source]
analyst.utils.logging_init.error(*msg_ls, sep=' ')[source]
analyst.utils.logging_init.in_ipynb()[source]

Return True if this code is running in a Jupyter Notebook environment.

analyst.utils.logging_init.info(*msg_ls, sep=' ')[source]
analyst.utils.logging_init.init_logging()[source]

Format logging messages to a nicer layout.

Initialize the logging level to INFO, log format to FORMAT, and date format to DATEFMT.

analyst.utils.logging_init.main()[source]
analyst.utils.logging_init.printify_logging_msg(msg_ls, sep)[source]
analyst.utils.logging_init.warning(*msg_ls, sep=' ')[source]

Module contents