Skip to main content

Store some information within the current .py file for the distribution of a single-source-file Python application.

Project description

py_single_file_storage

Store some information within the current .py file for the distribution of a single-source-file Python application.

Application Scenarios

py_single_file_storage is a lightweight utility designed for storing persistent data directly within a single Python source file. It is specially optimized for distributing standalone, single-file Python applications where no external configuration files, databases, or auxiliary resources are desired.

By embedding state and runtime data inside the .py file itself, this library enables scripts to retain information across executions — such as execution counters, user preferences, cached parameters, or one-time setup flags — without creating additional files on the filesystem. It works reliably in scripted environments while avoiding conflicts in interactive Python shells, making it ideal for portable tools, utility scripts, self-contained CLI programs, and lightweight deployment scenarios where clean distribution and minimal footprint are priorities.

Installation

pip install py_single_file_storage

Usage

[!IMPORTANT] You are recommanded to only use this function in .py source file, not interactive command line of python.

from py_single_file_storage import PSFS_Object
import sys
import os
in_script = not hasattr(sys, 'ps1')

if in_script:
    filepath = __file__
else:
    filepath = os.path.join(os.getcwd(), "a.py")
    open(filepath, "a").close()

# a simple program to count the number of launch for script
with PSFS_Object(filepath) as mem:
    data = mem.get()
    if data.get("run_time") is None:
        data["run_time"] = 0
    data["run_time"] += 1
    mem.put(data)
    print(f"run time now: {data["run_time"]}")

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

py_single_file_storage-0.1.0.tar.gz (3.2 kB view details)

Uploaded Source

Built Distribution

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

py_single_file_storage-0.1.0-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file py_single_file_storage-0.1.0.tar.gz.

File metadata

  • Download URL: py_single_file_storage-0.1.0.tar.gz
  • Upload date:
  • Size: 3.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.2 CPython/3.13.12 Windows/11

File hashes

Hashes for py_single_file_storage-0.1.0.tar.gz
Algorithm Hash digest
SHA256 bd404d7d0a95d95d9a41f97084c0ee46dbef37ba0a6d5aebe0cb28eb257f7f60
MD5 850ad36267ac28490cb68673b786c313
BLAKE2b-256 1d8d7f852599d306c25ca1d191d8c8b54e01d151dc91a9c2daad82831830d4d0

See more details on using hashes here.

File details

Details for the file py_single_file_storage-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for py_single_file_storage-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bacb7b156a3f030ddd707a55d49d60f534003bb27fbb78aeb6652025f929dfad
MD5 b15b53348a137ff7d7a87ff86f3677b2
BLAKE2b-256 ae033bb94abbe8c9c84a6936d8cd491cec9c43aac62ab495d3469126c68794f5

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