core

Some utility functions using the Python Standard Library.

source

get_source_code

 get_source_code (obj:object, markdown=False, remove_documentation=False)

Returns the source code of an object, with an optional markdown formatting.

Type Default Details
obj object The object whose source code you want to retrieve.
markdown bool False Return the source code formatted as markdown
remove_documentation bool False Remove docstrings and comments
get_source_code(get_source_code, markdown=True, remove_documentation=True)
def get_source_code(obj:object, # The object whose source code you want to retrieve.
                    markdown=False, # Return the source code formatted as markdown
                    remove_documentation=False): # Remove docstrings and comments
    source = inspect.getsource(obj)
    
    if remove_documentation:
        in_docstring = False
        lines = source.split('\n')
        source = ''
        for line in lines:
            if line.strip().startswith(('\'\'\'', '\"\"\"')):
                in_docstring = not in_docstring
            elif not in_docstring:
                source += line + '\n'
        source = '\n'.join([line for line in source.split('\n')
                            if not line.strip().startswith(('#'))])
        source = source.replace('\n\n', '\n')
    if markdown:
        source = f"```python\n{source}\n```"
        try:
            get_ipython
            from IPython.display import Markdown
            source = Markdown(source)
        except NameError:
            pass
    return source

source

file_extract

 file_extract (fname, dest=None)

Extract the contents of the given archive file to the destination directory.

Raises: Exception: if the archive file format is not recognized (supported formats are gz and zip)

Type Default Details
fname The path of the archive file
dest NoneType None The path of the destination directory
fname = "../images/images.zip"
dest = Path("./images")
print(dest.exists())
file_extract(fname, dest)
print(dest.exists())
print(list(os.walk(dest))[0][2])
False
True
['cat.jpg']

source

download_file

 download_file (url:str, directory:str, overwrite:bool=False)

Download a file from a given URL to a specified directory.

This function sends a HTTP request to the URL provided, downloads the file and saves it to the specified directory. If the directory does not exist, it creates it. If the file already exists and the overwrite flag is set to False, it skips the download process.

The function also displays a progress bar during the download using the tqdm module, and checks if the download was successful based on the total file size. If the download is not successful, it prints an error message.

Type Default Details
url str The URL of the file to download.
directory str The directory where the file should be saved.
overwrite bool False A flag to specify whether to overwrite the file if it already exists.
url = "https://raw.githubusercontent.com/cj-mills/cjm-psl-utils/main/images/cat.jpg"
dest = Path("./images")
download_file(url, dest)