Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(weave_query): Add string op to parse number with thousands and decimal separators #2864

Merged
merged 6 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions weave_query/tests/test_basic_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,26 @@ def test_string_ops():
# assert weave.use(foo in foobar) == True # Broken
# assert weave.use(foobar in foo) == False # Broken

class TestStringParseNumberWithSeparator:
def test_parseNumberWithSeparatorWithOnlyThousandsSeparatorSpecified(self):
assert weave.use(make_const_node(weave.types.String(), "123,456,008").parseNumberWithSeparator(thousands_separator=",", decimal_separator=None)) == 123456008.0
assert weave.use(make_const_node(weave.types.String(), "123,456,008").parseNumberWithSeparator(thousands_separator=".", decimal_separator=None)) == None
assert weave.use(make_const_node(weave.types.String(), "123,456.008").parseNumberWithSeparator(thousands_separator=",", decimal_separator=None)) == 123456.008
assert weave.use(make_const_node(weave.types.String(), "123_456_008").parseNumberWithSeparator(thousands_separator="_", decimal_separator=None)) == 123456008.0
assert weave.use(make_const_node(weave.types.String(), "123 456 008").parseNumberWithSeparator(thousands_separator=" ", decimal_separator=None)) == 123456008.0
assert weave.use(make_const_node(weave.types.String(), "123.456.008").parseNumberWithSeparator(thousands_separator=".", decimal_separator=None)) == 123456008.0

def test_parseNumberWithSeparatorWithOnlyDecimalSeparatorSpecified(self):
assert weave.use(make_const_node(weave.types.String(), "123456.008").parseNumberWithSeparator(thousands_separator=None, decimal_separator=".")) == 123456.008
assert weave.use(make_const_node(weave.types.String(), "123456,008").parseNumberWithSeparator(thousands_separator=None, decimal_separator=",")) == 123456.008
assert weave.use(make_const_node(weave.types.String(), "123456008").parseNumberWithSeparator(thousands_separator=None, decimal_separator=".")) == 123456008.0
assert weave.use(make_const_node(weave.types.String(), "123,456,008").parseNumberWithSeparator(thousands_separator=None, decimal_separator=".")) == None

def test_parseNumberWithSeparatorWithBothSpecified(self):
assert weave.use(make_const_node(weave.types.String(), "123,456.008").parseNumberWithSeparator(thousands_separator=",", decimal_separator=".")) == 123456.008
assert weave.use(make_const_node(weave.types.String(), "123.456,008").parseNumberWithSeparator(thousands_separator=".", decimal_separator=",")) == 123456.008
assert weave.use(make_const_node(weave.types.String(), "123 456,008").parseNumberWithSeparator(thousands_separator=" ", decimal_separator=",")) == 123456.008
assert weave.use(make_const_node(weave.types.String(), "123 456,008").parseNumberWithSeparator(thousands_separator=",", decimal_separator=".")) == None

def test_null_consuming_numbers_ops():
data = [box.box(1), box.box(None), box.box(2)]
Expand Down
22 changes: 21 additions & 1 deletion weave_query/weave_query/ops_primitives/string.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,27 @@ def to_number(self):
if self.isnumeric():
return float(self) # type: ignore
return None


@op(name="string-parseNumberWithSeparator", output_type=types.optional(types.Number()))
def parse_number_with_separator(
self,
thousands_separator: typing.Optional[str],
decimal_separator: typing.Optional[str],
):
mNumber = self

if thousands_separator:
mNumber = mNumber.replace(thousands_separator, '')

if decimal_separator:
mNumber = mNumber.replace(decimal_separator, '.')

try:
number = float(mNumber)
except:
number = None

return number

types.String.instance_class = String

Expand Down
Loading