Mark | 14 May 09:42

Help with class.self variables

im trying to create a class user
so that

i can do

r=User(1)


r._user.keys()
['rating', 'last_name', 'pageviews', 'ip', 'number_polls', 'site', 'myrand', 'hotmail', 'number_activities', 'skype', 'id', 'city', 'rawpassword', 'number_useraudios', 'zip', 'number_votes', 'last_login', 'number_userpics', 'music', 'email', 'number_pages', 'username', 'number_posts', 'hash', 'number_comments', 'number_pictures', 'first_name', 'yahoo', 'groups', 'heroes', 'favoritestuff', 'number_usermessages_sent', 'vanity', 'interests', 'television', 'number_uservideos', 'created', 'dob', 'gender', 'number_friends', 'liketomeet', 'htmlcodes', 'aim', 'movies', 'password', 'books', 'profilepic', 'number_usermessages', 'email_subscribe', 'number_communities']

so i want this to become

r.rating
r.last_name
r.pageviews and so on
r.username
r.photos()
and so on

but

i want to set the keys of the user row as the keys of User self
but i m not sure how to do it

        for i in self._user.keys():
            self[i]=self._user[i]

gives an error
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "api.py", line 40, in __init__
    self[i]=self._user[i]
AttributeError: instance has no attribute '__setitem__'

You can do something like self.update(_user) doesnt work either
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "api.py", line 39, in __init__
    self.update(self._user)
AttributeError: user instance has no attribute 'update'

Any thoughts on how to set this?

class User:
    """user object
    """

    def __init__(self, id):
        self._id = id
        self._user=db.query('select * from users where id=$id',vars=locals())
        if not self._user:
            self._validuser=False
            self._username=''
            self._firstname=''
            self._lastname=''
            self._dob=''
            self._gender=''
            self._sex=''
        else:
            self._user=self._user[0]
            self._validuser=True
        self._username=self._user.username
        self._firstname=self._user.first_name
        self._lastname=self._user.last_name
        self._dob=self._user.dob
        self._gender=self._user.gender
        self._sex=self._user.gender
        for i in self._user.keys():
            self[i]=self._user[i]
    def userrow(self):
        return db.query('select * from users where id=$self._id',vars=locals())[0]
    def photos(self):
        return db.query('select pic,id, username from userpics where user_id=$self._id',vars=locals())


t
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor
Kent Johnson | 14 May 14:16

Re: Help with class.self variables

On Wed, May 14, 2008 at 3:45 AM, Mark <mobiledreamers <at> gmail.com> wrote:

>  i want to set the keys of the user row as the keys of User self
>  but i m not sure how to do it
>
>          for i in self._user.keys():
>              self[i]=self._user[i]

Should be
  setattr(self, i, self._user[i])

>  You can do something like
>
>  self.update(_user)

Should be
  self.__dict__.update(self._user)

You should probably consider an existing object-relation mapper such
as SQLAlchemy, SQLObject or Django ORM rather than rolling your own.

Kent
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor


Gmane