Skip to main content

Singleton & TtlSingleton meta-class

Project description

Singleton 및 TtlSingleton 메타클래스

이 프로젝트는 Python에서 싱글톤 클래스를 생성하기 위한 SingletonTtlSingleton 메타클래스를 제공합니다. TtlSingleton 메타클래스는 Singleton 기능을 확장하여 지정된 시간이 지나면 인스턴스가 만료되는 TTL(Time-To-Live) 기능을 추가합니다.

기능

  • Singleton: 클래스 이름과 초기화 매개변수가 동일한 경우 하나의 인스턴스만 생성되도록 보장합니다.
  • TtlSingleton: Singleton을 확장하여 TTL을 지원하며, 지정된 시간이 지나면 인스턴스가 만료되고 새로 생성됩니다.
  • 두 클래스 모두 use_class_name_only 매개변수를 true로 지정하면 초기화 매개변수와 관계없이 클래스 이름만으로 싱글톤을 생성할 수 있습니다. (전통적인 싱글톤과 동일)

설치

pip install advanced-python-singleton

` 이 프로젝트를 사용하려면 저장소를 클론하고 필요한 종속성을 설치하십시오:

git clone https://github.com/jogakdal/advanced-singleton.git
cd <repository-directory>
pip install -r requirements.txt

사용법

Singleton

  • 싱글톤 클래스를 생성하려면 Singleton 메타 클래스로 지정해 주면 됩니다.
  • use_class_name_only 매개변수를 true로 지정하면 초기화 매개변수와 관계없이 클래스 이름만으로 싱글톤을 생성합니다. (전통적인 싱글톤과 동일, default는 false)
from advanced_python_singleton.singleton import Singleton


class SomeClass(metaclass=Singleton):
    def __init__(self, name):
        self.name = name

TtlSingleton

  • TTL 기반 싱글톤 클래스를 생성하려면 TtlSingleton 메타클래스를 사용하고 ttl 매개변수를 지정해 주세요.
  • ttl 매개변수는 인스턴스가 만료되기까지의 시간(초)을 나타냅니다. default는 60초입니다.
  • use_class_name_only 매개변수를 true로 지정하면 초기화 매개변수와 관계없이 클래스 이름만으로 싱글톤을 생성합니다. (전통적인 싱글톤과 동일, default는 false)
from advanced_python_singleton.ttlsingleton import TtlSingleton


class SomeClass(metaclass=TtlSingleton, ttl=10):
    def __init__(self, name):
        self.name = name

예제

다음은 TtlSingleton 사용법을 보여주는 예제입니다:

import time

from advanced_python_singleton.singleton import Singleton
from advanced_python_singleton.ttlsingleton import TtlSingleton

# Singleton 테스트
class SingletonWithParamExample(metaclass=Singleton):
    def __init__(self, name):
        self.name = name
        print(f'{self.name}({self}) is created')


class SingletonWithoutParamExample(metaclass=Singleton, use_class_name_only=True):
    def __init__(self, name):
        self.name = name
        print(f'{self.name}({self}) is created')


a = SingletonWithParamExample('a')
b = SingletonWithParamExample('b')
c = SingletonWithParamExample('a')
assert a != b
assert a == c
assert b == SingletonWithParamExample('b')

a = SingletonWithoutParamExample('a')
b = SingletonWithoutParamExample('b')
assert a == b
assert a == SingletonWithoutParamExample('c')

# TtlSingleton 테스트
class TtlSingletonExample(metaclass=TtlSingleton, ttl=2):
    def __init__(self, name):
        self.name = name
        print(f'    {self.name} is created')


class SingletonTest():
    def test(self, p):
        return TtlSingletonExample(p)

print('a creating')
a = TtlSingletonExample('a')
assert a.name == 'a'

print('b creating')
b = TtlSingletonExample('b')
assert b.name == 'b'

print('sleep 1 sec')
time.sleep(1)

print('a creating')
c = TtlSingletonExample('a')
assert a == c
assert c.name == 'a'

print('sleep 2 sec')
time.sleep(2)

print('a creating')
d = TtlSingletonExample('a')
assert a != d  # 시간이 2초 이상 지났음으로 새로운 객체 생성

print('a creating via SingletonTest()')
assert d == SingletonTest().test('a')

print('sleep 2 sec')
time.sleep(2)

print('a creating via SingletonTest()')
e = SingletonTest().test('a')
assert d != e

print('a creating')
assert e == TtlSingletonExample('a')

# TTL이 서로 다른 클래스 테스트
class TtlSingletonExample_5(metaclass=TtlSingleton, ttl=5):
    def __init__(self, name):
        self.name = name

class TtlSingletonExample_8(metaclass=TtlSingleton, ttl=8):
    def __init__(self, name):
        self.name = name

a = TtlSingletonExample_5('a')
b = TtlSingletonExample_8('a')
assert a != b

time.sleep(6)

c = TtlSingletonExample_5('a')
d = TtlSingletonExample_8('a')
assert a != c
assert b == d

time.sleep(3)

assert c == TtlSingletonExample_5('a')
assert d != TtlSingletonExample_8('a')


# use_class_name_only=True 테스트
class SingletonClassNameOnly(metaclass=TtlSingleton, ttl=60, use_class_name_only=True):
    def __init__(self, name, value):
        self.name = name
        self.value = value

instance1 = SingletonClassNameOnly('test', 123)
instance2 = SingletonClassNameOnly('test', 456)

assert instance1 == instance2
assert instance1.value == 123
assert instance2.value == 123

라이선스

이 라이브러리는 누구나 사용할 수 있는 프리 소프트웨어입니다. 다만 코드를 수정할 경우 변경된 내용을 원작성자에게 통보해 주시면 감사하겠습니다.

작성자

황용호(jogakdal@gmail.com)

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

advanced-python-singleton-1.3.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

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

advanced_python_singleton-1.3-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file advanced-python-singleton-1.3.tar.gz.

File metadata

  • Download URL: advanced-python-singleton-1.3.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.9.6

File hashes

Hashes for advanced-python-singleton-1.3.tar.gz
Algorithm Hash digest
SHA256 f9500f57064410666d76e24fb76aff012b2c6571134d84b1f086f07893c8ba29
MD5 226f3e624c897c857aaaf4d71798f4d6
BLAKE2b-256 92fad70feb07b59aee290713a2deea8cbc6cf8efe779bfeee5c3fd0394548cff

See more details on using hashes here.

File details

Details for the file advanced_python_singleton-1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for advanced_python_singleton-1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 92172eda388dc3b09dfcba5c6f1ac11944a7d9da25f525dd29ce886efe89f660
MD5 3e817687d505204198b6f0fa868c748c
BLAKE2b-256 c7a14af6ce4934f90e6788c94947d9a9b95cf0be8b31f4c1ff85972e1655b7f8

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