mysql and dictionary

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dragrid
    New Member
    • Jan 2009
    • 29

    mysql and dictionary

    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

    --------------------------------------------------------------------------------
  • micmast
    New Member
    • Mar 2008
    • 144

    #2
    I assume the result you receive from the query is an array for example results

    [CODE=python]
    dict = {}
    for result in result:
    dict[result[0]] = result[1]
    [/CODE]

    After that all the information will be stored in dict in the following matter:
    [CODE=python]
    dict = {'acct':'<ip acct>', 'HR':'<ip hr>', ...}
    [/CODE]

    Is that an answer to your question?

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      I moved this thread into the Answers forum.

      BV
      Moderator

      Comment

      • dragrid
        New Member
        • Jan 2009
        • 29

        #4
        micmast
        This seems like the lead I am looking for , let me try it

        TY .. will le u kno

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          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:
          Code:
          import os
          
          # ip_array and dept_array are ordered lists from db query
          ip_array = ['1.1.1.1', '2.2.2.2', '3.3.3.3']
          dept_array = ['acct', 'hr', 'mktng']
          
          dd = {}
          
          for i, ip in enumerate(ip_array):
              f = os.popen('ping %s' % (ip), 'r')
              ping_result = [item.strip() for item in f.readlines() if item.strip()]
              f.close()
              dd[dept_array[i]] = (ip_array[i], ping_result)
          
          for dept in dept_array:
              print "%s: %s\n  Results:" % (dept, dd[dept][0])
              print '    '+'\n    '.join(dd[dept][1])

          Comment

          • dragrid
            New Member
            • Jan 2009
            • 29

            #6
            mysql and dictionary

            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

            Comment

            • bvdet
              Recognized Expert Specialist
              • Oct 2006
              • 2851

              #7
              dragrid,

              1. enumerate(itera ble) 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

              Comment

              • dragrid
                New Member
                • Jan 2009
                • 29

                #8
                BV - awsome stuff
                appreciate all the reply

                Thanks again for your help

                Comment

                Working...