Skip to content

Commit

Permalink
Refactor ConverterRegistry to use a list for storing converters and u…
Browse files Browse the repository at this point in the history
…pdate related methods
  • Loading branch information
arthur-debert committed Dec 6, 2024
1 parent 66417e3 commit 315a6a1
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 20 deletions.
13 changes: 8 additions & 5 deletions rangy/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,29 @@ def custom_to_numeric(custom_obj):
```
"""

types = {}
types = []

@classmethod
def register(cls, converter: Converter):
"""Registers a Converter."""
cls.types[converter._type] = converter
cls.types.append(converter)

@classmethod
def get(cls, _type):
"""Retrieves a Converter by type."""
if not isinstance(_type, type):
_type = _type.__class__
return cls.types[_type]
for converter in cls.types:
if converter._type == _type:
return converter
raise KeyError(f"No converter registered for type {_type}")

@classmethod
def clear(cls):
"""Clears all registered converters."""
cls.types = {}
cls.types.clear()

@classmethod
def __iter__(cls):
"""Returns an iterator over the registered converters."""
return iter(cls.types.values())
return iter(cls.types)
55 changes: 40 additions & 15 deletions tests/test_registry.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,55 @@

import pytest

from rangy import Converter, ConverterRegistry


def test_register_and_get_converter():
registry = ConverterRegistry()
ConverterRegistry.clear()
converter = Converter(int)
registry.register(converter)
assert registry.get(int) == converter
ConverterRegistry.register(converter)
assert ConverterRegistry.get(int) == converter

def test_get_converter_for_instance():
registry = ConverterRegistry()
ConverterRegistry.clear()
converter = Converter(int)
registry.register(converter)
assert registry.get(1) == converter
ConverterRegistry.register(converter)
assert ConverterRegistry.get(1) == converter

def test_get_nonexistent_converter():
registry = ConverterRegistry()
ConverterRegistry.clear()
with pytest.raises(KeyError):
registry.get(str)
ConverterRegistry.get(str)

def test_clear_registry():
registry = ConverterRegistry()
ConverterRegistry.clear()
converter = Converter(int)
registry.register(converter)
registry.clear()
ConverterRegistry.register(converter)
ConverterRegistry.clear()
with pytest.raises(KeyError):
registry.get(int)
ConverterRegistry.get(int)

def test_converter_registry_order():
class CustomType1:
pass

class CustomType2:
pass

def to_numeric1(obj):
return 1

def to_numeric2(obj):
return 2

converter1 = Converter(CustomType1, to_numeric1)
converter2 = Converter(CustomType2, to_numeric2)

ConverterRegistry.clear()
ConverterRegistry.register(converter1)
ConverterRegistry.register(converter2)

converters = list(ConverterRegistry())
assert converters[0] == converter1
assert converters[1] == converter2

if __name__ == "__main__":
pytest.main()

0 comments on commit 315a6a1

Please sign in to comment.