added funciton to convert string fpx into float

This commit is contained in:
Jakub Trllo 2022-03-03 14:52:24 +01:00
parent fb2c0d268c
commit 6a6ce4d5c5

View file

@ -2,6 +2,9 @@ import re
import json
import collections
import copy
import numbers
import six
from avalon.api import AvalonMongoDB
@ -32,6 +35,109 @@ CURRENT_DOC_SCHEMAS = {
"config": "openpype:config-2.0"
}
FPS_KEYS = {
"fps",
# For development purposes
"fps_string"
}
class InvalidFpsValue(Exception):
pass
def is_string_number(value):
"""Can string value be converted to number (float)."""
if not isinstance(value, six.string_types):
raise TypeError("Expected {} got {}".format(
", ".join(str(t) for t in six.string_types), str(type(value))
))
if value == ".":
return False
if value.startswith("."):
value = "0" + value
elif value.endswith("."):
value = value + "0"
if re.match(r"^\d+(\.\d+)?$", value) is None:
return False
return True
def convert_to_fps(source_value):
"""Convert value into fps value.
Non string values are kept untouched. String is tried to convert.
Valid values:
"1000"
"1000.05"
"1000,05"
",05"
".05"
"1000,"
"1000."
"1000/1000"
"1000.05/1000"
"1000/1000.05"
"1000.05/1000.05"
"1000,05/1000"
"1000/1000,05"
"1000,05/1000,05"
Invalid values:
"/"
"/1000"
"1000/"
","
"."
...any other string
Returns:
float: Converted value.
Raises:
InvalidFpsValue: When value can't be converted to float.
"""
if not isinstance(source_value, six.string_types):
if isinstance(source_value, numbers.Number):
return float(source_value)
return source_value
value = source_value.strip().replace(",", ".")
if not value:
raise InvalidFpsValue("Got empty value")
subs = value.split("/")
if len(subs) == 1:
str_value = subs[0]
if not is_string_number(str_value):
raise InvalidFpsValue(
"Value \"{}\" can't be converted to number.".format(value)
)
return float(str_value)
elif len(subs) == 2:
divident, divisor = subs
if not divident or not is_string_number(divident):
raise InvalidFpsValue(
"Divident value \"{}\" can't be converted to number".format(
divident
)
)
if not divisor or not is_string_number(divisor):
raise InvalidFpsValue(
"Divisor value \"{}\" can't be converted to number".format(
divident
)
)
return float(divident) / float(divisor)
raise InvalidFpsValue(
"Value can't be converted to number \"{}\"".format(source_value)
)
def create_chunks(iterable, chunk_size=None):
"""Separate iterable into multiple chunks by size.