Skip to main content

Strings

is_boolean_string(...)​

(string, values = None)​

Checks if a given string matches values defined as boolean. Custom values can be specified.

Parameters:string (str)
A string to compare to a list of string-values representing booleans.
values (list[str])
A list of string-values that serve as definition as boolean-values (for example ["Yes", "No"]) and are compared to the given string. By default the string is compared to ["true,"false"]
Returns:Whether the given string matches one of the values in the values-list.
Return Type:bool

get_boolean_from_string(...)​

(string, values = None)​

Checks if a given string matches values defined as boolean and returns the boolean-value, that the string is assigned to. Custom values can be specified.

Parameters:string (str)
A string to compare to a list of string-values representing booleans.
values (dict[str,list[str]])
A dictionary that map lists of string-values to the keys trueand false. The list of string-values serve as definitions for boolean-values (for example ["Yes", "Ja", "true"]) and are compared to the given string. By default the string is compared to the dictionary {"true": ["true","True","1",1], "false": ["false","False","0",0]}
Returns:A boolean value that the given string is assigned to.
Return Type:bool

string_to_int(...)​

(string:str, fallback=None)​

Convert a given string to an integer value. String has to consist of digits only.

Parameters:string (str)
A string representing a numeral that is convertable to integer.
fallback (Any)
Value that is returned as fallback, in case the given string can't be converted to integer. None by default.
Return Type:int|Any

string_to_float(...)​

(string:str, fallback=None)​

Convert a given string to an float value. String must be able to be converted to float.

Parameters:string (str)
A string representing a numeral that is convertable to float.
fallback (Any)
Value that is returned as fallback, in case the given string can't be converted to float. None by default.
Return Type:float|Any

string_to_bool(...)​

(string, fallback=None)​

Convert a given string to a boolean value. String has to consist of digits only.

Parameters:string (str)
A string consisting of digits only that are convertable to boolean (usually "0" or "1").
fallback (Any)
Value that is returned as fallback, in case the given string can't be converted to boolean. None by default.
Return Type:bool|Any

string_to_int_list(...)​

(string:str, separator=",")​

Convert a string to a list of integers by splitting the string at a given separator. Values that can't be converted to integer are omitted.

Parameters:string (str)
A string representing a list (for example commaseparated) of numerals that are convertable to integer.
separator (str)
String used as separator in the given list-string. Default separator is ","
Return Type:list[int]

get_gParam1_int()​

Shortcut to string_to_int with input comet.gParam1

Returns:The converted value of comet.gParam1 or None
Return Type:int|None

get_gParam2_int()​

Shortcut to string_to_int with input comet.gParam2

Returns:The converted value of comet.gParam2 or None
Return Type:int|None

get_gParam3_int()​

Shortcut to string_to_int with input comet.gParam3

Returns:The converted value of comet.gParam3 or None
Return Type:int|None

get_gParam4_int()​

Shortcut to string_to_int with input comet.gParam4

Returns:The converted value of comet.gParam4 or None
Return Type:int|None

get_gParam1_float()​

Shortcut to string_to_float with input comet.gParam1

Returns:The converted value of comet.gParam1 or None
Return Type:float|None

get_gParam2_float()​

Shortcut to string_to_float with input comet.gParam2

Returns:The converted value of comet.gParam2 or None
Return Type:float|None

get_gParam3_float()​

Shortcut to string_to_float with input comet.gParam3

Returns:The converted value of comet.gParam3 or None
Return Type:float|None

get_gParam4_float()​

Shortcut to string_to_float with input comet.gParam4

Returns:The converted value of comet.gParam4 or None
Return Type:float|None

get_gParam1_bool()​

Shortcut to string_to_bool with input comet.gParam1

Returns:The converted value of comet.gParam1 or None
Return Type:bool|None

get_gParam2_bool()​

Shortcut to string_to_bool with input comet.gParam2

Returns:The converted value of comet.gParam2 or None
Return Type:bool|None

get_gParam3_bool()​

Shortcut to string_to_bool with input comet.gParam3

Returns:The converted value of comet.gParam3 or None
Return Type:bool|None

get_gParam4_bool()​

Shortcut to string_to_bool with input comet.gParam4

Returns:The converted value of comet.gParam4 or None
Return Type:bool|None

get_gParam1_int_list()​

Shortcut to string_to_int_list with input comet.gParam1

Returns:The converted value of comet.gParam1
Return Type:list[int]

get_gParam2_int_list()​

Shortcut to string_to_int_list with input comet.gParam2

Returns:The converted value of comet.gParam2
Return Type:list[int]

get_gParam3_int_list()​

Shortcut to string_to_int_list with input comet.gParam3

Returns:The converted value of comet.gParam3
Return Type:list[int]

get_gParam4_int_list()​

Shortcut to string_to_int_list with input comet.gParam4

Returns:The converted value of comet.gParam4
Return Type:list[int]

value_from_string_id(...)​

( input_value, fallback_value )​

Retrieve a part of the comet.gRecordStringID based on the input-value's content. If the input-value matches the value of one, of the FVAR-constants in constants.py, it is replaced by the corresponding value from the string-ID. Otherwise the fallback-value is returned.

InputOutput
--string-id--comet.gRecordStringID
--roottable-id--comet.gRecordStringID1
--col-id--comet.gRecordStringID2
--row-id--comet.gRecordStringID3
elsefallback-value
Parameters:_input_value (str)
A string-value that may match a value of one of the FVAR-constants from constants.py
fallback_value (str)
Returns:Value or part of comet.gRecordStringID or fallback-value.
Return Type:str

resolve_doc_attributes(...)​

(string)​

Searches for values in a string that are enclosed in variable-markers like this: ${my_variable}
The found values are used as keys to retrieve values of document-attributes which replace the variables in the string.

Parameters:string (str)
Any string. May include variable-markers by the pattern ${my_variable}, representing keys to document-attributes.
Returns:A string where variables have been replaced by values of document-attributes.
Return Type:str

clean_function_variable(...)​

(string)​

Replaces encoded angled brackets / "greater and lesser than" in a given string. This is especially useful in strings of function variables, as Comet might encode the values which makes it necessary to do some replacements.
For example this would change "<0xEF11>0xEF11<0xEF12>" to "<"

Parameters:string (str)
Any string (usually retrieved from a function variable).
Returns:A string with replaced angled brackets.
Return Type:str

get_formatted_xml(...)​

(xml_string)​

Format an XML-string (linebreaks and indentation) using minidom's function toprettyxml.

Parameters:xml_string (str)
A string representing valid xml.
Returns:Formatted XML-string
Return Type:str

tidy_xhtml(...)​

(data, skip_tags=None, keep_attributes=True)​

Parse the given string as HTML and tidy its elements (for example close all tagsof orphaned tags).

Parameters:data (str)
An HTML-string to be tidied to valid xhtml.
skip_tags (list[str])
A list of tags to skip when parsing the given data.
keep_attributes (bool)
Whether to keep or remove attributes from the html-elements.
Returns:Formatted XHTML-string
Return Type:str

text_to_tagged_text(...)​

(value, prefix = "%!TT", cleanse=True, tidy=True)​

Converts a given string to tagged text based on the specified prefix, if the string is not empty.

Parameters:value (str)
A string that should be converted to TaggedText.
prefix (str)
One of the valid prefixes. Default is %!TT.
cleanse (bool)
Remove and replace certain contents from a string, that may cause problems in TaggedText.
tidy (bool)
Parse the given value as HTML and tidy its elements before converting to TaggedText.
Returns:Converted string or emtpty string
Return Type:str

html_apply_charstyle(...)​

(value, char_style)​

Applies a character-style to a HTML-string by wrapping the string in a span-element with the given character-style as class.

Parameters:value (str)
A string that can be wrapped in a HTML-span-element.
char_style (str)
A valid name of a characterstyle in the document.
Returns:Converted string or original string.
Return Type:str

html_apply_color(...)​

(value, color)​

Applies a color to a HTML-string by wrapping the string in a span-element with the given color as css-style-property.

Parameters:value (str)
A string that can be wrapped in a HTML-span-element.
char_style (str)
A valid name of a color in the document or valid color-values.
Returns:Converted string or original string.
Return Type:str

replace_size_in_tt(...)​

(input_tt, size, leading = None)​

Replace the fontsize (and leading-size) in TaggedText matching: "<ParaStyle:{X}><cSize:{N}>".

Parameters:input_tt (str)
A TaggedText-string.
size (float)
New fontsize in points (Pt) as float.
leading (float)
New leading in points (Pt) as float. (optional)
Returns:TaggedText-String.
Return Type:str

(text, pagenum, name = "")​

Creates a TaggedText-string with a hyperlink to a document page on a given text.

Parameters:text (str)
The text on which the hyperlink should be created.
pagenum (str)
A string of a pagenumber belonging to the current document.
name (str)
A name to be used for the hyperlink. If no name is given, the original text is used as name.
Return Type:str

(url, text = "", name = "")​

Creates a TaggedText-string with a hyperlink to an URL on a given text.

Parameters:url (str)
The URL that is used as target by the hyperlink.
text (str)
The text on which the hyperlink should be created.
name (str)
A name to be used for the hyperlink. If no name is given, the original text is used as name.
Return Type:str

(html)​

Finds all anchor-tags in an HTML-string and replaces them with TaggedText-tags for hyperlinks.

Parameters:html (str)
An HTML-string that may contain a-tags (anchors).
Returns:String with escaped TaggedText-hyperlinks.
Return Type:str

truncate_float(...)​

(string = "", decimals = 0, strip_value = True)​

Truncates a string-representation of a float-value to the amount of specified decimals.
By default the value is rounded and stripped of trailing zeros.
Stripping example:
123.45 -> 123.45
123.40 -> 123.4
123.00 -> 123

Parameters:string (str)
A string-representation of a float-value.
decimals (int)
How many decimals should the result have?
strip_value (bool)
Should trailing zeros after the decimal point be stripped off the value?
Returns:A truncated number-string or original string, if truncation did not work.
Return Type:str

format_string_number(...)​

(number_str, separator_thousand = ",", separator_decimal = ".")​

Adds the specified separators to a number-string.

Parameters:number_str (str)
A string representing an integer- or float-value.
separator_thousand (str)
Separator for thousand-parts of a number: 9,999,999 or 9.999.999
separator_decimal (str)
Separator for decimal-part of a number: 9.999 or 9,999
Returns:String with added / replaced separators.
Return Type:str

format_numeric_string(...)​

(value, options = None)​

Format a numeric string based on given options

Parameters:value (str)
A string representing a number that is convertable to integer.
options (dict)
A dictionary with formatting options. Supported key-values: decimals (int), strip_trailing_zeros (bool), numeric_separators (tuple[str,str])
Returns:String with ordinal-suffix appended. Original string if it can't be converted to an integer.
Return Type:str

get_decimal_places(...)​

(s)​

Finds the amount of decimals after "." in a number-string.

Parameters:number_str (str)
A string representing a number.
Returns:The amount of decimals after "." in the given string.
Return Type:int

get_ordinal_of_number(...)​

(number_str, lang = "en")​

Convert a string consisting of numerals only to its ordinal representation by appending an "ordinal suffix" (ususally ".").

Parameters:number_str (str)
A string representing a number that is convertable to integer.
lang (str)
In case value is "en", these suffixes are appended to numbers:
"st", "nd", "rd", "th" -> 1st, 2nd, 3rd, 4th, ...
Returns:String with ordinal-suffix appended. Original string if it can't be converted to an integer.
Return Type:str