By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,941 Members | 1,789 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,941 IT Pros & Developers. It's quick & easy.

mysql and dictionary

P: 29
Hi All,
Newbie to Python
I ran a query from a MySql DB to get IP address and ping them and get reachable, congested, not reachable - say in the table I have column Dept IP ADDR
acct 1.1.2.3
HR 2.3.3.4
Mktng 3.4.4.5 - query gives me IP array and I ping from IP array and get 1.1.2.3 -reachable. 2.3.3.4 - congested and 3.4.4.5 - not reachable how can I re map using dictionary- the values in the Dept column to go with the right IPs and their results so I would get for e.g. HR 2.3.3.4 congested.

any help appreciated

--------------------------------------------------------------------------------
Jul 19 '09 #1
Share this Question
Share on Google+
7 Replies


micmast
100+
P: 144
I assume the result you receive from the query is an array for example results

Expand|Select|Wrap|Line Numbers
  1. dict = {}
  2. for result in result:
  3.  dict[result[0]] = result[1]
  4.  
After that all the information will be stored in dict in the following matter:
Expand|Select|Wrap|Line Numbers
  1. dict = {'acct':'<ip acct>', 'HR':'<ip hr>', ...}
  2.  
Is that an answer to your question?
Jul 20 '09 #2

bvdet
Expert Mod 2.5K+
P: 2,851
I moved this thread into the Answers forum.

BV
Moderator
Jul 20 '09 #3

P: 29
micmast
This seems like the lead I am looking for , let me try it

TY .. will le u kno
Jul 21 '09 #4

bvdet
Expert Mod 2.5K+
P: 2,851
Avoid using built-in Python function names for variable names such as dict. Assuming you were to receive ordered lists (arrays) for a department query and ip address query:
Expand|Select|Wrap|Line Numbers
  1. import os
  2.  
  3. # ip_array and dept_array are ordered lists from db query
  4. ip_array = ['1.1.1.1', '2.2.2.2', '3.3.3.3']
  5. dept_array = ['acct', 'hr', 'mktng']
  6.  
  7. dd = {}
  8.  
  9. for i, ip in enumerate(ip_array):
  10.     f = os.popen('ping %s' % (ip), 'r')
  11.     ping_result = [item.strip() for item in f.readlines() if item.strip()]
  12.     f.close()
  13.     dd[dept_array[i]] = (ip_array[i], ping_result)
  14.  
  15. for dept in dept_array:
  16.     print "%s: %s\n  Results:" % (dept, dd[dept][0])
  17.     print '    '+'\n    '.join(dd[dept][1])
Jul 22 '09 #5

P: 29
bvdet
appreciate the reply - lots of good , new stuff
I am trying to disect your answer and understand every step but can you breifly explain 1. look like you are using i and ip 2 variables in your for loop to rep and loop thru each value in ip_array ?
and 2. dd[dept_array[i]] = (ip_array[i], ping_result) - are you assigning values ip_array[ i] and ping_result value to dd key dept_array[i]. ?

I would like to make the right comments so I can avoid asking this and similar questions in the future

Thx
Jul 23 '09 #6

bvdet
Expert Mod 2.5K+
P: 2,851
dragrid,

1. enumerate(iterable) is a built-in Python function that returns an iterator object given an iterable object. The next() method of the iterator returns a tuple containing a count (similar to the range() function) and the corresponding item from the iterable. So the variables i and ip represent the count (starting at 0) and corresponding item from the iterable object ip_array.

2. Yes. Each the dictionary value is a list containing the ip address and result of the ping attempt for the corresponding ip address, and the key to the value is the department name.

-BV
Jul 23 '09 #7

P: 29
BV - awsome stuff
appreciate all the reply

Thanks again for your help
Jul 28 '09 #8

Post your reply

Sign in to post your reply or Sign up for a free account.