Skip to content

Commit

Permalink
singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
tanzhijian committed Feb 7, 2024
1 parent 5e16dec commit acb4322
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
18 changes: 16 additions & 2 deletions fifacodes/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import csv
from abc import ABCMeta
from collections.abc import Mapping
from pathlib import Path
from typing import Any, Callable, Generator, Iterator, NamedTuple

from rapidfuzz import process

__version__ = "0.1.2"
__version__ = "0.1.3"

_DATA_PATH = Path(__file__).parent
_DEFAULT_DATA_PATH = _DATA_PATH / "default.csv"
Expand All @@ -20,7 +21,20 @@ class Member(NamedTuple):
_DataTypes = dict[str, Member]


class Members(Mapping[str, Member]):
class Singleton(ABCMeta):
"""
A metaclass for creating singletons.
"""

_instances: dict[type, Any] = {}

def __call__(cls, *args: Any, **kwargs: Any) -> Any:
if cls not in cls._instances:
cls._instances[cls] = super().__call__(*args, **kwargs)
return cls._instances[cls]


class Members(Mapping[str, Member], metaclass=Singleton):
"""
A mapping of FIFA member codes to member names.
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "fifacodes"
version = "0.1.2"
version = "0.1.3"
description = "FIFA member associations codes query and search."
authors = ["tanzhijian <[email protected]>"]
license = "MIT"
Expand Down
6 changes: 6 additions & 0 deletions tests/test_fifacodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
from fifacodes import Members


def test_singleton() -> None:
s1 = Members()
s2 = Members()
assert s1 is s2


class TestMembers:
@pytest.fixture(scope="class")
def members(self) -> Members:
Expand Down

0 comments on commit acb4322

Please sign in to comment.