From 75d96074f3c93fbbe0ca0c6d9401e45d5794e30f Mon Sep 17 00:00:00 2001 From: iLLiCiTiT Date: Wed, 24 Mar 2021 12:39:01 +0100 Subject: [PATCH] number entity entity can convert stringified number to to number type --- pype/settings/entities/input_entities.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/pype/settings/entities/input_entities.py b/pype/settings/entities/input_entities.py index a1beaef9f4..921171cfff 100644 --- a/pype/settings/entities/input_entities.py +++ b/pype/settings/entities/input_entities.py @@ -1,3 +1,4 @@ +import re import copy from abc import abstractmethod @@ -320,6 +321,8 @@ class InputEntity(EndpointEntity): class NumberEntity(InputEntity): schema_types = ["number"] + float_number_regex = re.compile(r"^\d+\.\d+$") + int_number_regex = re.compile(r"^\d+$") def _item_initalization(self): self.minimum = self.schema_data.get("minimum", -99999) @@ -334,15 +337,32 @@ class NumberEntity(InputEntity): self.value_on_not_set = 0 def _convert_to_valid_type(self, value): + if isinstance(value, str): + new_value = None + if self.float_number_regex.match(value): + new_value = float(value) + elif self.int_number_regex.match(value): + new_value = int(value) + + if new_value is not None: + self.log.info("{} - Converted str {} to {} {}".format( + self.path, value, type(new_value).__name__, new_value + )) + value = new_value + if self.decimal: + if isinstance(value, float): + return value if isinstance(value, int): return float(value) else: + if isinstance(value, int): + return value if isinstance(value, float): new_value = int(value) if new_value != value: - self.log.info("Converted float {} to int {}".format( - value, new_value + self.log.info("{} - Converted float {} to int {}".format( + self.path, value, new_value )) return new_value return NOT_SET