Seite 1 von 1

Automatische Hilfe von `optparse`

Verfasst: Dienstag 30. Januar 2007, 21:53
von CrackPod
Hallo,

im Zuge der Entwicklung meines derzeitigen Projekts hab ich mich mit optparse auseinadergesetzt. Nur bekomme ich bei diesem Code einen Fehler, wenn ich die automatisch generierte Hilfe benutzen möchte

Code: Alles auswählen

def main():
    u = "usage: %prog [options] DIR"
    v = "%prog",__version__
    d = "This programm deletes the *.txt files and renames the *.jpg",
    "file of all Jamendo subdirs in the given directory. It also creates",
    "playlists of the albums and if requestet of all albums from a author.",
    "You have also the possibility to change the directorytree."
    h={"-c":"prevents the renaming of the *.jpg file into cover.jpg.",
    "-t":"prevents the deleting of the *.txt files.",
    "-a":"tells the programm to change only the directoryname not the tree.",
    "-f":"cuts off the Jamendo text after the two hyphens.",
    "-v":"prints exactly what's being done.",
    "-p":"prevents the creating of playlists",
    "-d":"the folder DIR which contains the downloaded albumbs"}

    parser = OptionParser(usage=u, version=v, description=d,
                          add_help_option=True)

    parser.add_option("-f", "--folderrename", action="store_true",
                      dest="folderrename", default=False, help=h["-d"])
    parser.add_option("-v", "--verbose", action="store_true",
                      dest="verbose", default=False, help=h["-v"])
    parser.add_option("-t", "--notxtdelete", action="store_false",
                      dest="txtdelete", default=True, help=h["-t"])
    parser.add_option("-a", "--noartistdirs", action="store_false",
                      dest="artistdir", default=True, help=h["-a"])
    parser.add_option("-c", "--nocorrename", action="store_false",
                      dest="coverrename", default=True, help=h["-c"])
    parser.add_option("-p", "--noplaylists", action="store_false",
                      dest="playlists", default=True, help=h["-p"])
    parser.add_option("-d", "--directory", metavar="DIR",
                      dest="dir", help=h["-d"])

    (options, args) = parser.parse_args()
    print options
    print '-'*20
    print args
    if options.verbose:
        pass
    #jte=JTE(options)

if __name__ == '__main__':
    main()
Fehlerausgabe:

Code: Alles auswählen

tobsl@blechbuechse:~$ python torrentrename.py -h
Traceback (most recent call last):
  File "torrentrename.py", line 102, in ?
    main()
  File "torrentrename.py", line 93, in main
    (options, args) = parser.parse_args()
  File "/usr/lib/python2.4/optparse.py", line 1280, in parse_args
    stop = self._process_args(largs, rargs, values)
  File "/usr/lib/python2.4/optparse.py", line 1324, in _process_args
    self._process_short_opts(rargs, values)
  File "/usr/lib/python2.4/optparse.py", line 1431, in _process_short_opts
    option.process(opt, value, values, self)
  File "/usr/lib/python2.4/optparse.py", line 712, in process
    return self.take_action(
  File "/usr/lib/python2.4/optparse.py", line 733, in take_action
    parser.print_help()
  File "/usr/lib/python2.4/optparse.py", line 1538, in print_help
    file.write(self.format_help())
  File "/usr/lib/python2.4/optparse.py", line 1526, in format_help
    result.append(self.format_description(formatter) + "\n")
  File "/usr/lib/python2.4/optparse.py", line 998, in format_description
    return formatter.format_description(self.get_description())
  File "/usr/lib/python2.4/optparse.py", line 1449, in get_description
    return self.expand_prog_name(self.description)
  File "/usr/lib/python2.4/optparse.py", line 1446, in expand_prog_name
    return s.replace("%prog", self.get_prog_name())
AttributeError: 'tuple' object has no attribute 'replace'
Nur hab ich selber kein Tupel eingebaut - oder ich finde es nicht. Ist das ein Bug von optparse? Was hab ich falsch gemacht? :wink:

Bitte klärt mich mal auf :)
LG Tobi

Verfasst: Dienstag 30. Januar 2007, 22:13
von Rebecca

Code: Alles auswählen

>>> __version__ = "1.1.1"
>>> v = "%prog", __version__
>>> type(v)
<type 'tuple'>

Verfasst: Dienstag 30. Januar 2007, 22:22
von CrackPod
Hallo,

danke - das war das einzige, was für mich in Frage kam, was ich aber nich geändert hab und getestet hab, weil man doch bei prints auch Kommata benutzen kann, um Zeichenketten zu verketten. Wieso geht das bei Variablen zuweisungen nicht? Da fehlen doch noch die Klammern, die man für Tupelzuweisungen benutzt odeR?
Versteh ich grad nich..

LG Tobi

--------------- Edit: -----------------
Hab den Code geändert

Code: Alles auswählen

def main():
    __version__ = '0.0.1a'
    u = "usage: %prog [options] DIR"
    d = "This programm deletes the *.txt files and renames the *.jpg",
    "file of all Jamendo subdirs in the given directory. It also creates",
    "playlists of the albums and if requestet of all albums from a author.",
    "You have also the possibility to change the directorytree."
    h={"-c":"prevents the renaming of the *.jpg file into cover.jpg.",
    "-t":"prevents the deleting of the *.txt files.",
    "-a":"tells the programm to change only the directoryname not the tree.",
    "-f":"cuts off the Jamendo text after the two hyphens.",
    "-v":"prints exactly what's being done.",
    "-p":"prevents the creating of playlists",
    "-d":"the folder DIR which contains the downloaded albumbs"}

    parser = OptionParser(usage=u, version=__version__, description=d,
                          add_help_option=True)
Bekomme aber immernoch diesselbe Fehlermeldung.

Verfasst: Mittwoch 31. Januar 2007, 11:06
von BlackJack
CrackPod hat geschrieben:danke - das war das einzige, was für mich in Frage kam, was ich aber nich geändert hab und getestet hab, weil man doch bei prints auch Kommata benutzen kann, um Zeichenketten zu verketten. Wieso geht das bei Variablen zuweisungen nicht?
Bei ``print`` trennt das Komma die einzelnen Argumente und verkettet keine Zeichenkette(n).
Da fehlen doch noch die Klammern, die man für Tupelzuweisungen benutzt odeR?
Klammern erzeugen keine Tupel, sondern die Kommata tun das. Einzige Ausnahme ist das leere Tupel, dass man mit ``()`` bekommt. Schon bei einem einelementigen Tupel braucht man zwingend das Komma: ``(1,)``; anderfalls wäre das einfach eine ``1`` in Klammern.

Ferner braucht man Klammern, wenn das Konstrukt sonst nicht eindeutig wäre. Zum Beispiel: ``[a, b for b in c]``. Ist das eine Liste von Tupeln mit festem `a` und jeweils einem `b` aus `c` oder eine Liste mit zwei Elementen ─ ein `a` und ein Generator-Objekt.

Code: Alles auswählen

In [32]: [a, b for b in c]
------------------------------------------------------------
   File "<ipython console>", line 1
     [a, b for b in c]
             ^
SyntaxError: invalid syntax


In [33]: [(a, b) for b in c]
Out[33]: [(42, 0), (42, 1), (42, 2)]

In [34]: [a, (b for b in c)]
Out[34]: [42, <generator object at 0xb78e7c4c>]

Verfasst: Mittwoch 31. Januar 2007, 18:37
von CrackPod
ok. Dankeschön für die Erklärung. Das einzige Problem, was ich noch habe, dass ich trotz des geänderten Codes noch immer Probleme mit dem Tupel habe.

Code: Alles auswählen

def main():
    u = "usage: %prog [options] DIR"
    d = "This programm deletes the *.txt files and renames the *.jpg",
    "file of all Jamendo subdirs in the given directory. It also creates",
    "playlists of the albums and if requestet of all albums from a author.",
    "You have also the possibility to change the directorytree."
    h={"-c":"prevents the renaming of the *.jpg file into cover.jpg.",
    "-t":"prevents the deleting of the *.txt files.",
    "-a":"tells the programm to change only the directoryname not the tree.",
    "-f":"cuts off the Jamendo text after the two hyphens.",
    "-v":"prints exactly what's being done.",
    "-p":"prevents the creating of playlists",
    "-d":"the folder DIR which contains the downloaded albumbs"}

    parser = OptionParser(usage=u, version=__version__, description=d,
                          add_help_option=True)

    parser.add_option("-f", "--folderrename", action="store_true",
                      dest="folderrename", default=False, help=h["-d"])
    parser.add_option("-v", "--verbose", action="store_true",
                      dest="verbose", default=False, help=h["-v"])
    parser.add_option("-t", "--notxtdelete", action="store_false",
                      dest="txtdelete", default=True, help=h["-t"])
    parser.add_option("-a", "--noartistdirs", action="store_false",
                      dest="artistdir", default=True, help=h["-a"])
    parser.add_option("-c", "--nocorrename", action="store_false",
                      dest="coverrename", default=True, help=h["-c"])
    parser.add_option("-p", "--noplaylists", action="store_false",
                      dest="playlists", default=True, help=h["-p"])
    parser.add_option("-d", "--directory", metavar="DIR",
                      dest="dir", help=h["-d"])

    (options, args) = parser.parse_args()
LG Tobi

Verfasst: Mittwoch 31. Januar 2007, 19:41
von Rebecca
Du machst in der Variable "d" genau den gleichen Fehler.

Verfasst: Mittwoch 31. Januar 2007, 19:45
von birkenfeld
Und zwar nicht nur einen:

"d" ist hier ein 1-Tupel, und die folgenden Zeilen sind jeweils Expression Statements (1-Tupel) ohne Auswirkung.

Verfasst: Mittwoch 31. Januar 2007, 20:21
von CrackPod
Rebecca hat geschrieben:Du machst in der Variable "d" genau den gleichen Fehler.
Ok, jetz wirds richtig peinlich...
Danke LG