converter.transformers.transform

Module Contents

Classes

TransformerMapping

dict() -> new empty dictionary

GroupWrapper

Group operations preformed on a list of elements

AnyWrapper

Wraps the values and checks if any of their values return true when tested

AllWrapper

Wraps the values and checks if all of their values return true when tested

BaseTreeTransformer

Abstract implementation of the Tree transformer class

Functions

default_in_transformer(row, lhs, rhs)

Performs the in check of the lhs in in the rhs. If the lhs has an is_in

default_not_in_transformer(row, lhs, rhs)

Performs the in check of the lhs is not in the rhs. If the lhs has an

default_replace(row, target, *pattern_repl)

Replaces the pattern in the target string with a given string. The pattern

default_match(row, target, pattern: Pattern)

Checks if a pattern matches the target. The pattern can be either a string

default_search(row, target, pattern: Pattern)

Checks if a pattern is in the target. The pattern can be either a string

default_join(row, join, *elements)

Joins a set of objects as strings

create_transformer_class(row, transformer_mapping)

Creates a transformer class from the provided mapping overrides.

parse(expression: Union[str, lark.Tree]) → lark.Tree

Parse an expression from the transformation language

transform(row, tree: lark.Tree, transformer_mapping: TransformerMapping = None)

Performs the transformation on the row

run(row, expression: Union[str, lark.Tree], transformer_mapping: TransformerMapping = None)

Runs a transformation expression on a row

Attributes

RowType

converter.transformers.transform.RowType
class converter.transformers.transform.TransformerMapping

Bases: TypedDict

dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object’s

(key, value) pairs

dict(iterable) -> new dictionary initialized as if via:

d = {} for k, v in iterable:

d[k] = v

dict(**kwargs) -> new dictionary initialized with the name=value pairs

in the keyword argument list. For example: dict(one=1, two=2)

lookup :Callable[[RowType, str], Any]
add :Callable[[RowType, Any, Any], Any]
subtract :Callable[[RowType, Any, Any], Any]
multiply :Callable[[RowType, Any, Any], Any]
divide :Callable[[RowType, Any, Any], Any]
eq :Callable[[RowType, Any, Any], Any]
not_eq :Callable[[RowType, Any, Any], Any]
is_in :Callable[[RowType, Any, Any], Any]
not_in :Callable[[RowType, Any, Any], Any]
gt :Callable[[RowType, Any, Any], Any]
gte :Callable[[RowType, Any, Any], Any]
lt :Callable[[RowType, Any, Any], Any]
lte :Callable[[RowType, Any, Any], Any]
logical_and :Callable[[RowType, Any, Any], Any]
logical_or :Callable[[RowType, Any, Any], Any]
logical_not :Callable[[RowType, Any], Any]
all :Callable[[RowType, List[Any]], Any]
any :Callable[[RowType, List[Any]], Any]
str_join :Callable[Ellipsis, Any]
str_replace :Callable[[RowType, Any, Pattern, str], Any]
str_match :Callable[[RowType, Any, Pattern], Any]
class converter.transformers.transform.GroupWrapper(values)

Group operations preformed on a list of elements

Parameters

values – A list of the grouped values

abstract check_fn(self, checks: Iterable[Any])

Checks the results of the operator. This should be a reduction of each result in the values list into a single value.

Parameters

checks – The results from the operator comparison

Returns

The reduced result

eq_operator(self, lhs, rhs)

Checks the equality of elements

Parameters
  • lhs – The left hand side of the operator

  • rhs – The right hand side of the operator

Returns

True if the elements are equal, False otherwise

__eq__(self, other)

Checks if the group equals the other based on the check_fn and eq_operator

Parameters

other – The value to check each element against

Returns

The reduced result

__ne__(self, other)

Checks if the group does not equal the other based on the check_fn and the inverse of the eq_operator

Parameters

other – The value to check each element against

Returns

The reduced result

gt_operator(self, lhs, rhs)

Checks the left hand side of the operator is greater than the right hand side

Parameters
  • lhs – The left hand side of the operator

  • rhs – The right hand side of the operator

Returns

True if lhs > rhs, False otherwise

__gt__(self, other)

Checks if the group is greater than the other based on the check_fn and the gt_operator

Parameters

other – The value to check each element against

Returns

The reduced result

gte_operator(self, lhs, rhs)

Checks the left hand side of the operator is greater than or equal to the right hand side

Parameters
  • lhs – The left hand side of the operator

  • rhs – The right hand side of the operator

Returns

True if lhs >= rhs, False otherwise

__ge__(self, other)

Checks if the group is greater than or equal to the other based on the check_fn and the gte_operator

Parameters

other – The value to check each element against

Returns

The reduced result

lt_operator(self, lhs, rhs)

Checks the left hand side of the operator is less than the right hand side

Parameters
  • lhs – The left hand side of the operator

  • rhs – The right hand side of the operator

Returns

True if lhs < rhs, False otherwise

__lt__(self, other)

Checks if the group is less than the other based on the check_fn and the lt_operator

Parameters

other – The value to check each element against

Returns

The reduced result

lte_operator(self, lhs, rhs)

Checks the left hand side of the operator is less than or equal to the right hand side

Parameters
  • lhs – The left hand side of the operator

  • rhs – The right hand side of the operator

Returns

True if lhs > rhs, False otherwise

__le__(self, other)

Checks if the group is less than or equal to the other based on the ` check_fn` and the le_operator

Parameters

other – The value to check each element against

Returns

The reduced result

in_operator(self, lhs, rhs)

Checks the left hand side of the operator is contained in the right hand side

Parameters
  • lhs – The left hand side of the operator

  • rhs – The right hand side of the operator

Returns

True if lhs in rhs, False otherwise

is_in(self, other)

Checks if the group is in the other based on the check_fn and the in_operator

Parameters

other – The value to check each element against

Returns

The reduced result

not_in_operator(self, lhs, rhs)

Checks the left hand side of the operator is not contained in the right hand side

Parameters
  • lhs – The left hand side of the operator

  • rhs – The right hand side of the operator

Returns

True if lhs not in rhs, False otherwise

is_not_in(self, other)

Checks if the group is not in the other based on the check_fn and the not_in_operator

Parameters

other – The value to check each element against

Returns

The reduced result

class converter.transformers.transform.AnyWrapper(values)

Bases: GroupWrapper

Wraps the values and checks if any of their values return true when tested

check_fn(self, checks)

Checks the results of the operator. This should be a reduction of each result in the values list into a single value.

Parameters

checks – The results from the operator comparison

Returns

The reduced result

class converter.transformers.transform.AllWrapper(values)

Bases: GroupWrapper

Wraps the values and checks if all of their values return true when tested

check_fn(self, checks)

Checks the results of the operator. This should be a reduction of each result in the values list into a single value.

Parameters

checks – The results from the operator comparison

Returns

The reduced result

converter.transformers.transform.default_in_transformer(row, lhs, rhs)

Performs the in check of the lhs in in the rhs. If the lhs has an is_in method this is used, if not the in operator is used.

Parameters
  • row – The row being checked (not used)

  • lhs – The left hand side of the operator

  • rhs – The right hand side of the operator

Returns

True if lhs is in right, False otherwise

converter.transformers.transform.default_not_in_transformer(row, lhs, rhs)

Performs the in check of the lhs is not in the rhs. If the lhs has an is_not_in method this is used, if not the not in operator is used.

Parameters
  • row – The row being checked (not used)

  • lhs – The left hand side of the operator

  • rhs – The right hand side of the operator

Returns

True if lhs is not in right, False otherwise

converter.transformers.transform.default_replace(row, target, *pattern_repl)

Replaces the pattern in the target string with a given string. The pattern can be either a string or regular expression, if a regular expression is used groups can be used in the replacement string.

Parameters
  • row – The row being transformed (not used)

  • target – The value to perform the replacement on

  • pattern_repl – Any number of parameters that have pattern and replacement strings, there should be an even number of elements with the 1st, 3rd, 5th etc representing the patterns and teh 2nd, 4th, 6th ets representing the corresponding replacements

Returns

The transformed object

converter.transformers.transform.default_match(row, target, pattern: Pattern)

Checks if a pattern matches the target. The pattern can be either a string or regular expression, if a string is used it is the same as pattern == target.

Parameters
  • row – The row being checked (not used)

  • target – The value to perform the check on

  • pattern – The pattern to find match the target

Returns

True if the pattern matches the pattern, False otherwise

Checks if a pattern is in the target. The pattern can be either a string or regular expression, if a string is used it is the same as pattern in target.

Parameters
  • row – The row being checked (not used)

  • target – The value to perform the check on

  • pattern – The pattern to find in the target

Returns

True if the pattern matches the pattern, False otherwise

converter.transformers.transform.default_join(row, join, *elements)

Joins a set of objects as strings

Parameters
  • row – The row being transformed (not used)

  • join – The string used to join the elements

  • elements – The elements to join

Returns

The joined string

class converter.transformers.transform.BaseTreeTransformer(visit_tokens=True)

Bases: lark.Transformer

Abstract implementation of the Tree transformer class

lookup :Callable[[RowType, str], Any]
add :Callable[[RowType, Any, Any], Any]
subtract :Callable[[RowType, Any, Any], Any]
multiply :Callable[[RowType, Any, Any], Any]
divide :Callable[[RowType, Any, Any], Any]
eq :Callable[[RowType, Any, Any], Any]
not_eq :Callable[[RowType, Any, Any], Any]
is_in :Callable[[RowType, Any, Any], Any]
not_in :Callable[[RowType, Any, Any], Any]
gt :Callable[[RowType, Any, Any], Any]
gte :Callable[[RowType, Any, Any], Any]
lt :Callable[[RowType, Any, Any], Any]
lte :Callable[[RowType, Any, Any], Any]
logical_not :Callable[[RowType, Any, Any], Any]
logical_or :Callable[[RowType, Any, Any], Any]
logical_and :Callable[[RowType, Any, Any], Any]
any :Callable[[RowType, List[Any]], GroupWrapper]
all :Callable[[RowType, List[Any]], GroupWrapper]
str_join :Callable[Ellipsis, Any]
str_replace :Callable[[RowType, Any, Pattern, Any], Any]
str_match :Callable[[RowType, Any, Pattern, Any], Any]
array
string_escape_re
string(self, value='')

Parses a string from the transformer language and performs any necessary escaping. value has a default value to account for the empty string case.

Parameters

value – The value to parse

Returns

The parsed value

regex(self, value='')

Generates a regex from teh provided string

Parameters

value – The pattern

Returns

The regex object

iregex(self, value='')

Generates a case insensitive regex from teh provided string

Parameters

value – The pattern

Returns

The regex object

boolean(self, value)

Pareses a boolean from the transformer language.

Parameters

value – The value to parse

Returns

True if the value is “True”, False otherwise

null(self, value)

Pareses a null from the transformer language.

Parameters

value – The value to parse (ignored as its always Null)

Returns

None

number(self, value)

Parses a number from the transformer language. First tries to parse an integer but on failure parses as a float.

Parameters

value – The value to parse

Returns

The parsed value

converter.transformers.transform.create_transformer_class(row, transformer_mapping)

Creates a transformer class from the provided mapping overrides.

Parameters
  • row – The row to transform

  • transformer_mapping – The overrides for the transform functions

Returns

The new transformer class

converter.transformers.transform.parse(expression: Union[str, lark.Tree]) lark.Tree

Parse an expression from the transformation language

Parameters

expression – The expression to pass

Returns

The parsd expression tree

converter.transformers.transform.transform(row, tree: lark.Tree, transformer_mapping: TransformerMapping = None)

Performs the transformation on the row

Parameters
  • row – The row to transform

  • tree – The parsed tree for the expression

  • transformer_mapping – Overrides for the transformer operations

Returns

The transformation result

converter.transformers.transform.run(row, expression: Union[str, lark.Tree], transformer_mapping: TransformerMapping = None)

Runs a transformation expression on a row

Parameters
  • row – The row to transform

  • expression – The transformation to perform

  • transformer_mapping – Overrides for the transformer operations

Returns

The transformed result