473,322 Members | 1,268 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

mysql and dictionary

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
7 2113
micmast
144 100+
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
2,851 Expert Mod 2GB
I moved this thread into the Answers forum.

BV
Moderator
Jul 20 '09 #3
dragrid
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
2,851 Expert Mod 2GB
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
dragrid
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
2,851 Expert Mod 2GB
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
dragrid
29
BV - awsome stuff
appreciate all the reply

Thanks again for your help
Jul 28 '09 #8

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

Similar topics

10
by: Yang Li Ke | last post by:
Hi guys! I have some datas that I must check everytime a visitor comes to my site What is better to do: 1- Read data from a file or 2- Read data from a mysql db Thank you
1
by: bissatch | last post by:
Hi, I have just started a new job as a webmaster where they use PostgreSQL. I have always used MySQL. What are the differences? How does PostgreSQL work with PHP as apposed to MySQL? And...
0
by: Philip Stoev | last post by:
Hi all, Please tell me if any of this makes sense. Any pointers to relevant projects/articles will be much appreciated. Philip Stoev http://www.stoev.org/pivot/manifest.htm ...
0
by: Ersin Basaran | last post by:
I'm using MySQL 4.0.13 and having problem with the following query. It was ok prior MySQL 4. select substring(SomeField,1,1) as Letter from sometable group by Letter I use this query to get...
4
by: Michi | last post by:
I was wondering what the best solution is for making large numbers of TEXT (or BLOB?) fields searchable. For example, if I have a forum, what is the best way to be able to search for specific...
2
by: news | last post by:
We currently have our mySQL server on the same box as the Apache server. For security and load balancing, we're going to be moving the mySQL server to another box. We're already using a single...
39
by: Mairhtin O'Feannag | last post by:
Hello, I have a client (customer) who asked the question : "Why would I buy and use UDB, when MySql is free?" I had to say I was stunned. I have no experience with MySql, so I was left sort...
8
by: Derick van Niekerk | last post by:
I have found many posts that deal with writing a dictionary to MySQL in a blob field - which I can't imagine why anybody would want to do it. I want to write each element of a dictionary onto a...
27
by: Pom | last post by:
Hello I want to convert a Mysql resulset to a dictionary. I made some code myself, and want to ask you if I do this the right way. def remapmysql(a): return (a, (a)) def test_map():
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.