ActivePython word wrap in Shell or PythonWin during list printout

Tomasz Lisowski lisowski.tomasz at sssa.NOSPAM.com.pl
Thu Jul 12 03:52:37 EDT 2001


U¿ytkownik "Rufus V. Smith" <nospam at nospam> napisa³ w wiadomo¶ci
news:3b4cc844$0$172 at wodc7nh6.news.uu.net...
> I am running the latest Python from ActiveState and I have a
> problem that when I print a long list (Like dir()), the entire list
> prints out on one line and I have to scroll over to read it.
>
> Is there an environment setting to force word wrap at
> a certain line length?
>
> Or is there a list function that can take a list and
> pretty print it with word wrap.
>
> (Not having one gave me an opportunity to learn
> some  Python scripting as I wrote my own, but
> if there was a library standard, that might be better. )
>
>
> def marginprint(L,margin=80):
>      b=""
>      for a in L:
>          if len(str(a)+b)+3 >= margin :
>                 print b
>                 b = "'"+str(a)+"'"
>          else:
>                 if b: b+=","
>                 b += "'"+str(a)+"'"
>      if b: print b
>      b=""
>
>
> With this function, I just type marginprint(dir())
> and it will respect the margins.
>
> Only I have to load this into my environment every time
> I bring it up.

You could possibly add the statement:

from my_print_utils import marginprint

to your site.py file

I have not tried that, but it is supposed to work fin, as far as I know.

Regards,
Tomasz





More information about the Python-list mailing list