Some utility functions using the Python Standard Library.



 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```"
            from IPython.display import Markdown
            source = Markdown(source)
        except NameError:
    return source



 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/"
dest = Path("./images")
file_extract(fname, dest)



 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 = ""
dest = Path("./images")
download_file(url, dest)