Skip to content

Commit

Permalink
closes: #6 - Migrate to pydantic (#14)
Browse files Browse the repository at this point in the history
* updates dev dependencies

* Types out the schema as pydantic BaseModels

* process now marshalls to pydantic

* If statements migrated to pydantic

* Slicing migrated to pydantic

* Regexp migrated to pydantic

* Casting migrated to pydantic

* Fixes doctests in functions.py

* Handler.handle_mapping migrated to pydantic

* Handlers.handle_attributes merged to pydantic

* Possibly done migrating mapper code to pydantic

* Migrates collection handlers to pydantic

* Migrates collection handlers tests to pydantic

* Migrates process function to pydantic

* fix process function so that it correctly returns a Failure(Exc)

* Add validation test that checks that the errors we get are correct and that things a marshalled correctly

* removes old jsonschema tests

* removes unused common.py and gets tests coverage up to 100% again

* Fixes linting errors

* cleans up constants.py

* Fixes mypy errors

* Fixes import sort

* fixes high jonas complexity lines

* in function stop using mapvalue and use same AnyType type that's used in pydantic schema

* fixes linting errors

* removes unused import

* Removes jsonschema dependency

* formatting

* Bump minor version

* Test that we can create a jsonschema

* Adds docstring.
  • Loading branch information
thomasborgen committed Jun 7, 2021
1 parent a5ad9e7 commit 07295dd
Show file tree
Hide file tree
Showing 49 changed files with 1,051 additions and 1,481 deletions.
12 changes: 9 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@

## Version 1.0.0 Release: Kaiba

Kaiba is a data transformation tool written in Python that uses a DTL(Data Transformation Language) expressed in normal JSON to govern output structure and input transformation and mappings.

Kaiba is forked from the greenbird/piri @ version 2.2.0
Kaiba is a data transformation tool written in Python that uses a DTL(Data Transformation Language) expressed in normal JSON to govern output structure, input transformation and mappings.

### Features

Expand All @@ -27,8 +25,16 @@ Kaiba is forked from the greenbird/piri @ version 2.2.0
* Slicing


## Version 0.3.0 - Migrate to pydantic

This version changes how we validate our json configuration and also how we parse it. Using pydantic it is much easier to handle to handle typing and the code in general.

* Removes json schema adds pydantic for config validation

## Version 0.2.1 - Schema troubles

Kaiba is forked from the greenbird/piri @ version 2.2.0

Fixes problems with Schema validation

* In attribute make sure either name + mappings or name + default is required
Expand Down
24 changes: 6 additions & 18 deletions kaiba/casting.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,21 @@
from returns.result import Failure, ResultE, safe
from typing_extensions import final

from kaiba.constants import (
COMMA,
DATE,
DECIMAL,
EMPTY,
INTEGER,
INTEGER_CONTAINING_DECIMALS,
PERIOD,
)
from kaiba.constants import COMMA, EMPTY, INTEGER_CONTAINING_DECIMALS, PERIOD
from kaiba.pydantic_schema import CastingEnum
from kaiba.valuetypes import MapValue


@safe
def get_casting_function(cast_to: str) -> Callable:
def get_casting_function(cast_to: CastingEnum) -> Callable:
"""Return casting function depending on name."""
if cast_to == INTEGER:
if cast_to == CastingEnum.INTEGER:
return CastToInteger()

elif cast_to == DECIMAL:
elif cast_to == CastingEnum.DECIMAL:
return CastToDecimal()

elif cast_to == DATE:
return CastToDate()

raise NotImplementedError(
'Unsupported cast to value ({0})'.format(cast_to),
)
return CastToDate()


@final
Expand Down
30 changes: 18 additions & 12 deletions kaiba/collection_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from returns.result import Failure, ResultE, Success, safe

from kaiba.constants import ALIAS, PATH
from kaiba.pydantic_schema import Iterable
from kaiba.valuetypes import MapValue


Expand Down Expand Up @@ -65,37 +65,43 @@ def fetch_list_by_keys(
raise ValueError('Non list data found: ', str(collection))


def iterable_data_handler(raw_data, paths) -> ResultE[list]:
"""Iterate and create all combinations from list of paths."""
if not paths:
return Failure(ValueError('No paths'))
def iterable_data_handler(
raw_data,
iterables: List[Iterable],
) -> ResultE[list]:
"""Iterate and create all combinations from list of iterables."""
if not iterables:
return Failure(ValueError('No iterables'))

path, rest = paths[0], paths[1:]
iterable, rest = iterables[0], iterables[1:]

if not rest:
return create_iterable(raw_data, path)
return create_iterable(raw_data, iterable)

my_list: list = []

for iterable in create_iterable(raw_data, path).unwrap():
for iterable_list in create_iterable(raw_data, iterable).unwrap():

iterable_data_handler(iterable, rest).map(
iterable_data_handler(iterable_list, rest).map(
my_list.extend,
)

return Success(my_list)


def create_iterable(input_data, iterable) -> ResultE[list]:
def create_iterable(
input_data,
iterable: Iterable,
) -> ResultE[list]:
"""Return set of set of data per entry in list at iterable[path]."""
return fetch_list_by_keys(
input_data,
iterable[PATH],
iterable.path,
).map(
lambda collections: [
{
**input_data,
**{iterable[ALIAS]: collection},
**{iterable.alias: collection},
}
for collection in collections
],
Expand Down
43 changes: 0 additions & 43 deletions kaiba/common.py

This file was deleted.

66 changes: 8 additions & 58 deletions kaiba/constants.py
Original file line number Diff line number Diff line change
@@ -1,70 +1,20 @@
from typing_extensions import Final

# Objects
NAME = 'name'
ARRAY: Final = 'array'
ITERABLES: Final = 'iterables'

ATTRIBUTES: Final = 'attributes'
OBJECTS: Final = 'objects' # noqa: WPS110
BRANCHING_OBJECTS: Final = 'branching_objects'
BRANCHING_ATTRIBUTES: Final = 'branching_attributes'

MAPPINGS: Final = 'mappings'
SEPARATOR: Final = 'separator'
IF_STATEMENTS: Final = 'if_statements'
CASTING: Final = 'casting'
DEFAULT: Final = 'default'

# Iterables
ALIAS: Final = 'alias'
PATH: Final = 'path'

# Mapping
# 'path' from iterables
# 'default'

# Regexp
REGEXP: Final[str] = 'regexp'
SEARCH: Final[str] = 'search'
GROUP: Final[str] = 'group'
DEFAULT_GROUP: Final[int] = 0

# IF STATEMENT
CONDITION: Final = 'condition'
IS: Final = 'is'
IN: Final = 'in'
NOT: Final = 'not'
CONTAINS: Final = 'contains'
TARGET: Final = 'target'
THEN: Final = 'then'
OTHERWISE: Final = 'otherwise'

# Slicing
SLICING: Final = 'slicing'
FROM: Final = 'from'
TO: Final = 'to'

# Casting
# 'to' from slicing
INTEGER: Final = 'integer'
DECIMAL: Final = 'decimal'
DATE: Final = 'date'
ORIGINAL_FORMAT: Final = 'original_format'
INTEGER_CONTAINING_DECIMALS = 'integer_containing_decimals'
INTEGER_CONTAINING_DECIMALS: Final[str] = 'integer_containing_decimals'
YMD_DATE_FORMAT: Final = r'(^(yy|yyyy)[^\w]?mm[^\w]?dd$)'
DMY_DATE_FORMAT: Final = r'(^dd[^\w]?mm[^\w]?(yy|yyyy)$)'
MDY_DATE_FORMAT = r'(^mm[^\w]?dd[^\w]?(yy|yyyy)$)'
MDY_DATE_FORMAT: Final = r'(^mm[^\w]?dd[^\w]?(yy|yyyy)$)'

# Casting helpers
COMMA: Final[str] = ','
PERIOD: Final[str] = '.'
EMPTY: Final[str] = ''

# ISO
ALPHA_TWO = 'alpha_2'
ALPHA_THREE = 'alpha_3'
NUMERIC = 'numeric'
OFFICIAL_NAME = 'official_name'
INVALID = 'invalid'
# NAME
NAME: Final[str] = 'name'
ALPHA_TWO: Final[str] = 'alpha_2'
ALPHA_THREE: Final[str] = 'alpha_3'
NUMERIC: Final[str] = 'numeric'
OFFICIAL_NAME: Final[str] = 'official_name'
INVALID: Final[str] = 'invalid'
Loading

0 comments on commit 07295dd

Please sign in to comment.