Skip to main content

Decorator for implementing singleton classes

Project description

Decorator for implementing singleton pattern

Usage

To install, add pygleton module to your project. Then add from pygleton import singleton to your file.

There are two ways to use the decorator: without repetetive __init__() calls and with them (you do not want this unless you know what are you doing).

Main scenario: call init only the first time

Do not pass any parameter to the decorator:

@singleton()
class Foo:
    ...

Now, when you do Foo(...) for the first time, object instance will be created, and Foo init method would be called. Any subsequent Foo(...) calls will return the same instance, and Foo.__init__ will be completely ignored.

I know what I am doing: I want to call init every time

Pass truthy value to recall_init parameter of the decorator:

@singleton(recall_init=True)
class Foo:
    ...

Now, when you do Foo(...) for the first time, object instance will be created, and all subsequent Foo(...) calls will return the same instance. However, init method will be called every time.

Usage example

See test file.

Development

The project uses poetry (highly recommended) and you should know your way around. The supplied Makefiles allows to run linters, check formatting, and run tests (100% coverage is required!) simply by typing make. Everything should pass.

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

pygleton-1.0.1.tar.gz (3.0 kB view details)

Uploaded Source

Built Distribution

pygleton-1.0.1-py3-none-any.whl (3.4 kB view details)

Uploaded Python 3

File details

Details for the file pygleton-1.0.1.tar.gz.

File metadata

  • Download URL: pygleton-1.0.1.tar.gz
  • Upload date:
  • Size: 3.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.4 FreeBSD/14.1-RELEASE-p2

File hashes

Hashes for pygleton-1.0.1.tar.gz
Algorithm Hash digest
SHA256 b92298b54fe54ef8d98b77c40ceda5d0eb762050949ab281757905b05a0f812b
MD5 8f464522d94fbc048de25440fda541a5
BLAKE2b-256 3f8017bc4629d9acf4601e69d00c1af8f7d5fe767028b9d2dfeaf9cf0eb81b06

See more details on using hashes here.

File details

Details for the file pygleton-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: pygleton-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 3.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.4 FreeBSD/14.1-RELEASE-p2

File hashes

Hashes for pygleton-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3a95e60eb4fbc7b14382c268e2709a9cbae7deb931a58e79c03d41d1ff1c96f7
MD5 aa4bad979fdf16d3db68e395a26aca34
BLAKE2b-256 825eeb4775bc4a947cb52e97141c5abee85cbf7c6ca9a6d839f434de2913fcea

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