Skip to content

Commit

Permalink
Fixed the tracker, got a bad steer about singletons.
Browse files Browse the repository at this point in the history
  • Loading branch information
mdellabitta committed Dec 18, 2024
1 parent 02a9c94 commit 52a2b1f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
14 changes: 9 additions & 5 deletions ingest_wikimedia/tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,16 @@ class Result(Enum):
BAD_IMAGE_API = auto()


class SingletonBase:
class Singleton(type):
_instances = {}

def __new__(cls, *args, **kwargs):
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
instance = super().__new__(cls)
cls._instances[cls] = instance
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]


class Tracker(SingletonBase):
class Tracker(metaclass=Singleton):
def __init__(self):
self.data = {}
for value in Result:
Expand All @@ -37,6 +36,11 @@ def increment(self, status: Result, amount=1) -> None:
def count(self, status: Result) -> int:
return self.data[status]

def reset(self):
with self.lock:
for value in Result:
self.data[value] = 0

def __str__(self) -> str:
result = "COUNTS:\n"
for key in self.data:
Expand Down
6 changes: 6 additions & 0 deletions tests/test_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
from ingest_wikimedia.tracker import Tracker, Result


@pytest.fixture(autouse=True)
def before_and_after():
yield
Tracker().reset()


@pytest.fixture
def tracker():
return Tracker()
Expand Down

0 comments on commit 52a2b1f

Please sign in to comment.