You are not logged in.

#1 2014-12-18 23:11:45

Rasi
Member
From: Germany
Registered: 2007-08-14
Posts: 1,914
Website

[SOLVED] python: write a list, sorted by column and unify it?

So I have this function in python:

def lastCache(args):                                                            
    artistlist=client.list('artist')                                            
    content = set()                                                             
    errors = set()                                                              
    errors2 = set()                                                             
    fields = ('last-modified', 'date', 'albumartist', 'album')                  
    fh = open(os.getenv('HOME')+'/.config/clerk/last.cache', "w")               
    for artist in artistlist:                                                   
        try:                                                                    
            for element in client.find('albumartist', artist):                  
                content.add(os.getenv('seperator').join((element[f] for f in fields)))
            for line in sorted(content, key=str.lower, reverse=True):           
                fh.write(line+"\n")                                        
        except KeyError:                                                        
            errors.add(element['file'])                                         
        except TypeError:                                                       
            errors2.add(element['file'])                                        
    for line in sorted(errors, key=str.lower):                                  
        print("Missing tags for file: "+line, file=sys.stderr)                  
    for line in sorted(errors2, key=str.lower):                                 
        print("Duplicate tags for file: "+line, file=sys.stderr)                
    fh.close()                                                                  

Goal: a list of albums in mpd, formatted as "date $seperator artist $seperator album"

What works: a list is created

Issues:
last-modified is needed to sort the list, but should not be in final output.
the list should be without duplicates.

I am a total python newb and this seems to be above my head smile

Last edited by Rasi (2014-12-19 13:35:25)


He hoped and prayed that there wasn't an afterlife. Then he realized there was a contradiction involved here and merely hoped that there wasn't an afterlife.

Douglas Adams

Offline

#2 2014-12-18 23:47:43

SahibBommelig
Member
From: Germany
Registered: 2010-05-28
Posts: 80

Re: [SOLVED] python: write a list, sorted by column and unify it?

You could do something along the lines of:

for element in client.find('albumartist', artist):                  
    content.add(element[f] for f in fields)

seen = set()
for element in sorted(content, key=lambda elem: elem[0]):
    if element not in seen:
        line = element[1:]
        fh.write(os.getenv('seperator').join(line), '\n')
        seen.add(line)

(Not tested - content could/should be changed to a normal list/deque too)

Offline

#3 2014-12-19 00:44:18

Rasi
Member
From: Germany
Registered: 2007-08-14
Posts: 1,914
Website

Re: [SOLVED] python: write a list, sorted by column and unify it?

...gives no output at all...


He hoped and prayed that there wasn't an afterlife. Then he realized there was a contradiction involved here and merely hoped that there wasn't an afterlife.

Douglas Adams

Offline

#4 2014-12-19 13:35:14

Rasi
Member
From: Germany
Registered: 2007-08-14
Posts: 1,914
Website

Re: [SOLVED] python: write a list, sorted by column and unify it?

Thanks sahib vor helping me out on IRC...

this is the solution:

def lastCache(args):                                                            
    artistlist=client.list('albumartist')                                       
    content = set()                                                             
    errors = set()                                                              
    errors2 = set()                                                             
    fields = ('last-modified', 'date', 'albumartist', 'album')                  
    fh = open(os.getenv('HOME')+'/.config/clerk/last.cache', "w")               
    seen = set()                                                                
    for artist in artistlist:                                                   
        try:                                                                    
            for element in client.find('albumartist', artist):                  
                content.add(tuple(element[f] for f in fields))                  
            for element in sorted(content, key=lambda elem: elem[0]):           
                if element[1:] not in seen:                                     
                    line = element[1:]                                          
                    fh.write(os.getenv('seperator').join(line)+"\n")            
                    seen.add(line)                                              
        except KeyError:                                                        
            errors.add(element['file'])                                         
        except TypeError:                                                       
            errors2.add(element['file'])                                        
    for line in sorted(errors, key=str.lower):                                  
        print("Missing tags for file: "+line, file=sys.stderr)                  
    for line in sorted(errors2, key=str.lower):                                 
        print("Duplicate tags for file: "+line, file=sys.stderr)                
    fh.close()                                                                  

He hoped and prayed that there wasn't an afterlife. Then he realized there was a contradiction involved here and merely hoped that there wasn't an afterlife.

Douglas Adams

Offline

Board footer

Powered by FluxBB