Re: (noob alert) why doesn't this work?

Discussion in 'Python' started by Bouke Woudstra, Mar 22, 2005.

  1. Thanks for all suggestions. Your first point I knew, but was too lazy to type
    it. It made no difference for the test files had all tags. Knowing that it
    was not a asynchrous thing helped me a lot though. There had to be something
    wrong with the command line for metaflac.

    It turned out that some flac files have tags like Artist=artistname and others
    have artist=artistname. Therefore it couldn't find the artist! So now I just
    look for 'rtist=' which works great.

    Thanks for all the help!

    Op dinsdag 22 maart 2005 12:18, schreef Simon Brunning:
    > On Tue, 22 Mar 2005 12:10:50 +0100, Bouke Woudstra
    >
    > <> wrote:
    > > Hi,
    > >
    > > I'm a bit stuck with this python script. It's aim is to encode all flac
    > > files to wav and then to mp3. The only problem I have is to preserve the
    > > tags. The code works when there's just one flac file in a directory but
    > > fails for more. I can't see why the for loop fails here (flactags).
    > >
    > > The error thrown is: UnboundLocalError: local variable 'title' referenced
    > > before assignment

    >
    > Perhaps you get to line 56 without having gone through line 51? Might
    > one of your flac files not have a title line?
    >
    > > I don't understand why this isn't assigned. I suspect that self.wav2mp3
    > > doesn't wait for the first part to finish, but how to force it to wait?

    >
    > Nope. Looks asynchronous to me.
    >
    > --
    > Cheers,
    > Simon B,
    > ,
    > http://www.brunningonline.net/simon/blog/
    Bouke Woudstra, Mar 22, 2005
    #1
    1. Advertising

  2. Bouke Woudstra wrote:
    > It turned out that some flac files have tags like Artist=artistname and others
    > have artist=artistname. Therefore it couldn't find the artist! So now I just
    > look for 'rtist=' which works great.


    You might want try using something like this:

    wanted = set('artist album date title tracknumber genre'.split())
    ...

    def read_and_call(self, flac, source):
    parts = {}
    for line in source:
    try:
    head, remainder = line.split('=', 1)
    except ValueError:
    pass # No equal sign in the line
    else:
    head = head.strip().lower()
    if head in wanted:
    parts[head] = remainder.strip()
    self.wav2mp3(flac, **parts)
    Scott David Daniels, Mar 22, 2005
    #2
    1. Advertising

  3. On Tue, 22 Mar 2005 09:21:49 -0800, Scott David Daniels <> wrote:

    >Bouke Woudstra wrote:
    >> It turned out that some flac files have tags like Artist=artistname and others
    >> have artist=artistname. Therefore it couldn't find the artist! So now I just
    >> look for 'rtist=' which works great.

    >
    >You might want try using something like this:
    >
    > wanted = set('artist album date title tracknumber genre'.split())
    > ...
    >
    > def read_and_call(self, flac, source):
    > parts = {}
    > for line in source:
    > try:
    > head, remainder = line.split('=', 1)
    > except ValueError:
    > pass # No equal sign in the line
    > else:
    > head = head.strip().lower()
    > if head in wanted:
    > parts[head] = remainder.strip()
    > self.wav2mp3(flac, **parts)


    Doesn't wav2mp3 have to have default arg values for that, if parts doesn't have all the args?

    Alternatively (and untested!) hopefully providing all wav2mp3 args in order:

    def flactags(self):
    flacfiles = glob.glob('*flac')
    flacfiles.sort()
    for flac in flacfiles:
    cmd = 'metaflac --export-tags=- "%s"' % flac
    # collect available tags, then make arg list with '??' for missing values
    tagdict = dict((pair[0].strip().lower(), pair[1].strip()) for pair in
    (line.split('=', 1) for line in os.popen(cmd).readlines()) if len(pair)==2)
    args = [tagdict.get(tag, '??') for tag in 'artist, album, year, number, genre'.split()]
    self.wav2mp3(flac, *args)

    Regards,
    Bengt Richter
    Bengt Richter, Mar 23, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bouke Woudstra

    (noob alert) why doesn't this work?

    Bouke Woudstra, Mar 22, 2005, in forum: Python
    Replies:
    1
    Views:
    251
    Diez B. Roggisch
    Mar 22, 2005
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,844
    Smokey Grindel
    Dec 2, 2006
  3. John Maclean
    Replies:
    12
    Views:
    232
    Robert Klemme
    Jan 21, 2006
  4. John Maclean

    Files and variables (noob alert)

    John Maclean, Jan 27, 2006, in forum: Ruby
    Replies:
    3
    Views:
    90
    John Maclean
    Jan 27, 2006
  5. Vincent Angeloni

    noob 'why doesn't this work' question

    Vincent Angeloni, Apr 14, 2008, in forum: Ruby
    Replies:
    15
    Views:
    165
    Stephen Schor
    Apr 15, 2008
Loading...

Share This Page