From 13106d255cce05071971c5950641d4d9479d19ca Mon Sep 17 00:00:00 2001 From: Andrej730 Date: Thu, 5 Dec 2024 15:52:23 +0500 Subject: [PATCH] int formatting function #5039 Example - https://i.imgur.com/wDGFNci.png --- .../docs/ifcopenshell-python/selector_syntax.rst | 1 + src/ifcopenshell-python/ifcopenshell/util/selector.py | 6 +++++- src/ifcopenshell-python/test/util/test_selector.py | 2 ++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/ifcopenshell-python/docs/ifcopenshell-python/selector_syntax.rst b/src/ifcopenshell-python/docs/ifcopenshell-python/selector_syntax.rst index 60df6ac84e..2cd3557ad6 100644 --- a/src/ifcopenshell-python/docs/ifcopenshell-python/selector_syntax.rst +++ b/src/ifcopenshell-python/docs/ifcopenshell-python/selector_syntax.rst @@ -240,6 +240,7 @@ 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}}``." + "``int({{value}})``", "``int(3.123)``", "``3``", "Truncates the decimal part of the ``{{value}}``." "``number({{value}}[, {{decimal_separator}}[, {{thousands_separator}}]])``", "``number(1234.56, "","", ""."")``", "``1.234,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 2c598df030..0a7fff0c0e 100644 --- a/src/ifcopenshell-python/ifcopenshell/util/selector.py +++ b/src/ifcopenshell-python/ifcopenshell/util/selector.py @@ -134,10 +134,11 @@ get_element_grammar = lark.Lark( format_grammar = lark.Lark( """start: function - function: round | number | format_length | lower | upper | title | concat | substr | ESCAPED_STRING | NUMBER + function: round | number | int | format_length | lower | upper | title | concat | substr | ESCAPED_STRING | NUMBER round: "round(" function "," NUMBER ")" number: "number(" function ["," ESCAPED_STRING ["," ESCAPED_STRING]] ")" + int: "int(" function ")" format_length: metric_length | imperial_length metric_length: "metric_length(" function "," NUMBER "," NUMBER ")" imperial_length: "imperial_length(" function "," NUMBER ["," ESCAPED_STRING "," ESCAPED_STRING] ")" @@ -249,6 +250,9 @@ class FormatTransformer(lark.Transformer): float(value), int(precision), unit_system="imperial", input_unit=input_unit, output_unit=output_unit ) + def int(self, args: list[str]) -> str: + return str(int(float(args[0]))) + class GetElementTransformer(lark.Transformer): def start(self, args): diff --git a/src/ifcopenshell-python/test/util/test_selector.py b/src/ifcopenshell-python/test/util/test_selector.py index 4cb29c0523..ed2f93448d 100644 --- a/src/ifcopenshell-python/test/util/test_selector.py +++ b/src/ifcopenshell-python/test/util/test_selector.py @@ -54,6 +54,8 @@ class TestFormat: def test_number_formatting(self): assert subject.format("round(123, 5)") == "125" assert subject.format('round("123", 5)') == "125" + assert subject.format("int(123.123)") == "123" + assert subject.format("int(123)") == "123" assert subject.format("number(123)") == "123" assert subject.format("number(1234.56)") == "1,234.56" assert subject.format('number(123, ".")') == "123"