-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added singleton module and save_config method to config module.
- Loading branch information
1 parent
52a117f
commit 9e0d9d6
Showing
9 changed files
with
280 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,4 @@ frequent | |
.. toctree:: | ||
|
||
frequent.config | ||
frequent.singleton |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
singleton | ||
========= | ||
|
||
.. automodule:: frequent.singleton | ||
:members: | ||
:undoc-members: | ||
:show-inheritance: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# This module is part of the frequent project: | ||
# https://github.com/douglasdaly/frequent-py | ||
# | ||
""" | ||
Singleton utility metaclass. | ||
""" | ||
from abc import ABC | ||
from typing import Any | ||
from typing import Mapping | ||
from weakref import WeakValueDictionary | ||
|
||
|
||
class Singleton(type): | ||
""" | ||
Metaclass for singleton objects. | ||
Example | ||
------- | ||
This class is easy to use: | ||
.. code-block:: python | ||
class MyClass(SomeBaseClass, metaclass=Singleton): | ||
def __init__(self, x: int) -> None: | ||
self.x = x | ||
return | ||
Note the behavior of subsequent calls: | ||
>>> my_instance = MyClass(42) | ||
>>> my_instance.x | ||
42 | ||
>>> another_instance = MyClass(43) | ||
>>> another_instance.x | ||
42 | ||
Note that values set in subsequent calls to `__init__` will have no | ||
effect on the attribute. To change the attribute do so on any of | ||
the instances: | ||
>>> another_instance.x = 43 | ||
>>> my_instance.x | ||
43 | ||
>>> my_instance.x = 42 | ||
>>> another_instance.x | ||
42 | ||
""" | ||
__instances = WeakValueDictionary() | ||
|
||
def __call__(cls: type, *args, **kwargs) -> None: | ||
if cls not in cls.__instances: | ||
instance = super().__call__(*args, **kwargs) | ||
cls.__instances[cls] = instance | ||
return cls.__instances[cls] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.