Latest recipes tagged "singleton"http://code.activestate.com/recipes/tags/singleton/new/2015-08-11T19:14:45-07:00ActiveState Code RecipesYet another singleton pattern in Python using class decorators (Python)
2015-08-11T19:14:45-07:00David Hollmanhttp://code.activestate.com/recipes/users/4182331/http://code.activestate.com/recipes/579090-yet-another-singleton-pattern-in-python-using-clas/
<p style="color: grey">
Python
recipe 579090
by <a href="/recipes/users/4182331/">David Hollman</a>
(<a href="/recipes/tags/patterns/">patterns</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/singleton/">singleton</a>).
Revision 8.
</p>
<p>Another pattern for creating singleton instances of classes in Python.</p>
Hivemind (Python)
2015-07-15T12:37:13-07:00Oscar Byrnehttp://code.activestate.com/recipes/users/4192487/http://code.activestate.com/recipes/579082-hivemind/
<p style="color: grey">
Python
recipe 579082
by <a href="/recipes/users/4192487/">Oscar Byrne</a>
(<a href="/recipes/tags/borg/">borg</a>, <a href="/recipes/tags/defaultdict/">defaultdict</a>, <a href="/recipes/tags/hash/">hash</a>, <a href="/recipes/tags/hashing/">hashing</a>, <a href="/recipes/tags/hivemind/">hivemind</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/singleton/">singleton</a>, <a href="/recipes/tags/state/">state</a>).
</p>
<p>Inspired by the ever-popular Borg pattern, objects inheriting from Hivemind share state if initialised with the same arguments</p>
Python single instance (cross-platform) (Python)
2013-02-28T04:14:08-08:00Deepakhttp://code.activestate.com/recipes/users/4183429/http://code.activestate.com/recipes/578476-python-single-instance-cross-platform/
<p style="color: grey">
Python
recipe 578476
by <a href="/recipes/users/4183429/">Deepak</a>
(<a href="/recipes/tags/application/">application</a>, <a href="/recipes/tags/instance/">instance</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/single/">single</a>, <a href="/recipes/tags/singleton/">singleton</a>).
</p>
<p>Yet another way to get a single instance application.
This recipe uses file locking only.</p>
Python single instance (cross-platform) (Python)
2013-02-10T16:22:33-08:00Esteban Castro Borsanihttp://code.activestate.com/recipes/users/4184010/http://code.activestate.com/recipes/578453-python-single-instance-cross-platform/
<p style="color: grey">
Python
recipe 578453
by <a href="/recipes/users/4184010/">Esteban Castro Borsani</a>
(<a href="/recipes/tags/application/">application</a>, <a href="/recipes/tags/instance/">instance</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/single/">single</a>, <a href="/recipes/tags/singleton/">singleton</a>).
</p>
<p>Yet another way to get a single instance application.
This recipe uses file locking only.</p>
Singleton (parameter based) (Python)
2012-04-18T06:13:25-07:00Thomas Lehmannhttp://code.activestate.com/recipes/users/4174477/http://code.activestate.com/recipes/578103-singleton-parameter-based/
<p style="color: grey">
Python
recipe 578103
by <a href="/recipes/users/4174477/">Thomas Lehmann</a>
(<a href="/recipes/tags/parameters/">parameters</a>, <a href="/recipes/tags/singleton/">singleton</a>).
Revision 3.
</p>
<p><strong>Why?</strong></p>
<ul>
<li>There are many recipes but nearly all cover the global singleton only.</li>
<li>I need a singleton which can handle things in a specific context (environment).</li>
</ul>
<p><strong>The final singleton is a combination of following two recipes</strong>:</p>
<ul>
<li><a href="http://www.python.org/dev/peps/pep-0318/#examples" rel="nofollow">http://www.python.org/dev/peps/pep-0318/#examples</a></li>
<li><a href="http://stackoverflow.com/a/9489387" rel="nofollow">http://stackoverflow.com/a/9489387</a></li>
</ul>
<p><strong>The basic advantages</strong>:</p>
<ul>
<li>Hiding the singleton code by a simple decorator</li>
<li>Flexible, because you can define fully global singletons or parameter based singletons.</li>
</ul>
<p><strong>Latest changes</strong>:</p>
<ul>
<li>Although a function/method does not have parameters you can call it with parameters <strong>args</strong> and <strong>kwargs</strong> as you now see in the <strong>getInstance</strong> function.</li>
<li>...</li>
</ul>
Bless Classes into Singletons (Python)
2011-09-21T05:34:52-07:00Anand B Pillaihttp://code.activestate.com/recipes/users/4169530/http://code.activestate.com/recipes/577875-bless-classes-into-singletons/
<p style="color: grey">
Python
recipe 577875
by <a href="/recipes/users/4169530/">Anand B Pillai</a>
(<a href="/recipes/tags/metaclasses/">metaclasses</a>, <a href="/recipes/tags/patterns/">patterns</a>, <a href="/recipes/tags/singleton/">singleton</a>).
</p>
<p>A pattern using metaclasses for "blessing" classes into Singletons.</p>
ReadOnlyPrivateSingletonImplicitThisMetaClass (Python)
2010-05-10T08:34:32-07:00Chris Joneshttp://code.activestate.com/recipes/users/4171447/http://code.activestate.com/recipes/577221-readonlyprivatesingletonimplicitthismetaclass/
<p style="color: grey">
Python
recipe 577221
by <a href="/recipes/users/4171447/">Chris Jones</a>
(<a href="/recipes/tags/enterprise/">enterprise</a>, <a href="/recipes/tags/implicit/">implicit</a>, <a href="/recipes/tags/private/">private</a>, <a href="/recipes/tags/readonly/">readonly</a>, <a href="/recipes/tags/self/">self</a>, <a href="/recipes/tags/singleton/">singleton</a>).
Revision 3.
</p>
<p>Are you a totally agile rock star developer? Turn your code into a rock concert with this handy metaclass that makes your classes suitable for production deployment!</p>
Singleton(subclass) with once initialization (Python)
2010-04-23T11:27:58-07:00Dmitryhttp://code.activestate.com/recipes/users/4173772/http://code.activestate.com/recipes/577208-singletonsubclass-with-once-initialization/
<p style="color: grey">
Python
recipe 577208
by <a href="/recipes/users/4173772/">Dmitry</a>
(<a href="/recipes/tags/initialization/">initialization</a>, <a href="/recipes/tags/singleton/">singleton</a>).
</p>
<p>Yet one singleton realization on Python without metaclass. Singleton may has __init__ method which will call only when first object create.</p>
Caching Singleton class (subclassable) (Python)
2008-11-26T14:02:55-08:00Eugene Mirotinhttp://code.activestate.com/recipes/users/4168194/http://code.activestate.com/recipes/576573-caching-singleton-class-subclassable/
<p style="color: grey">
Python
recipe 576573
by <a href="/recipes/users/4168194/">Eugene Mirotin</a>
(<a href="/recipes/tags/data_access/">data_access</a>, <a href="/recipes/tags/data_caching/">data_caching</a>, <a href="/recipes/tags/singleton/">singleton</a>).
Revision 7.
</p>
<p>Allows decorating and caching expensive data consuming tasks</p>