def wraplinessimple(lineslistsize=defaultsize)"split at fixed position sizewraplines [for line in lineslistwhile truewraplines append(line[:size]ok if len size line line[size:split without analysis if not linebreak return wraplines def wraplinessmart(lineslistsize=defaultsizedelimiters=,:\ ')"wrap at first delimiter left of sizewraplines [for line in lineslistwhile trueif len(line<sizewraplines +[linebreak elsefor look in range(size- size / - ) need /not if line[lookin delimitersfrontline line[:look+ ]line[look+ :break elsefrontline line[:size]line[size:wraplines +[frontreturn wraplines ##############################################################################common use case utilities ##############################################################################def wraptext (textsize=defaultsize)"when text read all at oncelines text split('\ 'lines wraplinessmart(linessizereturn '\njoin(linesbetter for line-based txtmail keeps original line brks struct split on newlines wrap lines on delimiters put back together def wraptext (textsize=defaultsize)"samebut treat as one long linetext text replace('\ ''lines wraplinessmart([text]sizereturn lines more uniform across lines but loses original line struct drop newlines if any wrap single line on delimiters caller puts back together def wraptext (textsize=defaultsize)"samebut put back togetherlines wraptext (textsizereturn '\njoin(lines'\nwrap as single long line make one string with newlines def wraplines (linessize=defaultsize)"when newline included at endlines [line[:- for line in lineslines wraplinessmart(linessizestrip off newlines (or rstripwrap on delimiters pymailgui implementation