Skip to main content

Single Instance

Project description

An Easy-Used Single Instance package

  • support simple single instance
  • support

install

pip install single-ins

how to use

SingleHashableInstance

from single_ins.hashable_si import SingleHashableInstance
from dataclasses import dataclass

class Foo(SingleHashableInstance):

    def __init__(self, v):
        self.v = v

    def __hash__(self):
        return self.v


@dataclass
class Data(SingleHashableInstance):
    v: int

    def __hash__(self):
        return self.v


if __name__ == '__main__':
    assert id(Foo(1)) == id(Foo(1))
    assert id(Foo(2)) != id(Foo(1))

    assert id(Data(1)) == id(Data(1))
    assert id(Data(2)) != id(Data(1))

SingleEqualableInstance

from single_ins import SingleEqualableInstance


class Foo(SingleEqualableInstance):

    def __init__(self, v, b=1, *args, **kwargs):
        self.v = v

    def __eq__(self, other):
        if not isinstance(other, Foo):
            return False
        return self.v == other.v


if __name__ == '__main__':
    assert id(Foo(1)) == id(Foo(1))
    assert id(Foo(2)) != id(Foo(1))

SingleInstance

from single_ins import SingleInstance


class Foo(SingleInstance):

    def __init__(self):
        pass


if __name__ == '__main__':
    assert id(Foo()) == id(Foo())
    assert id(Foo.instance) == id(Foo())

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

single-ins-0.0.1.tar.gz (2.5 kB view hashes)

Uploaded Source

Built Distribution

single_ins-0.0.1-py3-none-any.whl (3.8 kB view hashes)

Uploaded Python 3

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