[Tutor] Is this the right way to create a

Kent Johnson kent37 at tds.net
Sun Jun 22 13:24:43 CEST 2008


On Sun, Jun 22, 2008 at 12:00 AM, Zameer Manji <zmanji at gmail.com> wrote:
> I'm trying to create a library for the Last.fm webservice[1] and the
> first thing I created was a class for the Profile Information.[2] Is
> this the proper way of creating it? Is this useful to another programmer?
>
> import urllib
> import xml.etree.ElementTree as ET
> from BeautifulSoup import BeautifulStoneSoup as BSS
> BASEURL = 'http://ws.audioscrobbler.com/1.0/user/'
> class UserProfile(object):
>    """Represents the user profile data"""
>    def __init__(self, username):
>        """Give the username"""
>        self.username = username
>    def getxmldata(self):
>        url = BASEURL + self.username + '/profile.xml'
>        self.xmldata = urllib.urlopen(url).read()
>    def parsedata(self):
>        soup = BSS(self.xmldata)
>        self.url = soup.url.string
>        <snip>

This looks like a good start. A few things I would change:
- getxmldata() will be shared among all your API routines so you might
make it a separate function that returns the data instead of assigning
it to an attribute.
- This class is probably not very useful without the parsed data so
you might call getxmldata() & parsedata() from the constructor
(__init__() method). There is no reason to make client code call
these. You certainly don't want your client code to have to call
getxmldata() and parsedata() both in the correct order.
- You might want to use one of the two common naming conventions for
your methods, either getXmlData() or get_xml_data().

A couple of other ways you could organize this that might be useful:
- Have the UserProfile constructor take a BeautifulSoup node as its
argument. Then have a separate function that gets and parses the XML.
This wil be a useful structure for Artists, for example, because you
have to parse lists of Artists.
- alternately, you could have a function that pokes values into a UserProfile.

> Also, how do I then begin to approach the whole API ? Do I create a User
> class the inherits from the UserProfile class and other classes for
> their Neighbours', Top Artists, etc ? Do a create a separate class for
> each web service ? I have never coded something like this before and all
> advice is welcome.

A User class that has a UserProfile as an attribute, and accessors for
Neighbors, etc, sounds good to me. You may want an Artist class,
probably not a Top Artists class. The User.getTopArtists() method
would access the web services API and return a list of Artists. At a
first guess, it looks like you may want classes for each of the types
listed under "Categories" on the Web Services page.

Pull as much of the web services code into shared functions as
possible (or possibly a utility class). For example, you will probably
want a function that creates a list of Artists from a parsed XML page.

Kent


More information about the Tutor mailing list