Skip to content

Commit 8ef2fb1

Browse files
committed
Add support for locales
Support numeric data using locale-specific numeric separators and formating.
1 parent 86bf562 commit 8ef2fb1

File tree

8 files changed

+91
-12
lines changed

8 files changed

+91
-12
lines changed

.github/workflows/test.developer.branches.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
- uses: actions/checkout@v4
2424
- uses: actions/setup-python@v5
2525
with:
26-
python-version: "3.9"
26+
python-version: "3.11"
2727
cache: 'pip'
2828
cache-dependency-path: |
2929
**/pyproject.toml
@@ -42,7 +42,7 @@ jobs:
4242
fail-fast: false
4343
matrix:
4444
os: [ubuntu-latest]
45-
python-version: [ "3.11"]
45+
python-version: [ "3.9", "3.10", "3.11"]
4646

4747
needs: [lint]
4848
steps:

tests/common/test_cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright (c) 2021-2024 CloudZero, Inc. or its affiliates. All Rights Reserved.
22
# SPDX-License-Identifier: Apache-2.0
33
# Direct all questions to support@cloudzero.com
4-
import os
4+
from pathlib import Path
55

66
from uca.common.cli import print_uca_sample
77
from uca.common.files import load_jsonl
@@ -17,7 +17,7 @@ def test_print_uca_sample():
1717
1818
"""
1919
# load sample uca data
20-
uca_sample_data_path = os.path.join(os.path.dirname(__file__), "../data/sample_uca_data.jsonl")
20+
uca_sample_data_path = str((Path(__file__).parent / "../data/sample_uca_data.jsonl").resolve())
2121
uca_data = load_jsonl(uca_sample_data_path)
2222

2323
print("Running test\n\n")

tests/common/test_files.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright (c) 2024 CloudZero, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
# Direct all questions to support@cloudzero.com
4+
from pathlib import Path
5+
6+
from uca.common.files import load_data_files
7+
8+
9+
def test_load_data_files():
10+
"""
11+
Test to load data files
12+
"""
13+
test_data_file = str((Path(__file__).parent / "../data/test_data.csv").resolve())
14+
data_files = load_data_files(test_data_file, "TEXT")
15+
assert len(data_files) == 13
16+
17+
data_files = load_data_files(test_data_file, "CSV")
18+
assert len(data_files) == 12

tests/data/test_data.csv

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
timestamp,unit_id,unit_value
2+
2021-08-01,Sunbank,37
3+
2021-08-01,SoftwareCorp,17
4+
2021-08-01,"Parts, Inc.",140
5+
2021-08-01,Transport Co.,25
6+
2021-08-01,"WeShipit, Inc.",124
7+
2021-08-01,CapitalTwo,90
8+
2021-08-02,Sunbank,45
9+
2021-08-02,SoftwareCorp,"1,234.03"
10+
2021-08-02,"Parts, Inc.",12
11+
2021-08-02,Transport Co.,89
12+
2021-08-02,"WeShipit, Inc.",77
13+
2021-08-02,CapitalTwo,934

tests/features/test_generate.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
# Direct all questions to support@cloudzero.com
44

55
import datetime
6+
from pathlib import Path
67

7-
from uca.features.generate import _render_uca_data
8+
from uca.common.files import load_data_files
9+
from uca.features.generate import _render_uca_data, generate_uca
810

911

1012
def test_filter_nil_unit_values_exact_mode(input_uca_data, input_settings, input_template):
@@ -47,3 +49,28 @@ def test_filter_nil_unit_values_random_mode(input_uca_data, input_settings, inpu
4749

4850
result = _render_uca_data(input_uca_data, input_settings, input_template, input_timestamp)
4951
assert len(result) == 1
52+
53+
54+
def test_generate_uca_data_from_CSV(input_settings, input_template):
55+
"""
56+
Test to generate UCA data
57+
58+
Args:
59+
----
60+
input_uca_data:
61+
input_settings:
62+
input_template:
63+
64+
Returns:
65+
-------
66+
None
67+
68+
"""
69+
test_data_file = str((Path(__file__).parent / "../data/test_data.csv").resolve())
70+
test_data = load_data_files(test_data_file, "CSV")
71+
uca_to_send = generate_uca(None, input_template, input_settings, test_data)
72+
assert len(uca_to_send) == 12
73+
for x, li in enumerate(uca_to_send):
74+
assert test_data[x]["unit_value"] == li["value"]
75+
assert test_data[x]["timestamp"] == li["timestamp"]
76+
assert test_data[x]["unit_id"] == li["id"]

uca/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
# SPDX-License-Identifier: Apache-2.0
33
# Direct all questions to support@cloudzero.com
44

5-
__version__ = "0.7.10"
5+
__version__ = "0.7.11"

uca/data/test_data.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ timestamp,unit_id,unit_value
66
2021-08-01,"WeShipit, Inc.",124
77
2021-08-01,CapitalTwo,90
88
2021-08-02,Sunbank,45
9-
2021-08-02,SoftwareCorp,234
9+
2021-08-02,SoftwareCorp,"1,234.03"
1010
2021-08-02,"Parts, Inc.",12
1111
2021-08-02,Transport Co.,89
1212
2021-08-02,"WeShipit, Inc.",77

uca/features/generate.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import calendar
66
import copy
7+
import locale
78
import sys
89
from datetime import timedelta
910
from decimal import Decimal
@@ -115,14 +116,32 @@ def _render_uca_data(uca_data, settings, uca_template, timestamp=None):
115116

116117
uca_events = []
117118
# skipped = 0
119+
row_count = 1
120+
locale.setlocale(locale.LC_ALL, locale.getlocale())
118121
for row in uca_data:
119122
try:
120-
if not row[unit_value_header] or Decimal(row[unit_value_header]) <= 0:
123+
if not row[unit_value_header]:
121124
continue
125+
126+
# support localized numbers
127+
unit_value = locale.delocalize(row[unit_value_header])
128+
129+
# skip zero or negative values
130+
if Decimal(unit_value) <= 0:
131+
continue
132+
133+
row[unit_value_header] = unit_value
134+
122135
except Exception as err:
123-
print(f"Error: {err}")
124-
print(f"{row[unit_value_header]}")
125-
sys.exit(-1)
136+
if "Conversion" in str(err):
137+
eprint(f"Error processing input data in row {row_count}")
138+
eprint(f" Column: {unit_value_header}")
139+
eprint(f" Value: {row[unit_value_header]}")
140+
sys.exit(-1)
141+
else:
142+
eprint(f"Unexpected error: {err}")
143+
eprint(f"{row[unit_value_header]}")
144+
sys.exit(-1)
126145

127146
if generate_settings.get("mode") == "random":
128147
unit_value = preserve_precision(row[unit_value_header], precision)
@@ -182,6 +201,8 @@ def _render_uca_data(uca_data, settings, uca_template, timestamp=None):
182201
print(row)
183202
sys.exit(-1)
184203

204+
row_count += 1
205+
185206
return uca_events
186207

187208

@@ -224,7 +245,7 @@ def preserve_precision(input_number: (str, int, Decimal), precision: int) -> int
224245
return int(round_decimal(Decimal(input_number), precision) * precision)
225246

226247

227-
def restore_precision(input_number: (str, int, Decimal), precision: int) -> int:
248+
def restore_precision(input_number: (str, int, Decimal), precision: int) -> Decimal:
228249
"""
229250
Restore the precision of a number
230251

0 commit comments

Comments
 (0)