IDLE - Customizing output format

Ilias Lazaridis ilias at lazaridis.com
Sun Oct 8 01:59:20 CEST 2006


Ilias Lazaridis wrote:
> IDLE has an output format like this:
>
>  >>> object
> <type 'object'>
>  >>> type
> <type 'type'>
>  >>> object.__class__
> <type 'type'>
>  >>> object.__bases__
>
> How can I customize it to become like that:
>
>  >>> object
>      <type 'object'>
>  >>> type
>      <type 'type'>
>  >>> object.__class__
>      <type 'type'>
>  >>> object.__bases__
>
> or that:
>
>  >>> object
>    : <type 'object'>
>  >>> type
>    : <type 'type'>
>  >>> object.__class__
>    : <type 'type'>
>  >>> object.__bases__
>
> (preferably without modifying code)

I am close to finalizing this task, but have two tiny problems:

import the module from within a site_packages *.pth file, or
include the code into sitecustomize, or
import it withine an IDLE session:

# idlex.py
#---------------------------------------------------------------------------------------
import sys

class ClaimStdOut:
    def __init__(self, stream, prefix):
        self.stdout = stream   #remember original stream
        self.prefix = prefix

    def write(self, output):
        self.stdout.write(self.prefix + output)


#this one is _not_ executed on import:
sys.stdout = ClaimStdOut(sys.stdout, '=== ')

#workaround:
def f(obj):
    if obj is not None:
        print repr(obj)
    # Claiming StdOut here
    sys.stdout = ClaimStdOut(sys.stdout, '::: ')
    #disable displayhook (which is just used to set stdout)
    sys.displayhook = sys.__displayhook__

# this one _is_ executed on import
sys.displayhook = f
#---------------------------------------------------------------------------------------

So, why is this line not executed during import?

sys.stdout = ClaimStdOut(sys.stdout, '=== ')

additionally, the output of IDLE looks like this:

IDLE 1.1.3      ==== No Subprocess ====
>>> 1==1
True
[the new stdout is active now]
>>> 1==1
::: True:::
>>>

Possibly IDLE prints the CR/LF in a second call.

How can I compare this operating-system-independent?

if (output == <os.line-end-marker>):
        self.stdout.write(output)
else:
        self.stdout.write(self.prefix + output)

.

--
http://case.lazaridis.com/ticket/10




More information about the Python-list mailing list