Python
  Home arrow Python arrow Page 3 - Finishing the PyMailGUI Client: User H...
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM Rational Software Development Conference
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PYTHON

Finishing the PyMailGUI Client: User Help Tools
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2007-08-09

    Table of Contents:
  • Finishing the PyMailGUI Client: User Help Tools
  • popuputil: General-Purpose GUI Pop Ups
  • wraplines: Line Split Tools
  • mailcon
  • PyMailGuiHelp: User Help Text
  • Ideas for Improvement

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    TestComplete™ automates software testing for a fraction of what the big guys charge. Easy functional and load testing for all Windows, .NET, Java and Web apps. Download a free trial now.

    Finishing the PyMailGUI Client: User Help Tools - wraplines: Line Split Tools
    (Page 3 of 6 )

    The module in Example 15-7 implements general tools for wrapping long lines, at either a fixed column or the first delimiter at or before a fixed column. PyMailGUI uses this file’s wrapText1 function for text in view, reply, and forward windows, but this code is potentially useful in other programs. Run the file as a script to watch its self-test code at work, and study its functions to see its text-processing logic.

    Example 15-7. PP3E\Internet\Email\PyMailGui\wraplines.py

    ################################################################ # split lines on fixed columns or at delimiters before a column
    # see also: related but different textwrap standard library module (2.3+) ###############################################################

    defaultsize = 80

    def wrapLinesSimple(lineslist, size=defaultsize):
       
    "split at fixed position size"
        wraplines = []
        for line in lineslist:
            while True:
            wraplines.append(line[:size])   # OK if len < size
            line = line[size:]              # split without analysis
            if not line: break
        return wraplines

    def wrapLinesSmart(lineslist, size=defaultsize, delimiters='.,:\t '):
        "wrap at first delimiter left of size" 
        wraplines = []
        for line in lineslist:
           
    while True:
               
    if len(line) <= size:
                    wraplines += [line]
                    break
                else:
                    for look in range(size-1, size/2, -1):
                       
    if line[look] in delimiters:
                            front, line = line[:look+1], line[look+1:]
                            break
                    
    else:
                        front, line = line[:size], line[size:]
                    wraplines += [front]
        return wraplines

    ################################################################ # common use case utilities ###############################################################

    def wrapText1(text,
    size=defaultsize):                 
    # better for line-based txt: mail
        "when text read all at once"    # keeps original line brks struct
        lines = text.split('\n')        # split on newlines

    lines = wrapLinesSmart(lines, size) # wrap lines on delimiters
    return '\n'.join(lines) # put back together
    def wrapText2(text, size=defaultsize): # more uniform across lines
    "same, but treat as one long line" # but loses original line struct
    text = text.replace('\n', ' ') # drop newlines if any
    lines = wrapLinesSmart([text], size) # wrap single line on delimiters
    return lines # caller puts back together
    def wrapText3(text, size=defaultsize): 
    "same, but put back together" 
    lines = wrapText2(text, size) # wrap as single long line
    return '\n'.join(lines) + '\n' # make one string with newlines
    def wrapLines1(lines, size=defaultsize): 
    "when newline included at end" 
    lines = [line[:-1] for line in lines] # strip off newlines (or .rstrip)
    lines = wrapLinesSmart(lines, size) # wrap on delimiters
    return [(line + '\n') for line in lines] # put them back
    def wrapLines2(lines, size=defaultsize): # more uniform across lines
    "same, but concat as one long line" # but loses original structure
    text = ''.join(lines) # put together as 1 line
    lines = wrapText2(text) # wrap on delimiters
    return [(line + '\n') for line in lines] # put newlines on ends

    ################################################################ a self-test ###############################################################

    if __name__ == '__main__':
       
    lines = ['spam ham ' * 20 + 'spam,ni' * 20,
                 'spam ham ' * 20,
                 'spam,ni'   * 20,
                 'spam ham.ni' * 20,
                 '',
                 'spam'*80,
                 ' ',
                 'spam ham eggs']
       
    print 'all', '-'*30
        for line in lines: print repr(line) 
        print 'simple', '-'*30
        for line in wrapLinesSimple(lines): print repr(line)
        print 'smart', '-'*30
        for line in wrapLinesSmart(lines): print repr(line)

        print 'single1', '-'*30
        for line in wrapLinesSimple([lines[0]], 60): print repr(line)
        print 'single2', '-'*30
        for line in wrapLinesSmart([lines[0]], 60): print repr(line)
        print 'combined text', '-'*30
        for line in wrapLines2(lines): print repr(line)
       
    print 'combined lines', '-'*30
        print wrapText1('\n'.join(lines))

        assert ''.join(lines) == ''.join(wrapLinesSimple(lines, 60))
        assert ''.join(lines) == ''.join(wrapLinesSmart(lines, 60))
        print len(''.join(lines)),
        print len(''.join(wrapLinesSimple(lines))),
        print len(''.join(wrapLinesSmart(lines))),
        print len(''.join(wrapLinesSmart(lines, 60))),
        raw_input('Press enter')

    More Python Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "Programming Python, Third Edition,"...
     

    Buy this book now. This article is excerpted from chapter 15 of the book Programming Python, Third Edition, written by Mark Lutz (O'Reilly, 2006; ISBN: 0596009259). Check it out today at your favorite bookstore. Buy this book now.

       

    PYTHON ARTICLES

    - SSH with Twisted
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along
    - Python Operators
    - Bluetooth Programming in Python: Network Pro...
    - Python Sets
    - Python Conditionals, Lists, Dictionaries, an...
    - Python: Input and Variables
    - Introduction to Python Programming
    - Mobile Programming in Python using PyS60: Ge...
    - Bluetooth Programming using Python
    - Finishing the PyMailGUI Client: User Help To...

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway