use sqlalchemy, it will do all that for you, and many other nice things. and it addresses the issue that you and Hermann mention and many others...
otherwise, you can copy the code for Storage (http://bazaar.launchpad.net/~anandology/webpy/webpy.dev/annotate/aaronsw%40frabjous.local-20080507190053-rk2uln4j6q0kbwcn?file_id=75%402c515a54-6b0d-0410-94c8-a23cf037ada6%3A%3Atrunk%252Fweb%252Futils.py) and override __setattr__ so it does something like: self[key] = value self.dirty[key] = value and then in save(), you'd have: db.update('users', self.id, **self.dirty) self.dirty = {} but, really, go with sqlalchemy. -b On Wed, May 14, 2008 at 2:17 PM, paul jobs <[EMAIL PROTECTED]> wrote: > Brent > thanks i m using the storage object now > any thoughts on how to implement the save() method of class user instead of > doing a massive update, is there a pythonic way to set the dirty columns and > save only those using db.update > thanks > > def save(self): > db.update('users',int(self.id),rating=self.rating, > last_name=self.last_name, pageviews=self.pageviews, ip=self.ip, > number_polls=self.number_polls, site=self.site, hotmail=self.hotmail, > number_activities=self.number > _activities, skype=self.skype, city=self.city,rawpassword=self.rawpassword, > number_useraudios=self.number_useraudios, zip=self.zip, > number_votes=self.number_votes,last_login=self.last_login, > number_userpics=self.number_userpics, music=self.music, email=self.email, > number_pages=self.number_pages, number_posts=self.number_posts, > number_comments=self.number_comments,number_pictures=self.number_pictures, > first_name=self.first_name, yahoo=self.yahoo, groups=self.groups, > heroes=self.heroes,favoritestuff=self.favoritestuff, > number_usermessages_sent=self.number_usermessages_sent, vanity=self.vanity, > interests=self.interests,television=self.television, > number_uservideos=self.number_uservideos, dob=self.dob, > gender=self.gender,number_friends=self.number_friends, > liketomeet=self.liketomeet, htmlcodes=self.htmlcodes, aim=self.aim, > movies=self.movies, books=self.books, profilepic=self.profilepic, > number_usermessages=self.number_usermessages,email_subscribe=self.email_subscribe, > number_communities=self.number_communities) > > this is user.save() > method > is there any way to figure out which key is changed and update only that key > this seems wasteful and unpythonic > - Hide quoted text - > > > On Wed, May 14, 2008 at 3:35 AM, Hermann Kaser <[EMAIL PROTECTED]> > wrote: > > > Doing a query everytime a property of an object changes is highly > > unefficient. I have a user object very similar to yours for a site of > > mine and it works this way: > > > > Constructor recieves the user row from the database (although you > > could make it recieve a user id and pull the info off the database in > > the constructor) and sets all the properties. > > Then you can change the values of the properties accessing them > > directly or if you prefer you can have get/set methods. When you're > > done with the user I have a save method, which just is a update query > > that grabs all the info from the object properties. That way I can do > > > > user_data = web.select('users', where='id=X') > > user = User(user_data) > > > > user.number_comments = user.number_comments + 1 > > > > user.save() > > > > > > > > > > > > On Wed, May 14, 2008 at 1:15 PM, Brent Pedersen <[EMAIL PROTECTED]> wrote: > > > > > "better" is relative. it is less code. (i dont think you need that > > setattr, just do it directly.) > > and i think you'd just use: > > j.keys() > > you wont seem the attrs in the autocompletion because they are dynamic. > > -b > > > > > > > > > > On Wed, May 14, 2008 at 12:41 PM, Paul Jobs <[EMAIL PROTECTED]> wrote: > > > > > > class User(Storage): > > > > > > def __init__(id): > > > self.userrow=db.query('select * from users where id= > > > $self._id',vars=locals())[0] > > > for k in self.userrow.keys(): > > > setattr(self,k,self.userrow[k]) > > > #self.k=self.userrow[k] > > > > > > both setattra and self.k = self.userrow methods are not working after > > > inheriting from storage > > > > > > >>> j. > > > j.__class__ j.__gt__ j.__repr__ > > > j.getliveuserrow j.popitem > > > j.__cmp__ j.__hash__ j.__setattr__ j.getphotos > > > j.publishfeed > > > j.__contains__ j.__init__ j.__setitem__ > j.getuserrow > > > j.save > > > j.__delattr__ j.__iter__ j.__str__ j.has_key > > > j.setdefault > > > j.__delitem__ j.__le__ j.__weakref__ j.items > > > j.setlastlogin > > > j.__dict__ j.__len__ j.clear j.iteritems > > > j.setlastlogin300 > > > j.__doc__ j.__lt__ j.copy j.iterkeys > > > j.update > > > j.__eq__ j.__module__ j.fromkeys > j.itervalues > > > j.values > > > j.__ge__ j.__ne__ j.get j.keys > > > j.__getattr__ j.__new__ j.getfeeds > > > j.kissallfriends > > > j.__getattribute__ j.__reduce__ j.getfriends > > > j.pokeallfriends > > > j.__getitem__ j.__reduce_ex__ j.getlastloginstr j.pop > > > > > > > > > without Storage > > > class User: > > > all the column elements are accessible as j.htmlcodes j.first_name and > so > > > on > > > j. > > > j.__class__ j.hotmail > j.number_userpics > > > j.__doc__ j.htmlcodes > j.number_uservideos > > > j.__init__ j.id j.number_votes > > > j.__module__ j.interests j.pageviews > > > j.aim j.ip j.password > > > j.books j.kissallfriends > j.pokeallfriends > > > j.city j.last_login j.profilepic > > > j.created j.last_name j.publishfeed > > > j.dob j.liketomeet j.rating > > > j.email j.movies j.rawpassword > > > j.email_subscribe j.music j.save > > > j.favoritestuff j.myrand j.setlastlogin > > > j.first_name j.number_activities > j.setlastlogin300 > > > j.gender j.number_comments j.site > > > j.getfeeds j.number_communities j.skype > > > j.getfriends j.number_friends j.television > > > j.getlastloginstr j.number_pages j.userid > > > j.getliveuserrow j.number_pictures j.username > > > j.getphotos j.number_polls j.userrow > > > j.getuserrow j.number_posts j.validuser > > > j.groups j.number_useraudios j.vanity > > > j.hash j.number_usermessages j.yahoo > > > j.heroes j.number_usermessages_sent j.zip > > > > > > > > > Brent Pedersen wrote: > > > i think you have it all, just have User be inherit from Storage: > > > > > > from web.utils import Storage > > > class User(Storage): > > > def __init__(....) > > > > > > -brent > > > > > > > > > > > > On Wed, May 14, 2008 at 12:45 AM, Paul <[EMAIL PROTECTED]> wrote: > > > > > > > > > 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()) > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "web.py" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/webpy?hl=en -~----------~----~----~----~------~----~------~--~---
