How do I extract and then access variables defined in a python module?

I am trying to create a simple environment check script for my test environment. My goal is to ping each of the nodes defined for a given instance of the test environment. The hosts are defined in the file as follows:

#!/usr/bin/env python
host_ip = '192.168.100.10'
router_ip = '192.168.100.254'
fs_ip = '192.168.200.10'

      

How can I get all these values ​​in a way that is iterable (i.e. I need to loop and ping each IP address)?

I looked at local () and vars () but tried to do something like this:

for key, value in vars():
    print key, value

      

generates this error:

ValueError: too many values to unpack

      

I was able to extract the names of all variables by checking dir(local_variables)

for values ​​that do not contain the string '__', but then I have a list of strings, and I cannot figure out how to get the value of the variable of the same name from the string.

+2
variables python import


source to share


5 answers


Here is a hack that I use all the time. When you returned the list of string names, you are indeed closed, but you need to use the eval () function to return the actual object that carries the name represented by the string:

hosts = [eval('modulename.' + x) for x in dir(local_variables) if '_ip' in x]

      



If I am not mistaken, this method also does not create the same disadvantages as locals () and vars () explained by Glen Maynard.

-1


source to share


First, I highly recommend not doing it this way. Instead, do:

hosts = {
    "host_ip": '192.168.100.10',
    "router_ip": '192.168.100.254',
    "fs_ip": '192.168.200.10',
}

      

Then you can just import the module and reference it normally - this gives the usual standard way of accessing that data from any Python code:



import config
for host, ip in config.hosts.iteritems():
    ...

      

If you use access variables directly, you end up with a bunch of things you don't need: built-in functions ( __builtins__

, __package__

etc.); whatever was imported when setting other variables, etc.

You will also want to make sure that the context you are working in is different from which variables you are iterating over, or you will create new variables in locals () (or vars (), or globals ()) while you iterate over it, and you will receive "RuntimeError: dictionary changed size during iteration"

.

+8


source to share


You need to do vars (). iteritems (). (or .items ())

Looping over a dictionary like vars () will only retrieve keys in the dictionary.

Example.

>>> for key in vars(): print key
...
__builtins__
__name__
__doc__
key
__package__


>>> for key, value in vars().items(): print key, value
...
__builtins__ <module '__builtin__' (built-in)>
value None
__package__ None
key __doc__
__name__ __main__
__doc__ None

      

+2


source to share


Glenn Maynard is great using vocabulary, simpler and more standard. That being said, here are a few tricks I've used sometimes:

In the file hosts.py

:

#!/usr/bin/env python
host_ip = '192.168.100.10'
router_ip = '192.168.100.254'
fs_ip = '192.168.200.10'

      

and in another file:

hosts_dict = {}
execfile('hosts.py', hosts_dict)

      

or

import hosts
hosts_dict = hosts.__dict__

      

But then again, they're both pretty hacks.

+1


source to share


If this is indeed what your imported file contains, you can also read it as a text file as well and then parse the input lines using basic string methods.

iplistfile = open("iplist.py")

host_addr_map = {}
for line in iplistfile:
    if not line or line[0] == '#':
        continue

    host, ipaddr = map(str.strip, line.split('='))
    host_addr_map[host] = ipaddr

iplistfile.close()

      

You now have a pointer to your IP addresses, addressed by the hostname. To get them all, just use the basic dict style methods:

for hostname in host_addr_map:
    print hostname, host_addr_map[hostname]

print host_addr_map.keys()

      

This also has the advantage that it removes any temptation that any misguided user can add more complex Python logic to what you thought was just a config file.

0


source to share







All Articles
Loading...
X
Show
Funny
Dev
Pics