Skip to content

Commit

Permalink
todos removed & dict returned to ordered dict
Browse files Browse the repository at this point in the history
  • Loading branch information
matveyvarg committed Apr 17, 2024
1 parent afd511d commit a996ad5
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 35 deletions.
33 changes: 8 additions & 25 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 @@ -462,26 +460,11 @@ def _create_from_meta(

try:
# To ensure the order of attributes
primary_attributes: dict = dict()
custom_attributes: dict = dict()

# 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_from_meta[attr_schema.name]
if value is None and not attr_schema.primary:
result_attributes[attr_schema.name] = value
continue

result_attributes[attr_schema.name] = deserialize_attribute_value(
value, attr_schema.dtype, False
)
primary_attributes, custom_attributes = make_ordered_dict(
meta["primary_attributes"],
meta["custom_attributes"],
attrs_schema, # type: ignore[arg-type]
)

arr_params = {
"collection": collection,
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`.
"""
43 changes: 42 additions & 1 deletion deker/tools/attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,19 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import re

from collections import OrderedDict
from datetime import datetime
from typing import Any, Tuple, Type, Union
from typing import TYPE_CHECKING, Any, List, Optional, Tuple, Type, Union

import numpy as np

from deker_tools.time import get_utc


if TYPE_CHECKING:
from deker import AttributeSchema


def serialize_attribute_value(
val: Any,
) -> Union[Tuple[str, int, float, tuple], str, int, float, tuple]:
Expand Down Expand Up @@ -112,3 +117,39 @@ def deserialize_attribute_nested_tuples(value: Tuple[Any, ...]) -> Tuple[Any, ..
value = deserialize_attribute_value(el, type(el), True)
deserialized.append(value)
return tuple(deserialized)


def make_ordered_dict(
primary_attributes: Optional[dict],
custom_attributes: Optional[dict],
attrs_schema: Union[List["AttributeSchema"], Tuple[AttributeSchema, ...]],
) -> Tuple[OrderedDict, OrderedDict]:
"""Ensure that attributes in dict are located in correct order (Based on schema).
:param primary_attributes: Primary attributes dict
:param custom_attributes: Custom attributes dict
:param attrs_schema: Schema of attributes to get order
"""
# To ensure the order of attributes
ordered_primary_attributes: OrderedDict = OrderedDict()
ordered_custom_attributes: OrderedDict = OrderedDict()

# Iterate over every attribute in schema:
for attr_schema in attrs_schema:
if attr_schema.primary:
attributes_from_meta = primary_attributes
result_attributes = ordered_primary_attributes
else:
attributes_from_meta = custom_attributes
result_attributes = ordered_custom_attributes

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

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

return ordered_primary_attributes, ordered_custom_attributes
2 changes: 1 addition & 1 deletion tests/parameters/schemas_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ def WRONG_params_dataclass_raises(cls) -> List[Any]:
*cls._generate_types(
base_dict={"dtype": dtype, "dimensions": dimensions, "vgrid": vgrid},
key="attributes",
exception_types=[tuple, list],
exception_types=[tuple, list, None],
),
# wrong vgrid
*cls._generate_types(
Expand Down

0 comments on commit a996ad5

Please sign in to comment.