How to get a list of variables in specific Python module?

Solution 1:

print [item for item in dir(adfix) if not item.startswith("__")]

Is usually the recipe for doing this, but it begs the question.

Why?

Solution 2:

#!/usr/local/bin/python
# coding: utf-8
__author__ = 'spouk'

def get_book_variable_module_name(module_name):
    module = globals().get(module_name, None)
    book = {}
    if module:
        book = {key: value for key, value in module.__dict__.iteritems() if not (key.startswith('__') or key.startswith('_'))}
    return book

import config

book = get_book_variable_module_name('config')
for key, value in book.iteritems():
    print "{:<30}{:<100}".format(key, value)

example config

#!/usr/local/bin/python
# coding: utf-8
__author__ = 'spouk'

import os

_basedir = os.path.abspath(os.path.dirname(__file__))

# database section MYSQL section
DBHOST = 'localhost'
DBNAME = 'simple_domain'
DBPORT = 3306
DBUSER = 'root'
DBPASS = 'root'

# global section
DEBUG = True
HOSTNAME = 'simpledomain.com'
HOST = '0.0.0.0'
PORT = 3000
ADMINS = frozenset(['admin@localhost'])
SECRET_KEY = 'dfg45DFcx4rty'
CSRF_ENABLED = True
CSRF_SESSION_KEY = "simplekey"

result function

/usr/local/bin/python2 /home/spouk/develop/python/2015/utils_2015/parse_config_py.py
DBPORT                        3306                                                                                                
os                            <module 'os' from '/usr/local/lib/python2.7/os.pyc'>                                                
DBHOST                        localhost                                                                                           
HOSTNAME                      simpledomain.com                                                                                    
HOST                          0.0.0.0                                                                                             
DBPASS                        root                                                                                                
PORT                          3000                                                                                                
ADMINS                        frozenset(['admin@localhost'])                                                                      
CSRF_SESSION_KEY              simplekey                                                                                           
DEBUG                         1                                                                                                   
DBUSER                        root                                                                                                
SECRET_KEY                    dfg45DFcx4rty                                                                                       
CSRF_ENABLED                  1                                                                                                   
DBNAME                        simple_domain                                                                                       

Process finished with exit code 0

Enjoy, dude. :)

Solution 3:

This is the version I wrote for python 3.7 (it excludes the internal dunder methods via the condition in the comprehension)

print([v for v in dir(data) if v[:2] != "__"])

A longer but complete working example is below:

"""an example of a config file whose variables may be accessed externally"""
# Module variables
server_address = "172.217.167.68"
server_port = 8010
server_to_client_port = 8020
client_to_server_port = 8030
client_buffer_length = 4096
server_buffer_length = 2048

def printVariables(variable_names):
    """Renders variables and their values on the terminal."""
    max_name_len = max([len(k) for k in variable_names])
    max_val_len = max([len(str(globals()[k])) for k in variable_names])

    for k in variable_names:
        print(f'  {k:<{max_name_len}}:  {globals()[k]:>{max_val_len}}')

if __name__ == "__main__":
    print(__doc__)
    ks = [k for k in dir() if (k[:2] != "__" and not callable(globals()[k]))]
    printVariables(ks)

The above code outputs:

an example of a config file whose variables may be accessed externally
  client_buffer_length :            4096
  client_to_server_port:            8030
  server_address       :  172.217.167.68
  server_buffer_length :            2048
  server_port          :            8010
  server_to_client_port:            8020