Skip to main content

Singletonize for Python

PyPI version License Publish to PyPi

Overview

Singletonize is a Python package providing a robust, thread-safe Singleton pattern implementation using metaclasses. It ensures strict singleton enforcement while offering useful utilities for instance management.

Features

  • Strict Singleton Enforcement: Ensures only one instance of a class exists.
  • Abstract Base Singleton Support: Allows defining abstract singleton classes.
  • Thread-Safe Implementation: Uses metaclasses for thread safety.
  • Instance Management Utilities: Methods for checking, resetting, updating, and serializing instances.
  • Fully Typed with MyPy: Ensures type safety.
  • Modern Package Management with Poetry: Streamlined dependency and package management.

Installation

Install using pip:

pip install singletonize

Or with poetry:

poetry add singletonize

Usage

Basic Singleton Example

from singletonize import Singleton

class ExampleSingleton(Singleton):
    def __init__(self, value: int):
        self.value = value

# Creating an instance
instance1 = ExampleSingleton(10)
instance2 = ExampleSingleton.get_instance()

assert instance1 is instance2  # True
assert instance1.value == instance2.value == 10  # True

Abstract Base Singleton

from singletonize import SingletonABC
from abc import abstractmethod

class AbstractExample(SingletonABC):
    @abstractmethod
    def some_method(self):
        pass

class ConcreteExample(AbstractExample):
    def __init__(self, value: int):
        self.a = value

    def some_method(self):
        return self.a

# Creating a singleton instance
singleton_instance = ConcreteExample(10)
assert singleton_instance.some_method() == 10

Instance Management

# Checking if an instance exists
assert ExampleSingleton.has_instance()  # True

# Getting the instance or None
instance = ExampleSingleton.get_instance_or_none()

# Updating instance attributes
ExampleSingleton.update_instance(value=20)
assert instance.value == 20

# Converting instance to dictionary
instance_dict = ExampleSingleton.instance_as_dict()
print(instance_dict)  # {'value': 20}

# Resetting the instance
ExampleSingleton.reset_instance(30)
assert ExampleSingleton.get_instance().value == 30

Performance Benchmark

Singletonize has been benchmarked to evaluate its efficiency in both single-threaded and multi-threaded environments.

Single-threaded Performance

Standard Singleton

Name Min (ns) Max (ns) Mean (ns) StdDev (ns) Median (ns) IQR (ns) Outliers OPS (Mops/s) Rounds Iterations
test_singleton_performance 493.0189 29,423.0122 566.5408 424.3932 535.0157 20.0234 2081;8156 1.7651 114969 1

SingletonABC

Name Min (ns) Max (ns) Mean (ns) StdDev (ns) Median (ns) IQR (ns) Outliers OPS (Mops/s) Rounds Iterations
test_singleton_abc_performance 418.9002 7,668.2001 461.9932 96.6991 445.9507 8.2495 3741;12382 2.1645 108425 20

Multi-threaded Performance

SingletonABC with Multithreading

Name Min (ms) Max (ms) Mean (ms) StdDev (ms) Median (ms) IQR (ms) Outliers OPS Rounds Iterations
test_singleton_abc_multithreading_performance 11.7924 25.3676 13.9574 3.3869 12.5568 1.0450 11;11 71.6466 70 1

Standard Singleton with Multithreading

Name Min (ms) Max (ms) Mean (ms) StdDev (ms) Median (ms) IQR (ms) Outliers OPS Rounds Iterations
test_singleton_multithreading_performance 11.8365 22.9825 13.9596 3.3061 12.7048 1.2596 8;8 71.6355 53 1

These benchmarks indicate that SingletonABC has slightly better performance in single-threaded scenarios, while both implementations perform similarly under multi-threaded conditions.

Development

Setting Up the Project

  1. Clone the repository:

    git clone https://github.com/juans-castellanosr/singletonize.git
    cd singletonize
    
  2. Install dependencies using poetry:

    poetry install
    

Code Quality & Type Checking

This project enforces strict type checking and linting:

mypy singletonize
ruff check singletonize

Running Tests

Tests are managed with pytest:

pytest tests/

Performance benchmarks can be run using:

pytest --benchmark-only

Contributing

We welcome contributions! Follow these guidelines to ensure smooth collaboration:

Forking and Branching Strategy

  1. Fork the repository and clone it locally.
  2. Create a branch from development:
    git checkout -b feature/your-feature development
    
  3. Make your changes and commit with meaningful messages.
  4. Push your branch to your fork:
    git push origin feature/your-feature
    
  5. Create a Pull Request (PR) to the development branch.
  6. Wait for approval and feedback before merging.

Code Standards

  • Follow PEP8 (ruff is used for linting).
  • Use mypy for type checking.
  • Ensure all tests pass before submitting PRs.

License

This project is licensed under the MIT License. See LICENSE for details.

Contact

For support, open an issue or contact the maintainers via GitHub Discussions.


Happy coding! 🚀

Metadata

Release files for singletonize 1.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for singletonize 1.1.2
File Size Uploaded Provenance Actions
singletonize-1.1.2.tar.gz 9.5 kB Without provenance Details for singletonize-1.1.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for singletonize 1.1.2
File Interpreter ABI Platform Actions
singletonize-1.1.2-py3-none-any.whl Python 3 none any Details for singletonize-1.1.2-py3-none-any.whl

Total release size: 20.6 kB

Release files / singletonize-1.1.2.tar.gz

Download URL singletonize-1.1.2.tar.gz
Size 9.5 kB
Tags Source
SHA-256 checksum
How to use checksums
1f22264ba45014cdb1df05e6c91d8be9a420aa4b97bf1eea4dad82e47d1b18d6
BLAKE2b-256 checksum
How to use checksums
b096ad636baa49944bd00619dd92d0462c5d269219c6ec6073ab5cd8717f1425
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.0 CPython/3.10.16 Linux/6.8.0-1021-azure

Release files / singletonize-1.1.2-py3-none-any.whl

Download URL singletonize-1.1.2-py3-none-any.whl
Size 11.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a86f876554a871b74905a2dcf5b0cb61cb2c3195cf073467d1ee9bf7001541fc
BLAKE2b-256 checksum
How to use checksums
7a2a26b14b4454447b8be2757aedfb1225cbd47f39f2d64b9eacc56c087ced1e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.0 CPython/3.10.16 Linux/6.8.0-1021-azure

Security for singletonize 1.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.1.2 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page