Ligare.programming.R.type_conversion
Functions
|
|
|
Returns the R string "TRUE" or "FALSE" from the Python strings "True", "T", "False", or "F" (lowercased), or boolean True or False, respectively. |
|
|
|
This method is a pass-through for string(value, comma_separated=True). |
|
This method is a pass-through for string(value, vector=True). |
|
Add a new key to the parts dictionary named new_part_key. |
- Ligare.programming.R.type_conversion.boolean(value)[source]
Returns the R string “TRUE” or “FALSE” from the Python strings “True”, “T”, “False”, or “F” (lowercased), or boolean True or False, respectively. If value is None, “FALSE” is returned.
- Parameters:
value (str) – The input string “True”, “T”, “False”, or “F”, or boolean True or False.
- Return str:
The R string “TRUE” or “FALSE”.
- Return type:
str
Usage
>>> boolean(True) 'TRUE' >>> boolean("True") 'TRUE' >>> boolean("T") 'TRUE' >>> boolean("true") 'TRUE' >>> boolean(False) 'FALSE' >>> boolean("False") 'FALSE' >>> boolean("F") 'FALSE' >>> boolean("false") 'FALSE' >>> boolean(1) 'FALSE' >>> boolean(0) 'FALSE' >>> boolean(None) 'FALSE'
- Ligare.programming.R.type_conversion.string(value, *, comma_separated=False, vector=False)[source]
- Return type:
str
|None
- Ligare.programming.R.type_conversion.string_from_csv(value)[source]
This method is a pass-through for string(value, comma_separated=True).
Remove all characters from a string that are not whitelisted in the SAFE_COMMA_SEPARATED_STRING_PATTERN regex.
The string is comma-separated and this method accepts , as valid.
- Parameters:
value (str) – The string to sanitize.
- Return string:
The sanitized string, or None if the string only consists of invalid characters.
- Return type:
str
|None
Usage
>>> string_from_csv("abc") "'abc'" >>> string_from_csv("a,b,c") "'a','b','c'" >>> string_from_csv(None) is None True >>> string_from_csv("\t,\t") "'\t','\t'" >>> string_from_csv(",") is None True
- Ligare.programming.R.type_conversion.vector_from_csv(value)[source]
This method is a pass-through for string(value, vector=True).
Remove all characters from a string that are not whitelisted in the SAFE_COMMA_SEPARATED_STRING_PATTERN regex.
- The string is comma-separated
and this method accepts , as valid. This method returns a string value formatted as an R vector c(…) containing the values from the CSV value string.
- Parameters:
value (str) – The string to sanitize.
- Return str | None:
The vectorized CSV string, or None if no valid characters were found
- Return type:
str
|None
Usage
>>> vector_from_csv("'") is None True >>> vector_from_csv("''") is None True >>> vector_from_csv("'a'") "c('a')" >>> vector_from_csv("a-b-c") "c('a-b-c')" >>> vector_from_csv("a-b-.c") "c('a-b-.c')" >>> vector_from_csv("a-b-^%^$%^c") "c('a-b-c')" >>> vector_from_csv("") 'c()' >>> vector_from_csv("abc") "c('abc')" >>> vector_from_csv("a,b,c") "c('a','b','c')" >>> vector_from_csv(None) 'c()' >>> vector_from_csv("!!!") is None True >>> vector_from_csv("a!b!c!") "c('abc')" >>> vector_from_csv("a!,b!,c!") "c('a','b','c')" >>> vector_from_csv("a,b,c,") "c('a','b','c')" >>> vector_from_csv("a.b.c") "c('a.b.c')"
- Ligare.programming.R.type_conversion.vector_from_parts(parts, new_part_key, existing_part_keys, default='__NULL__')[source]
Add a new key to the parts dictionary named new_part_key.
The value of the new key is:
A Python string representing an R vector
"c(...)"
where each value comes from the parts dictionary for each key in the existing_part_keys list.If every value from the parts dictionary is None or an empty string, the new key’s value is the values of default.
Each key in existing_part_keys is deleted from the parts dictionary.
- Parameters:
parts (dict[str, Any]) – A dictionary of parameters
new_part_key (str) – The name of the new key to add to the dictionary
existing_part_keys (list[str]) – The names of the keys from which to create the value of the new key
default (Any) – The default value of the new key if all key values in parts for the keys existing_part_keys are None or an empty string, defaults to “__NULL__”
- Return type:
None
Usage
Convert two keys with values into a single two-item vector.
>>> query_params = { ... "scale.bar.coords.x": 0.5, ... "scale.bar.coords.y": 1.0 ... } >>> vector_from_parts( ... query_params, ... "scale.bar.coords", ... ["scale.bar.coords.x", "scale.bar.coords.y"] ... ) >>> query_params {'scale.bar.coords': 'c(0.5,1.0)'}
Convert two keys with numerical and string values into a single two-item vector.
>>> query_params = { ... "scale.bar.coords.x": 0.5, ... "scale.bar.coords.y": '1.0' ... } >>> vector_from_parts( ... query_params, ... "scale.bar.coords", ... ["scale.bar.coords.x", "scale.bar.coords.y"] ... ) >>> query_params {'scale.bar.coords': "c(0.5,'1.0')"}
Convert two keys into a single two-item vector where one value is `None`
>>> query_params = { ... "scale.bar.coords.x": 0.5, ... "scale.bar.coords.y": None ... } >>> vector_from_parts( ... query_params, ... "scale.bar.coords", ... ["scale.bar.coords.x", "scale.bar.coords.y"] ... ) >>> query_params {'scale.bar.coords': "c(0.5,'__NULL__')"}
Convert two keys into an empty value where all values are `None`
>>> query_params = { ... "scale.bar.coords.x": None, ... "scale.bar.coords.y": None ... } >>> vector_from_parts( ... query_params, ... "scale.bar.coords", ... ["scale.bar.coords.x", "scale.bar.coords.y"] ... ) >>> query_params {'scale.bar.coords': '__NULL__'}
Convert many keys of varying types into a single vector.
>>> query_params = { ... "scale.bar.coords.x": 0.5, ... "scale.bar.coords.y": '1.0', ... "scale.bar.coords.z": None, ... "scale.bar.coords.a": 123, ... "scale.bar.coords.b": True, ... "scale.bar.coords.c": False ... } >>> vector_from_parts( ... query_params, ... "scale.bar.coords", ... [ ... "scale.bar.coords.x", ... "scale.bar.coords.y", ... "scale.bar.coords.z", ... "scale.bar.coords.a", ... "scale.bar.coords.b", ... "scale.bar.coords.c", ... ] ... ) >>> query_params {'scale.bar.coords': "c(0.5,'1.0','__NULL__',123,'TRUE','FALSE')"}