Parsing input

Parsing Functions Reference

sympy.parsing.sympy_parser.parse_expr(s, local_dict=None, rationalize=False, convert_xor=False)[source]

Converts the string s to a SymPy expression, in local_dict

Examples

>>> from sympy.parsing.sympy_parser import parse_expr
>>> parse_expr("1/2")
1/2
>>> type(_)
<class 'sympy.core.numbers.Half'>
sympy.parsing.sympy_tokenize.printtoken(type, token, srow_scol, erow_ecol, line)[source]
sympy.parsing.sympy_tokenize.tokenize(readline, tokeneater=<function printtoken at 0x10e0de758>)[source]

The tokenize() function accepts two parameters: one representing the input stream, and one providing an output mechanism for tokenize().

The first parameter, readline, must be a callable object which provides the same interface as the readline() method of built-in file objects. Each call to the function should return one line of input as a string.

The second parameter, tokeneater, must also be a callable object. It is called once for each token, with five arguments, corresponding to the tuples generated by generate_tokens().

sympy.parsing.sympy_tokenize.untokenize(iterable)[source]

Transform tokens back into Python source code.

Each element returned by the iterable must be a token sequence with at least two elements, a token number and token value. If only two tokens are passed, the resulting output is poor.

Round-trip invariant for full input:
Untokenized source will match input source exactly

Round-trip invariant for limited intput:

# Output text will tokenize the back to the input
t1 = [tok[:2] for tok in generate_tokens(f.readline)]
newcode = untokenize(t1)
readline = iter(newcode.splitlines(1)).next
t2 = [tok[:2] for tok in generate_tokens(readline)]
assert t1 == t2
sympy.parsing.sympy_tokenize.generate_tokens(readline)[source]

The generate_tokens() generator requires one argment, readline, which must be a callable object which provides the same interface as the readline() method of built-in file objects. Each call to the function should return one line of input as a string. Alternately, readline can be a callable function terminating with StopIteration:

readline = open(myfile).next    # Example of alternate readline

The generator produces 5-tuples with these members: the token type; the token string; a 2-tuple (srow, scol) of ints specifying the row and column where the token begins in the source; a 2-tuple (erow, ecol) of ints specifying the row and column where the token ends in the source; and the line on which the token was found. The line passed is the logical line; continuation lines are included.

sympy.parsing.sympy_tokenize.group(*choices)[source]
sympy.parsing.sympy_tokenize.any(*choices)[source]
sympy.parsing.sympy_tokenize.maybe(*choices)[source]
sympy.parsing.maxima.parse_maxima(str, globals=None, name_dict={})[source]
sympy.parsing.mathematica.mathematica(s)[source]

Parsing Exceptions Reference

class sympy.parsing.sympy_tokenize.TokenError[source]
class sympy.parsing.sympy_tokenize.StopTokenizing[source]

Table Of Contents

Previous topic

Timing Utilities

Next topic

Physics Module

This Page