Skip to content

Regex type cast

RegexCastType

Bases: Input

Model for Regex Cast Type.

Parameters:

Name Type Description Default
regex str

Regex to test against.

required
cast_type str

Python type to cast to.

required
Source code in extraction_methods/plugins/regex_type_cast.py
24
25
26
27
28
29
30
31
32
33
34
class RegexCastType(Input):
    """
    Model for Regex Cast Type.
    """

    regex: str = Field(
        description="Regex to test against.",
    )
    cast_type: str = Field(
        description="Python type to cast to.",
    )

RegexTypeCastExtract

Bases: ExtractionMethod

Takes a list of regex and cast type combinations. Any existing properties that full match a regex are cast to the associated type.

Method name: regex_type_cast

Example configuration

.. code-block:: yaml

- method: regex_type_cast
  inputs:
    regex_casts:
      - regex: clound_cover
        cast_type: int

noqa: W605

Source code in extraction_methods/plugins/regex_type_cast.py
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
class RegexTypeCastExtract(ExtractionMethod):
    """
    Takes a list of regex and cast type combinations. Any existing properties
    that full match a regex are cast to the associated type.

    **Method name:** ``regex_type_cast``

    Example configuration:
        .. code-block:: yaml

            - method: regex_type_cast
              inputs:
                regex_casts:
                  - regex: clound_cover
                    cast_type: int

    # noqa: W605
    """

    input_class = RegexTypeCastInput

    @update_input
    def run(self, body: dict[str, Any]) -> dict[str, Any]:

        output = body.copy()
        for key in body.keys():
            for regex_cast in self.input.regex_casts:
                if re.fullmatch(rf"{regex_cast.regex}", key):
                    cast_type = ast.literal_eval(regex_cast.cast_type)
                    output[key] = cast_type(body[key])

        return output

RegexTypeCastInput

Bases: Input

Model for Regex Cast Type Input.

Parameters:

Name Type Description Default
regex_casts list[RegexCastType]

Regex and cast type combinations.

required
Source code in extraction_methods/plugins/regex_type_cast.py
37
38
39
40
41
42
43
44
class RegexTypeCastInput(Input):
    """
    Model for Regex Cast Type Input.
    """

    regex_casts: list[RegexCastType] = Field(
        description="Regex and cast type combinations.",
    )