Python编程格式风格

风格一:

来源:https://www.jianshu.com/p/e266137ebca0

方法里,每个空行分割成段,每段功能简介,都写在开头注释中,段介绍以:开头

# coding=utf-8
import pymysql
from tool.read_config import ReadConfig
from tool import project_path

class DoMysql:
    def do_mysql(self,query_sql,state="all"):
        """
        :param query_sql: 查询语句
        :param state: 1:一条结果  all:所有结果
        :return:
        """
        #read_config文件中ReadConfig是静态方法,不用实例化
        db_config = eval(ReadConfig.get_config(project_path.case_config_path,"DB","db_config"))
        print(db_config)
        #关键字参数传递
        cnn = pymysql.connect(**db_config)

        # 使用cursor()方法创建一个游标对象
        cur = cnn.cursor()
        #执行语句
        cur.execute(query_sql)
        #获取结果
        if state == 1:
            res = cur.fetchone() #元组针对一条数据
        else:
            res = cur.fetchall() #元组嵌套元组


        #关闭游标
        cur.close()
        cnn.close()
        return res

if __name__ == '__main__':
    domysql= DoMysql()
    res = domysql.do_mysql("select * from areas",1)
    print(res)

  

 

 

 

 

 

-

posted @ 2022-05-04 13:13  悟透  阅读(44)  评论(0)    收藏  举报