Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使我的代码更加简化?_Python_Python 2.7_Python 3.x_Function - Fatal编程技术网

Python 如何使我的代码更加简化?

Python 如何使我的代码更加简化?,python,python-2.7,python-3.x,function,Python,Python 2.7,Python 3.x,Function,有人能帮我简化我的代码吗我用同一个函数做了5个不同的函数唯一的区别是列表中的名称会显示信息 给我一个简短的想法 我的代码: def country(): cur = mysql.connection.cursor() cur.callproc('spGetAllCountry') data = cur.fetchall() country_list=[]; for country in data: i = { 'Name' : cou

有人能帮我简化我的代码吗我用同一个函数做了5个不同的函数唯一的区别是列表中的名称会显示信息

给我一个简短的想法

我的代码:

def country():
   cur = mysql.connection.cursor()
   cur.callproc('spGetAllCountry')
   data = cur.fetchall()

   country_list=[];
   for country in data:
       i = {
        'Name' : country[2],
        'Code' : country[0],
        'Description' : country[1]
        }
       country_list.append(i)

   return jsonify(country_list)

def currency():
   cur = mysql.connection.cursor()
   cur.callproc('spGetAllCurrency')
   data = cur.fetchall()

   currency_list=[];
   for currency in data:
      i = {
        'Name' : currency[2],
        'Code' : currency[0],
        'Description' : currency[1]
        }
      currency_list.append(i)

   return jsonify(currency_list)

def paymentbrand():
   cur = mysql.connection.cursor()
   cur.callproc('spGetPaymentbrand')
   data = cur.fetchall()

   paymentbrand_list=[];
   for paymentbrand in data:
      i = {
        'Name' : paymentbrand[2],
        'Code' : paymentbrand[0],
        'Description' : paymentbrand[1],
                    'Payment Code' : paymentbrand[3]
        }
      paymentbrand_list.append(i)

   return jsonify(paymentbrand_list)

def paymentmode():
   cur = mysql.connection.cursor()
   cur.callproc('spGetPaymentmode')
   data = cur.fetchall()

   paymentmode_list=[];
   for paymentmode in data:
       i = {
        'Name' : paymentmode[2],
        'Code' : paymentmode[0],
        'Description' : paymentmode[1]
        }
       paymentmode_list.append(i)

   return jsonify(paymentmode_list)
试试这个:

def get_data(func_name, value_idx_map):
    cur = mysql.connection.cursor()
    cur.callproc(func_name)
    dataArr = cur.fetchall()

    res=[]
    for item in dataArr:
        v = { key: item[idx] for key, idx in value_idx_map.items()}
        res.append(v)
    return pprint.pprint(res)

common_idx = {'Name': 2, 'Code': 0, 'Description': 1}

get_data('spGetAllCountry', common_idx)
get_data('spGetAllCurrency', common_idx)
get_data('spGetPaymentmode', common_idx)

common_idx['Payment Code'] = 3
get_data('spGetPaymentbrand', common_idx)

为什么要用Java标记?你应该把它贴到你能修复缩进吗?编辑完毕@Nuageux@RichardTao,仍有一些错误(返回到def之外)。你也应该考虑Steve Smith的评论。谢谢兄弟:我会试试这个。