Skip to content

Commit

Permalink
Data type converter: Remove more methods for mutability
Browse files Browse the repository at this point in the history
  • Loading branch information
amotl committed Oct 12, 2022
1 parent 624faf8 commit 14a355d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 21 deletions.
26 changes: 13 additions & 13 deletions src/crate/client/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,11 @@ class DataType(Enum):
ARRAY = 100


ConverterMapping = Dict[DataType, ConverterFunction]


# Map data type identifier to converter function.
_DEFAULT_CONVERTERS: Dict[DataType, ConverterFunction] = {
_DEFAULT_CONVERTERS: ConverterMapping = {
DataType.IP: _to_ipaddress,
DataType.TIMESTAMP_WITH_TZ: _to_datetime,
DataType.TIMESTAMP_WITHOUT_TZ: _to_datetime,
Expand All @@ -100,21 +103,14 @@ class DataType(Enum):
class Converter:
def __init__(
self,
mappings: Optional[Dict[DataType, ConverterFunction]] = None,
mappings: Optional[ConverterMapping] = None,
default: ConverterFunction = _to_default,
) -> None:
self._mappings = mappings or {}
self._default = default

@property
def mappings(self) -> Dict[DataType, ConverterFunction]:
return self._mappings

def get(self, type_: DataType) -> ConverterFunction:
return self.mappings.get(type_, self._default)

def set(self, type_: DataType, converter: ConverterFunction) -> None:
self.mappings[type_] = converter
return self._mappings.get(type_, self._default)

def convert(self, converter_definition: ConverterDefinition, value: Optional[Any]) -> Optional[Any]:
"""
Expand Down Expand Up @@ -153,7 +149,11 @@ def col_type_to_converter(self, type_: ColTypesDefinition) -> ConverterDefinitio


class DefaultTypeConverter(Converter):
def __init__(self) -> None:
super(DefaultTypeConverter, self).__init__(
mappings=deepcopy(_DEFAULT_CONVERTERS), default=_to_default
def __init__(self, more_mappings: Optional[ConverterMapping] = None) -> None:
mappings: ConverterMapping = {}
mappings.update(deepcopy(_DEFAULT_CONVERTERS))
if more_mappings:
mappings.update(deepcopy(more_mappings))
super().__init__(
mappings=mappings, default=_to_default
)
10 changes: 6 additions & 4 deletions src/crate/client/cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
# with Crate these terms will supersede the license and you may use the
# software solely pursuant to the terms of the relevant commercial agreement.

from .converter import Converter, DefaultTypeConverter
from .exceptions import ProgrammingError
import warnings
from typing import Optional

from .converter import Converter, DefaultTypeConverter, ConverterMapping
from .exceptions import ProgrammingError


class Cursor(object):
Expand Down Expand Up @@ -242,8 +244,8 @@ def _convert_rows(self):
]

@staticmethod
def get_default_converter() -> Converter:
def get_default_converter(mappings: Optional[ConverterMapping] = None) -> Converter:
"""
Return the standard converter instance.
"""
return DefaultTypeConverter()
return DefaultTypeConverter(more_mappings=mappings)
3 changes: 1 addition & 2 deletions src/crate/client/doctests/cursor.txt
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,7 @@ To create a simple converter for converging CrateDB's ``BIT`` type to Python's

>>> from crate.client.converter import Converter, DataType

>>> converter = Converter()
>>> converter.set(DataType.BIT, lambda value: int(value[2:-1], 2))
>>> converter = Converter({DataType.BIT: lambda value: int(value[2:-1], 2)})
>>> cursor = connection.cursor(converter=converter)

Proof that the converter works correctly, ``B\'0110\'`` should be converted to
Expand Down
4 changes: 2 additions & 2 deletions src/crate/client/test_cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ def test_execute_with_converter(self):

# Use the set of data type converters from `DefaultTypeConverter`
# and add another custom converter.
converter = Cursor.get_default_converter()
converter.set(DataType.BIT, lambda value: value is not None and int(value[2:-1], 2) or None)
converter = Cursor.get_default_converter(
{DataType.BIT: lambda value: value is not None and int(value[2:-1], 2) or None})

# Create a `Cursor` object with converter.
c = conn.cursor(converter=converter)
Expand Down

0 comments on commit 14a355d

Please sign in to comment.