Mirror of https://github.com/roostorg/osprey github.com/roostorg/osprey
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add ParseInt udf (#190)

authored by

Beals and committed by
GitHub
e56b2023 102e7a6b

+41
+1
CHANGELOG.md
··· 14 14 15 15 ### 🎉 New features 16 16 - Add Postgres execution result store ([#171](https://github.com/roostorg/osprey/pull/171) by [@serendipty01](https://github.com/serendipty01)) 17 + - Add `ParseInt` UDF — converts a numeric string to an integer ([#190](https://github.com/roostorg/osprey/pull/190) by [@bealsbe](https://github.com/bealsbe)) 17 18 - Add `StringSlice` UDF which extracts a substring by index range ([#189](https://github.com/roostorg/osprey/pull/189) by [@bealsbe](https://github.com/bealsbe)) 18 19 - Add `InExperiment` UDF which checks if an entity is in an experiment ([#203](https://github.com/roostorg/osprey/pull/203) by [@bealsbe](https://github.com/bealsbe)) 19 20
+1
osprey_worker/src/osprey/engine/stdlib/udfs/categories.py
··· 13 13 HTTP = 'HTTP' 14 14 IP = 'IP' 15 15 PHONE = 'Phone' 16 + CAST = 'Cast' 16 17 RANDOM = 'Random' 17 18 STRING = 'String'
+20
osprey_worker/src/osprey/engine/stdlib/udfs/parse_int.py
··· 1 + from osprey.engine.executor.execution_context import ExpectedUdfException 2 + 3 + from ._prelude import ArgumentsBase, ExecutionContext, UDFBase 4 + from .categories import UdfCategories 5 + 6 + 7 + class Arguments(ArgumentsBase): 8 + s: str 9 + 10 + 11 + class ParseInt(UDFBase[Arguments, int]): 12 + """Converts a numeric string to an integer.""" 13 + 14 + category = UdfCategories.CAST 15 + 16 + def execute(self, execution_context: ExecutionContext, arguments: Arguments) -> int: 17 + try: 18 + return int(arguments.s) 19 + except ValueError: 20 + raise ExpectedUdfException()
+17
osprey_worker/src/osprey/engine/stdlib/udfs/tests/test_parse_int.py
··· 1 + import pytest 2 + from osprey.engine.conftest import ExecuteFunction 3 + from osprey.engine.stdlib.udfs.parse_int import ParseInt 4 + from osprey.engine.udf.registry import UDFRegistry 5 + 6 + pytestmark = [pytest.mark.use_udf_registry(UDFRegistry.with_udfs(ParseInt))] 7 + 8 + 9 + @pytest.mark.parametrize('s,expected', [('15', 15), ('04', 4), ('0', 0), ('-7', -7), ('-092', -92)]) 10 + def test_parse_int(execute: ExecuteFunction, s: str, expected: int) -> None: 11 + data = execute(f'Result = ParseInt(s="{s}")') 12 + assert data == {'Result': expected} 13 + 14 + 15 + def test_parse_int_invalid(execute: ExecuteFunction) -> None: 16 + data = execute('Result = ParseInt(s="ABC")') 17 + assert data == {'Result': None}
+2
osprey_worker/src/osprey/worker/_stdlibplugin/udf_register.py
··· 21 21 from osprey.engine.stdlib.udfs.list_read import ListRead 22 22 from osprey.engine.stdlib.udfs.list_sort import ListSort 23 23 from osprey.engine.stdlib.udfs.mx_lookup import MXLookup 24 + from osprey.engine.stdlib.udfs.parse_int import ParseInt 24 25 from osprey.engine.stdlib.udfs.phone_country import PhoneCountry 25 26 from osprey.engine.stdlib.udfs.phone_prefix import PhonePrefix 26 27 from osprey.engine.stdlib.udfs.random_bool import RandomBool ··· 96 97 ListRead, 97 98 ListSort, 98 99 MXLookup, 100 + ParseInt, 99 101 PhoneCountry, 100 102 PhonePrefix, 101 103 RandomBool,