pumpwood_communication.serializers

Miscellaneous to help with serializers in communication.

  1"""Miscellaneous to help with serializers in communication."""
  2import base64
  3import simplejson as json
  4import orjson
  5import numpy as np
  6import pandas as pd
  7from typing import List, Union, Dict, Any
  8from simplejson import JSONEncoder
  9from datetime import datetime
 10from datetime import date
 11from datetime import time
 12from decimal import Decimal
 13from pandas import Timestamp
 14from shapely.geometry.base import BaseGeometry
 15from shapely.geometry import mapping
 16from sqlalchemy_utils.types.choice import Choice
 17from pumpwood_communication.exceptions import (
 18    PumpWoodException, PumpWoodNotImplementedError)
 19from pumpwood_communication.type import (
 20    PumpwoodSentinel, PumpwoodDataclassMixin, MISSING)
 21
 22
 23def default_encoder(obj):
 24    """Serialize complex objects."""
 25    # Return None if object is NaN
 26    if not isinstance(obj, (pd.DataFrame, pd.Series, np.ndarray, list, dict)):
 27        if pd.isna(obj):
 28            return None
 29
 30    if isinstance(obj, (datetime, Timestamp, date, time)):
 31        return obj.isoformat()
 32    if isinstance(obj, np.ndarray):
 33        return obj.tolist()
 34    if isinstance(obj, pd.DataFrame):
 35        return obj.to_dict('records')
 36    if isinstance(obj, pd.Series):
 37        return obj.tolist()
 38    if isinstance(obj, np.generic):
 39        return obj.item()
 40    if isinstance(obj, Decimal):
 41        return float(obj)
 42    if isinstance(obj, BaseGeometry):
 43        if obj.is_empty:
 44            return None
 45        else:
 46            return mapping(obj)
 47    if isinstance(obj, Choice):
 48        return obj.code
 49    if isinstance(obj, set):
 50        return list(obj)
 51
 52    #########################################################
 53    # TODO: Adjust convertion of decimal to preseve precision
 54    # There is lost of precision when converting decimal to float,
 55    # but Decimal is not currently parsiable using orjson
 56    if isinstance(obj, Decimal):
 57        return float(obj)
 58
 59    ###################################
 60    # Serialize Pumpwood expecial types
 61    if isinstance(obj, PumpwoodDataclassMixin):
 62        return obj.to_dict()
 63    if isinstance(obj, PumpwoodSentinel):
 64        return obj.value()
 65    else:
 66        raise TypeError(
 67            "Unserializable object {} of type {}".format(obj, type(obj)))
 68
 69
 70class PumpWoodJSONEncoder(JSONEncoder):
 71    """PumpWood default serializer.
 72
 73    Treat not simple python types to facilitate at serialization of
 74    pandas, numpy, data, datetime and other data types.
 75    """
 76
 77    def default(self, obj):
 78        """Serialize complex objects."""
 79        return default_encoder(obj)
 80
 81
 82def pumpJsonDump(x: any, sort_keys: bool = False,  # NOQA
 83                 indent: Union[int, bool] = None):
 84    """Dump a Json to python object.
 85
 86    Args:
 87        x (any):
 88            Object to be serialized using PumpWoodJSONEncoder encoder.
 89        sort_keys (bool):
 90            If json serialized data should have its keys sorted. This option
 91            makes serialization return of data reproductable.
 92        indent (int):
 93            Pass indent argument to simplejson dumps.
 94    """
 95    # Compatibility with simplejson serialization
 96    is_indent = indent is not None
 97    if sort_keys and is_indent:
 98        return orjson.dumps(x, default=default_encoder, option=(
 99            orjson.OPT_NAIVE_UTC | orjson.OPT_NON_STR_KEYS |
100            orjson.OPT_SORT_KEYS | orjson.OPT_INDENT_2 |
101            orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_PASSTHROUGH_DATACLASS))
102    elif sort_keys:
103        return orjson.dumps(x, default=default_encoder, option=(
104            orjson.OPT_NAIVE_UTC | orjson.OPT_NON_STR_KEYS |
105            orjson.OPT_SORT_KEYS | orjson.OPT_SERIALIZE_NUMPY |
106            orjson.OPT_PASSTHROUGH_DATACLASS))
107    elif is_indent:
108        return orjson.dumps(x, default=default_encoder, option=(
109            orjson.OPT_NAIVE_UTC | orjson.OPT_NON_STR_KEYS |
110            orjson.OPT_INDENT_2 | orjson.OPT_SERIALIZE_NUMPY |
111            orjson.OPT_PASSTHROUGH_DATACLASS))
112    else:
113        return orjson.dumps(x, default=default_encoder, option=(
114            orjson.OPT_NAIVE_UTC | orjson.OPT_NON_STR_KEYS |
115            orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_PASSTHROUGH_DATACLASS))
116
117
118class CompositePkBase64Converter:
119    """Convert composite primary keys in base64 dictionary."""
120
121    @staticmethod
122    def get_attribute(obj: Any, att: str) -> Any:
123        """Get attribute from object or dictinary.
124
125        Args:
126            obj (Any):
127                Object or a dictinary.
128            att (str):
129                Name of the attribute/key that will be used to return
130                the value.
131
132        Return:
133            Return object/dictionary value associated with attribute.
134        """
135        if type(obj) is dict:
136            return obj.get(att, MISSING)
137        else:
138            return getattr(obj, att, MISSING)
139
140    @classmethod
141    def dump(cls, obj: object | dict,
142             primary_keys: Union[str, List[str], Dict[str, str]]
143             ) -> Union[str, int]:
144        """Convert primary keys and composite to a single value.
145
146        Treat cases when more than one column are used as primary keys,
147        at this cases, a base64 used on url serialization of the dictionary
148        is returned.
149
150        Args:
151            obj:
152                SQLAlchemy object or dictionary with data to build the
153                forenging key.
154            primary_keys (Union[str, List[str], Dict[str, str]):
155                As string, a list or a dictionary leading to different
156                behaviour.
157                - **str:** It will return the value associated with object
158                    attribute.
159                - **List[str]:** If list has length equal to 1, it will have
160                    same behaviour as str. If greater than 1, it will be
161                    returned a base64 encoded dictionary with the keys at
162                    primary_keys.
163                - **Dict[str, str]:** Dictionary to map object fields to
164                    other keys. This is usefull when querying related fields
165                    by composite forenging keys to match original data fieds.
166
167        Returns:
168            If the primary key is unique, return the value of the primary
169            key, if is have more than one column as primary key, return
170            a dictionary of the primary keys encoded as base64 url safe.
171        """
172        if obj is None:
173            return None
174
175        missing_keys = []
176        return_value = None
177        if isinstance(primary_keys, str):
178            key_value = cls.get_attribute(obj, primary_keys)
179            if key_value != MISSING:
180                return_value = key_value
181            else:
182                missing_keys.append(primary_keys)
183
184        elif isinstance(primary_keys, list):
185            if len(primary_keys) == 1:
186                key_value = cls.get_attribute(obj, primary_keys[0])
187                if key_value != MISSING:
188                    return key_value
189                else:
190                    missing_keys.append(primary_keys[0])
191            else:
192                composite_pk_dict = {}
193                for pk_col in primary_keys:
194                    key_value = cls.get_attribute(obj, pk_col)
195                    if key_value != MISSING:
196                        composite_pk_dict[pk_col] = key_value
197                    else:
198                        missing_keys.append(pk_col)
199                return_value = composite_pk_dict
200
201        # Map object values to other, this is used when builds forenging
202        # key references and request related field using microservice.
203        elif isinstance(primary_keys, dict):
204            # Treat the case when the dictinary is only an id->value
205            composite_pk_dict = {}
206            for key, value in primary_keys.items():
207                key_value = cls.get_attribute(obj, key)
208                if key_value != MISSING:
209                    composite_pk_dict[value] = key_value
210                else:
211                    missing_keys.append(key)
212            return_value = composite_pk_dict
213
214        # Check if some missing keys are there
215        if len(missing_keys) != 0:
216            msg = (
217                "Some keys were not found on object/dict to create "
218                "the composite forenging key. "
219                "Primary Keys: {primary_keys}; "
220                "Missing keys: {missing_keys}")
221            raise PumpWoodException(msg, payload={
222                "primary_keys": list(primary_keys.keys()),
223                "missing_keys": missing_keys})
224
225        if isinstance(return_value, dict):
226            if {'id'} == set(return_value.keys()):
227                return return_value['id']            
228            return cls.dump_dict(primary_key_dict=return_value)
229        else:
230            return return_value
231
232    @classmethod
233    def validate_primary_key_dict(cls, primary_key_dict: dict):
234        """Validate primary key dictionary.
235
236        Args:
237            primary_key_dict (dict):
238                Dictionary with the primary key values.
239
240        Raises:
241            PumpWoodNotImplementedError:
242                If primary key dictionary is not a dictionary.
243            PumpWoodException:
244                If primary key dictionary contains nested types.
245        """
246        if not isinstance(primary_key_dict, dict):
247            msg = "primary_key_dict must be a dictionary. Received: {type}"
248            raise PumpWoodNotImplementedError(
249                msg, payload={'type': type(primary_key_dict).__name__})
250
251        for key, value in primary_key_dict.items():
252            if isinstance(value, (dict, list, tuple)):
253                msg = (
254                    "primary_key_dict must be flat dictionary. "
255                    "Nested type[{type}] found at key [{key}]")
256                raise PumpWoodException(
257                    msg.format(),
258                    payload={
259                        'primary_key_dict': primary_key_dict,
260                        'type': type(value).__name__,
261                        'key': key})
262
263    @classmethod
264    def dump_dict(cls, primary_key_dict: dict) -> str:
265        """Dump a primary key dictionary to base64 url safe string.
266
267        Args:
268            primary_key_dict (dict):
269                Dictionary with the primary key values.
270
271        Returns:
272            str:
273                Base64 url safe string of the primary key dictionary.
274        """
275        cls.validate_primary_key_dict(primary_key_dict=primary_key_dict)
276        composite_pk_str = pumpJsonDump(primary_key_dict)
277        base64_composite_pk = base64.urlsafe_b64encode(composite_pk_str)\
278            .decode()
279        return base64_composite_pk
280
281    @staticmethod
282    def load(value: Union[str, int, dict]) -> dict:
283        """Convert encoded primary keys to a flat dictionary.
284
285        Integer-like scalars normalize to ``{"id": <int>}``. Base64 JSON
286        strings decode to a flat dict. A ``dict`` input is returned after
287        validating that all keys are strings.
288
289        Args:
290            value (Union[str, int, dict]):
291                Primary key as int, numeric string, base64 JSON string,
292                or flat dict with string keys.
293
294        Returns:
295            dict:
296                Flat primary-key mapping; integer-like inputs use key
297                ``id``.
298
299        Raises:
300            PumpWoodException:
301                If ``value`` is a dict with non-string keys, a
302                non-integer float, or neither integer-like nor decodable
303                base64 JSON.
304        """
305        if isinstance(value, dict):
306            non_string_keys = [
307                key for key in value if not isinstance(key, str)]
308            if non_string_keys:
309                msg = (
310                    "primary_key_dict keys must be strings. "
311                    "Non-string keys: {keys}")
312                raise PumpWoodException(
313                    message=msg.format(keys=non_string_keys),
314                    payload={
315                        "value": value, "non_string_keys": non_string_keys})
316            return value
317
318        # Try to convert value to integer
319        try:
320            float_value = float(value)
321            if float_value.is_integer():
322                return {"id": int(float_value)}
323            else:
324                msg = "PK value [{value}]  is a float, but not integer."
325                raise PumpWoodException(
326                    msg, payload={"value": value})
327
328        # If not possible, try to decode a base64 JSON dictionary
329        except Exception as e1:
330            try:
331                return json.loads(base64.urlsafe_b64decode(value))
332            except Exception as e2:
333                msg = (
334                    "[{value}] value is not an integer and could no be "
335                    "decoded as a base64 encoded json dictionary. Value=")
336                raise PumpWoodException(
337                    message=msg, payload={
338                        "value": value,
339                        "exception_int": str(e1),
340                        "exception_base64": str(e2)})
def default_encoder(obj):
24def default_encoder(obj):
25    """Serialize complex objects."""
26    # Return None if object is NaN
27    if not isinstance(obj, (pd.DataFrame, pd.Series, np.ndarray, list, dict)):
28        if pd.isna(obj):
29            return None
30
31    if isinstance(obj, (datetime, Timestamp, date, time)):
32        return obj.isoformat()
33    if isinstance(obj, np.ndarray):
34        return obj.tolist()
35    if isinstance(obj, pd.DataFrame):
36        return obj.to_dict('records')
37    if isinstance(obj, pd.Series):
38        return obj.tolist()
39    if isinstance(obj, np.generic):
40        return obj.item()
41    if isinstance(obj, Decimal):
42        return float(obj)
43    if isinstance(obj, BaseGeometry):
44        if obj.is_empty:
45            return None
46        else:
47            return mapping(obj)
48    if isinstance(obj, Choice):
49        return obj.code
50    if isinstance(obj, set):
51        return list(obj)
52
53    #########################################################
54    # TODO: Adjust convertion of decimal to preseve precision
55    # There is lost of precision when converting decimal to float,
56    # but Decimal is not currently parsiable using orjson
57    if isinstance(obj, Decimal):
58        return float(obj)
59
60    ###################################
61    # Serialize Pumpwood expecial types
62    if isinstance(obj, PumpwoodDataclassMixin):
63        return obj.to_dict()
64    if isinstance(obj, PumpwoodSentinel):
65        return obj.value()
66    else:
67        raise TypeError(
68            "Unserializable object {} of type {}".format(obj, type(obj)))

Serialize complex objects.

class PumpWoodJSONEncoder(simplejson.encoder.JSONEncoder):
71class PumpWoodJSONEncoder(JSONEncoder):
72    """PumpWood default serializer.
73
74    Treat not simple python types to facilitate at serialization of
75    pandas, numpy, data, datetime and other data types.
76    """
77
78    def default(self, obj):
79        """Serialize complex objects."""
80        return default_encoder(obj)

PumpWood default serializer.

Treat not simple python types to facilitate at serialization of pandas, numpy, data, datetime and other data types.

def default(self, obj):
78    def default(self, obj):
79        """Serialize complex objects."""
80        return default_encoder(obj)

Serialize complex objects.

def pumpJsonDump( x: <built-in function any>, sort_keys: bool = False, indent: Union[int, bool] = None):
 83def pumpJsonDump(x: any, sort_keys: bool = False,  # NOQA
 84                 indent: Union[int, bool] = None):
 85    """Dump a Json to python object.
 86
 87    Args:
 88        x (any):
 89            Object to be serialized using PumpWoodJSONEncoder encoder.
 90        sort_keys (bool):
 91            If json serialized data should have its keys sorted. This option
 92            makes serialization return of data reproductable.
 93        indent (int):
 94            Pass indent argument to simplejson dumps.
 95    """
 96    # Compatibility with simplejson serialization
 97    is_indent = indent is not None
 98    if sort_keys and is_indent:
 99        return orjson.dumps(x, default=default_encoder, option=(
100            orjson.OPT_NAIVE_UTC | orjson.OPT_NON_STR_KEYS |
101            orjson.OPT_SORT_KEYS | orjson.OPT_INDENT_2 |
102            orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_PASSTHROUGH_DATACLASS))
103    elif sort_keys:
104        return orjson.dumps(x, default=default_encoder, option=(
105            orjson.OPT_NAIVE_UTC | orjson.OPT_NON_STR_KEYS |
106            orjson.OPT_SORT_KEYS | orjson.OPT_SERIALIZE_NUMPY |
107            orjson.OPT_PASSTHROUGH_DATACLASS))
108    elif is_indent:
109        return orjson.dumps(x, default=default_encoder, option=(
110            orjson.OPT_NAIVE_UTC | orjson.OPT_NON_STR_KEYS |
111            orjson.OPT_INDENT_2 | orjson.OPT_SERIALIZE_NUMPY |
112            orjson.OPT_PASSTHROUGH_DATACLASS))
113    else:
114        return orjson.dumps(x, default=default_encoder, option=(
115            orjson.OPT_NAIVE_UTC | orjson.OPT_NON_STR_KEYS |
116            orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_PASSTHROUGH_DATACLASS))

Dump a Json to python object.

Arguments:
  • x (any): Object to be serialized using PumpWoodJSONEncoder encoder.
  • sort_keys (bool): If json serialized data should have its keys sorted. This option makes serialization return of data reproductable.
  • indent (int): Pass indent argument to simplejson dumps.
class CompositePkBase64Converter:
119class CompositePkBase64Converter:
120    """Convert composite primary keys in base64 dictionary."""
121
122    @staticmethod
123    def get_attribute(obj: Any, att: str) -> Any:
124        """Get attribute from object or dictinary.
125
126        Args:
127            obj (Any):
128                Object or a dictinary.
129            att (str):
130                Name of the attribute/key that will be used to return
131                the value.
132
133        Return:
134            Return object/dictionary value associated with attribute.
135        """
136        if type(obj) is dict:
137            return obj.get(att, MISSING)
138        else:
139            return getattr(obj, att, MISSING)
140
141    @classmethod
142    def dump(cls, obj: object | dict,
143             primary_keys: Union[str, List[str], Dict[str, str]]
144             ) -> Union[str, int]:
145        """Convert primary keys and composite to a single value.
146
147        Treat cases when more than one column are used as primary keys,
148        at this cases, a base64 used on url serialization of the dictionary
149        is returned.
150
151        Args:
152            obj:
153                SQLAlchemy object or dictionary with data to build the
154                forenging key.
155            primary_keys (Union[str, List[str], Dict[str, str]):
156                As string, a list or a dictionary leading to different
157                behaviour.
158                - **str:** It will return the value associated with object
159                    attribute.
160                - **List[str]:** If list has length equal to 1, it will have
161                    same behaviour as str. If greater than 1, it will be
162                    returned a base64 encoded dictionary with the keys at
163                    primary_keys.
164                - **Dict[str, str]:** Dictionary to map object fields to
165                    other keys. This is usefull when querying related fields
166                    by composite forenging keys to match original data fieds.
167
168        Returns:
169            If the primary key is unique, return the value of the primary
170            key, if is have more than one column as primary key, return
171            a dictionary of the primary keys encoded as base64 url safe.
172        """
173        if obj is None:
174            return None
175
176        missing_keys = []
177        return_value = None
178        if isinstance(primary_keys, str):
179            key_value = cls.get_attribute(obj, primary_keys)
180            if key_value != MISSING:
181                return_value = key_value
182            else:
183                missing_keys.append(primary_keys)
184
185        elif isinstance(primary_keys, list):
186            if len(primary_keys) == 1:
187                key_value = cls.get_attribute(obj, primary_keys[0])
188                if key_value != MISSING:
189                    return key_value
190                else:
191                    missing_keys.append(primary_keys[0])
192            else:
193                composite_pk_dict = {}
194                for pk_col in primary_keys:
195                    key_value = cls.get_attribute(obj, pk_col)
196                    if key_value != MISSING:
197                        composite_pk_dict[pk_col] = key_value
198                    else:
199                        missing_keys.append(pk_col)
200                return_value = composite_pk_dict
201
202        # Map object values to other, this is used when builds forenging
203        # key references and request related field using microservice.
204        elif isinstance(primary_keys, dict):
205            # Treat the case when the dictinary is only an id->value
206            composite_pk_dict = {}
207            for key, value in primary_keys.items():
208                key_value = cls.get_attribute(obj, key)
209                if key_value != MISSING:
210                    composite_pk_dict[value] = key_value
211                else:
212                    missing_keys.append(key)
213            return_value = composite_pk_dict
214
215        # Check if some missing keys are there
216        if len(missing_keys) != 0:
217            msg = (
218                "Some keys were not found on object/dict to create "
219                "the composite forenging key. "
220                "Primary Keys: {primary_keys}; "
221                "Missing keys: {missing_keys}")
222            raise PumpWoodException(msg, payload={
223                "primary_keys": list(primary_keys.keys()),
224                "missing_keys": missing_keys})
225
226        if isinstance(return_value, dict):
227            if {'id'} == set(return_value.keys()):
228                return return_value['id']            
229            return cls.dump_dict(primary_key_dict=return_value)
230        else:
231            return return_value
232
233    @classmethod
234    def validate_primary_key_dict(cls, primary_key_dict: dict):
235        """Validate primary key dictionary.
236
237        Args:
238            primary_key_dict (dict):
239                Dictionary with the primary key values.
240
241        Raises:
242            PumpWoodNotImplementedError:
243                If primary key dictionary is not a dictionary.
244            PumpWoodException:
245                If primary key dictionary contains nested types.
246        """
247        if not isinstance(primary_key_dict, dict):
248            msg = "primary_key_dict must be a dictionary. Received: {type}"
249            raise PumpWoodNotImplementedError(
250                msg, payload={'type': type(primary_key_dict).__name__})
251
252        for key, value in primary_key_dict.items():
253            if isinstance(value, (dict, list, tuple)):
254                msg = (
255                    "primary_key_dict must be flat dictionary. "
256                    "Nested type[{type}] found at key [{key}]")
257                raise PumpWoodException(
258                    msg.format(),
259                    payload={
260                        'primary_key_dict': primary_key_dict,
261                        'type': type(value).__name__,
262                        'key': key})
263
264    @classmethod
265    def dump_dict(cls, primary_key_dict: dict) -> str:
266        """Dump a primary key dictionary to base64 url safe string.
267
268        Args:
269            primary_key_dict (dict):
270                Dictionary with the primary key values.
271
272        Returns:
273            str:
274                Base64 url safe string of the primary key dictionary.
275        """
276        cls.validate_primary_key_dict(primary_key_dict=primary_key_dict)
277        composite_pk_str = pumpJsonDump(primary_key_dict)
278        base64_composite_pk = base64.urlsafe_b64encode(composite_pk_str)\
279            .decode()
280        return base64_composite_pk
281
282    @staticmethod
283    def load(value: Union[str, int, dict]) -> dict:
284        """Convert encoded primary keys to a flat dictionary.
285
286        Integer-like scalars normalize to ``{"id": <int>}``. Base64 JSON
287        strings decode to a flat dict. A ``dict`` input is returned after
288        validating that all keys are strings.
289
290        Args:
291            value (Union[str, int, dict]):
292                Primary key as int, numeric string, base64 JSON string,
293                or flat dict with string keys.
294
295        Returns:
296            dict:
297                Flat primary-key mapping; integer-like inputs use key
298                ``id``.
299
300        Raises:
301            PumpWoodException:
302                If ``value`` is a dict with non-string keys, a
303                non-integer float, or neither integer-like nor decodable
304                base64 JSON.
305        """
306        if isinstance(value, dict):
307            non_string_keys = [
308                key for key in value if not isinstance(key, str)]
309            if non_string_keys:
310                msg = (
311                    "primary_key_dict keys must be strings. "
312                    "Non-string keys: {keys}")
313                raise PumpWoodException(
314                    message=msg.format(keys=non_string_keys),
315                    payload={
316                        "value": value, "non_string_keys": non_string_keys})
317            return value
318
319        # Try to convert value to integer
320        try:
321            float_value = float(value)
322            if float_value.is_integer():
323                return {"id": int(float_value)}
324            else:
325                msg = "PK value [{value}]  is a float, but not integer."
326                raise PumpWoodException(
327                    msg, payload={"value": value})
328
329        # If not possible, try to decode a base64 JSON dictionary
330        except Exception as e1:
331            try:
332                return json.loads(base64.urlsafe_b64decode(value))
333            except Exception as e2:
334                msg = (
335                    "[{value}] value is not an integer and could no be "
336                    "decoded as a base64 encoded json dictionary. Value=")
337                raise PumpWoodException(
338                    message=msg, payload={
339                        "value": value,
340                        "exception_int": str(e1),
341                        "exception_base64": str(e2)})

Convert composite primary keys in base64 dictionary.

@staticmethod
def get_attribute(obj: Any, att: str) -> Any:
122    @staticmethod
123    def get_attribute(obj: Any, att: str) -> Any:
124        """Get attribute from object or dictinary.
125
126        Args:
127            obj (Any):
128                Object or a dictinary.
129            att (str):
130                Name of the attribute/key that will be used to return
131                the value.
132
133        Return:
134            Return object/dictionary value associated with attribute.
135        """
136        if type(obj) is dict:
137            return obj.get(att, MISSING)
138        else:
139            return getattr(obj, att, MISSING)

Get attribute from object or dictinary.

Arguments:
  • obj (Any): Object or a dictinary.
  • att (str): Name of the attribute/key that will be used to return the value.
Return:

Return object/dictionary value associated with attribute.

@classmethod
def dump( cls, obj: object | dict, primary_keys: Union[str, List[str], Dict[str, str]]) -> Union[str, int]:
141    @classmethod
142    def dump(cls, obj: object | dict,
143             primary_keys: Union[str, List[str], Dict[str, str]]
144             ) -> Union[str, int]:
145        """Convert primary keys and composite to a single value.
146
147        Treat cases when more than one column are used as primary keys,
148        at this cases, a base64 used on url serialization of the dictionary
149        is returned.
150
151        Args:
152            obj:
153                SQLAlchemy object or dictionary with data to build the
154                forenging key.
155            primary_keys (Union[str, List[str], Dict[str, str]):
156                As string, a list or a dictionary leading to different
157                behaviour.
158                - **str:** It will return the value associated with object
159                    attribute.
160                - **List[str]:** If list has length equal to 1, it will have
161                    same behaviour as str. If greater than 1, it will be
162                    returned a base64 encoded dictionary with the keys at
163                    primary_keys.
164                - **Dict[str, str]:** Dictionary to map object fields to
165                    other keys. This is usefull when querying related fields
166                    by composite forenging keys to match original data fieds.
167
168        Returns:
169            If the primary key is unique, return the value of the primary
170            key, if is have more than one column as primary key, return
171            a dictionary of the primary keys encoded as base64 url safe.
172        """
173        if obj is None:
174            return None
175
176        missing_keys = []
177        return_value = None
178        if isinstance(primary_keys, str):
179            key_value = cls.get_attribute(obj, primary_keys)
180            if key_value != MISSING:
181                return_value = key_value
182            else:
183                missing_keys.append(primary_keys)
184
185        elif isinstance(primary_keys, list):
186            if len(primary_keys) == 1:
187                key_value = cls.get_attribute(obj, primary_keys[0])
188                if key_value != MISSING:
189                    return key_value
190                else:
191                    missing_keys.append(primary_keys[0])
192            else:
193                composite_pk_dict = {}
194                for pk_col in primary_keys:
195                    key_value = cls.get_attribute(obj, pk_col)
196                    if key_value != MISSING:
197                        composite_pk_dict[pk_col] = key_value
198                    else:
199                        missing_keys.append(pk_col)
200                return_value = composite_pk_dict
201
202        # Map object values to other, this is used when builds forenging
203        # key references and request related field using microservice.
204        elif isinstance(primary_keys, dict):
205            # Treat the case when the dictinary is only an id->value
206            composite_pk_dict = {}
207            for key, value in primary_keys.items():
208                key_value = cls.get_attribute(obj, key)
209                if key_value != MISSING:
210                    composite_pk_dict[value] = key_value
211                else:
212                    missing_keys.append(key)
213            return_value = composite_pk_dict
214
215        # Check if some missing keys are there
216        if len(missing_keys) != 0:
217            msg = (
218                "Some keys were not found on object/dict to create "
219                "the composite forenging key. "
220                "Primary Keys: {primary_keys}; "
221                "Missing keys: {missing_keys}")
222            raise PumpWoodException(msg, payload={
223                "primary_keys": list(primary_keys.keys()),
224                "missing_keys": missing_keys})
225
226        if isinstance(return_value, dict):
227            if {'id'} == set(return_value.keys()):
228                return return_value['id']            
229            return cls.dump_dict(primary_key_dict=return_value)
230        else:
231            return return_value

Convert primary keys and composite to a single value.

Treat cases when more than one column are used as primary keys, at this cases, a base64 used on url serialization of the dictionary is returned.

Arguments:
  • obj: SQLAlchemy object or dictionary with data to build the forenging key.
  • primary_keys (Union[str, List[str], Dict[str, str]): As string, a list or a dictionary leading to different behaviour.
    • str: It will return the value associated with object attribute.
    • List[str]: If list has length equal to 1, it will have same behaviour as str. If greater than 1, it will be returned a base64 encoded dictionary with the keys at primary_keys.
    • Dict[str, str]: Dictionary to map object fields to other keys. This is usefull when querying related fields by composite forenging keys to match original data fieds.
Returns:

If the primary key is unique, return the value of the primary key, if is have more than one column as primary key, return a dictionary of the primary keys encoded as base64 url safe.

@classmethod
def validate_primary_key_dict(cls, primary_key_dict: dict):
233    @classmethod
234    def validate_primary_key_dict(cls, primary_key_dict: dict):
235        """Validate primary key dictionary.
236
237        Args:
238            primary_key_dict (dict):
239                Dictionary with the primary key values.
240
241        Raises:
242            PumpWoodNotImplementedError:
243                If primary key dictionary is not a dictionary.
244            PumpWoodException:
245                If primary key dictionary contains nested types.
246        """
247        if not isinstance(primary_key_dict, dict):
248            msg = "primary_key_dict must be a dictionary. Received: {type}"
249            raise PumpWoodNotImplementedError(
250                msg, payload={'type': type(primary_key_dict).__name__})
251
252        for key, value in primary_key_dict.items():
253            if isinstance(value, (dict, list, tuple)):
254                msg = (
255                    "primary_key_dict must be flat dictionary. "
256                    "Nested type[{type}] found at key [{key}]")
257                raise PumpWoodException(
258                    msg.format(),
259                    payload={
260                        'primary_key_dict': primary_key_dict,
261                        'type': type(value).__name__,
262                        'key': key})

Validate primary key dictionary.

Arguments:
  • primary_key_dict (dict): Dictionary with the primary key values.
Raises:
  • PumpWoodNotImplementedError: If primary key dictionary is not a dictionary.
  • PumpWoodException: If primary key dictionary contains nested types.
@classmethod
def dump_dict(cls, primary_key_dict: dict) -> str:
264    @classmethod
265    def dump_dict(cls, primary_key_dict: dict) -> str:
266        """Dump a primary key dictionary to base64 url safe string.
267
268        Args:
269            primary_key_dict (dict):
270                Dictionary with the primary key values.
271
272        Returns:
273            str:
274                Base64 url safe string of the primary key dictionary.
275        """
276        cls.validate_primary_key_dict(primary_key_dict=primary_key_dict)
277        composite_pk_str = pumpJsonDump(primary_key_dict)
278        base64_composite_pk = base64.urlsafe_b64encode(composite_pk_str)\
279            .decode()
280        return base64_composite_pk

Dump a primary key dictionary to base64 url safe string.

Arguments:
  • primary_key_dict (dict): Dictionary with the primary key values.
Returns:

str: Base64 url safe string of the primary key dictionary.

@staticmethod
def load(value: Union[str, int, dict]) -> dict:
282    @staticmethod
283    def load(value: Union[str, int, dict]) -> dict:
284        """Convert encoded primary keys to a flat dictionary.
285
286        Integer-like scalars normalize to ``{"id": <int>}``. Base64 JSON
287        strings decode to a flat dict. A ``dict`` input is returned after
288        validating that all keys are strings.
289
290        Args:
291            value (Union[str, int, dict]):
292                Primary key as int, numeric string, base64 JSON string,
293                or flat dict with string keys.
294
295        Returns:
296            dict:
297                Flat primary-key mapping; integer-like inputs use key
298                ``id``.
299
300        Raises:
301            PumpWoodException:
302                If ``value`` is a dict with non-string keys, a
303                non-integer float, or neither integer-like nor decodable
304                base64 JSON.
305        """
306        if isinstance(value, dict):
307            non_string_keys = [
308                key for key in value if not isinstance(key, str)]
309            if non_string_keys:
310                msg = (
311                    "primary_key_dict keys must be strings. "
312                    "Non-string keys: {keys}")
313                raise PumpWoodException(
314                    message=msg.format(keys=non_string_keys),
315                    payload={
316                        "value": value, "non_string_keys": non_string_keys})
317            return value
318
319        # Try to convert value to integer
320        try:
321            float_value = float(value)
322            if float_value.is_integer():
323                return {"id": int(float_value)}
324            else:
325                msg = "PK value [{value}]  is a float, but not integer."
326                raise PumpWoodException(
327                    msg, payload={"value": value})
328
329        # If not possible, try to decode a base64 JSON dictionary
330        except Exception as e1:
331            try:
332                return json.loads(base64.urlsafe_b64decode(value))
333            except Exception as e2:
334                msg = (
335                    "[{value}] value is not an integer and could no be "
336                    "decoded as a base64 encoded json dictionary. Value=")
337                raise PumpWoodException(
338                    message=msg, payload={
339                        "value": value,
340                        "exception_int": str(e1),
341                        "exception_base64": str(e2)})

Convert encoded primary keys to a flat dictionary.

Integer-like scalars normalize to {"id": <int>}. Base64 JSON strings decode to a flat dict. A dict input is returned after validating that all keys are strings.

Arguments:
  • value (Union[str, int, dict]): Primary key as int, numeric string, base64 JSON string, or flat dict with string keys.
Returns:

dict: Flat primary-key mapping; integer-like inputs use key id.

Raises:
  • PumpWoodException: If value is a dict with non-string keys, a non-integer float, or neither integer-like nor decodable base64 JSON.