Kaydet (Commit) 1b1498b4 authored tarafından Collin Winter's avatar Collin Winter

Idiom adjustment in the docs for the parser module.

üst d1d9a890
...@@ -474,19 +474,17 @@ representation to be ``['variable_name']``. A simple recursive function can ...@@ -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:: ...@@ -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"""'}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment