From a996ad53f0017292ad0bfedf9a54f8fe1486f891 Mon Sep 17 00:00:00 2001 From: matveyvarg Date: Wed, 17 Apr 2024 09:36:32 +0200 Subject: [PATCH] todos removed & dict returned to ordered dict --- deker/ABC/base_array.py | 33 ++++++----------------- deker/errors.py | 20 ++++++++------ deker/tools/attributes.py | 43 +++++++++++++++++++++++++++++- tests/parameters/schemas_params.py | 2 +- 4 files changed, 63 insertions(+), 35 deletions(-) diff --git a/deker/ABC/base_array.py b/deker/ABC/base_array.py index d395cdd..dce3819 100644 --- a/deker/ABC/base_array.py +++ b/deker/ABC/base_array.py @@ -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 @@ -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 @@ -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 @@ -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, diff --git a/deker/errors.py b/deker/errors.py index 2b169aa..596dcc1 100644 --- a/deker/errors.py +++ b/deker/errors.py @@ -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. @@ -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`. + """ diff --git a/deker/tools/attributes.py b/deker/tools/attributes.py index 91f2d68..8689d38 100644 --- a/deker/tools/attributes.py +++ b/deker/tools/attributes.py @@ -15,14 +15,19 @@ # along with this program. If not, see . 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]: @@ -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 diff --git a/tests/parameters/schemas_params.py b/tests/parameters/schemas_params.py index 039c6d2..1263a13 100644 --- a/tests/parameters/schemas_params.py +++ b/tests/parameters/schemas_params.py @@ -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(