Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix/ordered-dict #46

Merged
merged 10 commits into from
Apr 17, 2024
30 changes: 20 additions & 10 deletions deker/ABC/base_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,25 +459,35 @@ def _create_from_meta(
attrs_schema = collection.varray_schema.attributes
else:
attrs_schema = collection.array_schema.attributes

try:
for attr in attrs_schema:
attributes = (
meta["primary_attributes"] if attr.primary else meta["custom_attributes"]
)
# To ensure the order of attributes
primary_attributes, custom_attributes = OrderedDict(), OrderedDict()
SerGeRybakov marked this conversation as resolved.
Show resolved Hide resolved

# Iterate over every attribute in schema:
for attr_schema in attrs_schema:
if attr_schema.primary:
attributes_from_meta = meta["primary_attributes"]
result_attributes = primary_attributes
else:
attributes_from_meta = meta["custom_attributes"]
result_attributes = custom_attributes

value = attributes[attr.name]
if value is None and not attr.primary:
attributes[attr.name] = value
value = attributes_from_meta[attr_schema.name]
if value is None and not attr_schema.primary:
result_attributes[attr_schema.name] = value
continue

attributes[attr.name] = deserialize_attribute_value(value, attr.dtype, False)
result_attributes[attr_schema.name] = deserialize_attribute_value(
value, attr_schema.dtype, False
)

arr_params = {
"collection": collection,
"adapter": array_adapter,
"id_": meta["id"],
"primary_attributes": meta.get("primary_attributes"),
"custom_attributes": meta.get("custom_attributes"),
"primary_attributes": primary_attributes,
"custom_attributes": custom_attributes,
}
if varray_adapter:
arr_params.update({"adapter": varray_adapter, "array_adapter": array_adapter})
Expand Down
14 changes: 7 additions & 7 deletions deker/ABC/base_locks.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.

"""Abstract interfaces for locks."""

# TODO: MOVED
from abc import ABC, abstractmethod
from functools import wraps
from pathlib import Path
Expand All @@ -31,16 +31,15 @@ class BaseLock(SelfLoggerMixin, ABC):
ALLOWED_TYPES: List[str] = []
lock: Optional[Union[Flock, Path]] = None
instance: Optional[Any] = None
args: Optional[List[Any]] = None
kwargs: Optional[Dict] = None

@abstractmethod
def get_path(self, func_args: Sequence, func_kwargs: Dict) -> Optional[Path]:
def get_path(self) -> Optional[Path]:
"""Get path to the lock file.

For Arrays it shall be .arrlock (array read lock) or path to the file (array write lock)
For VArrays there are no specific locks for reading, for writing - lock on .json

:param func_args: Arguments of method call
:param func_kwargs: Keyword arguments of method call
"""
pass

Expand Down Expand Up @@ -98,7 +97,7 @@ def _inner_method_logic(lock: "BaseLock", args: Sequence, kwargs: Dict, func: Ca
:param kwargs: Keyword arguments of decorated function
"""
lock.check_existing_lock(args, kwargs)
path = lock.get_path(args, kwargs)
path = lock.get_path()
lock.acquire(path)
try:
# Logic is in the separate method, so we can override its behavior
Expand Down Expand Up @@ -129,7 +128,8 @@ def inner(*args: Sequence, **kwargs: Dict[str, Any]) -> Any:
lock = self.__class__()
# as we wrap methods, we should have access to 'self' objects
lock.instance = kwargs.get("self") or args[0]

lock.args = args
lock.kwargs = kwargs
# Check that we don't have lock on anything that besides methods that require lock
lock.check_type()

Expand Down
3 changes: 3 additions & 0 deletions deker/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from typing import List


# TODO: MOVED
class DekerBaseApplicationError(Exception):
"""Base attribute exception."""

Expand Down Expand Up @@ -84,6 +85,7 @@ class DekerIntegrityError(DekerBaseApplicationError):
pass


# TODO: MOVED
class DekerLockError(DekerBaseApplicationError):
"""If a Collection or a Array or VArray instance is locked."""

Expand Down Expand Up @@ -142,5 +144,6 @@ def __str__(self) -> str:
return f"{self.message}; exceptions:\n\n{joined} "


# TODO: MOVED
class DekerMemoryError(DekerBaseApplicationError, MemoryError):
"""Early memory overflow exception."""
Loading
Loading