Idiom adjustment in the docs for the parser module.
This commit is contained in:
parent
d1d9a890eb
commit
1b1498b42e
@ -474,19 +474,17 @@ representation to be ``['variable_name']``. A simple recursive function can
|
|||||||
implement the pattern matching, returning a Boolean and a dictionary of variable
|
implement the pattern matching, returning a Boolean and a dictionary of variable
|
||||||
name to value mappings. (See file :file:`example.py`.) ::
|
name to value mappings. (See file :file:`example.py`.) ::
|
||||||
|
|
||||||
from types import ListType, TupleType
|
|
||||||
|
|
||||||
def match(pattern, data, vars=None):
|
def match(pattern, data, vars=None):
|
||||||
if vars is None:
|
if vars is None:
|
||||||
vars = {}
|
vars = {}
|
||||||
if type(pattern) is ListType:
|
if isinstance(pattern, list):
|
||||||
vars[pattern[0]] = data
|
vars[pattern[0]] = data
|
||||||
return 1, vars
|
return True, vars
|
||||||
if type(pattern) is not TupleType:
|
if not instance(pattern, tuple):
|
||||||
return (pattern == data), vars
|
return (pattern == data), vars
|
||||||
if len(data) != len(pattern):
|
if len(data) != len(pattern):
|
||||||
return 0, vars
|
return False, vars
|
||||||
for pattern, data in map(None, pattern, data):
|
for pattern, data in zip(pattern, data):
|
||||||
same, vars = match(pattern, data, vars)
|
same, vars = match(pattern, data, vars)
|
||||||
if not same:
|
if not same:
|
||||||
break
|
break
|
||||||
@ -528,7 +526,7 @@ docstring from the parse tree created previously is easy::
|
|||||||
|
|
||||||
>>> found, vars = match(DOCSTRING_STMT_PATTERN, tup[1])
|
>>> found, vars = match(DOCSTRING_STMT_PATTERN, tup[1])
|
||||||
>>> found
|
>>> found
|
||||||
1
|
True
|
||||||
>>> vars
|
>>> vars
|
||||||
{'docstring': '"""Some documentation.\n"""'}
|
{'docstring': '"""Some documentation.\n"""'}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user