Skip to content

Pyplot update in setup.py #928

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jun 11, 2012
Prev Previous commit
Next Next commit
Make boilerplate.py Python 3 compatible
  • Loading branch information
mdboom committed Jun 8, 2012
commit 12f407bbbb83301109d35bbad28f0d8270d9d5c6
60 changes: 30 additions & 30 deletions boilerplate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

When this script is run, the current contents of pyplot are
split into generatable and non-generatable content (via the magic header
:attr:`PYPLOT_MAGIC_HEADER`) and the generatable content is overwritten.
:attr:`PYPLOT_MAGIC_HEADER`) and the generatable content is overwritten.
Hence, the non-generatable content should be edited in the pyplot.py file
itself, whereas the generatable content must be edited via templates in
itself, whereas the generatable content must be edited via templates in
this file.

"""
Expand All @@ -30,7 +30,7 @@
# appended
PYPLOT_MAGIC_HEADER = '################# REMAINING CONTENT GENERATED BY boilerplate.py ##############\n'

PYPLOT_PATH = os.path.join(os.path.dirname(__file__), 'lib',
PYPLOT_PATH = os.path.join(os.path.dirname(__file__), 'lib',
'matplotlib', 'pyplot.py')


Expand Down Expand Up @@ -87,7 +87,7 @@ def {name}():

def boilerplate_gen():
"""Generator of lines for the automated part of pyplot."""

# these methods are all simple wrappers of Axes methods by the same
# name.
_plotcommands = (
Expand Down Expand Up @@ -140,7 +140,7 @@ def boilerplate_gen():
'xcorr',
'barbs',
)

_misccommands = (
'cla',
'grid',
Expand All @@ -154,7 +154,7 @@ def boilerplate_gen():
'margins',
'autoscale',
)

cmappable = {
'contour' : 'if %(ret)s._A is not None: sci(%(ret)s)',
'contourf': 'if %(ret)s._A is not None: sci(%(ret)s)',
Expand All @@ -171,24 +171,24 @@ def boilerplate_gen():
'tricontour' : 'if %(ret)s._A is not None: sci(%(ret)s)',
'tricontourf': 'if %(ret)s._A is not None: sci(%(ret)s)',
'tripcolor' : 'sci(%(ret)s)',

}

def format_value(value):
"""
Format function default values as needed for inspect.formatargspec.
The interesting part is a hard-coded list of functions used
as defaults in pyplot methods.
"""
if isinstance(value, types.FunctionType):
if value.func_name in ('detrend_none', 'window_hanning'):
return '=mlab.' + value.func_name
if value.func_name == 'mean':
return '=np.' + value.func_name
if value.__name__ in ('detrend_none', 'window_hanning'):
return '=mlab.' + value.__name__
if value.__name__ == 'mean':
return '=np.' + value.__name__
raise ValueError(('default value %s unknown to boilerplate.' + \
'formatvalue') % value)
return '='+repr(value)

for fmt, cmdlist in [(PLOT_TEMPLATE, _plotcommands),
(MISC_FN_TEMPLATE, _misccommands)]:
for func in cmdlist:
Expand All @@ -198,21 +198,21 @@ def format_value(value):
mappable = cmappable[func] % locals()
else:
mappable = ''

# Get argspec of wrapped function
args, varargs, varkw, defaults = inspect.getargspec(getattr(Axes, func))
args.pop(0) # remove 'self' argument
if defaults is None:
defaults = ()

# How to call the wrapped function
call = map(str, args)
call = list(map(str, args))
if varargs is not None:
call.append('*'+varargs)
if varkw is not None:
call.append('**'+varkw)
call = ', '.join(call)

# Add a hold keyword argument if needed (fmt is PLOT_TEMPLATE) and
# possible (if *args is used, we can't just add a hold
# argument in front of it since it would gobble one of the
Expand All @@ -223,12 +223,12 @@ def format_value(value):
args.append('hold')
defaults = defaults + (None,)
sethold = ''

# Now we can build the argspec for defining the wrapper
argspec = inspect.formatargspec(args, varargs, varkw, defaults,
formatvalue=format_value)
argspec = argspec[1:-1] # remove parens

# A gensym-like facility in case some function takes an
# argument named washold, ax, or ret
washold, ret, ax = 'washold', 'ret', 'ax'
Expand All @@ -237,16 +237,16 @@ def format_value(value):
washold = 'washold' + str(random.randrange(10**12))
ret = 'ret' + str(random.randrange(10**12))
ax = 'ax' + str(random.randrange(10**12))

# Since we can't avoid using some function names,
# bail out if they are used as argument names
for reserved in ('gca', 'gci', 'draw_if_interactive'):
if reserved in bad:
msg = 'Axes method %s has kwarg named %s' % (func, reserved)
raise ValueError(msg)

yield fmt % locals()

cmaps = (
'autumn',
'bone',
Expand All @@ -270,24 +270,24 @@ def format_value(value):


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

pyplot_orig = open(pyplot_path, 'r').readlines()


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

pyplot = open(pyplot_path, 'w')
pyplot.writelines(pyplot_orig)
pyplot.write('\n')

pyplot.writelines(boilerplate_gen())


if __name__ == '__main__':
if __name__ == '__main__':
# Write the matplotlib.pyplot file
build_pyplot()
build_pyplot()