aerosandbox.tools.string_formatting =================================== .. py:module:: aerosandbox.tools.string_formatting Functions --------- .. autoapisummary:: aerosandbox.tools.string_formatting.eng_string aerosandbox.tools.string_formatting.latex_sci_notation_string aerosandbox.tools.string_formatting.hash_string aerosandbox.tools.string_formatting.trim_string aerosandbox.tools.string_formatting.has_balanced_parentheses aerosandbox.tools.string_formatting.wrap_text_ignoring_mathtext Module Contents --------------- .. py:function:: eng_string(x, unit = '', format='%.3g', si=True, add_space_after_number = None) Taken from: https://stackoverflow.com/questions/17973278/python-decimal-engineering-notation-for-mili-10e-3-and-micro-10e-6/40691220 Returns float/int value formatted in a simplified engineering format - using an exponent that is a multiple of 3. :param x: The value to be formatted. Float or int. :param unit: A unit of the quantity to be expressed, given as a string. Example: Newtons -> "N" :param format: A printf-style string used to format the value before the exponent. :param si: if true, use SI suffix for exponent. (k instead of e3, n instead of e-9, etc.) Examples: With format='%.2f': 1.23e-08 -> 12.30e-9 123 -> 123.00 1230.0 -> 1.23e3 -1230000.0 -> -1.23e6 With si=True: 1230.0 -> "1.23k" -1230000.0 -> "-1.23M" With unit="N" and si=True: 1230.0 -> "1.23 kN" -1230000.0 -> "-1.23 MN" .. py:function:: latex_sci_notation_string(x, format='%.2e') Converts a floating-point number to a LaTeX-style formatted string. Does not include the `$$` wrapping to put you in math mode. Does not use scientific notation if the base would be zero. .. rubric:: Examples latex_sci_notation_string(3000) -> '3 \times 10^{3}' .. py:function:: hash_string(string) Hashes a string into a quasi-random 32-bit integer! (Based on an MD5 checksum algorithm.) Usual warnings apply: it's MD5, don't use this for anything intended to be cryptographically secure. .. py:function:: trim_string(string, length = 80) Trims a string to be less than a given length. If the string would exceed the length, makes it end in ellipses ("…"). :param string: The string to be trimmed. :param length: The length to trim the string to, including any ellipses that may be added. Returns: The trimmed string, including ellipses if needed. .. py:function:: has_balanced_parentheses(string, left='(', right=')') Determines whether a string has matching parentheses or not. .. rubric:: Examples >>> has_balanced_parentheses("3 * (x + (2 ** 5))") -> True >>> has_balanced_parentheses("3 * (x + (2 ** 5)") -> False :param string: The string to be evaluated. :param left: The left parentheses. Can be modified if, for example, you need to check square brackets. :param right: The right parentheses. Can be modified if, for example, you need to check square brackets. Returns: A boolean of whether or not the string has balanced parentheses. .. py:function:: wrap_text_ignoring_mathtext(text, width = 70) Reformat the single paragraph in 'text' to fit in lines of no more than 'width' columns, and return a new string containing the entire wrapped paragraph. Tabs are expanded and other whitespace characters converted to space. Similar to `textwrap.fill`, but keeps any mathtext blocks contiguous and unaltered. Mathtext blocks are segments of `text` that are between $ markers, to indicate LaTeX-like formatting. Dollar-sign literals (\$) do not trigger Mathtext, and that is respected here as well. For example: >>> wrap_text_ignoring_mathtext() :param text: The text to be wrapped. :param width: The maximum width of wrapped lines (unless break_long_words is false) :returns: A string containing the entire paragraph with line breaks as newline ("\n") characters.