Skip to content

Commit

Permalink
Merge pull request #46 from openweathermap/fix/ordered-dict
Browse files Browse the repository at this point in the history
fix/ordered-dict
  • Loading branch information
matveyvarg authored Apr 17, 2024
2 parents b992e58 + 98b2272 commit 21b1b96
Show file tree
Hide file tree
Showing 11 changed files with 337 additions and 194 deletions.
30 changes: 12 additions & 18 deletions deker/ABC/base_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import json

from abc import ABC, abstractmethod
from collections import OrderedDict
from copy import deepcopy
from datetime import datetime, timedelta
from typing import TYPE_CHECKING, Any, List, Optional, Tuple, Type, Union
Expand All @@ -35,7 +34,7 @@
from deker.schemas import ArraySchema, VArraySchema
from deker.subset import Subset, VSubset
from deker.tools.array import check_memory, get_id
from deker.tools.attributes import deserialize_attribute_value, serialize_attribute_value
from deker.tools.attributes import make_ordered_dict, serialize_attribute_value
from deker.tools.schema import create_dimensions
from deker.types.private.classes import ArrayMeta, Serializer
from deker.types.private.typings import FancySlice, Numeric, Slice
Expand Down Expand Up @@ -296,10 +295,9 @@ def __init__(
self.schema, primary_attributes, custom_attributes
)

self.primary_attributes: OrderedDict = (
OrderedDict({**primary_attributes}) if primary_attributes else OrderedDict()
self.primary_attributes, self.custom_attributes = make_ordered_dict(
primary_attributes, custom_attributes, self.schema.attributes # type: ignore[arg-type]
)
self.custom_attributes: dict = custom_attributes if custom_attributes else {}

def __del__(self) -> None:
del self.__adapter
Expand Down Expand Up @@ -459,25 +457,21 @@ 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"]
)

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

attributes[attr.name] = deserialize_attribute_value(value, attr.dtype, False)
try:
# To ensure the order of attributes
primary_attributes, custom_attributes = make_ordered_dict(
meta["primary_attributes"],
meta["custom_attributes"],
attrs_schema, # type: ignore[arg-type]
)

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
13 changes: 6 additions & 7 deletions deker/ABC/base_locks.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.

"""Abstract interfaces for locks."""

from abc import ABC, abstractmethod
from functools import wraps
from pathlib import Path
Expand All @@ -31,16 +30,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 +96,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 +127,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
20 changes: 12 additions & 8 deletions deker/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,10 @@ class DekerSubsetError(DekerArrayError):
"""If something goes wrong while Subset managing."""


class DekerVSubsetError(DekerSubsetError):
"""If something goes wrong while VSubset managing.
class DekerExceptionGroup(DekerBaseApplicationError):
"""If one or more threads finished with any exception.
Regarding that VSubset's inner Subsets' processing
is made in an Executor, this exception actually is
an `exceptions messages group`.
If one or more threads finished with any exception,
name, message and reasonable tracebacks of all
Name, message and reasonable tracebacks of all
of these exceptions shall be collected in a list
and passed to this class along with some message.
Expand Down Expand Up @@ -144,3 +139,12 @@ def __str__(self) -> str:

class DekerMemoryError(DekerBaseApplicationError, MemoryError):
"""Early memory overflow exception."""


class DekerVSubsetError(DekerSubsetError, DekerExceptionGroup):
"""If something goes wrong while VSubset managing.
Regarding that VSubset's inner Subsets' processing
is made in an Executor, this exception actually is
an `exceptions messages group`.
"""
Loading

0 comments on commit 21b1b96

Please sign in to comment.