From df4478a90a735654e4a22c8dd5fb8bc4754d649e Mon Sep 17 00:00:00 2001 From: Dion Moult Date: Wed, 10 Apr 2024 17:32:39 +1000 Subject: [PATCH] Fix #4290. You can now format custom thousands separators and decimal separators in IfcCSV. --- .../docs/ifcopenshell-python/selector_syntax.rst | 1 + .../ifcopenshell/util/selector.py | 12 +++++++++++- .../test/api/sequence/test_edit_work_time.py | 4 ++-- src/ifcopenshell-python/test/util/test_selector.py | 7 +++++++ 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/ifcopenshell-python/docs/ifcopenshell-python/selector_syntax.rst b/src/ifcopenshell-python/docs/ifcopenshell-python/selector_syntax.rst index c9900a5aad..3ae94c26a8 100644 --- a/src/ifcopenshell-python/docs/ifcopenshell-python/selector_syntax.rst +++ b/src/ifcopenshell-python/docs/ifcopenshell-python/selector_syntax.rst @@ -232,5 +232,6 @@ nest formulas, for example ``concat(title("foo"), lower("Bar"))`` will produce "``title({{value}})``", "``title(""foo"")``", "``Foo``", "Titlecases a string." "``concat({{value}}[, {{value2}}]*)``", "``concat(""foo"", ""bar"")``", "``foobar``", "Concatenates two or more strings." "``round({{value}}, {{precision}})``", "``round(3.123, 0.1)``", "``3.1``", "Rounds ``{{value}}`` to the nearest ``{{precision}}``." + "``number({{value}}[, {{decimal_separator}}[, {{thousands_separator}}]])``", "``number(1234.56, "","", ""."")``", "123.4,56", "Formats {{value}} with an optional custom {{decimal_separator}} and {{thousands_separator}}. The default separators are ``.`` and ``,``." "``metric_length({{value}}, {{precision}}, {{decimals}})``", "``metric_length(3.123, 0.1, 2)``", "``3.10``", "Rounds ``{{value}}`` to the nearest ``{{precision}}`` then displays using a certain amount of decimal places." "``imperial_length({{value}}, {{precision}}, {{input_unit}}, {{output_unit}})``", "``imperial_length(3.22, 4, ""foot"")``", "``3' - 3 3/4""``", "``The {{value}}`` may be specified either as ``foot`` or ``inch`` depending on ``{{input_unit}}``. The ``{{value}}`` is then rounded to the nearest ``1/{{precision}}`` inch then formatted using fractional feet and inches if ``{{output_unit}}`` is set to ``foot`` or just inches if ``{{output_unit}}`` is set to ``inch``." diff --git a/src/ifcopenshell-python/ifcopenshell/util/selector.py b/src/ifcopenshell-python/ifcopenshell/util/selector.py index 144fad9cf1..ee94fbc310 100644 --- a/src/ifcopenshell-python/ifcopenshell/util/selector.py +++ b/src/ifcopenshell-python/ifcopenshell/util/selector.py @@ -129,9 +129,10 @@ get_element_grammar = lark.Lark( format_grammar = lark.Lark( """start: function - function: round | format_length | lower | upper | title | concat | substr | ESCAPED_STRING | NUMBER + function: round | number | format_length | lower | upper | title | concat | substr | ESCAPED_STRING | NUMBER round: "round(" function "," NUMBER ")" + number: "number(" function ["," ESCAPED_STRING ["," ESCAPED_STRING]] ")" format_length: metric_length | imperial_length metric_length: "metric_length(" function "," NUMBER "," NUMBER ")" imperial_length: "imperial_length(" function "," NUMBER ["," ESCAPED_STRING "," ESCAPED_STRING] ")" @@ -212,6 +213,15 @@ class FormatTransformer(lark.Transformer): return str(int(result)) return str(result) + def number(self, args): + if isinstance(args[0], str): + args[0] = float(args[0]) if "." in args[0] else int(args[0]) + if len(args) >= 3 and args[2]: + return "{:,}".format(args[0]).replace(".", "*").replace(",", args[2]).replace("*", args[1]) + elif len(args) >= 2 and args[1]: + return "{}".format(args[0]).replace(".", args[1]) + return "{:,}".format(args[0]) + def format_length(self, args): return args[0] diff --git a/src/ifcopenshell-python/test/api/sequence/test_edit_work_time.py b/src/ifcopenshell-python/test/api/sequence/test_edit_work_time.py index 87b484cef8..d200116878 100644 --- a/src/ifcopenshell-python/test/api/sequence/test_edit_work_time.py +++ b/src/ifcopenshell-python/test/api/sequence/test_edit_work_time.py @@ -38,8 +38,8 @@ class TestEditWorkTime(test.bootstrap.IFC4): assert work_time.DataOrigin == attributes["DataOrigin"] assert work_time.UserDefinedDataOrigin == attributes["UserDefinedDataOrigin"] assert work_time.RecurrencePattern == attributes["RecurrencePattern"] - assert work_time.Start == "2020-01-01" - assert work_time.Finish == "2020-02-01" + assert work_time[4] == "2020-01-01" + assert work_time[5] == "2020-02-01" class TestEditWorkTimeIFC4X3(test.bootstrap.IFC4X3): diff --git a/src/ifcopenshell-python/test/util/test_selector.py b/src/ifcopenshell-python/test/util/test_selector.py index 3053c1a0a6..703558969c 100644 --- a/src/ifcopenshell-python/test/util/test_selector.py +++ b/src/ifcopenshell-python/test/util/test_selector.py @@ -44,6 +44,13 @@ class TestFormat(): def test_number_formatting(self): assert subject.format("round(123, 5)") == "125" assert subject.format('round(\"123\", 5)') == "125" + assert subject.format('number(123)') == "123" + assert subject.format('number(1234.56)') == "1,234.56" + assert subject.format('number(123, ".")') == "123" + assert subject.format('number(\"123\", ".")') == "123" + assert subject.format('number(123.12, ".")') == "123.12" + assert subject.format('number(123.12, ",")') == "123,12" + assert subject.format('number(1234.12, ",", ".")') == "1.234,12" assert subject.format('metric_length(123, 5, 2)') == "125.00" assert subject.format('metric_length(123.123, 0.1, 2)') == "123.10" assert subject.format('metric_length(\"123\", 5, 2)') == "125.00"