utils.strings.conversion
1from typing import Optional 2 3 4def floatToStringForADQLcastVarchar( 5 floatValue: float, 6 dropLeadingZero: bool = False 7) -> str: 8 """ 9 Convert float value to string for using in ADQL queries. For instance, 10 the float value is `1.2345`, and we know that database rounds the values, 11 so we need to drop the last digit and use `%` instead of it, so in ADQL 12 query it will be something like: 13 14 ``` sql 15 -- ... 16 WHERE CAST(some_float AS VARCHAR(10)) LIKE '1.234%'" 17 ``` 18 19 Example: 20 21 ``` py 22 from phab.utils.strings import conversion 23 from phab.utils.databases import tap 24 25 dropsLeadingZeroOnCastToVarchar = tap.services.get( 26 "nasa", {} 27 ).get( 28 "drops-leading-zero-on-cast-to-varchar", False 29 ) 30 val = conversion.floatToStringForADQLcastVarchar( 31 1.2345, 32 dropsLeadingZeroOnCastToVarchar 33 ) 34 print(val) 35 ``` 36 """ 37 stringValue: str = "" 38 39 # NASA's ADQL casts `0.123` float value as `.123` string 40 if dropLeadingZero and abs(floatValue) < 1: 41 if floatValue < 0: # just in case, preserve the `-` sign 42 stringValue = f"-{str(floatValue)[2:-1]}%" 43 else: 44 stringValue = f"{str(floatValue)[1:-1]}%" 45 else: 46 stringValue = f"{str(floatValue)[:-1]}%" 47 48 return stringValue
def
floatToStringForADQLcastVarchar(floatValue: float, dropLeadingZero: bool = False) -> str:
5def floatToStringForADQLcastVarchar( 6 floatValue: float, 7 dropLeadingZero: bool = False 8) -> str: 9 """ 10 Convert float value to string for using in ADQL queries. For instance, 11 the float value is `1.2345`, and we know that database rounds the values, 12 so we need to drop the last digit and use `%` instead of it, so in ADQL 13 query it will be something like: 14 15 ``` sql 16 -- ... 17 WHERE CAST(some_float AS VARCHAR(10)) LIKE '1.234%'" 18 ``` 19 20 Example: 21 22 ``` py 23 from phab.utils.strings import conversion 24 from phab.utils.databases import tap 25 26 dropsLeadingZeroOnCastToVarchar = tap.services.get( 27 "nasa", {} 28 ).get( 29 "drops-leading-zero-on-cast-to-varchar", False 30 ) 31 val = conversion.floatToStringForADQLcastVarchar( 32 1.2345, 33 dropsLeadingZeroOnCastToVarchar 34 ) 35 print(val) 36 ``` 37 """ 38 stringValue: str = "" 39 40 # NASA's ADQL casts `0.123` float value as `.123` string 41 if dropLeadingZero and abs(floatValue) < 1: 42 if floatValue < 0: # just in case, preserve the `-` sign 43 stringValue = f"-{str(floatValue)[2:-1]}%" 44 else: 45 stringValue = f"{str(floatValue)[1:-1]}%" 46 else: 47 stringValue = f"{str(floatValue)[:-1]}%" 48 49 return stringValue
Convert float value to string for using in ADQL queries. For instance,
the float value is 1.2345, and we know that database rounds the values,
so we need to drop the last digit and use % instead of it, so in ADQL
query it will be something like:
-- ...
WHERE CAST(some_float AS VARCHAR(10)) LIKE '1.234%'"
Example:
from phab.utils.strings import conversion
from phab.utils.databases import tap
dropsLeadingZeroOnCastToVarchar = tap.services.get(
"nasa", {}
).get(
"drops-leading-zero-on-cast-to-varchar", False
)
val = conversion.floatToStringForADQLcastVarchar(
1.2345,
dropsLeadingZeroOnCastToVarchar
)
print(val)