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

前端 未结 5 784
野趣味
野趣味 2020-11-30 10:42

Let\'s assume I have the following file structure:

data.py

foo = []
bar = []
abc = \"def\"

core.py

5条回答
  •  抹茶落季
    2020-11-30 11:21

    #!/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                                                                            
    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. :)

提交回复
热议问题