So just write a custom pprint() function (or call it whatever you want), and call it instead of print.<div><br></div><div>Aaron Meurer</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Nov 15, 2012 at 8:28 PM, Gökhan Sever <span dir="ltr"><<a href="mailto:gokhansever@gmail.com" target="_blank">gokhansever@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<div><br></div><div>This works fine for a single array printing, but I want to be able to execute a script and have arrays printed in this form.</div>
<div><br></div><div>I think, its best to wait for Python 3 adaption, or adapt my scripts to Py3 in order to achieve this with the least amount of extra coding :)</div><div class="HOEnZb"><div class="h5">
<div><br><div class="gmail_quote">On Sat, Nov 10, 2012 at 1:42 AM, Aaron Meurer <span dir="ltr"><<a href="mailto:asmeurer@gmail.com" target="_blank">asmeurer@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Why not just use the regular IPython display hook, and not use print, i.e., just call the object like <div><br></div><div>In [1]: <span style="font-family:arial,sans-serif;font-size:13px">findgen(5)</span></div><div><span style="font-family:arial,sans-serif;font-size:13px">Out[1]:</span><span style="font-family:arial,sans-serif;font-size:13px"> 0.00000 1.00000 2.00000 3.00000 4.00000</span></div>
<div><span style="font-family:arial,sans-serif;font-size:13px"><br></span></div><div><span style="font-family:arial,sans-serif;font-size:13px">If you insist on using print, one option would be to just monkey-patch array.__str__.</span></div>
<span><font color="#888888">
<div><span style="font-family:arial,sans-serif;font-size:13px"><br></span></div><div><span style="font-family:arial,sans-serif;font-size:13px">Aaron Meurer</span></div></font></span><div><div><div>
<br></div><div class="gmail_extra"><br><br><div class="gmail_quote">
On Sat, Nov 10, 2012 at 1:34 AM, Carl Smith <span dir="ltr"><<a href="mailto:carl.input@gmail.com" target="_blank">carl.input@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Can you not redirect stdout to a method with something like...<br>
<br>
class StandardOut(object):<br>
<br>
def __init__(self): pass<br>
<br>
def write(self, string):<br>
do_stuff()<br>
<br>
I'm not sure if that'll work. Just thinking out loud really.<br>
<div><div><br>
On 10 November 2012 08:17, Thomas Kluyver <<a href="mailto:takowl@gmail.com" target="_blank">takowl@gmail.com</a>> wrote:<br>
> The only thing I can think of is to hack an ast transformation into %run, to<br>
> turn print statements into calls to a function you've defined.<br>
><br>
> The good news is that a lot of scientific python packages now support python<br>
> 3.<br>
><br>
> Thanks,<br>
> Thomas<br>
><br>
> On Nov 10, 2012 1:10 AM, "Gökhan Sever" <<a href="mailto:gokhansever@gmail.com" target="_blank">gokhansever@gmail.com</a>> wrote:<br>
>><br>
>> Thanks for the reply Thomas.<br>
>><br>
>> I still use Python 2.7, along with all of the scientific Python stack.<br>
>><br>
>> Do you think is there a simple way to modify IPython's "run" command so<br>
>> that output's pretty printed just like IPython shell?<br>
>><br>
>><br>
>> On Fri, Nov 9, 2012 at 2:28 AM, Thomas Kluyver <<a href="mailto:takowl@gmail.com" target="_blank">takowl@gmail.com</a>> wrote:<br>
>>><br>
>>> In python 3, you can replace the builtin print function. I don't know of<br>
>>> any way to modify the behaviour of print statements in python 2.<br>
>>><br>
>>> Thomas<br>
>>><br>
>>> On Nov 9, 2012 6:59 AM, "Gökhan Sever" <<a href="mailto:gokhansever@gmail.com" target="_blank">gokhansever@gmail.com</a>> wrote:<br>
>>>><br>
>>>> Hello,<br>
>>>><br>
>>>> I would like to be able print NumPy arrays matching the IDL's array<br>
>>>> print outout. For instance:<br>
>>>><br>
>>>> IDL> print, findgen(5)<br>
>>>> 0.00000 1.00000 2.00000 3.00000 4.00000<br>
>>>> IDL> print, dindgen(5)<br>
>>>> 0.0000000 1.0000000 2.0000000 3.0000000<br>
>>>> 4.0000000<br>
>>>> 0.0000000 1.0000000 2.0000000 3.0000000<br>
>>>> 4.0000000<br>
>>>><br>
>>>><br>
>>>> I1 np.arange(5, dtype='float64')<br>
>>>> O1 0.0000000 1.0000000 2.0000000 3.0000000<br>
>>>> 4.0000000<br>
>>>><br>
>>>> I2 np.arange(5, dtype='float32')<br>
>>>> O2 0.00000 1.00000 2.00000 3.00000 4.00000<br>
>>>><br>
>>>> Ignoring O1 and O2 lines, outputs (formatting) are matching. I achieved<br>
>>>> this by defining some custom pprint formatters in the IPy config file.<br>
>>>><br>
>>>> However, this is only valid within IPython while just typing array<br>
>>>> names. For instance, running these lines revert the printing settings back<br>
>>>> to normal.<br>
>>>><br>
>>>> Is there a way to make this type of customized printing default for<br>
>>>> executing a file via Python? ie., can we replace python's print statement<br>
>>>> with pprint?<br>
>>>><br>
>>>> If not, should I edit numpy/core/arrayprint.py and use a pythonrc file<br>
>>>> to achieve the effect?<br>
>>>><br>
>>>> I am also interested in custom printing other data types than nd.arrays.<br>
>>>><br>
>>>> Thanks.<br>
>>>><br>
>>>> --<br>
>>>> Gökhan<br>
>>>><br>
>>>> _______________________________________________<br>
>>>> IPython-User mailing list<br>
>>>> <a href="mailto:IPython-User@scipy.org" target="_blank">IPython-User@scipy.org</a><br>
>>>> <a href="http://mail.scipy.org/mailman/listinfo/ipython-user" target="_blank">http://mail.scipy.org/mailman/listinfo/ipython-user</a><br>
>>>><br>
>>><br>
>>> _______________________________________________<br>
>>> IPython-User mailing list<br>
>>> <a href="mailto:IPython-User@scipy.org" target="_blank">IPython-User@scipy.org</a><br>
>>> <a href="http://mail.scipy.org/mailman/listinfo/ipython-user" target="_blank">http://mail.scipy.org/mailman/listinfo/ipython-user</a><br>
>>><br>
>><br>
>><br>
>><br>
>> --<br>
>> Gökhan<br>
>><br>
>> _______________________________________________<br>
>> IPython-User mailing list<br>
>> <a href="mailto:IPython-User@scipy.org" target="_blank">IPython-User@scipy.org</a><br>
>> <a href="http://mail.scipy.org/mailman/listinfo/ipython-user" target="_blank">http://mail.scipy.org/mailman/listinfo/ipython-user</a><br>
>><br>
><br>
> _______________________________________________<br>
> IPython-User mailing list<br>
> <a href="mailto:IPython-User@scipy.org" target="_blank">IPython-User@scipy.org</a><br>
> <a href="http://mail.scipy.org/mailman/listinfo/ipython-user" target="_blank">http://mail.scipy.org/mailman/listinfo/ipython-user</a><br>
><br>
_______________________________________________<br>
IPython-User mailing list<br>
<a href="mailto:IPython-User@scipy.org" target="_blank">IPython-User@scipy.org</a><br>
<a href="http://mail.scipy.org/mailman/listinfo/ipython-user" target="_blank">http://mail.scipy.org/mailman/listinfo/ipython-user</a><br>
</div></div></blockquote></div><br></div>
</div></div><br>_______________________________________________<br>
IPython-User mailing list<br>
<a href="mailto:IPython-User@scipy.org" target="_blank">IPython-User@scipy.org</a><br>
<a href="http://mail.scipy.org/mailman/listinfo/ipython-user" target="_blank">http://mail.scipy.org/mailman/listinfo/ipython-user</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div>-- <br>Gökhan<br>
</div>
</div></div><br>_______________________________________________<br>
IPython-User mailing list<br>
<a href="mailto:IPython-User@scipy.org">IPython-User@scipy.org</a><br>
<a href="http://mail.scipy.org/mailman/listinfo/ipython-user" target="_blank">http://mail.scipy.org/mailman/listinfo/ipython-user</a><br>
<br></blockquote></div><br></div>