2 """
3 Utility function for printing var values conveniently::
4 -- C{intro_string}: arbitrary label of type string. Will
5 be printed as prefix to the info below. Usually the name
6 of the function calling C{debug_print}, but can be the
7 empty string.
8 __ C{Vars}: A tuple of varnames, that is, a tuple of expressions
9 that evaluate to var names.
10 -- C{Env}: A dictionary of variable name value pairs, usually
11 by calling C{vars()}.
12 For example::
13
14 >>> var_printer.debug_print('scanner', ('goal_cat','index'),vars())
15
16 prints::
17
18 scanner
19 goal_cat: np
20 index : 0
21
22 if the values of the variables C{goal_cat} and C{index} in
23 environment C{vars()} are 'np' and '0' respectively. Note
24 the default indent of 3.
25 """
26
27 print '%s: ' % intro_string
28 print_vars(Env, Vars, indent)
29
31 """C{Env} is a dictionary containing bindings
32 for all the variables whose strings names are in the
33 sequence of strings C{Vars} (which may be a singleton string
34 as well). C{Env} is usually gotten by calling C{vars()}.
35 So a typical call::
36
37 >>> print_vars(vars(),('foo','foobar'))
38 foo : 'xxx'
39 foobar: 'yyyyy'
40
41 If C{Vars} is empty, all the keys of Env are printed.
42 """
43 if isinstance(Vars, str):
44 Vars=(Vars,)
45 elif not Vars:
46 Vars = Env.keys()
47 MinWidth=0
48 for Var in Vars:
49 if len(Var) > MinWidth:
50 MinWidth=len(Var)
51 for Var in Vars:
52
53
54
55 pstr = '%-*s: %%(%s)r' % (MinWidth,Var,Var)
56 print ' ' * Indent,
57 try:
58 print pstr % Env
59 except KeyError, Key:
60 raise NameError, "name '%s' is not defined" % Key
61