diff --git a/complex_tokenization/examples/bne.py b/complex_tokenization/examples/bne.py index f9ac265..c91c0f4 100644 --- a/complex_tokenization/examples/bne.py +++ b/complex_tokenization/examples/bne.py @@ -19,7 +19,6 @@ def train_bne_tokenizer(texts: list[str], GraphSettings.ONLY_MINIMAL_MERGES = True GraphSettings.MAX_MERGE_SIZE = n - GraphSettings.USE_SINGLETONS = False graphs = tuple(words(text, connected=connected, units=units) for text in texts) diff --git a/complex_tokenization/graph.py b/complex_tokenization/graph.py index 1a174d0..919a9b0 100644 --- a/complex_tokenization/graph.py +++ b/complex_tokenization/graph.py @@ -15,17 +15,6 @@ def dot_escape(s: str) -> str: class GraphVertex: - _instances = {} # Singleton pattern - - def __new__(cls, *args, **kwargs): - if not GraphSettings.USE_SINGLETONS: - return super().__new__(cls) - - key = (cls, args, tuple(sorted(kwargs.items()))) - if key not in cls._instances: - cls._instances[key] = super().__new__(cls) - return cls._instances[key] - def __bytes__(self): raise NotImplementedError @@ -36,12 +25,6 @@ def __str__(self): return token_replacement return self_str - def __eq__(self, other): - return self is other - - def __hash__(self): - return id(self) - def dot(self, level=0) -> Iterable[str]: raise NotImplementedError diff --git a/complex_tokenization/graphs/settings.py b/complex_tokenization/graphs/settings.py index dac35c2..b0498b2 100644 --- a/complex_tokenization/graphs/settings.py +++ b/complex_tokenization/graphs/settings.py @@ -1,4 +1,3 @@ class GraphSettings: - USE_SINGLETONS = False # speeds up computation but hurts visualization MAX_MERGE_SIZE = 2 ONLY_MINIMAL_MERGES = True diff --git a/tests/conftest.py b/tests/conftest.py index 56c70ec..833776e 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -6,19 +6,9 @@ @pytest.fixture(autouse=True) def reset_graph_settings(): original = { - "USE_SINGLETONS": GraphSettings.USE_SINGLETONS, "MAX_MERGE_SIZE": GraphSettings.MAX_MERGE_SIZE, "ONLY_MINIMAL_MERGES": GraphSettings.ONLY_MINIMAL_MERGES, } yield - GraphSettings.USE_SINGLETONS = original["USE_SINGLETONS"] GraphSettings.MAX_MERGE_SIZE = original["MAX_MERGE_SIZE"] GraphSettings.ONLY_MINIMAL_MERGES = original["ONLY_MINIMAL_MERGES"] - - -@pytest.fixture(autouse=True) -def clear_singleton_cache(): - from complex_tokenization.graph import GraphVertex - GraphVertex._instances.clear() - yield - GraphVertex._instances.clear() diff --git a/tests/test_singletons.py b/tests/test_singletons.py deleted file mode 100644 index 6555bbe..0000000 --- a/tests/test_singletons.py +++ /dev/null @@ -1,36 +0,0 @@ -from complex_tokenization.graph import Node, NodesSequence -from complex_tokenization.graphs.settings import GraphSettings -from complex_tokenization.graphs.units import utf8 - - -class TestSingletons: - def test_singletons_off_creates_distinct_objects(self): - GraphSettings.USE_SINGLETONS = False - a = Node(value=b'a') - b = Node(value=b'a') - assert a == b - assert a is not b - - def test_singletons_on_returns_same_object(self): - GraphSettings.USE_SINGLETONS = True - a = Node(value=b'a') - b = Node(value=b'a') - assert a is b - - def test_singletons_different_values_different_objects(self): - GraphSettings.USE_SINGLETONS = True - a = Node(value=b'a') - b = Node(value=b'b') - assert a is not b - - def test_singletons_different_classes_not_shared(self): - GraphSettings.USE_SINGLETONS = True - node = Node(value=b'a') - seq = NodesSequence(nodes=(node,)) - assert type(node) is not type(seq) - - def test_singleton_merge_preserves_identity(self): - GraphSettings.USE_SINGLETONS = True - graph = utf8("aa") - assert isinstance(graph, NodesSequence) - assert graph.nodes[0] is graph.nodes[1]