zoukankan      html  css  js  c++  java
  • UI自动化框架搭建(六): 工具类解析

    主要讲下3个工具

    一、日志类工具

    功能:自动化运行打印日志

    日志级别                 setLevel(logging.DEBUG或INFO ERROR),一般为INFO

    生成文件命名格式   datetime.now().strftime("%Y-%m-%d"),如果是按时分秒,()可以加%H%M%S

    其他按默认即可

    # -*- coding:utf-8 -*-
    import logging
    from datetime import datetime
    import os
    
    
    class LogUtil():
        def __init__(self, logname=None):
            # 日志名称
            self.logger = logging.getLogger(logname)
            # 日志级别
            self.logger.setLevel(logging.DEBUG)
            # 日志输出到控制台
            self.console = logging.StreamHandler()
            self.console.setLevel(logging.DEBUG)
            # 输出到文件
            self.date = datetime.now().strftime("%Y-%m-%d") + '.log'
            self.filename = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'logs', self.date)
            self.file = logging.FileHandler(self.filename, encoding='utf-8')
            self.file.setLevel(logging.DEBUG)
            # 日志显示内容
            self.formatstr = '%(asctime)s %(filename)s [line:%(lineno)d] %(levelname)s %(message)s'
            self.format = logging.Formatter(self.formatstr)
            self.console.setFormatter(self.format)
            self.file.setFormatter(self.format)
            # 加入到hander
            self.logger.addHandler(self.console)
            self.logger.addHandler(self.file)
    
        def get_logger(self):
            return self.logger

    二、数据库操作类

    目前支持mysql和oracle的数据操作

    由于mysql和oracle的增删查改操作方法一致,所以方法统一放置在DataBase类里了

    但游标conn获取方式不一致,通过OracleDataBase和MysqlDataBase类区别获取

    # -*- coding:utf-8 -*-
    from utils.log_util import LogUtil
    from utils.yaml_util import YamlUtil
    import pymysql
    import cx_Oracle
    
    logger = LogUtil('database_util').getLogger()
    
    class DataBase(object):
        def __init__(self):
            pass
    
        def queryDataBase(self, querySql):
            # 获取游标
            try:
                cursor = self.con.cursor()
                cursor.execute(querySql)
                return cursor.fetchone()[0]
            except Exception as e:
                logger.error(e)
            finally:
                self.con.close()
    
        def updateData(self, querySql):
            # 修改数据库数据
            try:
                cursor = self.con.cursor()
                cursor.execute(querySql)
                self.con.commit()
            except Exception as e:
                self.con.rollback()
                logger.error(e)
            finally:
                self.con.close()
    
    
    class OracleDataBase(DataBase):
        def __init__(self):
            sysConfig = YamlUtil('sysconfig.yaml').readYaml()
            host = sysConfig['oralceConfig']['host']
            port = sysConfig['oralceConfig']['port']
            user = sysConfig['oralceConfig']['username']
            pwd = sysConfig['oralceConfig']['password']
            database = sysConfig['oralceConfig']['database']
            self.con = cx_Oracle.connect("{}/{}@{}:{}/{}".format(user, pwd, host, port, 
                                                                 database).format(), encoding="UTF-8", nencoding="UTF-8")
    
    class MysqlDataBase(DataBase):
        def __init__(self):
            sysConfig = YamlUtil('sysconfig.yaml').readYaml()
            host = sysConfig['mysqlConfig']['host']
            port = sysConfig['mysqlConfig']['port']
            user = sysConfig['mysqlConfig']['username']
            pwd = sysConfig['mysqlConfig']['password']
            database = sysConfig['mysqlConfig']['database']
            self.con = pymysql.Connect(
                host=host,
                port=port,
                user=user,
                passwd=pwd,
                db=database,
                charset='utf8'
            )
    
    if __name__ == "__main__":
        pass

    三、时间操作类

    按格式获得时间,目前支持年月日时分秒 以及 年-月-日返回

    可自行扩展

    # -*- coding:utf-8 -*-
    from datetime import datetime
    
    
    class DateTimeUtil(object):
        def __init__(self):
            pass
    
        def get_current_time(self):
            return datetime.now().strftime("%Y%m%d%H%M%S")
    
        def get_current_date(self):
            return datetime.now().strftime("%Y-%m-%d")
    
    if __name__=="__main__":
        dateTime = DateTimeUtil()
        print(dateTime.get_current_time())
  • 相关阅读:
    基于python的知乎开源爬虫 zhihu_oauth使用介绍
    python scrapy 抓取脚本之家文章(scrapy 入门使用简介)
    模拟退火算法(SA)求解TSP 问题(C语言实现)
    遗传算法的C语言实现(二)-----以求解TSP问题为例
    遗传算法的C语言实现(一):以非线性函数求极值为例
    C语言实现粒子群算法(PSO)二
    C语言实现粒子群算法(PSO)一
    python wordcloud 对电影《我不是潘金莲》制作词云
    svn更新失败,解决
    java发送邮箱验证码
  • 原文地址:https://www.cnblogs.com/heng-xin/p/14149401.html
Copyright © 2011-2022 走看看