Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
satella
Manage
Activity
Members
Labels
Plan
Issues
1
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
public
satella
Commits
f8924b66
Commit
f8924b66
authored
7 years ago
by
Piotr Maślanka
Browse files
Options
Downloads
Patches
Plain Diff
#15
parent
95f28a28
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
satella/coding/singleton.py
+50
-23
50 additions, 23 deletions
satella/coding/singleton.py
with
50 additions
and
23 deletions
satella/coding/singleton.py
+
50
−
23
View file @
f8924b66
# coding=UTF-8
from
__future__
import
print_function
,
absolute_import
,
division
import
functools
import
six
def
Singleton
(
cls
):
"""
Make a singleton out of decorated class.
if
six
.
PY3
:
Taken from https://wiki.python.org/moin/PythonDecoratorLibrary
# Taken from https://wiki.python.org/moin/PythonDecoratorLibrary
def
Singleton
(
cls
):
"""
Make a singleton out of decorated class.
Usage:
Usage:
@Singleton
class MyClass(object):
...
"""
@Singleton
class MyClass(object):
...
"""
cls
.
__new_old__
=
cls
.
__new__
cls
.
__new_old__
=
cls
.
__new__
@functools.wraps
(
cls
.
__new__
)
def
singleton_new
(
cls
,
*
args
,
**
kw
):
it
=
cls
.
__dict__
.
get
(
'
__it__
'
)
if
it
is
not
None
:
return
it
cls
.
__it__
=
it
=
cls
.
__new_old__
(
cls
,
*
args
,
**
kw
)
it
.
__init_old__
(
*
args
,
**
kw
)
return
it
@functools.wraps
(
cls
.
__new__
)
def
singleton_new
(
cls
,
*
args
,
**
kw
):
it
=
cls
.
__dict__
.
get
(
'
__it__
'
)
if
it
is
not
None
:
return
it
cls
.
__
new
__
=
singleton_new
cls
.
__init_old__
=
cls
.
__init__
cls
.
__init__
=
object
.
__init__
cls
.
__
it
__
=
it
=
cls
.
__new_old__
(
cls
,
*
args
,
**
kw
)
it
.
__init_old__
(
*
args
,
**
kw
)
return
it
return
cls
cls
.
__new__
=
singleton_new
cls
.
__init_old__
=
cls
.
__init__
cls
.
__init__
=
object
.
__init__
return
cls
else
:
class
_SingletonWrapper
:
"""
A singleton wrapper class. Its instances would be created
for each decorated class.
"""
def
__init__
(
self
,
cls
):
self
.
__wrapped__
=
cls
self
.
_instance
=
None
def
__call__
(
self
,
*
args
,
**
kwargs
):
"""
Returns a single instance of decorated class
"""
if
self
.
_instance
is
None
:
self
.
_instance
=
self
.
__wrapped__
(
*
args
,
**
kwargs
)
return
self
.
_instance
# taken from https://pypi.python.org/pypi/singleton-decorator/1.0.0
def
Singleton
(
cls
):
"""
A singleton decorator. Returns a wrapper objects. A call on that object
returns a single instance object of decorated class. Use the __wrapped__
attribute to access decorated class directly in unit tests.
"""
return
_SingletonWrapper
(
cls
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment