使用从函数 A 到函数 B 的 var [英] Using var from from function A to function B

查看:36
本文介绍了使用从函数 A 到函数 B 的 var的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此示例代码中,我想在 connect_and_query 函数中使用 函数 db_​​properties 上的变量.为此,我选择了 return.因此,使用该策略,代码可以完美运行.但是,在这个例子中 db.properties 文件只有 4 个变量.也就是说,如果属性文件有 20 多个变量,我应该继续使用 return 吗?或者有没有最优雅/更干净/正确的方法来做到这一点?

On this sample code i want to use the variables on the function db_properties at the function connect_and_query. To accomplish that I choose the return. So, using that strategy the code works perfectly. But, in this example the db.properties files only has 4 variables. That said, if the properties file had 20+ variables, should I continue using return? Or is there a most elegant/cleaner/correct way to do that?

import psycopg2
import sys
from ConfigParser import SafeConfigParser

class Main:

    def db_properties(self):
        cfgFile='c:\test\db.properties'
        parser = SafeConfigParser()
        parser.read(cfgFile)
        dbHost = parser.get('database','db_host')
        dbName = parser.get('database','db_name')
        dbUser = parser.get('database','db_login')
        dbPass = parser.get('database','db_pass')
        return dbHost,dbName,dbUser,dbPass

    def connect_and_query(self):
        try:
            con = None

            dbHost=self.db_properties()[0]
            dbName=self.db_properties()[1]
            dbUser=self.db_properties()[2]
            dbPass=self.db_properties()[3]

            con = None
            qry=("select star from galaxy")
            con = psycopg2.connect(host=dbHost,database=dbName, user=dbUser,
                                   password=dbPass)
            cur = con.cursor()
            cur.execute(qry)
            data = cur.fetchall()
            for result in data:
                qryResult   = result[0]
                print "the test result is : " +qryResult
        except psycopg2.DatabaseError, e:
                print 'Error %s' % e
                sys.exit(1)
        finally:
            if con:
                con.close()

operation=Main()
operation.connect_and_query()

我使用的是 python 2.7问候

Im using python 2.7 Regards

推荐答案

如果变量很多,或者如果你想轻松改变正在读取的变量,返回一个字典.

If there are a lot of variables, or if you want to easily change the variables being read, return a dictionary.

def db_properties(self, *variables):
    cfgFile='c:\test\db.properties'
    parser = SafeConfigParser()
    parser.read(cfgFile)
    return {
        variable: parser.get('database', variable) for variable in variables
    }

def connect_and_query(self):
    try:
        con = None
        config = self.db_properties(
            'db_host',
            'db_name',
            'db_login',
            'db_pass',
        )
        #or you can use:
        #   variables = ['db_host','db_name','db_login','db_pass','db_whatever','db_whatever2',...]
        #   config = self.db_properties(*variables)
        #now you can use any variable like: config['db_host']
        # ---rest of the function here---

我重构了代码,以便您可以在调用函数本身中指定要加载的变量.

I refactored the code so you can specify the variables you want to load in the calling function itself.

这篇关于使用从函数 A 到函数 B 的 var的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆