Skip to content

Commit

Permalink
ENH: Added file keyword to setp to redirect output
Browse files Browse the repository at this point in the history
  • Loading branch information
madphysicist committed Mar 25, 2016
1 parent 1111c1d commit 918179a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
20 changes: 15 additions & 5 deletions lib/matplotlib/artist.py
Expand Up @@ -1526,6 +1526,12 @@ def setp(obj, *args, **kwargs):
>>> setp(line)
... long output listing omitted
You may specify an output file to `setp` if `sys.stdout` is not
acceptable for some reason::
>>> with fopen('output.log') as f:
>>> setp(line, file=f)
:func:`setp` operates on a single instance or a list of instances.
If you are in query mode introspecting the possible values, only
the first instance in the sequence is used. When actually setting
Expand All @@ -1548,12 +1554,16 @@ def setp(obj, *args, **kwargs):

insp = ArtistInspector(obj)

if len(kwargs) == 0 and len(args) == 0:
print('\n'.join(insp.pprint_setters()))
return
# file has to be popped before checking if kwargs is empty
printArgs = {}
if 'file' in kwargs:
printArgs['file'] = kwargs.pop('file')

if len(kwargs) == 0 and len(args) == 1:
print(insp.pprint_setters(prop=args[0]))
if not kwargs and len(args) < 2:
if args:
print(insp.pprint_setters(prop=args[0]), **printArgs)
else:
print('\n'.join(insp.pprint_setters()), **printArgs)
return

if not cbook.iterable(obj):
Expand Down
8 changes: 8 additions & 0 deletions lib/matplotlib/tests/test_artist.py
Expand Up @@ -208,6 +208,14 @@ def test_properties():
assert len(w) == 0


@cleanup
def test_setp():
sio = io.StringIO()
lines = plt.plot(range(3))
plt.setp(lines, 'zorder', file=sio)
assert sio.getvalue() == ' zorder: any number '


if __name__ == '__main__':
import nose
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

0 comments on commit 918179a

Please sign in to comment.