Skip to content
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

Unshadowing a function definition #1

Open
wants to merge 2 commits into
base: script_export
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 70 additions & 45 deletions glue/clients/script_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def unindent(txt):
return '\n'.join(l[indent:].rstrip() for l in lines)


def export(typ):
def export_wrapper(typ):
def wrapper(func):
_export_table[typ] = func
return func
Expand All @@ -31,25 +31,35 @@ def wrapper(func):
def scatter_layer_code(artist, idx, tokens):
layer = artist.layer
style = layer.style
tokens['l%i' % idx] = layer
tokens['l{}'.format(idx)] = layer
tokens['x'] = artist.xatt
tokens['y'] = artist.yatt

props = dict(l='{l%i}' % idx,
ms=style.markersize,
ec=repr(style.color if style.marker == '+' else 'none'),
c=repr(style.color),
alpha=style.alpha,
z=artist.zorder,
marker=repr(style.marker))
prop = dict(
l='{{l{}}}'.format(idx),
ms=style.markersize,
ec=(style.color if style.marker == '+' else 'none'),
c=style.color,
alpha=style.alpha,
z=artist.zorder,
marker=style.marker,
)

block = """
plt.plot({l}[{{x}}].ravel(), {l}[{{y}}].ravel(),
markersize={ms}, mec={ec},
mew=3, mfc={c}, linestyle='None',
marker={marker},
alpha={alpha}, zorder={z})
""".format(**props)
plt.plot(
{l}[{{x}}].ravel(),
{l}[{{y}}].ravel(),
markersize={ms},
mec={ec!r},
mew=3,
mfc={c!r},
linestyle='None',
marker={marker!r},
alpha={alpha},
zorder={z},
)
""".format(**prop)

return unindent(block)


Expand All @@ -59,26 +69,35 @@ def histogram_code(artist, idx, tokens):
tokens['l%i' % idx] = layer

rng = artist.lo, artist.hi
props = dict(l='{l%i}' % idx,
color=repr(style.color),
alpha=style.alpha,
z=artist.zorder,
normed=artist.normed,
cumulative=artist.cumulative,
ylog=artist.ylog,
xlog=artist.xlog,
nbins=artist.nbins,
rng=rng)
props = dict(
l='{{l{}}}'.format(idx),
color=style.color,
alpha=style.alpha,
z=artist.zorder,
normed=artist.normed,
cumulative=artist.cumulative,
ylog=artist.ylog,
xlog=artist.xlog,
nbins=artist.nbins,
rng=rng,
)

block = """
plt.hist({l}[{{att}}].ravel(), bins={nbins}, range={rng},
normed={normed}, cumulative={cumulative},
facecolor={color}, alpha={alpha}, zorder={z})
plt.hist(
{l}[{{att}}].ravel(),
bins={nbins},
range={rng},
normed={normed},
cumulative={cumulative},
facecolor={color!r},
alpha={alpha},
zorder={z},
)
"""
return unindent(block).format(**props)


@export(Client)
@export_wrapper(Client)
def export_client(client):

ax = client.axes
Expand All @@ -105,35 +124,41 @@ def export_client(client):
def _sync_axes(ax):

result = """
plt.xlabel({xlbl})
plt.ylabel({ylbl})
plt.xlabel({xlbl!r})
plt.ylabel({ylbl!r})
plt.xlim({xlim})
plt.ylim({ylim})
plt.xscale({xsc})
plt.yscale({ysc})
""".format(xlbl=repr(ax.get_xlabel()),
ylbl=repr(ax.get_ylabel()),
xlim=ax.get_xlim(),
ylim=ax.get_ylim(),
xsc=repr(ax.get_xscale()),
ysc=repr(ax.get_yscale()))
plt.xscale({xsc!r})
plt.yscale({ysc!r})
""".format(
xlbl=ax.get_xlabel(),
ylbl=ax.get_ylabel(),
xlim=ax.get_xlim(),
ylim=ax.get_ylim(),
xsc=ax.get_xscale(),
ysc=ax.get_yscale(),
)
return unindent(result)


@export(DataCollection)
@export_wrapper(DataCollection)
def export_data_collection(dc):
# test module for now

result = Node("")
for data in dc:
result += Node("{d}=Data(label=%s)" % repr(data.label),
result += Node("{{d}}=Data(label={0!r})".format(data.label),
gives=[data],
tokens=dict(d=data))
for comp in data.components:
result += Node("{c}={d}.add_component(%s, label=%s)" %
(data[comp].tolist(), repr(comp.label)),
gives=[comp],
tokens=dict(c=comp, d=data))
result += Node(
"{{c}}={{d}}.add_component({0}, label={1!r})".format(
data[comp].tolist(),
comp.label,
),
gives=[comp],
tokens=dict(c=comp, d=data),
)

return result

Expand Down