Git Product home page Git Product logo

asingleton's Introduction

asingleton

Run tests Upload release assets

>>> from asingleton import singleton

>>> print(singleton.__doc__)

    singleton(cls, 
                attr_name='instance', 
                disable_name_mangling=False, 
                not_just_this_class=False) -> cls
    
    singleton(attr_name='instance', 
                disable_name_mangling=False, 
                not_just_this_class=False)(cls) -> cls
    
    >>> @singleton
    ... class Service:
    ...     pass
    ... 
    >>> Service() is Service.instance
    True
    >>> Service()  # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
    Traceback (most recent call last):
        ...
    AssertionError: There is already an instance of type <class '...Service'>;
    it can be accessed through the class attribute 'Service.instance'.
    
    >>> @singleton('__instance')
    ... class Service:
    ...     @classmethod
    ...     def get_instance(cls):
    ...         return cls.__instance
    ... 
    >>> Service() is Service.get_instance()
    True
    >>> Service.__instance
    Traceback (most recent call last):
        ...
    AttributeError: type object 'Service' has no attribute '__instance'
    
    >>> @singleton(not_just_this_class=True)
    ... class Service:
    ...     pass
    ... 
    >>> class Apache(Service):
    ...     pass
    ... 
    >>> Apache() is Apache.instance
    True
    >>> Apache()  # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
    Traceback (most recent call last):
        ...
    AssertionError: There is already an instance of type <class '...Apache'>;
    it can be accessed through the class attribute 'Apache.instance'.
    
    >>> class Service:
    ...     def __new__(cls, *args, **kwargs):
    ...         """My custom __new__"""
    ...         return super().__new__(cls, *args, **kwargs)
    ...     original_new = __new__
    ... 
    >>> singleton(Service) is Service
    True
    >>> Service.__new__ is Service.original_new
    False
    >>> Service.__new__.__wrapped__ is Service.original_new
    True
    >>> Service.__new__.__doc__
    'My custom __new__'
    

asingleton's People

Contributors

guallo avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.