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