pytwd.jupyter_hub package

Submodules

pytwd.jupyter_hub.display module

pytwd.jupyter_hub.find module

Search for files where the file contents contain text in search_folder and its subfolders.

Parameters:
  • text (str) -- text to search for

  • project (Optional[str]) -- specific project to search in

  • search_folder (Optional[Path]) -- folder to start search from

  • extension (str) -- the file type being searched

  • print_progress (bool) -- whether to print out progress statement to console

Return type:

List[str]

file_content_search('my text', project='TWD-NL-202X-XXX', search_folder=Path('/srv', 'scratch', 'Projects'),
                    extension='.ipynb', print_progress = True) -> List[str]

Search for files where the filename contains text in search_folder and its subfolders.

Parameters:
  • text (str) -- text to search for

  • project (Optional[str]) -- specific project to search in

  • search_folder (Optional[Path]) -- folder to start search from

  • extension (str) -- the file type being searched

  • print_progress (bool) -- whether to print out progress statement to console

Return type:

List[str]

filename_search('my text', project='TWD-NL-202X-XXX', search_folder=Path('/srv', 'scratch'), extension='.ipynb')
find_in_file(file, text)

Check if text is present in the file lines of file

Parameters:
  • file (Path) -- file to check

  • text (str) -- text to check for

Return type:

bool

find_in_file(Path('/srv', 'scratch', 'TWD-NL-202X-XXX', 'example.ipynb'), text='my text')
get_search_folder(search_folder)

Get the default search folder if it is not provided. This is only relevant on jupyter-(cloud).twd.nl.

Parameters:

search_folder (Optional[Path]) -- Provided search folder

>>> custom = get_search_folder(Path("/my/own/folder"))
>>> custom.parts[1:]
('my', 'own', 'folder')
:rtype: :py:class:`~pathlib.Path`
>>> default = get_search_folder(None)
>>> default.parts[1]
'srv'
search_for_projects(text, search_folder=None, extension='.ipynb', project_level=None, require_TWD=False, print_progress=True)

Search for projects which might contain text, either in the filename or the data of the files

Parameters:
  • text (str) -- text to be matched

  • search_folder (Optional[Path]) -- folder to start search from

  • extension (str) -- the file type being searched

  • project_level (Optional[int]) -- can be used to set the level of the dir that is returned as the "project name"

  • require_TWD (bool) -- if set to True, projects must start with TWD-

  • print_progress (bool) -- whether search progress should be printed

Return type:

List[str]

Returns:

sorted list of matching of projects

search_for_projects('my text', search_folder=Path('/srv', 'scratch', 'Projects'), extension='.py')

pytwd.jupyter_hub.images module

pytwd.jupyter_hub.py_tools module

import_from_parent_dir(relative_path)
There are 3 steps:
  • set current working directory to that of the relative path

  • import the file

  • set the current working directory back to that at the start

Parameters:

relative_path (Union[str, Path]) -- path to file to be imported of the form: ../../example.py

Returns:

a module that can be imported from

run_file(relative_path)
There are 4 steps:
  • set current working directory to that of the relative path

  • run the file

  • reset the namespace (wipes out entire namespace)

  • set the current working directory back to that at the start

It is done this way to allow the file to be run in it's own folder Resetting is done to prevent weird constructions where values are imported from one file into another without noticing

Parameters:

relative_path (Union[str, Path]) -- path to file to be run of the form: ../../example.py

pytwd.jupyter_hub.utils module

class FileDetails(project_number, calc_code, revision, title, relative_path)

Bases: tuple

calc_code

Alias for field number 1

project_number

Alias for field number 0

relative_path

Alias for field number 4

revision

Alias for field number 2

title

Alias for field number 3

class NamingConvention(start_name, doc_codes, rev_key, separators)

Bases: tuple

doc_codes

Alias for field number 1

rev_key

Alias for field number 2

separators

Alias for field number 3

start_name

Alias for field number 0

check_twd_naming_convention(filename)

This function checks for the TWD naming convention and returns the four checks

Parameters:

filename (str) -- File name under consideration

Return type:

Tuple[bool, bool, bool, bool]

Usage example: >>> check_twd_naming_convention('TWD-NL-2022-104-C-124-REV-0, example calc.ipynb') (True, True, True, True)

get_author()

After finding the username translate all characters to upper case for standard user name conventions.

Return type:

str

get_calculations_from_dir(directory_path=None, file_extension='.ipynb', names_to_skip=None, print_excluded_files=False)

Find all calculations in a folder following the TWD naming convention.

Parameters:
  • directory_path (Union[str, Path, None]) -- Path of a directory

  • file_extension (str) -- name of the filetype that is searched

  • names_to_skip (Optional[Sequence[str]]) -- list with strings with folder names to exclude from the calculation search

  • print_excluded_files (bool) -- boolean to also return FileDetails of files that were excluded

Return type:

dict[str, FileDetails]

Usage example:

>>> from pytwd import REPO_DIR_PATH
>>> get_calculations_from_dir(REPO_DIR_PATH / "pytwd" / "widgets" / "showcase_template" / "10 ENGINEERING" / "Showcase notebook")
{'TWD-XX-20XX-XXX-C-XXX-REV-X, Widgets Showcase Template for XXX.ipynb': FileDetails(project_number='TWD-XX-20XX-XXX', calc_code='C-XXX', revision='X', title='Widgets Showcase Template for XXX.ipynb', relative_path='.')}
get_details_from_filename(filename)

This function finds all of the relevant information required for the pdf document to display in its header given a file. Files need to adhere to the TWD standard of calculation number and naming for the correct items to be found within the file name.

Parameters:

filename (str) -- File name under consideration

Return type:

Tuple[str, str, Union[int, str], str]

get_doc_details(calc_code=None, revision=None)

This function finds and gathers all of the relevant information of the calculation under consideration.

Parameters:
  • calc_code (Optional[str]) -- Calculation code

  • revision (Optional[str]) -- Revision of calculation

Return type:

Tuple[str, str, str, Union[int, str], str, str]

get_filename(calc_code, revision)

Find the filename of the given calculation code.

Parameters:
  • calc_code (str) -- Calculation code

  • revision (Union[str, int]) -- Revision of calculation

Return type:

str

get_filename_with_calc(calc_code, revision, directory=None, suffix='.ipynb')

Find filenames given a specified calc code and revision. This function ensures correct syntax, whether the specified calculations exist as well as uniqueness of the code used.

Parameters:
  • calc_code (str) -- Calculation code

  • revision (Union[str, int]) -- Revision of calculation

  • directory (Optional[Path]) -- Directory to look in for files. Defaults to current working directory and all sub directories

  • suffix (str) -- suffix of file to look for e.g. '.ipynb' or '.pdf'

Return type:

str

get_username()

Find username in the Jupyter hub environment as long as it follows the 3-letter standard.

Return type:

str

Module contents