Skip to main content

Slushie: A Python library for relative path manipulation.

Project description

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.

Slushie Demo

Table of Contents

❔ Why Slushie?

Relative paths and imports in Python are an absolute nightmare due to how PYTHONPATH works and finds modules.

For example,

project_root/
│   main.py
│
├───package1/
│   │   module1.py
│   │   file.csv
│   │
│   └───subpackage1/
│       │   module2.py

If I wanted to import module1.py from main.py, you'd think it would be something like the following:

from package1 import module1
from package1.subpackage1 import module2

This (most likely) will not work. Why?

Python relies on the dreaded PYTHONPATH environment variable to determine where to look for modules to import.

PYTHONPATH is a list of directories that Python checks whenever you attempt an import. If package1 and subpackage1 are not included in the PYTHONPATH, Python doesn’t know where to look for module1.py, and module2.py, resulting in an ImportError.

Additionally, attempting to open file.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 for file.csv is 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.

🚀 Installation

Install it directly from PyPI:

pip install slushie

🌈 Usage

sip(*parts: str) -> str

Purpose: 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:

Access hello.txt located in a sibling directory from script.py.

/project
    /folder1
        script.py
    /folder2
        hello.txt
path = 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:

import os
import sys

path = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'folder2', 'hello.txt')
print(path)

# Output:
# /path/to/project/folder2/hello.txt

This 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**: Temporarily include directories in the Python path, easing the import of modules/packages.

#### Parameters:
- `directory: str` - Directory to add directories from.

#### Usage:
Import a module from a sibling directory.

```python
with gulp('../sibling_directory'):
    import a_module_from_sibling_directory

freeze(path: str) -> None

Purpose: Make a specific directory permanently available for imports.

Parameters:

  • path: str - Path to append to sys.path.

Usage:

freeze('../another_directory')
import a_module_from_another_directory

pour(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:

with pour() as (current_dir, parent_dir):
    print(f"Current Directory: {current_dir}")
    print(f"Parent Directory: {parent_dir}")

melt() -> str

Purpose: 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.py

slurp() -> str

Purpose: 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/to

scoop(file: str, mode: str = 'r', ...) -> TextIO

Purpose: Simplify opening files by managing paths relative to the current script automatically.

Parameters:

Literally the same as the built-in open() function. It's just a wrapper around it that automatically manages paths relative to the current script.

Usage:

with scoop('../data.txt', 'r') as file:
    data = file.read()
    print(data)

🔬 Running Tests

Keeping Slushie frosty with some cool tests:

  • For Linux:

    ./run_tests.sh
    
  • For Windows:

    run_tests.bat
    

🤝 Contributing

Contribute your own flavors to make Slushie even more delightful! 🌈

📜 License

Slushie is lovingly served under the MIT License. Scoop into the LICENSE file for the full details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

slushie-0.2.4.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

slushie-0.2.4-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file slushie-0.2.4.tar.gz.

File metadata

  • Download URL: slushie-0.2.4.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.13

File hashes

Hashes for slushie-0.2.4.tar.gz
Algorithm Hash digest
SHA256 e912ea7624d83cf23aadcdd6e41bdac05a98bd35437d513ea67047ae0a65429e
MD5 da9f8e73f77d19d0979aa1546592c529
BLAKE2b-256 5aaebb554bb42dd7c85e8443344f32f12198e5fcb527119f9d40289c71e17936

See more details on using hashes here.

File details

Details for the file slushie-0.2.4-py3-none-any.whl.

File metadata

  • Download URL: slushie-0.2.4-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.13

File hashes

Hashes for slushie-0.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 3cc853e23ac25d04a88a8c585b1450b69e76c559ef49d3e9cff6e065e8ae5667
MD5 40bd582441594c391e79993637e6909a
BLAKE2b-256 f2754fbaf64543d6b455ad4f03cb64b72886bab5f2be5398416bc01e9d4db76d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page