Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Formated String in optparse

Reply
Thread Tools

Formated String in optparse

 
 
Norbert Thek
Guest
Posts: n/a
 
      03-31-2005
Hi

I'm using Python 24 on Windows > (2k)

Is there an easy way to convince optparse to accept newline in the helpstring?
and more importand also in the 'desc' string. I tried everything
(from the os.linesep) to \n, \r, \r\n, ...

Norbert
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      03-31-2005
Norbert Thek wrote:

> Is there an easy way to convince optparse to accept newline in the
> helpstring? and more importand also in the 'desc' string. I tried
> everything
> (from the os.linesep) to \n, \r, \r\n, ...


The "official" way (write your own Formatter class) is a bit tedious indeed.
Here's a hack that seems to work:

import textwrap
import optparse

class TextWrapper:
@staticmethod
def wrap(text, width=70, **kw):
result = []
for line in text.split("\n"):
result.extend(textwrap.wrap(line, width, **kw))
return result
@staticmethod
def fill(text, width=70, **kw):
result = []
for line in text.split("\n"):
result.append(textwrap.fill(line, width, **kw))
return "\n".join(result)


optparse.textwrap = TextWrapper()

parser = optparse.OptionParser(description="""\
einsamer nie als im august
erfuellungsstunde im gelaende
die roten und die goldenen braende
doch wo ist deiner gaerten lust
""")

parser.add_option("-x", "--example", help="""\
die seen hell die himmel weich
die aecker rein und glaenzen leise
doch wo sind sieg und siegsbeweise \
aus dem von dir vertretenen reich \
wo alles sich durch glueck""")

parser.print_help()

You should probably replace the "\n" literals with os.linesep.

Peter
 
Reply With Quote
 
 
 
 
MyHaz
Guest
Posts: n/a
 
      03-31-2005
If you haven't looked into it, you may like the way class
OptionParser() makes the help text for you.


- Haz

 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      03-31-2005
MyHaz wrote:

> If you haven't looked into it, you may like the way class
> OptionParser() makes the help text for you.


What do you mean?

To clarify: OptionParser's help message in the default format is

"""
usage: discard_newline.py [options]

einsamer nie als im august erfuellungsstunde im gelaende die roten und die
goldenen braende doch wo ist deiner gaerten lust

options:
-h, --help show this help message and exit
-x EXAMPLE, --example=EXAMPLE
die seen hell die himmel weich die aecker rein und
glaenzen leise doch wo sind sieg und siegsbeweise
aus
dem von dir vertretenen reich wo alles sich durch
glueck
"""


whereas that same OptionParser with the tweaked optparse.textwrap (my
TextWrapper instance replacing the textwrap module from the standard
library) will output

"""
usage: preserve_newline.py [options]

einsamer nie als im august
erfuellungsstunde im gelaende
die roten und die goldenen braende
doch wo ist deiner gaerten lust


options:
-h, --help show this help message and exit
-x EXAMPLE, --example=EXAMPLE
die seen hell die himmel weich
die aecker rein und glaenzen leise
doch wo sind sieg und siegsbeweise aus dem von dir
vertretenen reich wo alles sich durch glueck
"""

I guess that both poets in residence and limmerickistas will prefer the
latter form

Peter


 
Reply With Quote
 
Norbert Thek
Guest
Posts: n/a
 
      04-14-2005
Thank You for your help, its working!

Now I have an additional question.
The problem is the encoding of the Text
I'm using German, Can you tell me how to encode
the textstring that the Windows commandline shows the special letters
right?
For exampel i get 'fr' but i want 'fr' (maybe reader with only an english
enabled browser wouldn't see a difference..)

I tried to work with the encode method of string but It didn't work for me
some hint what to do?


Norbert
 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      04-15-2005
Norbert Thek wrote:

> Thank You for your help, its working!
>
> Now I have an additional question.


....which would warrant a separate thread...

> The problem is the encoding of the Text
> I'm using German, Can you tell me how to encode
> the textstring that the Windows commandline shows the special letters
> right?
> For exampel i get 'fr' but i want 'fr' (maybe reader with only an
> english enabled browser wouldn't see a difference..)
>
> I tried to work with the encode method of string but It didn't work for me
> some hint what to do?


>>> u"fr" # what you have (minus unicode)

u'f\xb3r'

>>> u"fr"

u'f\xfcr' # what you need

One way to get that (wrong) representation:

>>> u"fr".encode("cp1252").decode("cp850")

u'f\xb3r'

So it could be that you are interpreting cp1252 ("German" windows) as cp850
("German" DOS).

Try the following script:

# -*- coding: cp1252 -*-
text = u"text with umlauts ".encode("cp850")
print text


If that works correctly, you can prepare every string literal in your
program in the same way. Or redirect sys.stdout

sys.stdout = codecs.getwriter("cp850")(sys.stdout)

as posted by Martin von Lwis on de.comp.lang.python only two days ago.

Peter






 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert UNIX formated text files to DOS formated? walterbyrd Python 13 05-13-2009 02:32 PM
Convert currency formated string back into decimal =?Utf-8?B?R3JlZyBH?= ASP .Net 1 11-03-2007 04:48 PM
Can we populate a XML formated string into a dataset? =?Utf-8?B?ZGF2aWQ=?= ASP .Net 6 03-30-2007 02:12 PM
string to formated datetime Alejandro Penate-Diaz ASP .Net 2 03-11-2005 03:04 PM
tkinter canvas bitmaps w/ xbm formated string data? Brett Python 1 08-14-2004 07:49 PM



Advertisments