Skip to content

Commit

Permalink
Cleanup and de-nesting.
Browse files Browse the repository at this point in the history
  • Loading branch information
riga committed Sep 30, 2023
1 parent 9a6ad86 commit c148081
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 23 deletions.
14 changes: 5 additions & 9 deletions order/adapters/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@ class AdapterModel(BaseModel):

adapter: NonEmptyStrictStr
key: StrictStr
arguments: Dict[NonEmptyStrictStr, Any] = Field(default_factory=lambda: {})

@property
def name(self) -> str:
return self.adapter
arguments: Dict[NonEmptyStrictStr, Any] = Field(default_factory=dict)

def compare_signature(self, other: "AdapterModel") -> bool:
return (
Expand Down Expand Up @@ -109,7 +105,6 @@ def retrieve_data(self) -> Materialized:
return

def get_cache_key(self, **kwargs) -> tuple:
# must be implemented by subclasses
return tuple(
(
key,
Expand Down Expand Up @@ -137,7 +132,7 @@ def remove_scheme(cls, data_location: str) -> str:

class DataProvider(object):
"""
Interface between data locations plus caches and :py:class:`Adapter`'s.
Interface between data locations plus caches and :py:class:`Adapter` instances.
"""

__instance = None
Expand Down Expand Up @@ -166,6 +161,7 @@ def instance(cls) -> "DataProvider":

def __init__(
self,
*,
data_location: str,
cache_directory: str,
readonly_cache_directories: Sequence[str] = (),
Expand Down Expand Up @@ -202,7 +198,7 @@ def materialize(
adapter_model = AdapterModel(**adapter_model)

# get the adapter class and instantiate it
adapter = AdapterMeta.get_cls(adapter_model.name)()
adapter = AdapterMeta.get_cls(adapter_model.adapter)()

# merge kwargs
adapter_kwargs = {**adapter_model.arguments, **(adapter_kwargs or {})}
Expand Down Expand Up @@ -231,7 +227,7 @@ def materialize(
# complain when the return value is not a materialized container
if not isinstance(materialized, Materialized):
raise TypeError(
f"retrieve_data of adapter '{adapter_model.name}' must return Materialized "
f"retrieve_data of adapter '{adapter_model.adapter}' must return a Materialized "
f"instance, but got '{materialized}'",
)

Expand Down
4 changes: 2 additions & 2 deletions order/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def fget(self):
# complain when the adapter did not provide a value for this attribute
if adapter_model_.key not in materialized:
raise KeyError(
f"adapter '{adapter_model.name}' did not provide field "
f"adapter '{adapter_model.adapter}' did not provide field "
f"'{adapter_model_.key}' as required by attribute '{attr_}'",
)

Expand Down Expand Up @@ -173,4 +173,4 @@ def __repr_args__(self) -> GeneratorType:
continue

value = getattr(self, lazy_attr)
yield attr, f"lazy({value.name})" if isinstance(value, AdapterModel) else value
yield attr, f"lazy({value.adapter})" if isinstance(value, AdapterModel) else value
5 changes: 1 addition & 4 deletions order/models/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,4 @@ class LazyDataset(LazyUniqueObject):
class DatasetIndex(UniqueObjectIndex):

class_name: NonEmptyStrictStr = Field(default=Dataset)
objects: Lazy[List[Union[Dataset, LazyDataset]]] = Field(
default_factory=lambda: [],
repr=False,
)
objects: Lazy[List[Union[Dataset, LazyDataset]]] = Field(default_factory=list, repr=False)
20 changes: 13 additions & 7 deletions order/models/unique.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ class UniqueObjectIndex(WrapsUniqueClcass):
"""

objects: Lazy[List[Union[UniqueObject, LazyUniqueObject]]] = Field(
default_factory=lambda: [],
default_factory=list,
repr=False,
)

Expand Down Expand Up @@ -338,7 +338,7 @@ def __iter__(self) -> GeneratorType:
for obj in self.objects:
yield self.get(obj.name)

def __nonzero__(self):
def __nonzero__(self) -> bool:
"""
Boolish conversion that depends on the number of objects in the index.
"""
Expand Down Expand Up @@ -470,7 +470,7 @@ def get(self, obj: Any, default: T = no_value) -> UniqueObject | T:
# remember the position of the object
idx = self.objects.index(_obj)

# materializee
# materialize
with _obj.materialize(self) as _obj:
# add back the materialized object
self.objects[idx] = _obj
Expand All @@ -489,20 +489,26 @@ def get_first(self, default: T = no_value) -> UniqueObject | T:
Returns the first object of this index. If no object could be found, *default* is returned
if set. An exception is raised otherwise.
"""
if not self.objects and default != no_value:
if self.objects:
return self.get(self.objects[0].name)

if default != no_value:
return default

return self.get(self.objects[0].name)
raise Exception(f"cannot return first object, '{self}' is empty")

def get_last(self, default: T = no_value) -> UniqueObject | T:
"""
Returns the last object of this index. If no object could be found, *default* is returned if
set. An exception is raised otherwise.
"""
if not self.objects and default != no_value:
if self.objects:
return self.get(self.objects[-1].name)

if default != no_value:
return default

return self.get(self.objects[-1].name)
raise Exception(f"cannot return last object, '{self}' is empty")

def add(
self,
Expand Down
2 changes: 1 addition & 1 deletion order/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def __init__(
self._getter = getter
self._setter = setter

def __call__(self, *args, **kwargs):
def __call__(self, *args, **kwargs) -> Any:
return self._getter(*args, **kwargs)

def __getattr__(self, attr: str) -> Any:
Expand Down

0 comments on commit c148081

Please sign in to comment.