Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d7b2999

Browse files
committed
Tidied boilerplate a little. Seems more python3 compatible (although untested).
1 parent 79170fa commit d7b2999

File tree

2 files changed

+107
-113
lines changed

2 files changed

+107
-113
lines changed

‎boilerplate.py‎

Lines changed: 45 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,11 @@
1919
import os
2020
import inspect
2121
import random
22-
import re
23-
import sys
2422
import types
2523

2624
# import the local copy of matplotlib, not the installed one
2725
#sys.path.insert(0, './lib')
2826
from matplotlib.axes import Axes
29-
from matplotlib.cbook import dedent
3027

3128

3229
# this is the magic line that must exist in pyplot, after which the boilerplate content will be
@@ -37,13 +34,12 @@
3734
'matplotlib', 'pyplot.py')
3835

3936

37+
AUTOGEN_MSG = """
38+
# This function was autogenerated by boilerplate.py. Do not edit as
39+
# changes will be lost"""
4040

41-
def boilerplate_gen():
42-
"""Generator of lines for the automated part of pyplot."""
4341

44-
_fmtplot = """
45-
# This function was autogenerated by boilerplate.py. Do not edit as
46-
# changes will be lost
42+
PLOT_TEMPLATE = AUTOGEN_MSG + """
4743
@autogen_docstring(Axes.%(func)s)
4844
def %(func)s(%(argspec)s):
4945
%(ax)s = gca()
@@ -59,17 +55,38 @@ def %(func)s(%(argspec)s):
5955
%(ax)s.hold(%(washold)s)
6056
%(mappable)s
6157
return %(ret)s
62-
"""
63-
64-
_fmtmisc="""
65-
# This function was autogenerated by boilerplate.py. Do not edit as
66-
# changes will be lost
58+
"""
59+
60+
61+
# Used for misc functions such as cla/legend etc.
62+
MISC_FN_TEMPLATE=AUTOGEN_MSG+"""
6763
@docstring.copy_dedent(Axes.%(func)s)
6864
def %(func)s(%(argspec)s):
6965
%(ret)s = gca().%(func)s(%(call)s)
7066
draw_if_interactive()
7167
return %(ret)s
72-
"""
68+
"""
69+
70+
71+
# Used for colormap functions
72+
CMAP_TEMPLATE = AUTOGEN_MSG + """
73+
def {name}():
74+
'''
75+
set the default colormap to {name} and apply to current image if any.
76+
See help(colormaps) for more information
77+
'''
78+
rc('image', cmap='{name}')
79+
im = gci()
80+
81+
if im is not None:
82+
im.set_cmap(cm.{name})
83+
draw_if_interactive()
84+
85+
"""
86+
87+
88+
def boilerplate_gen():
89+
"""Generator of lines for the automated part of pyplot."""
7390

7491
# these methods are all simple wrappers of Axes methods by the same
7592
# name.
@@ -168,17 +185,12 @@ def format_value(value):
168185
return '=mlab.' + value.func_name
169186
if value.func_name == 'mean':
170187
return '=np.' + value.func_name
171-
raise ValueError, ('default value %s unknown to boilerplate.formatvalue'
172-
% value)
188+
raise ValueError(('default value %s unknown to boilerplate.'+ \
189+
'formatvalue') % value)
173190
return '='+repr(value)
174191

175-
def remove_final_whitespace(string):
176-
"""
177-
Return a copy of *string* with final whitespace removed from each line.
178-
"""
179-
return '\n'.join(x.rstrip() for x in string.split('\n'))
180-
181-
for fmt,cmdlist in (_fmtplot,_plotcommands),(_fmtmisc,_misccommands):
192+
for fmt, cmdlist in [(PLOT_TEMPLATE, _plotcommands),
193+
(MISC_FN_TEMPLATE, _misccommands)]:
182194
for func in cmdlist:
183195
# For some commands, an additional line is needed to set the
184196
# color map
@@ -201,13 +213,13 @@ def remove_final_whitespace(string):
201213
call.append('**'+varkw)
202214
call = ', '.join(call)
203215

204-
# Add a hold keyword argument if needed (fmt is _fmtplot) and
216+
# Add a hold keyword argument if needed (fmt is PLOT_TEMPLATE) and
205217
# possible (if *args is used, we can't just add a hold
206218
# argument in front of it since it would gobble one of the
207219
# arguments the user means to pass via *args)
208220
if varargs:
209221
sethold = "hold = %(varkw)s.pop('hold', None)" % locals()
210-
elif fmt is _fmtplot:
222+
elif fmt is PLOT_TEMPLATE:
211223
args.append('hold')
212224
defaults = defaults + (None,)
213225
sethold = ''
@@ -219,7 +231,7 @@ def remove_final_whitespace(string):
219231

220232
# A gensym-like facility in case some function takes an
221233
# argument named washold, ax, or ret
222-
washold,ret,ax = 'washold', 'ret', 'ax'
234+
washold,ret,ax = 'washold', 'ret', 'ax'
223235
bad = set(args) | set((varargs, varkw))
224236
while washold in bad or ret in bad or ax in bad:
225237
washold = 'washold' + str(random.randrange(10**12))
@@ -230,29 +242,11 @@ def remove_final_whitespace(string):
230242
# bail out if they are used as argument names
231243
for reserved in ('gca', 'gci', 'draw_if_interactive'):
232244
if reserved in bad:
233-
raise ValueError, \
234-
'Axes method %s has kwarg named %s' % (func, reserved)
235-
236-
yield remove_final_whitespace(fmt%locals())
237-
238-
# define the colormap functions
239-
_fmtcmap = """
240-
# This function was autogenerated by boilerplate.py. Do not edit as
241-
# changes will be lost
242-
def %(name)s():
243-
'''
244-
set the default colormap to %(name)s and apply to current image if any.
245-
See help(colormaps) for more information
246-
'''
247-
rc('image', cmap='%(name)s')
248-
im = gci()
249-
250-
if im is not None:
251-
im.set_cmap(cm.%(name)s)
252-
draw_if_interactive()
253-
254-
"""
245+
msg = 'Axes method %s has kwarg named %s' % (func, reserved)
246+
raise ValueError(msg)
255247

248+
yield fmt % locals()
249+
256250
cmaps = (
257251
'autumn',
258252
'bone',
@@ -272,22 +266,22 @@ def %(name)s():
272266
)
273267
# add all the colormaps (autumn, hsv, ....)
274268
for name in cmaps:
275-
yield _fmtcmap%locals()
269+
yield CMAP_TEMPLATE.format(name=name)
276270

277271

278272
def build_pyplot():
279273
pyplot_path = os.path.join(os.path.dirname(__file__), 'lib',
280274
'matplotlib', 'pyplot.py')
281275

282-
pyplot_orig = file(pyplot_path, 'r').readlines()
276+
pyplot_orig = open(pyplot_path, 'r').readlines()
283277

284278

285279
try:
286280
pyplot_orig = pyplot_orig[:pyplot_orig.index(PYPLOT_MAGIC_HEADER)+1]
287281
except IndexError:
288282
raise ValueError('The pyplot.py file *must* have the exact line: %s' % PYPLOT_MAGIC_HEADER)
289283

290-
pyplot = file(pyplot_path, 'w')
284+
pyplot = open(pyplot_path, 'w')
291285
pyplot.writelines(pyplot_orig)
292286
pyplot.write('\n')
293287

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /