3
3
4
4
When this script is run, the current contents of pyplot are
5
5
split into generatable and non-generatable content (via the magic header
6
- :attr:`PYPLOT_MAGIC_HEADER`) and the generatable content is overwritten.
6
+ :attr:`PYPLOT_MAGIC_HEADER`) and the generatable content is overwritten.
7
7
Hence, the non-generatable content should be edited in the pyplot.py file
8
- itself, whereas the generatable content must be edited via templates in
8
+ itself, whereas the generatable content must be edited via templates in
9
9
this file.
10
10
11
11
"""
30
30
# appended
31
31
PYPLOT_MAGIC_HEADER = '################# REMAINING CONTENT GENERATED BY boilerplate.py ##############\n '
32
32
33
- PYPLOT_PATH = os .path .join (os .path .dirname (__file__ ), 'lib' ,
33
+ PYPLOT_PATH = os .path .join (os .path .dirname (__file__ ), 'lib' ,
34
34
'matplotlib' , 'pyplot.py' )
35
35
36
36
@@ -87,7 +87,7 @@ def {name}():
87
87
88
88
def boilerplate_gen ():
89
89
"""Generator of lines for the automated part of pyplot."""
90
-
90
+
91
91
# these methods are all simple wrappers of Axes methods by the same
92
92
# name.
93
93
_plotcommands = (
@@ -140,7 +140,7 @@ def boilerplate_gen():
140
140
'xcorr' ,
141
141
'barbs' ,
142
142
)
143
-
143
+
144
144
_misccommands = (
145
145
'cla' ,
146
146
'grid' ,
@@ -154,7 +154,7 @@ def boilerplate_gen():
154
154
'margins' ,
155
155
'autoscale' ,
156
156
)
157
-
157
+
158
158
cmappable = {
159
159
'contour' : 'if %(ret)s._A is not None: sci(%(ret)s)' ,
160
160
'contourf' : 'if %(ret)s._A is not None: sci(%(ret)s)' ,
@@ -171,24 +171,24 @@ def boilerplate_gen():
171
171
'tricontour' : 'if %(ret)s._A is not None: sci(%(ret)s)' ,
172
172
'tricontourf' : 'if %(ret)s._A is not None: sci(%(ret)s)' ,
173
173
'tripcolor' : 'sci(%(ret)s)' ,
174
-
174
+
175
175
}
176
-
176
+
177
177
def format_value (value ):
178
178
"""
179
179
Format function default values as needed for inspect.formatargspec.
180
180
The interesting part is a hard-coded list of functions used
181
181
as defaults in pyplot methods.
182
182
"""
183
183
if isinstance (value , types .FunctionType ):
184
- if value .func_name in ('detrend_none' , 'window_hanning' ):
185
- return '=mlab.' + value .func_name
186
- if value .func_name == 'mean' :
187
- return '=np.' + value .func_name
184
+ if value .__name__ in ('detrend_none' , 'window_hanning' ):
185
+ return '=mlab.' + value .__name__
186
+ if value .__name__ == 'mean' :
187
+ return '=np.' + value .__name__
188
188
raise ValueError (('default value %s unknown to boilerplate.' + \
189
189
'formatvalue' ) % value )
190
190
return '=' + repr (value )
191
-
191
+
192
192
for fmt , cmdlist in [(PLOT_TEMPLATE , _plotcommands ),
193
193
(MISC_FN_TEMPLATE , _misccommands )]:
194
194
for func in cmdlist :
@@ -198,21 +198,21 @@ def format_value(value):
198
198
mappable = cmappable [func ] % locals ()
199
199
else :
200
200
mappable = ''
201
-
201
+
202
202
# Get argspec of wrapped function
203
203
args , varargs , varkw , defaults = inspect .getargspec (getattr (Axes , func ))
204
204
args .pop (0 ) # remove 'self' argument
205
205
if defaults is None :
206
206
defaults = ()
207
-
207
+
208
208
# How to call the wrapped function
209
- call = map (str , args )
209
+ call = list ( map (str , args ) )
210
210
if varargs is not None :
211
211
call .append ('*' + varargs )
212
212
if varkw is not None :
213
213
call .append ('**' + varkw )
214
214
call = ', ' .join (call )
215
-
215
+
216
216
# Add a hold keyword argument if needed (fmt is PLOT_TEMPLATE) and
217
217
# possible (if *args is used, we can't just add a hold
218
218
# argument in front of it since it would gobble one of the
@@ -223,12 +223,12 @@ def format_value(value):
223
223
args .append ('hold' )
224
224
defaults = defaults + (None ,)
225
225
sethold = ''
226
-
226
+
227
227
# Now we can build the argspec for defining the wrapper
228
228
argspec = inspect .formatargspec (args , varargs , varkw , defaults ,
229
229
formatvalue = format_value )
230
230
argspec = argspec [1 :- 1 ] # remove parens
231
-
231
+
232
232
# A gensym-like facility in case some function takes an
233
233
# argument named washold, ax, or ret
234
234
washold , ret , ax = 'washold' , 'ret' , 'ax'
@@ -237,16 +237,16 @@ def format_value(value):
237
237
washold = 'washold' + str (random .randrange (10 ** 12 ))
238
238
ret = 'ret' + str (random .randrange (10 ** 12 ))
239
239
ax = 'ax' + str (random .randrange (10 ** 12 ))
240
-
240
+
241
241
# Since we can't avoid using some function names,
242
242
# bail out if they are used as argument names
243
243
for reserved in ('gca' , 'gci' , 'draw_if_interactive' ):
244
244
if reserved in bad :
245
245
msg = 'Axes method %s has kwarg named %s' % (func , reserved )
246
246
raise ValueError (msg )
247
-
247
+
248
248
yield fmt % locals ()
249
-
249
+
250
250
cmaps = (
251
251
'autumn' ,
252
252
'bone' ,
@@ -270,24 +270,24 @@ def format_value(value):
270
270
271
271
272
272
def build_pyplot ():
273
- pyplot_path = os .path .join (os .path .dirname (__file__ ), 'lib' ,
273
+ pyplot_path = os .path .join (os .path .dirname (__file__ ), 'lib' ,
274
274
'matplotlib' , 'pyplot.py' )
275
-
275
+
276
276
pyplot_orig = open (pyplot_path , 'r' ).readlines ()
277
-
278
-
277
+
278
+
279
279
try :
280
280
pyplot_orig = pyplot_orig [:pyplot_orig .index (PYPLOT_MAGIC_HEADER )+ 1 ]
281
281
except IndexError :
282
282
raise ValueError ('The pyplot.py file *must* have the exact line: %s' % PYPLOT_MAGIC_HEADER )
283
-
283
+
284
284
pyplot = open (pyplot_path , 'w' )
285
285
pyplot .writelines (pyplot_orig )
286
286
pyplot .write ('\n ' )
287
-
287
+
288
288
pyplot .writelines (boilerplate_gen ())
289
289
290
290
291
- if __name__ == '__main__' :
291
+ if __name__ == '__main__' :
292
292
# Write the matplotlib.pyplot file
293
- build_pyplot ()
293
+ build_pyplot ()
0 commit comments