Skip to main content

A utility for managing data and log directory paths in Python projects

Project description

Data Path Config

A Python utility for managing data and log directory paths in projects and subprojects. This package provides a clean interface for handling path configurations across different environments.

Features

  • Configurable data and log directories
  • Environment variable support
  • .env file integration
  • Support for project and subproject structures
  • Automatic directory creation
  • Path validation

Installation

pip install data-path-config
pip install --upgrade data-path-config

Usage

from data_path_config import dpc

# Basic usage
config = dpc(project_name="my_project")
data_dir = config.data_dir()
log_dir = config.log_dir()

# With subproject
config = dpc(
    project_name="my_project",
    subproject="sub1",
    data_dir="/custom/data/path",
    log_dir="/custom/log/path"
)

# Get directories
project_data = config.project_dir()
subproject_data = config.sub_project_dir()
project_logs = config.project_log_dir()
subproject_logs = config.sub_project_log_dir()

License

MIT License

Python Path Configuration

Default env variable

DATA_DIR

Default log variable

LOG_DIR

When DATA_DIR doesn't exist, it will throw error

The pathconfig package is a Python utility designed to simplify the management of directory paths for data and logs in your projects. It provides a structured way to define, override, and access these paths using constructor arguments, environment variables, or default settings. This ensures consistency and flexibility across different environments, such as local development, production, or cron jobs.

Key Features

  • Environment Variable Integration: Reads paths from .env, .zshrc, or .profile files.
  • Constructor Overrides: Allows explicit path definitions via constructor arguments.
  • Default Paths: Falls back to sensible defaults (~/data, ~/logs) when no other configuration is provided.
  • Subproject Support: Easily manage paths for subprojects or experiments.
  • Cron Compatibility: Ensures paths are resolved correctly in scheduled tasks.
  • Security: Encourages secure handling of configuration files.

This package is ideal for developers who need a consistent and configurable way to manage project directories, especially in environments with varying configurations.

Usage Examples

from data_path_config import dpc

# Initialize with explicit data and log directories
config = dpc(
    project_name="my_project",
    data_dir="/custom/data/path",
    log_dir="/custom/log/path",
    subproject="experiment1"
)

# Base directory methods (do not append project/subproject, must exist)
try:
    print(f"Base data dir: {config.data_dir()}")  # /custom/data/path
    print(f"Base log dir: {config.log_dir()}")   # /custom/log/path
except FileNotFoundError as e:
    print(f"Error: {e}")

# Project and subproject directory methods (create if not exist)
print(f"Project data dir: {config.project_dir()}")   # /custom/data/path/my_project
print(f"Subproject data dir: {config.sub_project_dir()}")  # /custom/data/path/my_project/experiment1
print(f"Project log dir: {config.project_log_dir()}")  # /custom/log/path/my_project
print(f"Subproject log dir: {config.sub_project_log_dir()}")  # /custom/log/path/my_project/experiment1

# Initialize without subproject
config_no_sub = PathConfig(
    project_name="my_project",
    data_dir="/custom/data/path",
    log_dir="/custom/log/path"
)
print(f"Base data dir: {config_no_sub.data_dir()}")  # /custom/data/path
print(f"Project data dir: {config_no_sub.project_dir()}")  # /custom/data/path/my_project
try:
    print(config_no_sub.sub_project_dir())  # Raises ValueError
except ValueError as e:
    print(f"Error: {e}")

# Using environment variables or defaults
env_config = PathConfig(project_name="my_project", subproject="experiment1")
try:
    print(f"Base data dir from env: {env_config.data_dir()}")  # e.g., /path/to/data from DATA_DIR
except FileNotFoundError as e:
    print(f"Error: {e}")

# Get an environment variable directly
value = PathConfig.get_env_var("MY_CUSTOM_VAR", "default_value")
print(f"Custom env var: {value}")

Configuration Files

.env (Current Directory)

DATA_DIR=/path/to/data
LOG_DIR=/path/to/logs
MY_CUSTOM_VAR=something

.zshrc or .profile (Home Directory)

export DATA_DIR=/path/to/data
export LOG_DIR=/path/to/logs
export MY_CUSTOM_VAR=something

Path Resolution Priority

  1. Constructor arguments (data_dir, log_dir).
  2. Environment variables (DATA_DIR, LOG_DIR) from:
    • .env (current directory).
    • ~/.zshrc or ~/.profile (home directory).
  3. Default paths (~/data, ~/logs).

Additional Notes

  • Cron Compatibility: Works in cron by prioritizing constructor arguments. Ensure .zshrc or .profile is sourced:
    * * * * * source ~/.zshrc && python /path/to/script.py
    
  • Virtual Environments: Fully compatible.
  • Installation: Requires python-dotenv (pip install python-dotenv).
  • Security: Restrict permissions on configuration files (e.g., chmod 600 ~/.zshrc).
  • Testing: Verify explicit overrides and fallback behavior in clean environments.

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

data_path_config-0.5.6.tar.gz (16.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

data_path_config-0.5.6-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

Details for the file data_path_config-0.5.6.tar.gz.

File metadata

  • Download URL: data_path_config-0.5.6.tar.gz
  • Upload date:
  • Size: 16.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.12

File hashes

Hashes for data_path_config-0.5.6.tar.gz
Algorithm Hash digest
SHA256 72ec028f013ededecca13318465b6d2bf1cbc1a4cbb0f6fff7772238574dcc3b
MD5 ae7d4e2a49c350d0e8195709dd26265f
BLAKE2b-256 052b85e0aa354f7fc9b96f49ad75ab660c84b95a66ecf4461a3a42cf27a3d879

See more details on using hashes here.

File details

Details for the file data_path_config-0.5.6-py3-none-any.whl.

File metadata

File hashes

Hashes for data_path_config-0.5.6-py3-none-any.whl
Algorithm Hash digest
SHA256 0b145311035b35f1471a62cfe8df517b08ba901b576626e535016f00955ef30b
MD5 fce5ba5679777c104ae1480c8b66674a
BLAKE2b-256 1f7806f7d6a335f670b97c4e1abb8415fc9c0708cd7e4397b944f242ab925286

See more details on using hashes here.

Supported by

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