Question

I just finished some online tutorial and i was experimenting with Discogs Api

My code is as follows :

import discogs_client as discogs

discogs.user_agent = '--' #i removed it 

artist_input = raw_input("Enter artist\'s name : ")
artist = discogs.Artist(artist_input)

if artist._response.status_code in (400, 401, 402, 403, 404):
    print "Sorry, this artist %s does not exist in Discogs database" % (artist_input)
elif artist._response.status_code in (500, 501, 502, 503) :  
    print "Server is a bit sleepy, give it some time, won\'t you ._."
else:
        print "Artist : " + artist.name
        print "Description : " + str(artist.data["profile"])
        print "Members :",
        members = artist.data["members"]
        for index, name in enumerate(members):
            if index == (len(members) - 1):
                print name + "."
            else:    
                print name + ",",

The list's format i want to work with, is like this:

[<MasterRelease "264288">, <MasterRelease "10978">,  <Release "4665127">...

I want to isolate those with MasterRelease, so i can get their ids

I tried something like

for i in artist.releases:
        if i[1:7] in "Master":
            print i

or

for i in thereleases:
        if i[1:7] == "Master":
       print i

I;m certainly missing something, but it buffles me since i can do this

newlist = ["<abc> 43242"]
print newlist[0][1]  

and in this scenario

thereleases = artist.releases    
print thereleases[0][1]

i get

TypeError: 'MasterRelease' object does not support indexing

Feel free to point anything about the code, since i have limited python knowledge yet.

Was it helpful?

Solution

You are looking at the Python representation of objects, not strings. Each element in the list is an instance of the MasterRelease class.

These objects have an id attribute, simply refer to the attribute directly:

for masterrelease in thereleases:
    print masterrelease.id

Other attributes are .versions for other releases, .title for the release title, etc.

OTHER TIPS

The problem is that you are working with objects and assuming them to be strings. artist.releases is a list containing a number of objects.

newlist = ["<abc> 43242"]
print newlist[0][1]

This works because newlist is a list which contains a single element, a string. so newlist[0] gives you a string '<abc> 43242'. Applying newlist[0][1] gets you the second element of that string, that is 'a'.

But this works only for objects that support indexing. And the objects that you are working with don't. What you are seeing in the list is a string representation of the object.

I cannot really help you further than this because I have no idea about the MasterRelease and the Release instances - but there should be some attribute in them that gets you the required value.

If its only the MasterRelease that you want to filter then the code below might do:

[master for master in artist.releases if 'MasterRelease' in str(type(master))]

Assuming MasterRelease is not in the current scope. Or else you could:

[master for master in artist.releases if isinstance(master, MasterRelease)]

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top