Skip to content

Commit

Permalink
feat: Added record cache
Browse files Browse the repository at this point in the history
Allows to create easily new record types while caching the new record types
  • Loading branch information
bpiwowar committed Mar 4, 2024
1 parent e9eb91b commit 86af9e2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
23 changes: 23 additions & 0 deletions src/datamaestro/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,26 @@ def decorate(cls: Type[Record]):
return cls

return decorate


class RecordTypesCache:
"""Class to use when new record types need to be created on the fly by
adding new items"""

def __init__(self, name: str, *itemtypes: Type[T], module: str = None):
self._module = module
self._name = name
self._itemtypes = itemtypes
self._cache: Dict[Type[Record], Type[Record]] = {}

def __getitem__(self, record_type: Type[Record]):
if updated_type := self._cache.get(record_type, None):
return updated_type

updated_type = record_type.from_types(
f"{self._name}_{record_type.__name__}",
*self._itemtypes,
module=self._module,
)
self._cache[record_type] = updated_type
return updated_type
12 changes: 11 additions & 1 deletion src/datamaestro/test/test_record.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datamaestro.record import Record, Item, recordtypes
from datamaestro.record import Record, Item, RecordTypesCache, recordtypes
from attrs import define
import pytest

Expand Down Expand Up @@ -69,3 +69,13 @@ def test_record_newtype():

# For a dynamic class, we should have the same MyRecord type
assert r.__class__ is MyRecord


def test_record_onthefly():
cache = RecordTypesCache("OnTheFly", CItem)

MyRecord2 = cache[MyRecord]
r = MyRecord2(A1Item(1, 2), BItem(2), CItem(3))
assert r.__class__ is MyRecord

assert cache[MyRecord] is MyRecord2

0 comments on commit 86af9e2

Please sign in to comment.