aerosandbox.tools.string_formatting#

Module Contents#

Functions#

eng_string(x[, unit, format, si, add_space_after_number])

Taken from: https://stackoverflow.com/questions/17973278/python-decimal-engineering-notation-for-mili-10e-3-and-micro-10e-6/40691220

latex_sci_notation_string(x[, format])

Converts a floating-point number to a LaTeX-style formatted string. Does not include the $$ wrapping to put you in math mode.

hash_string(string)

Hashes a string into a quasi-random 32-bit integer! (Based on an MD5 checksum algorithm.)

trim_string(string[, length])

Trims a string to be less than a given length. If the string would exceed the length, makes it end in ellipses ("…").

has_balanced_parentheses(string[, left, right])

Determines whether a string has matching parentheses or not.

wrap_text_ignoring_mathtext(text[, width])

Reformat the single paragraph in 'text' to fit in lines of no more

aerosandbox.tools.string_formatting.eng_string(x, unit='', format='%.3g', si=True, add_space_after_number=None)[source]#

Taken from: https://stackoverflow.com/questions/17973278/python-decimal-engineering-notation-for-mili-10e-3-and-micro-10e-6/40691220

Returns float/int value <x> formatted in a simplified engineering format - using an exponent that is a multiple of 3.

Parameters:
  • x (float) – The value to be formatted. Float or int.

  • unit (str) – A unit of the quantity to be expressed, given as a string. Example: Newtons -> “N”

  • format – A printf-style string used to format the value before the exponent.

  • si – if true, use SI suffix for exponent. (k instead of e3, n instead of e-9, etc.)

  • add_space_after_number (bool) –

Return type:

str

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”

aerosandbox.tools.string_formatting.latex_sci_notation_string(x, format='%.2e')[source]#

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.

Examples

latex_sci_notation_string(3000) -> ‘3 times 10^{3}’

Parameters:

x (float) –

Return type:

str

aerosandbox.tools.string_formatting.hash_string(string)[source]#

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.

Parameters:

string (str) –

Return type:

int

aerosandbox.tools.string_formatting.trim_string(string, length=80)[source]#

Trims a string to be less than a given length. If the string would exceed the length, makes it end in ellipses (”…”).

Parameters:
  • string (str) – The string to be trimmed.

  • length (int) – The length to trim the string to, including any ellipses that may be added.

Return type:

str

Returns: The trimmed string, including ellipses if needed.

aerosandbox.tools.string_formatting.has_balanced_parentheses(string, left='(', right=')')[source]#

Determines whether a string has matching parentheses or not.

Examples

>>> has_balanced_parentheses("3 * (x + (2 ** 5))") -> True
>>> has_balanced_parentheses("3 * (x + (2 ** 5)") -> False
Parameters:
  • string (str) – The string to be evaluated.

  • left – The left parentheses. Can be modified if, for example, you need to check square brackets.

  • right – The right parentheses. Can be modified if, for example, you need to check square brackets.

Return type:

bool

Returns: A boolean of whether or not the string has balanced parentheses.

aerosandbox.tools.string_formatting.wrap_text_ignoring_mathtext(text, width=70)[source]#

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()
Parameters:
  • text (str) – The text to be wrapped.

  • width (int) – 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.

Return type:

str