def __str__(self):
def sort_key_variable(item):<EOL><INDENT>key, _ = item<EOL>is_product = isinstance(key, Product)<EOL>return is_product, text_type(key)<EOL><DEDENT>def all_terms():<EOL><INDENT>count_vars = <NUM_LIT:0><EOL>for name, value in sorted(<EOL>iteritems(self._variables), key=sort_key_variable):<EOL><INDENT>if value != <NUM_LIT:0>:<EOL><INDENT>count_vars += <NUM_LIT:1><EOL>if isinstance(name, VariableSet):<EOL><INDENT>yield '<STR_LIT>', value<EOL><DEDENT>elif isinstance(name, Product):<EOL><INDENT>yield '<STR_LIT:*>'.join(text_type(n) for n in name), value<EOL><DEDENT>else:<EOL><INDENT>yield name, value<EOL><DEDENT><DEDENT><DEDENT>if self._offset != <NUM_LIT:0> or count_vars == <NUM_LIT:0>:<EOL><INDENT>yield None, self._offset<EOL><DEDENT><DEDENT>terms = []<EOL>for i, (name, value) in enumerate(all_terms()):<EOL><INDENT>if i == <NUM_LIT:0>:<EOL><INDENT>if name is None:<EOL><INDENT>terms.append('<STR_LIT:{}>'.format(value))<EOL><DEDENT>elif abs(value) == <NUM_LIT:1>:<EOL><INDENT>terms.append(<EOL>text_type(name) if value > <NUM_LIT:0> else '<STR_LIT>'.format(name))<EOL><DEDENT>else:<EOL><INDENT>terms.append('<STR_LIT>'.format(value, name))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>prefix = '<STR_LIT:+>' if value >= <NUM_LIT:0> else '<STR_LIT:->'<EOL>if name is None:<EOL><INDENT>terms.append('<STR_LIT>'.format(prefix, abs(value)))<EOL><DEDENT>elif abs(value) == <NUM_LIT:1>:<EOL><INDENT>terms.append('<STR_LIT>'.format(prefix, name))<EOL><DEDENT>else:<EOL><INDENT>terms.append('<STR_LIT>'.format(prefix, abs(value), name))<EOL><DEDENT><DEDENT><DEDENT>return '<STR_LIT:U+0020>'.join(terms)<EOL>
Return string representation of expression