[Tutor] Help with class.self variables

2008-05-14 Thread Mark




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@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] James D Mcclatchey is out of the office.

2008-05-14 Thread James D Mcclatchey

I will be out of the office starting  05/14/2008 and will not return until
05/19/2008.

I will respond to your message when I return.


***IMPORTANT NOTICE: This communication, including any attachment, contains 
information that may be confidential or privileged, and is intended solely for 
the entity or individual to whom it is addressed.  If you are not the intended 
recipient, you should delete this message and are hereby notified that any 
disclosure, copying, or distribution of this message is strictly prohibited.  
Nothing in this email, including any attachment, is intended to be a legally 
binding signature.***

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with class.self variables

2008-05-14 Thread Kent Johnson
On Wed, May 14, 2008 at 3:45 AM, Mark [EMAIL PROTECTED] 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@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] destroying a window once a movie has stoped playing

2008-05-14 Thread bhaaluu
On Tue, May 13, 2008 at 9:07 PM,  [EMAIL PROTECTED] wrote:


 def movieu(self):
  mov_name = video.mpg
  pygame.mixer.quit()
  screen = pygame.display.set_mode((320, 240))
  video = pygame.movie.Movie(mov_name)
  screen = pygame.display.set_mode(video.get_size())
  video.play()
 while video.get_busy():
  for event in pygame.event.get():
  if event.type == pygame.QUIT:
  break

 this is the peice of code i am using to run a movie in my scissors paper
 rock game so when rock beats scissors a movie will play. i can get the movie
 to play but i would like to no if i can make the window destroy once the
 movie has stoped
  
  This message is intended for the addressee named and may contain privileged
 information or confidential information or both. If you are not the intended
 recipient please delete it and notify the sender.

 ___
  Tutor maillist  -  Tutor@python.org
  http://mail.python.org/mailman/listinfo/tutor


Hey Nick,
You may have better luck asking pygame-specific questions
on the pygame mailing list?

Pygame maintains an active mailing list. You can email the list at
[EMAIL PROTECTED]

To get on or off the mailing list, send an email message to
[EMAIL PROTECTED] with a simple command in the body.
Some examples; (note, no subject is needed). If subscribing or
unsubscribing, there's no need to enter any extra email address
information. Just use the commands like they are below, the email
address you mail from is the address mail will go to (or stop going to).
(from: http://pygame.org/wiki/info)

I think the Tutor list mainly helps with business type scripting
such as accessing a database, opening, writing, reading, closing
files, working with lists, dictionaries, tuples, and so forth? The
pygame list is specific to pygame, and the gurus there can
answer any pygame question you ask them!

Happy Programming!
-- 
b h a a l u u at g m a i l dot c o m
Kid on Bus: What are you gonna do today, Napoleon?
Napoleon Dynamite: Whatever I feel like I wanna do. Gosh!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Else Clause In A Loop

2008-05-14 Thread Simón A. Ruiz

kinuthia muchane wrote:

On Tue, 2008-05-13 at 11:09 -0400, Simón A. Ruiz wrote:
When i is 3, then we'll only check (2 % 3 == 0) which is False, so the 
loop ends unbroken and runs the else clause letting us know that 3 is 
indeed a prime number.
 
Shouldn't we be checking for (3%2 == 0) instead of (2%3 == 0)?


Absolutely right. My mistake.


Does this help at all?


It was crystal clear! Thanks Simon (I didn't know how to put the accent
mark over the 'o', though), the confusion is gone. ;)


Awesome. Glad I could be of help. :-D

Simón
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] basic lists and loops question

2008-05-14 Thread bob gailer

Jon Crump wrote:
Something basic about lists and loops that I'm not getting here. I've 
got a long list of dictionaries that looks like this:


lst = [{'placename': u'Stow, Lincolnshire', 'long-name': u'Stow, 
Lincolnshire.', 'end': datetime.date(1216, 9, 28), 'start': 
datetime.date(1216, 9, 26)},
{'placename': u'Lincoln, Lincolnshire', 'long-name': u'Lincoln, 
Lincolnshire.', 'end': datetime.date(1216, 9, 30), 'start': 
datetime.date(1216, 9, 28)},
{'placename': u'Lincoln, Lincolnshire', 'long-name': u'Lincoln, 
Lincolnshire.', 'end': datetime.date(1216, 10, 2), 'start': 
datetime.date(1216, 10, 1)},
{'placename': u'Grimsby, Lincolnshire', 'long-name': u'Grimsby, 
Lincolnshire.', 'end': datetime.date(1216, 10, 4), 'start': 
datetime.date(1216, 10, 3)},
{'placename': u'Louth, Lincolnshire', 'long-name': u'Louth, 
Lincolnshire.', 'end': datetime.date(1216, 10, 4), 'start': 
datetime.date(1216, 10, 4)}

]

I have a function that searches through them to find pairs of 
dictionaries that satisfy certain criteria. When the nested loops find 
such a pair, I need to merge them. So far so good. This works:


def events(data):
  evts = []
  for x in lst:
for y in lst:
  if (x['placename'] == y['placename']) and (x['end'].month + 1 == 
y['start'].month) and (y['start'] - x['end'] == datetime.timedelta(1)):
evts.append({'placename': x['placename'], 'long-name': 
x['long-name'], 'start': x['start'], 'end': y['end']})

evts.append(x)
  return evts

for x in events(lst):
  print x

But then I need to delete the two original dictionaries that I merged. 
If I do del x, I get an error local variable 'x' referenced before 
assignment


Try lst.remove(x)


I've also tried decorating the processed dictionaries in the if loop 
thus:

x['processed'] = True
y['processed'] = True

Then when I call events() I get back the merged dict and the decorated 
dicts:


{'placename': u'Lincoln, Lincolnshire', 'end': datetime.date(1216, 10, 
2), 'start': datetime.date(1216, 9, 28), 'long-name': u'Lincoln, 
Lincolnshire.'}
{'placename': u'Lincoln, Lincolnshire', 'processed': True, 'end': 
datetime.date(1216, 9, 30), 'start': datetime.date(1216, 9, 28), 
'long-name': u'Lincoln, Lincolnshire.'}
{'placename': u'Lincoln, Lincolnshire', 'processed': True, 'end': 
datetime.date(1216, 10, 2), 'start': datetime.date(1216, 10, 1), 
'long-name': u'Lincoln, Lincolnshire.'}


But if I try to call events() thus:

for x in events(lst):
  if x['processed'] == True:
print x

I get a KeyError.


Sounds like the key 'processed' is created by the assignment 
x['processed'] = True. So those dictionaries that have not experienced 
this assignment have no such key. You should instead use: if  
'processed' in x:


Also if x['processed'] == True: can be expressed if x['processed']: 
 


HTH

--
Bob Gailer
919-636-4239 Chapel Hill, NC

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] basic lists and loops question

2008-05-14 Thread Kent Johnson
On Wed, May 14, 2008 at 3:02 PM, Jon Crump [EMAIL PROTECTED] wrote:
 Something basic about lists and loops that I'm not getting here.
  I have a function that searches through them to find pairs of dictionaries
 that satisfy certain criteria. When the nested loops find such a pair, I
 need to merge them. So far so good. This works:

  def events(data):
   evts = []
   for x in lst:
 for y in lst:
   if (x['placename'] == y['placename']) and (x['end'].month + 1 ==
 y['start'].month) and (y['start'] - x['end'] == datetime.timedelta(1)):
 evts.append({'placename': x['placename'], 'long-name':
 x['long-name'], 'start': x['start'], 'end': y['end']})
 evts.append(x)
   return evts

  for x in events(lst):
   print x

  But then I need to delete the two original dictionaries that I merged. If I
 do del x, I get an error local variable 'x' referenced before assignment

Not sure why you got that error but it general it is not a good idea
to delete items from a container you are iterating; the results can be
unpredictable.

  I've also tried decorating the processed dictionaries in the if loop thus:
  x['processed'] = True
  y['processed'] = True

  Then when I call events() I get back the merged dict and the decorated
 dicts:

  {'placename': u'Lincoln, Lincolnshire', 'end': datetime.date(1216, 10, 2),
 'start': datetime.date(1216, 9, 28), 'long-name': u'Lincoln, Lincolnshire.'}
  {'placename': u'Lincoln, Lincolnshire', 'processed': True, 'end':
 datetime.date(1216, 9, 30), 'start': datetime.date(1216, 9, 28),
 'long-name': u'Lincoln, Lincolnshire.'}
  {'placename': u'Lincoln, Lincolnshire', 'processed': True, 'end':
 datetime.date(1216, 10, 2), 'start': datetime.date(1216, 10, 1),
 'long-name': u'Lincoln, Lincolnshire.'}

  But if I try to call events() thus:

  for x in events(lst):
   if x['processed'] == True:
 print x

  I get a KeyError.

The problem is that not every dict has the 'processed' key. Try
  if x.get('processed') == True:
or just
  if x.get('processed'):

x.get() will return None (instead of raising KeyError) if the key is
not present.

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] basic lists and loops question

2008-05-14 Thread Jon Crump

Bob, and Kent, Many thanks!

Sounds like the key 'processed' is created by the assignment x['processed'] = 
True. So those dictionaries that have not experienced this assignment have no 
such key. You should instead use: if  'processed' in x:


Doh! Now that WAS obvious


Try lst.remove(x)


Now this was odd. My trusty guide Learning Python (Lutz  Ascher) 2nd 
Ed. for python 2.3 says nothing about remove(). This must be 2.4 or 2.5 
yes?


this now has the desired effect (remains to be seen if it works for all 
instances):


def events(data):
  evts = []
  for x in data:
for y in data:
  if (x['placename'] == y['placename']) and (x['end'].month + 1 == 
y['start'].month) and (y['start'] - x['end'] == datetime.timedelta(1)):

x['end'] = y['end']
data.remove(x)
evts.append(x)
  return evts

I understand about removing elements from a container you're iterating. Is 
data.remove(x) problematic in this context?


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] basic lists and loops question

2008-05-14 Thread Kent Johnson
On Wed, May 14, 2008 at 6:03 PM, Jon Crump [EMAIL PROTECTED] wrote:
 def events(data):
  evts = []
  for x in data:
for y in data:
  if (x['placename'] == y['placename']) and (x['end'].month + 1 ==
 y['start'].month) and (y['start'] - x['end'] == datetime.timedelta(1)):
x['end'] = y['end']
data.remove(x)
evts.append(x)
  return evts

 I understand about removing elements from a container you're iterating. Is
 data.remove(x) problematic in this context?

Yes. It can cause the iteration to skip elements ofthe list. Better to
post-process the list with a list comprehension:
evts = [ evt for evt in evts if 'processed' not in evt ]

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] basic lists and loops question

2008-05-14 Thread Kent Johnson
On Wed, May 14, 2008 at 9:06 PM, Kent Johnson [EMAIL PROTECTED] wrote:

 I understand about removing elements from a container you're iterating. Is
 data.remove(x) problematic in this context?

 Yes. It can cause the iteration to skip elements of the list.

For example:
In [1]: l=range(5)
In [2]: for i in l:
   ...: print i
   ...: if i==2:
   ...: l.remove(i)
   ...:
   ...:
0
1
2
4

Notice how 3 is skipped. Don't do this!

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] basic lists and loops question

2008-05-14 Thread Jon Crump

Kent,

On Wed, 14 May 2008, Kent Johnson wrote:

I understand about removing elements from a container you're iterating. Is
data.remove(x) problematic in this context?


Yes. It can cause the iteration to skip elements ofthe list. Better to
post-process the list with a list comprehension:
evts = [ evt for evt in evts if 'processed' not in evt ]


Thanks very much. This became clear when I applied the .remove(x) solution 
to the whole list: very strange results. Altering x in the y loop also had 
unforseen effects. I finally decorated every dictionary acted upon so I 
could check everything that was going on. the appended merged dict got a 
merged key and the two dictionaries that were merged each got a 
processed key, then I post processed the list to remove the 'processed' 
dictionaries **whew**. Thanks for clarifying AND saving me from error.


Jon
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] looking for ways to get diff between two txt files

2008-05-14 Thread Kamal
Hi,

I would really appreciate if someone help to find a way to calculate
delta (diff) between two similar text files. I have looked at difflib
and found related script at this site
http://www.java2s.com/Code/Python/Utility/Printfilediffsincontextunifiedorndiffformats.htm

Is there a simple way to just get delta between two files? e.g. I have
below two files and the delta between these two files is in
diff_output.txt

file1.txt
hello world

file2.txt
hello world
hello python

diff_output.txt
hello python

-- 
Thanks,
Kamal
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor