tori.decorator.common¶
Author: | Juti Noppornpitak |
---|
This package contains decorators for common use.
-
class
tori.decorator.common.
BaseDecoratorForCallableObject
(reference)¶ Base decorator based from an example at http://www.artima.com/weblogs/viewpost.jsp?thread=240808.
-
tori.decorator.common.
make_singleton_class
(class_reference, *args, **kwargs)¶ Make the given class a singleton class.
class_reference is a reference to a class type, not an instance of a class.
args and kwargs are parameters used to instantiate a singleton instance.
To use this, suppose we have a class called
DummyClass
and later instantiate a variabledummy_instnace
as an instance of classDummyClass
.class_reference
will beDummyClass
, notdummy_instance
.Note that this method is not for direct use. Always use
@singleton
or@singleton_with
.
-
tori.decorator.common.
singleton
(*args, **kwargs)¶ Decorator to make a class to be a singleton class. This decorator is designed to be able to take parameters for the construction of the singleton instance.
Please note that this decorator doesn’t support the first parameter as a class reference. If you are using that way, please try to use
@singleton_with
instead.Example:
# Declaration @singleton class MyFirstClass(ParentClass): def __init__(self): self.number = 0 def call(self): self.number += 1 echo self.number # Or @singleton(20) class MySecondClass(ParentClass): def __init__(self, init_number): self.number = init_number def call(self): self.number += 1 echo self.number # Executing for i in range(10): MyFirstClass.instance().call() # Expecting 1-10 to be printed on the console. for i in range(10): MySecondClass.instance().call() # Expecting 11-20 to be printed on the console.
The end result is that the console will show the number from 1 to 10.
-
tori.decorator.common.
singleton_with
(*args, **kwargs)¶ Decorator to make a class to be a singleton class with given parameters for the constructor.
Please note that this decorator always requires parameters. Not giving one may result errors. Additionally, it is designed to solve the problem where the first parameter is a class reference. For normal usage, please use @singleton instead.
Example:
# Declaration class MyAdapter(AdapterClass): def broadcast(self): print "Hello, world." @singleton_with(MyAdapter) class MyClass(ParentClass): def __init__(self, adapter): self.adapter = adapter() def take_action(self): self.adapter.broadcast() # Executing MyClass.instance().take_action() # expecting the message on the console.
The end result is that the console will show the number from 1 to 10.