string_parser

Extract and convert common string to Python objects.

From simple data types like boolean, integer, and float to more complex data types like JSON, YAML, and list strings.

Classes

BooleanParser(*args, **kwargs)

Extracts boolean values from text.

FloatParser()

Extracts float values from text.

IntParser()

Extracts integer values from text.

JsonParser([add_missing_right_brace])

Extracts JSON strings {...} or [...] from text and parses them into a JSON object.

ListParser([add_missing_right_bracket])

Extracts list [...] strings from text and parses them into a list object.

YamlParser()

To extract YAML strings from text and parse them into a YAML object.

class BooleanParser(*args, **kwargs)[source]

Bases: DataComponent

Extracts boolean values from text.

Examples:

boolean_parser = BooleanParser()
test_input_1 = "True" # or "true" or "...true..."
print(boolean_parser(test_input_1))  # Expected to extract True
call(input: str) bool[source]

User must override this for the inference scenario if bicall is not defined.

class IntParser[source]

Bases: DataComponent

Extracts integer values from text.

Returns:

Extracted integer value.

Return type:

int

Raises:

ValueError – If the input text does not contain an integer

Examples:

int_parser = IntParser()
test_input_2 = "123" # or "...123..."
print(int_parser(test_input_2))  # Expected to extract 123
call(input: str) int[source]

User must override this for the inference scenario if bicall is not defined.

class FloatParser[source]

Bases: DataComponent

Extracts float values from text.

Returns:

Extracted float value.

Return type:

float

Raises:

ValueError – If the input text does not contain a float

Examples:

float_parser = FloatParser()
test_input_3 = "123.45" # or "...123.45..."
print(float_parser(test_input_3))  # Expected to extract 123.45
call(input: str) float[source]

User must override this for the inference scenario if bicall is not defined.

class ListParser(add_missing_right_bracket: bool = True)[source]

Bases: DataComponent

Extracts list […] strings from text and parses them into a list object.

Parameters:

add_missing_right_bracket (bool, optional) – Add a missing right bracket to the list string. Defaults to True.

Returns:

Extracted list object.

Return type:

List[object]

Raises:

ValueError – If the input text does not contain a list

Examples:

list_parser = ListParser()
test_input_4 = 'Some random text before ["item1", "item2"] and more after'
print(list_parser(test_input_4))  # Expected to extract ["item1", "item2"]
call(input: str) List[object][source]

User must override this for the inference scenario if bicall is not defined.

class JsonParser(add_missing_right_brace: bool = True)[source]

Bases: DataComponent

Extracts JSON strings {…} or […] from text and parses them into a JSON object.

It can output either a dictionary or a list as they are both valid JSON objects.

Parameters:

add_missing_right_brace (bool, optional) – Add a missing right brace to the JSON string. Defaults to True.

Returns:

Extracted JSON object.

Return type:

Union[Dict[str, object], List[object]]

Raises:

ValueError – If the input text does not contain a JSON object

Examples:

json_parser = JsonParser()
json_str = "```json\n{\"key\": \"value\"}\n```"
json_obj = json_parser(json_str)
print(json_obj)  # Expected to extract {"key": "value"}
call(input: str) Dict[str, object] | List[object][source]

User must override this for the inference scenario if bicall is not defined.

class YamlParser[source]

Bases: DataComponent

To extract YAML strings from text and parse them into a YAML object.

Returns:

Extracted YAML object.

Return type:

JSON_PARSER_OUTPUT_TYPE

Raises:

ValueError – If the input text does not contain a YAML object

Examples:

yaml_parser = YamlParser()
yaml_str = "```yaml\nkey: value\n```"
yaml_obj = yaml_parser(yaml_str)
print(yaml_obj)  # Expected to extract {"key": "value"}
call(input: str) Dict[str, object] | List[object][source]

User must override this for the inference scenario if bicall is not defined.