FAQ

[IPython-User] Pretty print as default print function

Gökhan Sever
Nov 9, 2012 at 6:59 am
Hello,

I would like to be able print NumPy arrays matching the IDL's array print
outout. For instance:

IDL> print, findgen(5)
0.00000 1.00000 2.00000 3.00000 4.00000
IDL> print, dindgen(5)
0.0000000 1.0000000 2.0000000 3.0000000
4.0000000
0.0000000 1.0000000 2.0000000 3.0000000
4.0000000


I1 np.arange(5, dtype='float64')
O1 0.0000000 1.0000000 2.0000000 3.0000000
4.0000000

I2 np.arange(5, dtype='float32')
O2 0.00000 1.00000 2.00000 3.00000 4.00000

Ignoring O1 and O2 lines, outputs (formatting) are matching. I achieved
this by defining some custom pprint formatters in the IPy config file.

However, this is only valid within IPython while just typing array names.
For instance, running these lines revert the printing settings back to
normal.

Is there a way to make this type of customized printing default for
executing a file via Python? ie., can we replace python's print statement
with pprint?

If not, should I edit numpy/core/arrayprint.py and use a pythonrc file to
achieve the effect?

I am also interested in custom printing other data types than nd.arrays.

Thanks.

--
Gökhan
reply

Search Discussions

9 responses

  • Gökhan Sever at Nov 10, 2012 at 1:10 am
    Thanks for the reply Thomas.

    I still use Python 2.7, along with all of the scientific Python stack.

    Do you think is there a simple way to modify IPython's "run" command so
    that output's pretty printed just like IPython shell?

    On Fri, Nov 9, 2012 at 2:28 AM, Thomas Kluyver wrote:

    In python 3, you can replace the builtin print function. I don't know of
    any way to modify the behaviour of print statements in python 2.

    Thomas
    On Nov 9, 2012 6:59 AM, "Gökhan Sever" wrote:

    Hello,

    I would like to be able print NumPy arrays matching the IDL's array print
    outout. For instance:

    IDL> print, findgen(5)
    0.00000 1.00000 2.00000 3.00000 4.00000
    IDL> print, dindgen(5)
    0.0000000 1.0000000 2.0000000 3.0000000
    4.0000000
    0.0000000 1.0000000 2.0000000 3.0000000
    4.0000000


    I1 np.arange(5, dtype='float64')
    O1 0.0000000 1.0000000 2.0000000 3.0000000
    4.0000000

    I2 np.arange(5, dtype='float32')
    O2 0.00000 1.00000 2.00000 3.00000 4.00000

    Ignoring O1 and O2 lines, outputs (formatting) are matching. I achieved
    this by defining some custom pprint formatters in the IPy config file.

    However, this is only valid within IPython while just typing array names.
    For instance, running these lines revert the printing settings back to
    normal.

    Is there a way to make this type of customized printing default for
    executing a file via Python? ie., can we replace python's print statement
    with pprint?

    If not, should I edit numpy/core/arrayprint.py and use a pythonrc file to
    achieve the effect?

    I am also interested in custom printing other data types than nd.arrays.

    Thanks.

    --
    Gökhan

    _______________________________________________
    IPython-User mailing list
    ipy...@...org
    http://mail.scipy.org/mailman/listinfo/ipython-user
    _______________________________________________
    IPython-User mailing list
    ipy...@...org
    http://mail.scipy.org/mailman/listinfo/ipython-user

    --
    Gökhan
  • Thomas Kluyver at Nov 10, 2012 at 8:18 am
    The only thing I can think of is to hack an ast transformation into %run,
    to turn print statements into calls to a function you've defined.

    The good news is that a lot of scientific python packages now support
    python 3.

    Thanks,
    Thomas
    On Nov 10, 2012 1:10 AM, "Gökhan Sever" wrote:

    Thanks for the reply Thomas.

    I still use Python 2.7, along with all of the scientific Python stack.

    Do you think is there a simple way to modify IPython's "run" command so
    that output's pretty printed just like IPython shell?

    On Fri, Nov 9, 2012 at 2:28 AM, Thomas Kluyver wrote:

    In python 3, you can replace the builtin print function. I don't know of
    any way to modify the behaviour of print statements in python 2.

    Thomas
    On Nov 9, 2012 6:59 AM, "Gökhan Sever" wrote:

    Hello,

    I would like to be able print NumPy arrays matching the IDL's array
    print outout. For instance:

    IDL> print, findgen(5)
    0.00000 1.00000 2.00000 3.00000 4.00000
    IDL> print, dindgen(5)
    0.0000000 1.0000000 2.0000000 3.0000000
    4.0000000
    0.0000000 1.0000000 2.0000000 3.0000000
    4.0000000


    I1 np.arange(5, dtype='float64')
    O1 0.0000000 1.0000000 2.0000000 3.0000000
    4.0000000

    I2 np.arange(5, dtype='float32')
    O2 0.00000 1.00000 2.00000 3.00000 4.00000

    Ignoring O1 and O2 lines, outputs (formatting) are matching. I achieved
    this by defining some custom pprint formatters in the IPy config file.

    However, this is only valid within IPython while just typing array
    names. For instance, running these lines revert the printing settings back
    to normal.

    Is there a way to make this type of customized printing default for
    executing a file via Python? ie., can we replace python's print statement
    with pprint?

    If not, should I edit numpy/core/arrayprint.py and use a pythonrc file
    to achieve the effect?

    I am also interested in custom printing other data types than nd.arrays.

    Thanks.

    --
    Gökhan

    _______________________________________________
    IPython-User mailing list
    ipy...@...org
    http://mail.scipy.org/mailman/listinfo/ipython-user
    _______________________________________________
    IPython-User mailing list
    ipy...@...org
    http://mail.scipy.org/mailman/listinfo/ipython-user

    --
    Gökhan

    _______________________________________________
    IPython-User mailing list
    ipy...@...org
    http://mail.scipy.org/mailman/listinfo/ipython-user
  • Carl Smith at Nov 10, 2012 at 8:39 am
    Just to be clear, you'd then do `sys.stdout = StandardOut()` and be
    careful not to recur by writing out within the write method. Again,
    I'm not sure this makes sense, but surely you want to modify stdout on
    it's way to the notebook, not overload print.
    On 10 November 2012 08:34, Carl Smith wrote:
    Can you not redirect stdout to a method with something like...

    class StandardOut(object):

    def __init__(self): pass

    def write(self, string):
    do_stuff()

    I'm not sure if that'll work. Just thinking out loud really.
    On 10 November 2012 08:17, Thomas Kluyver wrote:
    The only thing I can think of is to hack an ast transformation into %run, to
    turn print statements into calls to a function you've defined.

    The good news is that a lot of scientific python packages now support python
    3.

    Thanks,
    Thomas
    On Nov 10, 2012 1:10 AM, "Gökhan Sever" wrote:

    Thanks for the reply Thomas.

    I still use Python 2.7, along with all of the scientific Python stack.

    Do you think is there a simple way to modify IPython's "run" command so
    that output's pretty printed just like IPython shell?

    On Fri, Nov 9, 2012 at 2:28 AM, Thomas Kluyver wrote:

    In python 3, you can replace the builtin print function. I don't know of
    any way to modify the behaviour of print statements in python 2.

    Thomas
    On Nov 9, 2012 6:59 AM, "Gökhan Sever" wrote:

    Hello,

    I would like to be able print NumPy arrays matching the IDL's array
    print outout. For instance:

    IDL> print, findgen(5)
    0.00000 1.00000 2.00000 3.00000 4.00000
    IDL> print, dindgen(5)
    0.0000000 1.0000000 2.0000000 3.0000000
    4.0000000
    0.0000000 1.0000000 2.0000000 3.0000000
    4.0000000


    I1 np.arange(5, dtype='float64')
    O1 0.0000000 1.0000000 2.0000000 3.0000000
    4.0000000

    I2 np.arange(5, dtype='float32')
    O2 0.00000 1.00000 2.00000 3.00000 4.00000

    Ignoring O1 and O2 lines, outputs (formatting) are matching. I achieved
    this by defining some custom pprint formatters in the IPy config file.

    However, this is only valid within IPython while just typing array
    names. For instance, running these lines revert the printing settings back
    to normal.

    Is there a way to make this type of customized printing default for
    executing a file via Python? ie., can we replace python's print statement
    with pprint?

    If not, should I edit numpy/core/arrayprint.py and use a pythonrc file
    to achieve the effect?

    I am also interested in custom printing other data types than nd.arrays.

    Thanks.

    --
    Gökhan

    _______________________________________________
    IPython-User mailing list
    ipy...@...org
    http://mail.scipy.org/mailman/listinfo/ipython-user
    _______________________________________________
    IPython-User mailing list
    ipy...@...org
    http://mail.scipy.org/mailman/listinfo/ipython-user


    --
    Gökhan

    _______________________________________________
    IPython-User mailing list
    ipy...@...org
    http://mail.scipy.org/mailman/listinfo/ipython-user
    _______________________________________________
    IPython-User mailing list
    ipy...@...org
    http://mail.scipy.org/mailman/listinfo/ipython-user
    _______________________________________________
    IPython-User mailing list
    ipy...@...org
    http://mail.scipy.org/mailman/listinfo/ipython-user
  • Gökhan Sever at Nov 16, 2012 at 3:25 am
    Hi Carl,

    Can you provide a simple working example here?

    Thanks.
    On Sat, Nov 10, 2012 at 1:34 AM, Carl Smith wrote:

    Can you not redirect stdout to a method with something like...

    class StandardOut(object):

    def __init__(self): pass

    def write(self, string):
    do_stuff()

    I'm not sure if that'll work. Just thinking out loud really.
    On 10 November 2012 08:17, Thomas Kluyver wrote:
    The only thing I can think of is to hack an ast transformation into %run, to
    turn print statements into calls to a function you've defined.

    The good news is that a lot of scientific python packages now support python
    3.

    Thanks,
    Thomas
    On Nov 10, 2012 1:10 AM, "Gökhan Sever" wrote:

    Thanks for the reply Thomas.

    I still use Python 2.7, along with all of the scientific Python stack.

    Do you think is there a simple way to modify IPython's "run" command so
    that output's pretty printed just like IPython shell?

    On Fri, Nov 9, 2012 at 2:28 AM, Thomas Kluyver wrote:

    In python 3, you can replace the builtin print function. I don't know
    of
    any way to modify the behaviour of print statements in python 2.

    Thomas
    On Nov 9, 2012 6:59 AM, "Gökhan Sever" wrote:

    Hello,

    I would like to be able print NumPy arrays matching the IDL's array
    print outout. For instance:

    IDL> print, findgen(5)
    0.00000 1.00000 2.00000 3.00000 4.00000
    IDL> print, dindgen(5)
    0.0000000 1.0000000 2.0000000 3.0000000
    4.0000000
    0.0000000 1.0000000 2.0000000 3.0000000
    4.0000000


    I1 np.arange(5, dtype='float64')
    O1 0.0000000 1.0000000 2.0000000 3.0000000
    4.0000000

    I2 np.arange(5, dtype='float32')
    O2 0.00000 1.00000 2.00000 3.00000 4.00000

    Ignoring O1 and O2 lines, outputs (formatting) are matching. I
    achieved
    this by defining some custom pprint formatters in the IPy config file.

    However, this is only valid within IPython while just typing array
    names. For instance, running these lines revert the printing settings
    back
    to normal.

    Is there a way to make this type of customized printing default for
    executing a file via Python? ie., can we replace python's print
    statement
    with pprint?

    If not, should I edit numpy/core/arrayprint.py and use a pythonrc file
    to achieve the effect?

    I am also interested in custom printing other data types than
    nd.arrays.
    Thanks.

    --
    Gökhan

    _______________________________________________
    IPython-User mailing list
    ipy...@...org
    http://mail.scipy.org/mailman/listinfo/ipython-user
    _______________________________________________
    IPython-User mailing list
    ipy...@...org
    http://mail.scipy.org/mailman/listinfo/ipython-user


    --
    Gökhan

    _______________________________________________
    IPython-User mailing list
    ipy...@...org
    http://mail.scipy.org/mailman/listinfo/ipython-user
    _______________________________________________
    IPython-User mailing list
    ipy...@...org
    http://mail.scipy.org/mailman/listinfo/ipython-user
    _______________________________________________
    IPython-User mailing list
    ipy...@...org
    http://mail.scipy.org/mailman/listinfo/ipython-user


    --
    Gökhan
  • Gökhan Sever at Nov 16, 2012 at 3:28 am
    Hi,

    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.

    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 :)
    On Sat, Nov 10, 2012 at 1:42 AM, Aaron Meurer wrote:

    Why not just use the regular IPython display hook, and not use print,
    i.e., just call the object like

    In [1]: findgen(5)
    Out[1]: 0.00000 1.00000 2.00000 3.00000 4.00000

    If you insist on using print, one option would be to just monkey-patch
    array.__str__.

    Aaron Meurer


    On Sat, Nov 10, 2012 at 1:34 AM, Carl Smith wrote:

    Can you not redirect stdout to a method with something like...

    class StandardOut(object):

    def __init__(self): pass

    def write(self, string):
    do_stuff()

    I'm not sure if that'll work. Just thinking out loud really.
    On 10 November 2012 08:17, Thomas Kluyver wrote:
    The only thing I can think of is to hack an ast transformation into %run, to
    turn print statements into calls to a function you've defined.

    The good news is that a lot of scientific python packages now support python
    3.

    Thanks,
    Thomas
    On Nov 10, 2012 1:10 AM, "Gökhan Sever" wrote:

    Thanks for the reply Thomas.

    I still use Python 2.7, along with all of the scientific Python stack.

    Do you think is there a simple way to modify IPython's "run" command so
    that output's pretty printed just like IPython shell?


    On Fri, Nov 9, 2012 at 2:28 AM, Thomas Kluyver <tak...@...com>
    wrote:
    In python 3, you can replace the builtin print function. I don't know
    of
    any way to modify the behaviour of print statements in python 2.

    Thomas
    On Nov 9, 2012 6:59 AM, "Gökhan Sever" wrote:

    Hello,

    I would like to be able print NumPy arrays matching the IDL's array
    print outout. For instance:

    IDL> print, findgen(5)
    0.00000 1.00000 2.00000 3.00000 4.00000
    IDL> print, dindgen(5)
    0.0000000 1.0000000 2.0000000 3.0000000
    4.0000000
    0.0000000 1.0000000 2.0000000 3.0000000
    4.0000000


    I1 np.arange(5, dtype='float64')
    O1 0.0000000 1.0000000 2.0000000 3.0000000
    4.0000000

    I2 np.arange(5, dtype='float32')
    O2 0.00000 1.00000 2.00000 3.00000 4.00000

    Ignoring O1 and O2 lines, outputs (formatting) are matching. I
    achieved
    this by defining some custom pprint formatters in the IPy config
    file.
    However, this is only valid within IPython while just typing array
    names. For instance, running these lines revert the printing
    settings back
    to normal.

    Is there a way to make this type of customized printing default for
    executing a file via Python? ie., can we replace python's print
    statement
    with pprint?

    If not, should I edit numpy/core/arrayprint.py and use a pythonrc
    file
    to achieve the effect?

    I am also interested in custom printing other data types than
    nd.arrays.
    Thanks.

    --
    Gökhan

    _______________________________________________
    IPython-User mailing list
    ipy...@...org
    http://mail.scipy.org/mailman/listinfo/ipython-user
    _______________________________________________
    IPython-User mailing list
    ipy...@...org
    http://mail.scipy.org/mailman/listinfo/ipython-user


    --
    Gökhan

    _______________________________________________
    IPython-User mailing list
    ipy...@...org
    http://mail.scipy.org/mailman/listinfo/ipython-user
    _______________________________________________
    IPython-User mailing list
    ipy...@...org
    http://mail.scipy.org/mailman/listinfo/ipython-user
    _______________________________________________
    IPython-User mailing list
    ipy...@...org
    http://mail.scipy.org/mailman/listinfo/ipython-user

    _______________________________________________
    IPython-User mailing list
    ipy...@...org
    http://mail.scipy.org/mailman/listinfo/ipython-user

    --
    Gökhan
  • Aaron Meurer at Nov 17, 2012 at 7:56 am
    So just write a custom pprint() function (or call it whatever you want),
    and call it instead of print.

    Aaron Meurer

    On Thu, Nov 15, 2012 at 8:28 PM, Gökhan Sever wrote:

    Hi,

    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.

    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 :)
    On Sat, Nov 10, 2012 at 1:42 AM, Aaron Meurer wrote:

    Why not just use the regular IPython display hook, and not use print,
    i.e., just call the object like

    In [1]: findgen(5)
    Out[1]: 0.00000 1.00000 2.00000 3.00000 4.00000

    If you insist on using print, one option would be to just monkey-patch
    array.__str__.

    Aaron Meurer


    On Sat, Nov 10, 2012 at 1:34 AM, Carl Smith wrote:

    Can you not redirect stdout to a method with something like...

    class StandardOut(object):

    def __init__(self): pass

    def write(self, string):
    do_stuff()

    I'm not sure if that'll work. Just thinking out loud really.
    On 10 November 2012 08:17, Thomas Kluyver wrote:
    The only thing I can think of is to hack an ast transformation into %run, to
    turn print statements into calls to a function you've defined.

    The good news is that a lot of scientific python packages now support python
    3.

    Thanks,
    Thomas
    On Nov 10, 2012 1:10 AM, "Gökhan Sever" wrote:

    Thanks for the reply Thomas.

    I still use Python 2.7, along with all of the scientific Python stack.

    Do you think is there a simple way to modify IPython's "run" command
    so
    that output's pretty printed just like IPython shell?


    On Fri, Nov 9, 2012 at 2:28 AM, Thomas Kluyver <tak...@...com>
    wrote:
    In python 3, you can replace the builtin print function. I don't
    know of
    any way to modify the behaviour of print statements in python 2.

    Thomas

    On Nov 9, 2012 6:59 AM, "Gökhan Sever" <gok...@...com>
    wrote:
    Hello,

    I would like to be able print NumPy arrays matching the IDL's array
    print outout. For instance:

    IDL> print, findgen(5)
    0.00000 1.00000 2.00000 3.00000 4.00000
    IDL> print, dindgen(5)
    0.0000000 1.0000000 2.0000000 3.0000000
    4.0000000
    0.0000000 1.0000000 2.0000000 3.0000000
    4.0000000


    I1 np.arange(5, dtype='float64')
    O1 0.0000000 1.0000000 2.0000000 3.0000000
    4.0000000

    I2 np.arange(5, dtype='float32')
    O2 0.00000 1.00000 2.00000 3.00000 4.00000

    Ignoring O1 and O2 lines, outputs (formatting) are matching. I
    achieved
    this by defining some custom pprint formatters in the IPy config
    file.
    However, this is only valid within IPython while just typing array
    names. For instance, running these lines revert the printing
    settings back
    to normal.

    Is there a way to make this type of customized printing default for
    executing a file via Python? ie., can we replace python's print
    statement
    with pprint?

    If not, should I edit numpy/core/arrayprint.py and use a pythonrc
    file
    to achieve the effect?

    I am also interested in custom printing other data types than
    nd.arrays.
    Thanks.

    --
    Gökhan

    _______________________________________________
    IPython-User mailing list
    ipy...@...org
    http://mail.scipy.org/mailman/listinfo/ipython-user
    _______________________________________________
    IPython-User mailing list
    ipy...@...org
    http://mail.scipy.org/mailman/listinfo/ipython-user


    --
    Gökhan

    _______________________________________________
    IPython-User mailing list
    ipy...@...org
    http://mail.scipy.org/mailman/listinfo/ipython-user
    _______________________________________________
    IPython-User mailing list
    ipy...@...org
    http://mail.scipy.org/mailman/listinfo/ipython-user
    _______________________________________________
    IPython-User mailing list
    ipy...@...org
    http://mail.scipy.org/mailman/listinfo/ipython-user

    _______________________________________________
    IPython-User mailing list
    ipy...@...org
    http://mail.scipy.org/mailman/listinfo/ipython-user

    --
    Gökhan

    _______________________________________________
    IPython-User mailing list
    ipy...@...org
    http://mail.scipy.org/mailman/listinfo/ipython-user
  • Gökhan Sever at Nov 17, 2012 at 7:52 pm
    Yes, this is the solution that I will follow as long as I continue with
    Python 2.x series.

    Thanks.
    On Sat, Nov 17, 2012 at 12:55 AM, Aaron Meurer wrote:

    So just write a custom pprint() function (or call it whatever you want),
    and call it instead of print.

    Aaron Meurer

    On Thu, Nov 15, 2012 at 8:28 PM, Gökhan Sever wrote:

    Hi,

    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.

    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 :)
    On Sat, Nov 10, 2012 at 1:42 AM, Aaron Meurer wrote:

    Why not just use the regular IPython display hook, and not use print,
    i.e., just call the object like

    In [1]: findgen(5)
    Out[1]: 0.00000 1.00000 2.00000 3.00000 4.00000

    If you insist on using print, one option would be to just monkey-patch
    array.__str__.

    Aaron Meurer


    On Sat, Nov 10, 2012 at 1:34 AM, Carl Smith wrote:

    Can you not redirect stdout to a method with something like...

    class StandardOut(object):

    def __init__(self): pass

    def write(self, string):
    do_stuff()

    I'm not sure if that'll work. Just thinking out loud really.
    On 10 November 2012 08:17, Thomas Kluyver wrote:
    The only thing I can think of is to hack an ast transformation into %run, to
    turn print statements into calls to a function you've defined.

    The good news is that a lot of scientific python packages now support python
    3.

    Thanks,
    Thomas

    On Nov 10, 2012 1:10 AM, "Gökhan Sever" <gok...@...com>
    wrote:
    Thanks for the reply Thomas.

    I still use Python 2.7, along with all of the scientific Python
    stack.
    Do you think is there a simple way to modify IPython's "run" command
    so
    that output's pretty printed just like IPython shell?


    On Fri, Nov 9, 2012 at 2:28 AM, Thomas Kluyver <tak...@...com>
    wrote:
    In python 3, you can replace the builtin print function. I don't
    know of
    any way to modify the behaviour of print statements in python 2.

    Thomas

    On Nov 9, 2012 6:59 AM, "Gökhan Sever" <gok...@...com>
    wrote:
    Hello,

    I would like to be able print NumPy arrays matching the IDL's array
    print outout. For instance:

    IDL> print, findgen(5)
    0.00000 1.00000 2.00000 3.00000 4.00000
    IDL> print, dindgen(5)
    0.0000000 1.0000000 2.0000000 3.0000000
    4.0000000
    0.0000000 1.0000000 2.0000000 3.0000000
    4.0000000


    I1 np.arange(5, dtype='float64')
    O1 0.0000000 1.0000000 2.0000000 3.0000000
    4.0000000

    I2 np.arange(5, dtype='float32')
    O2 0.00000 1.00000 2.00000 3.00000
    4.00000
    Ignoring O1 and O2 lines, outputs (formatting) are matching. I
    achieved
    this by defining some custom pprint formatters in the IPy config
    file.
    However, this is only valid within IPython while just typing array
    names. For instance, running these lines revert the printing
    settings back
    to normal.

    Is there a way to make this type of customized printing default for
    executing a file via Python? ie., can we replace python's print
    statement
    with pprint?

    If not, should I edit numpy/core/arrayprint.py and use a pythonrc
    file
    to achieve the effect?

    I am also interested in custom printing other data types than
    nd.arrays.
    Thanks.

    --
    Gökhan

    _______________________________________________
    IPython-User mailing list
    ipy...@...org
    http://mail.scipy.org/mailman/listinfo/ipython-user
    _______________________________________________
    IPython-User mailing list
    ipy...@...org
    http://mail.scipy.org/mailman/listinfo/ipython-user


    --
    Gökhan

    _______________________________________________
    IPython-User mailing list
    ipy...@...org
    http://mail.scipy.org/mailman/listinfo/ipython-user
    _______________________________________________
    IPython-User mailing list
    ipy...@...org
    http://mail.scipy.org/mailman/listinfo/ipython-user
    _______________________________________________
    IPython-User mailing list
    ipy...@...org
    http://mail.scipy.org/mailman/listinfo/ipython-user

    _______________________________________________
    IPython-User mailing list
    ipy...@...org
    http://mail.scipy.org/mailman/listinfo/ipython-user

    --
    Gökhan

    _______________________________________________
    IPython-User mailing list
    ipy...@...org
    http://mail.scipy.org/mailman/listinfo/ipython-user
    _______________________________________________
    IPython-User mailing list
    ipy...@...org
    http://mail.scipy.org/mailman/listinfo/ipython-user

    --
    Gökhan
  • Robert Kern at Nov 16, 2012 at 11:27 am

    On 2012-11-16 10:53 AM, Thomas Kluyver wrote:
    On 10 November 2012 08:34, Carl Smith wrote:

    Can you not redirect stdout to a method with something like...

    class StandardOut(object):

    def __init__(self): pass

    def write(self, string):
    do_stuff()

    I'm not sure if that'll work. Just thinking out loud really.


    I don't think this (overriding sys.stdout) will work for what Gökhan is trying
    to do. Unlike displayhook(), which gets an object, sys.stdout.write() only gets
    the string form, so you can't easily produce a different representation.

    For a Python class, you could monkeypatch __str__() to produce your preferred
    representation, but ndarray is defined in compiled code, so that's not an option.
    The array formatting code is pure Python and monkeypatchable:

    https://github.com/numpy/numpy/blob/master/numpy/core/arrayprint.py

    --
    Robert Kern

    _______________________________________________
    IPython-User mailing list
    ipy...@...org
    http://mail.scipy.org/mailman/listinfo/ipython-user
  • Carl Smith at Nov 16, 2012 at 11:29 am
    I think Thomas is right.

    I'm not sure what option remains, beyond preprocessing scripts and swapping
    out print statements, which sounds like a world of pain. Or migrating to
    Python3.

Related Discussions

Discussion Navigation
viewthread | post