Commit c5dac6f3 authored by Guido van Rossum's avatar Guido van Rossum

Final set of changes by Fred before 1.4beta3

parent 6e07c097
...@@ -3,6 +3,10 @@ parser.dvi: parser.tex ../../Doc/libparser.tex ...@@ -3,6 +3,10 @@ parser.dvi: parser.tex ../../Doc/libparser.tex
# Use a new name for this; the included file uses 'clean' already.... # Use a new name for this; the included file uses 'clean' already....
clean-parser: clean-parser:
rm -f *.log *.aux *.dvi *.pyc rm -f *.log *.aux *.dvi *.pyc *.ps
dist:
(cd ../..; \
tar cf - `cat Demo/parser/FILES` | gzip >parsermodule-1.4.tar.gz)
include ../../Doc/Makefile include ../../Doc/Makefile
...@@ -4,12 +4,29 @@ to the Python Library Reference for more information. ...@@ -4,12 +4,29 @@ to the Python Library Reference for more information.
Files: Files:
------ ------
FILES -- list of files associated with the parser module.
README -- this file.
example.py -- module that uses the `parser' module to extract example.py -- module that uses the `parser' module to extract
information from the parse tree of Python source information from the parse tree of Python source
code. code.
docstring.py -- sample source file containing only a module docstring.
simple.py -- sample source containing a "short form" definition.
source.py -- sample source code used to demonstrate ability to source.py -- sample source code used to demonstrate ability to
handle nested constructs easily using the functions handle nested constructs easily using the functions
and classes in example.py. and classes in example.py.
pprint.py -- function to pretty-print Python values.
test_parser.py program to put the parser module through it's paces.
parser.tex -- LaTex driver file for formatting the parser module
documentation separately from the library reference.
Makefile -- `make' rule set to format the parser module manual.
Enjoy! Enjoy!
"""Simple code to extract class & function docstrings from a module. """Simple code to extract class & function docstrings from a module.
This code is used as an example in the library reference manual in the
section on using the parser module. Refer to the manual for a thorough
discussion of the operation of this code.
""" """
import symbol import symbol
...@@ -23,12 +25,35 @@ def get_docs(fileName): ...@@ -23,12 +25,35 @@ def get_docs(fileName):
return ModuleInfo(tup, basename) return ModuleInfo(tup, basename)
class DefnInfo: class SuiteInfoBase:
_docstring = '' _docstring = ''
_name = '' _name = ''
def __init__(self, tree): def __init__(self, tree = None):
self._name = tree[2][1] self._class_info = {}
self._function_info = {}
if tree:
self._extract_info(tree)
def _extract_info(self, tree):
# extract docstring
if len(tree) == 2:
found, vars = match(DOCSTRING_STMT_PATTERN[1], tree[1])
else:
found, vars = match(DOCSTRING_STMT_PATTERN, tree[3])
if found:
self._docstring = eval(vars['docstring'])
# discover inner definitions
for node in tree[1:]:
found, vars = match(COMPOUND_STMT_PATTERN, node)
if found:
cstmt = vars['compound']
if cstmt[0] == symbol.funcdef:
name = cstmt[2][1]
self._function_info[name] = FunctionInfo(cstmt)
elif cstmt[0] == symbol.classdef:
name = cstmt[2][1]
self._class_info[name] = ClassInfo(cstmt)
def get_docstring(self): def get_docstring(self):
return self._docstring return self._docstring
...@@ -36,38 +61,21 @@ class DefnInfo: ...@@ -36,38 +61,21 @@ class DefnInfo:
def get_name(self): def get_name(self):
return self._name return self._name
class SuiteInfoBase(DefnInfo):
def __init__(self):
self._class_info = {}
self._function_info = {}
def get_class_names(self): def get_class_names(self):
return self._class_info.keys() return self._class_info.keys()
def get_class_info(self, name): def get_class_info(self, name):
return self._class_info[name] return self._class_info[name]
def _extract_info(self, tree): def __getitem__(self, name):
if len(tree) >= 4: try:
found, vars = match(DOCSTRING_STMT_PATTERN, tree[3]) return self._class_info[name]
if found: except KeyError:
self._docstring = eval(vars['docstring']) return self._function_info[name]
for node in tree[1:]:
if (node[0] == symbol.stmt
and node[1][0] == symbol.compound_stmt): class SuiteFuncInfo:
if node[1][1][0] == symbol.funcdef: # Mixin class providing access to function names and info.
name = node[1][1][2][1]
self._function_info[name] = \
FunctionInfo(node[1][1])
elif node[1][1][0] == symbol.classdef:
name = node[1][1][2][1]
self._class_info[name] = ClassInfo(node[1][1])
class SuiteInfo(SuiteInfoBase):
def __init__(self, tree):
SuiteInfoBase.__init__(self)
self._extract_info(tree)
def get_function_names(self): def get_function_names(self):
return self._function_info.keys() return self._function_info.keys()
...@@ -76,23 +84,16 @@ class SuiteInfo(SuiteInfoBase): ...@@ -76,23 +84,16 @@ class SuiteInfo(SuiteInfoBase):
return self._function_info[name] return self._function_info[name]
class FunctionInfo(SuiteInfo): class FunctionInfo(SuiteInfoBase, SuiteFuncInfo):
def __init__(self, tree): def __init__(self, tree = None):
DefnInfo.__init__(self, tree) self._name = tree[2][1]
suite = tree[-1] SuiteInfoBase.__init__(self, tree and tree[-1] or None)
if len(suite) >= 4:
found, vars = match(DOCSTRING_STMT_PATTERN, suite[3])
if found:
self._docstring = eval(vars['docstring'])
SuiteInfoBase.__init__(self)
self._extract_info(suite)
class ClassInfo(SuiteInfoBase): class ClassInfo(SuiteInfoBase):
def __init__(self, tree): def __init__(self, tree = None):
SuiteInfoBase.__init__(self) self._name = tree[2][1]
DefnInfo.__init__(self, tree) SuiteInfoBase.__init__(self, tree and tree[-1] or None)
self._extract_info(tree[-1])
def get_method_names(self): def get_method_names(self):
return self._function_info.keys() return self._function_info.keys()
...@@ -101,19 +102,40 @@ class ClassInfo(SuiteInfoBase): ...@@ -101,19 +102,40 @@ class ClassInfo(SuiteInfoBase):
return self._function_info[name] return self._function_info[name]
class ModuleInfo(SuiteInfo): class ModuleInfo(SuiteInfoBase, SuiteFuncInfo):
def __init__(self, tree, name="<string>"): def __init__(self, tree = None, name = "<string>"):
self._name = name self._name = name
SuiteInfo.__init__(self, tree) SuiteInfoBase.__init__(self, tree)
found, vars = match(DOCSTRING_STMT_PATTERN, tree[1]) if tree:
if found: found, vars = match(DOCSTRING_STMT_PATTERN, tree[1])
self._docstring = vars["docstring"] if found:
self._docstring = vars["docstring"]
from types import ListType, TupleType from types import ListType, TupleType
def match(pattern, data, vars=None): def match(pattern, data, vars=None):
""" """Match `data' to `pattern', with variable extraction.
pattern
Pattern to match against, possibly containing variables.
data
Data to be checked and against which variables are extracted.
vars
Dictionary of variables which have already been found. If not
provided, an empty dictionary is created.
The `pattern' value may contain variables of the form ['varname'] which
are allowed to match anything. The value that is matched is returned as
part of a dictionary which maps 'varname' to the matched value. 'varname'
is not required to be a string object, but using strings makes patterns
and the code which uses them more readable.
This function returns two values: a boolean indicating whether a match
was found and a dictionary mapping variable names to their associated
values.
""" """
if vars is None: if vars is None:
vars = {} vars = {}
...@@ -131,6 +153,15 @@ def match(pattern, data, vars=None): ...@@ -131,6 +153,15 @@ def match(pattern, data, vars=None):
return same, vars return same, vars
# This pattern identifies compound statements, allowing them to be readily
# differentiated from simple statements.
#
COMPOUND_STMT_PATTERN = (
symbol.stmt,
(symbol.compound_stmt, ['compound'])
)
# This pattern will match a 'stmt' node which *might* represent a docstring; # This pattern will match a 'stmt' node which *might* represent a docstring;
# docstrings require that the statement which provides the docstring be the # docstrings require that the statement which provides the docstring be the
# first statement in the class or function, which this pattern does not check. # first statement in the class or function, which this pattern does not check.
......
# pprint.py # pprint.py
# #
# Author: Fred L. Drake, Jr. # Author: Fred L. Drake, Jr.
# fdrake@vt.edu # fdrake@cnri.reston.va.us, fdrake@intr.net
# #
# This is a simple little module I wrote to make life easier. I didn't # This is a simple little module I wrote to make life easier. I didn't
# see anything quite like it in the library, though I may have overlooked # see anything quite like it in the library, though I may have overlooked
...@@ -9,34 +9,28 @@ ...@@ -9,34 +9,28 @@
# tuples with fairly non-descriptive content. This is modelled very much # tuples with fairly non-descriptive content. This is modelled very much
# after Lisp/Scheme - style pretty-printing of lists. If you find it # after Lisp/Scheme - style pretty-printing of lists. If you find it
# useful, thank small children who sleep at night. # useful, thank small children who sleep at night.
#
"""Support to pretty-print lists, tuples, & dictionaries recursively. """Support to pretty-print lists, tuples, & dictionaries recursively.
Very simple, but at least somewhat useful, especially in debugging Very simple, but useful, especially in debugging data structures.
data structures.
Constants
INDENT_PER_LEVEL -- Amount of indentation to use for each new ---------
recursive level. The default is 1. This
must be a non-negative integer, and may be
set by the caller before calling pprint().
MAX_WIDTH -- Maximum width of the display. This is only
used if the representation *can* be kept
less than MAX_WIDTH characters wide. May
be set by the user before calling pprint().
TAB_WIDTH -- The width represented by a single tab. This
value is typically 8, but 4 is the default
under MacOS. Can be changed by the user if
desired, but is probably not a good idea.
pprint(seq [, stream]) -- The pretty-printer. This takes a Python
object (presumably a sequence, but that
doesn't matter) and an optional output
stream. See the function documentation
for details.
"""
INDENT_PER_LEVEL
Amount of indentation to use for each new recursive level. The
default is 1. This must be a non-negative integer, and may be set
by the caller before calling pprint().
MAX_WIDTH
Maximum width of the display. This is only used if the
representation *can* be kept less than MAX_WIDTH characters wide.
May be set by the user before calling pprint().
TAB_WIDTH
The width represented by a single tab. This value is typically 8,
but 4 is the default under MacOS. Can be changed by the user if
desired, but is probably not a good idea.
"""
INDENT_PER_LEVEL = 1 INDENT_PER_LEVEL = 1
...@@ -46,46 +40,45 @@ import os ...@@ -46,46 +40,45 @@ import os
TAB_WIDTH = (os.name == 'mac' and 4) or 8 TAB_WIDTH = (os.name == 'mac' and 4) or 8
del os del os
from types import DictType, ListType, TupleType
def _indentation(cols): def _indentation(cols):
"Create tabbed indentation string COLS columns wide." """Create tabbed indentation string.
# This is used to reduce the byte-count for the output, allowing
# files created using this module to use as little external storage
# as possible. This is primarily intended to minimize impact on
# a user's quota when storing resource files, or for creating output
# intended for transmission.
cols
Width of the indentation, in columns.
"""
return ((cols / TAB_WIDTH) * '\t') + ((cols % TAB_WIDTH) * ' ') return ((cols / TAB_WIDTH) * '\t') + ((cols % TAB_WIDTH) * ' ')
def pprint(seq, stream = None, indent = 0, allowance = 0): def pprint(seq, stream = None, indent = 0, allowance = 0):
"""Pretty-print a list, tuple, or dictionary. """Pretty-print a list, tuple, or dictionary.
pprint(seq [, stream]) ==> None seq
List, tuple, or dictionary object to be pretty-printed. Other
If STREAM is provided, output is written to that stream, otherwise object types are permitted by are not specially interpreted.
sys.stdout is used. Indentation is done according to
INDENT_PER_LEVEL, which may be set to any non-negative integer stream
before calling this function. The output written on the stream is Output stream. If not provided, `sys.stdout' is used. This
a perfectly valid representation of the Python object passed in, parameter must support the `write()' method with a single
with indentation to suite human-readable interpretation. The parameter, which will always be a string. It may be a
output can be used as input without error, given readable `StringIO.StringIO' object if the result is needed as a
representations of all sequence elements are available via repr(). string.
Output is restricted to MAX_WIDTH columns where possible. The
STREAM parameter must support the write() method with a single Indentation is done according to `INDENT_PER_LEVEL', which may be
parameter, which will always be a string. The output stream may be set to any non-negative integer before calling this function. The
a StringIO.StringIO object if the result is needed as a string. output written on the stream is a perfectly valid representation
of the Python object passed in, with indentation to assist
human-readable interpretation. The output can be used as input
without error, given readable representations of all elements are
available via `repr()'. Output is restricted to `MAX_WIDTH'
columns where possible.
""" """
if stream is None: if stream is None:
import sys import sys
stream = sys.stdout stream = sys.stdout
from types import DictType, ListType, TupleType
rep = `seq` rep = `seq`
typ = type(seq) typ = type(seq)
sepLines = len(rep) > (MAX_WIDTH - 1 - indent - allowance) sepLines = len(rep) > (MAX_WIDTH - 1 - indent - allowance)
...@@ -140,4 +133,4 @@ def pprint(seq, stream = None, indent = 0, allowance = 0): ...@@ -140,4 +133,4 @@ def pprint(seq, stream = None, indent = 0, allowance = 0):
# #
# end of pprint.py # end of file
def f(): "maybe a docstring"
This diff is collapsed.
This diff is collapsed.
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