Re: [Tutor] MySQLdb question

2006-04-14 Thread Allen John Schmidt, Jr.
Patty wrote:

I have a data structure in a python file that looks something like this:

my_map= { host1:  {target1, target2, target3 },
   host2:  {target4, target5, target6 }, 
 }

cursor.execute(SELECT %s FROM targets
WHERE target_name = %s  %  (ahost, target))

Can anybody show me the right way to do it?
  


Hi Patty,

I don't know if this is the right way, but here is basically how I would 
do it.

First of all, I will assume that you are looping over the items in the 
dictionary to get the variables ahost and target.

for ahost,target in my_map:
cursor.execute(SELECT %s FROM targets
WHERE target_name in (%s)  %  (ahost, ,.join(target)))

This would generate a list that MySQL will look through to find 
target_name. It is the same as one would do in Python to find an item in 
a list:

if target_name in (target1, target2, target3)


Hope that helps!
Allen J. Schmidt, Jr.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] printing an acronym (fwd)

2005-09-26 Thread Allen John Schmidt, Jr.




Or a shorter version,
a=lambda n: "".join([x[0].upper() for x in n.split()])

Then it is just:
 a('random access memory')
'RAM'





Danny Yoo wrote:

  Forwarding to tutor

-- Forwarded message --
Date: Mon, 26 Sep 2005 08:32:02 -0500
From: Jason Massey [EMAIL PROTECTED]
To: Danny Yoo [EMAIL PROTECTED]
Subject: Re: [Tutor] printing an acronym

Something like this:

def acro(a):
... 	b = a.split()
... 	c = ""
... 	for d in b:
... 		c+=d[0].upper()
... 	return c

other than the horrible variable naming, it works.

  
  

  
acro('international business machines')

  

  
  'IBM'

On 9/25/05, Danny Yoo [EMAIL PROTECTED] wrote:
  



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


Re: [Tutor] Please help

2005-08-03 Thread Allen John Schmidt, Jr.
Hey, I had a similar problem not too long ago. My data came in the first 
format, but I didn't need it formated like that.

Here is how I would have written it:

import re
col=re.compile('(AD.*?)\s*$')
datas=re.compile('\s*(.+?)\s+(.+?)')
f1 = open('xx','r')
mind={}
matching=''

for i in meat:
match=col.find(i)
if match:
mind[match.group(1)]=[]
matching=match.group(1)
match=datas.find(i)
if match:
mind[matching].append([match.group(1),match.group(2)])


That would collect the data and put it into a dictionary with the values 
being a list of the two part data. You could print like this:

for each in mind.keys():
print each
for value in mind[each]:
print each++value[0]++value[1]


That should do it!

Ask if you don't understand any part of it.

Allen J. Schmidt, Jr.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Please help

2005-08-03 Thread Allen John Schmidt, Jr.
Whoops! Had to correct it!

Hey, I had a similar problem not too long ago. My data came in the first
format, but I didn't need it formated like that.

Here is how I would have written it:

import re
col=re.compile('(AD.*?)\s*$')
datas=re.compile('\s*(.+?)\s+(.+?)')
f1 = open('xx','r')
mind={}
matching=''

for i in fl:
match=col.find(i)
if match:
mind[match.group(1)]=[]
matching=match.group(1)
match=datas.find(i)
if match:
mind[matching].append([match.group(1),match.group(2)])


That would collect the data and put it into a dictionary with the values
being a list of the two part data. You could print like this:

for each in mind.keys():
print each
for value in mind[each]:
print each++value[0]++value[1]


That should do it!

Ask if you don't understand any part of it.

Allen J. Schmidt, Jr.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


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


[Tutor] Dictionary Inserts...

2005-05-05 Thread Allen John Schmidt, Jr.
Ok, I have had enough. I have looked all through the python docs and I 
cannot find it. How do you insert an entry into a dictionary?

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


Re: [Tutor] Dictionary Inserts...

2005-05-05 Thread Allen John Schmidt, Jr.
Arrgh! How could I be so stupid! :)

Thanks for the help! I know many things about python, but I can't 
believe that I didn't know that!

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


Re: [Tutor] problem

2005-05-03 Thread Allen John Schmidt, Jr.
Feziwe Mpondo wrote:

hi
problem :modification of a guessing game excersize to a password asking 
program. her's what i tried.
s = raw_input
  

What is this here for?

#asks for a password
#prints it if correct
password = input( Tell me a password: )
  

This is correct, but

password ==dal
print password,Tell me a password: 
elif password ==dal
print accurate
  

should be:
if password!=dal:   
print password,Tell me a password: 
elif password==dal:
print accurate

while password != flower :
password = input (tell me a password: )
  

And here you should have flower as flower, since you are checking a 
string.

Hope that helps!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor