tostr

(function from pyomo.common.formatting)

pyomo.common.formatting.tostr(value, quote_str=False)[source]

Convert a value to a string

This function is a thin wrapper around str(value) to resolve a problematic __str__ implementation in the standard Python container types (tuple, list, and dict). Those classes implement __str__ the same as __repr__ (by calling repr() on each contained object). That is frequently undesirable, as you may wish the string representation of a container to contain the string representations of the contained objects.

This function generates string representations for native Python containers (tuple, list, and dict) that contains the string representations of the contained objects. In addition, it also applies the same special handling to any types that derive from the standard containers without overriding either __repn__ or __str__.

Parameters:
  • value (object) – the object to convert to a string

  • quote_str (bool) – if True, and if value is a str, then return a “quoted string” (as generated by repr()). This is primarily used when recursively processing native Python containers.

Return type:

str