Package: slushie
Slushie 🍧🍭 Ever wanted to just get a file from a sibling or parent directory without pulling your hair out? Slushie is the perfect "it just works" solution to relative paths in Python.Table of Contents❔ Why Slushie?🚀 Installation🌈 Usage🔬 Running Tests🤝 Contributing📜 License❔ Why Slushie?Relative paths and imports in Python are an absolute nightmare due to howPYTHONPATHworks and finds modules.For example,project_root/
│   main.py
│
├───package1/
│   │   module1.py
│   │   file.csv
│   │
│   └───subpackage1/
│       │   module2.pyIf I wanted to importmodule1.pyfrommain.py, you'd think it would be something like the following:from package1 import module1
from package1.subpackage1 import module2This (most likely) will not work. Why?Python relies on the dreadedPYTHONPATHenvironment variable to determine where to look for modules to import.PYTHONPATHis a list of directories that Python checks whenever you attempt an import. Ifpackage1andsubpackage1are not included in thePYTHONPATH, Python doesn’t know where to look formodule1.py, andmodule2.py, resulting in anImportError.Additionally, attempting to openfile.csv, using the traditional open command like this:open("package1/file.csv")will most likely not even find the file, and even if it does, there's a high chance it will break if it is ever moved to another machine or ran from a different directory.This is because the search forfile.csvis relative to the current working directory where the Python script is executed, not necessarily where main.py is located.TL;DR: If you use python's default import and open commands, you either have to do some Python witchcraft or risk randomly breaking your code.🚀 InstallationInstall it directly from PyPI:pip install slushie🌈 Usagesip(*parts: str) -> strPurpose: Create absolute paths relative to the current FILE. Ideal for accessing files in parent or sibling directories without a fuss.Parameters:*parts: str- Parts of the path to join.Usage:Accesshello.txtlocated in a sibling directory fromscript.py./project
    /folder1
        script.py
    /folder2
        hello.txtpath=sip('..','folder2','hello.txt')print(path)# Output:# /path/to/project/folder2/hello.txt# In this case, sip('.') refers to /path/to/project/folder1/The above code is fundamentally equivalent to the following:importosimportsyspath=os.path.join(os.path.dirname(os.path.abspath(__file__)),'..','folder2','hello.txt')print(path)# Output:# /path/to/project/folder2/hello.txtThis is extremely useful, as if you ever need to open a file, such as a csv for data analysis or a text file for logging, you should almost always be using relative paths as to avoid breaking your code when you move it to a different machine or share it with someone else. Slushie makes this easy.### gulp(directory: str = '.') -> Iterator[None]**Purpose**:TemporarilyincludedirectoriesinthePythonpath,easingtheimportofmodules/packages.#### Parameters:-`directory:str`-Directorytoadddirectoriesfrom.#### Usage:Importamodulefromasiblingdirectory.```pythonwithgulp('../sibling_directory'):importa_module_from_sibling_directoryfreeze(path: str) -> NonePurpose: Make a specific directory permanently available for imports.Parameters:path: str- Path to append tosys.path.Usage:freeze('../another_directory')importa_module_from_another_directorypour(directory: str = '.') -> Iterator[Tuple[str, str]]Purpose: Easily access the current and parent directory paths of the current file the code is being written in.Parameters:directory: str- Directory to get paths for.Usage:withpour()as(current_dir,parent_dir):print(f"Current Directory:{current_dir}")print(f"Parent Directory:{parent_dir}")melt() -> strPurpose: Find the directory of the calling script, aiding in understanding the execution context.Usage:caller_path=melt()print(f"Caller Path:{caller_path}")# Output:# Caller Path: /path/to/calling/script.py# This is the path of the script that called melt(), not the path of melt() itself.# So if I had script /path/to/calling/script.py that called melt(), and melt() was located at /path/to/melt.py, the output would still be:# Caller Path: /path/to/calling/script.pyslurp() -> strPurpose: Identify where the terminal command was executed from.Usage:terminal_path=slurp()print(f"Terminal Path:{terminal_path}")# So if the script was located at /path/to/script.py and the terminal command was executed from /path/to, the output would be:# Terminal Path: /path/toscoop(file: str, mode: str = 'r', ...) -> TextIOPurpose: Simplify opening files by managing paths relative to the current script automatically.Parameters:Literally the same as the built-inopen()function. It's just a wrapper around it that automatically manages paths relative to the current script.Usage:withscoop('../data.txt','r')asfile:data=file.read()print(data)🔬 Running TestsKeeping Slushie frosty with some cool tests:For Linux:./run_tests.shFor Windows:run_tests.bat🤝 ContributingContribute your own flavors to make Slushie even more delightful! 🌈📜 LicenseSlushie is lovingly served under the MIT License. Scoop into theLICENSEfile for the full details.