def run(): ns = Solution() from common import CommonUtil root = None root = CommonUtil.generateTree(root) CommonUtil.printTree(root) print ns.countNodes(root)
def run(): from common import CommonUtil root = None root = CommonUtil.generateTree(root) CommonUtil.printTree(root, 0) ns = Solution() ret = ns.pathSum(root, 17) print ret
def run(): from common import CommonUtil root = None root = CommonUtil.generateTree(root) CommonUtil.printTree(root , 0) ns = Solution() ret = ns.hasPathSum(root , 17) print ret
def run(): from common import CommonUtil root = None root = CommonUtil.generateTree(root) CommonUtil.printTree(root , 0) ns = Solution() ret = ns.binaryTreePaths(root) print ret
def run(): from common import CommonUtil ns = Solution() head = None head = CommonUtil.generateList(2, head) CommonUtil.printList(head) hed = ns.reverseList(head) CommonUtil.printList(hed)
def run(): from common import CommonUtil ns = Solution() head = None head = CommonUtil.generateList( 2 , head) CommonUtil.printList(head) hed = ns.reverseList(head) CommonUtil.printList(hed)
def run(): l1 = ListNode(1) l2 = ListNode(3) r1 = ListNode(2) r2 = ListNode(4) l1.next = l2 r1.next = r2 s = Solution() res = s.mergeTwoLists(l1, r1) CommonUtil.printList(res)
def run(): l1 = ListNode(1) l2 = ListNode(3) r1 = ListNode(2) r2 = ListNode(4) l1.next = l2 r1.next = r2 s = Solution() ss = [l1, r1] res = s.mergeKLists(ss) CommonUtil.printList(res)
class YamlUtil: ''' Yaml处理类 用于从yaml配置文件中读取字典,并持有该字典 ''' root_path = CommonUtil.get_abspath() _yaml_dict = None # with open(os.path.join(root_path, "config", "config.yml"), encoding="utf8") as config_file: with open(os.path.join(root_path, "config", "config.yml"), encoding="utf8") as config_file: content = config_file.read() _yaml_dict = yaml.load(content, yaml.FullLoader) # pprint.pprint(_yaml_dict) @classmethod def get_yaml(cls): ''' 类方法 :return: 读取到的字典 ''' return cls._yaml_dict
def _get_log(): ''' 通过YamlUtil类获取yaml字典 通过字典中的参数生成日志对象 :return: 日志对象 ''' yaml_dict = YamlUtil.get_yaml() #使用导航方式获得配置文件中的参数 name = CommonUtil.get_value_by_navigate_from_dict(yaml_dict, "log.name", "myLog") format = CommonUtil.get_value_by_navigate_from_dict(yaml_dict, "log.format", "%(asctime)s %(name)s %(levelname)s %(filename)s line:%(lineno)d %(message)s") level = CommonUtil.get_value_by_navigate_from_dict(yaml_dict, "log.level", "INFO") file_backup_count = CommonUtil.get_value_by_navigate_from_dict(yaml_dict, "log.file.backupCount", 5) file_encoding = CommonUtil.get_value_by_navigate_from_dict(yaml_dict, "log.file.encoding", "utf8") file_maxBytes = CommonUtil.get_value_by_navigate_from_dict(yaml_dict, "log.file.maxBytes", 1024) if file_maxBytes: file_maxBytes = eval(file_maxBytes) file_path = CommonUtil.get_value_by_navigate_from_dict(yaml_dict, "log.file.path", "../output/logs/testProject.log") file_path = CommonUtil.get_abspath(file_path) #创建日志对象 log = logging.getLogger(name) log.setLevel(level) fmt = format formatter = logging.Formatter(fmt) #创建控制台处理器 handler = logging.StreamHandler() handler.setFormatter(formatter) log.addHandler(handler) #如果日志目录不存在,则创建目录 parent_dir = os.path.dirname(file_path) if not os.path.exists(parent_dir): os.makedirs(parent_dir, exist_ok=True) #创建文件日志处理器 file_handler = handlers.RotatingFileHandler(file_path, encoding=file_encoding, maxBytes=file_maxBytes, backupCount=file_backup_count) file_handler.setFormatter(formatter) log.addHandler(file_handler) return log
import pymysql from common.ConfigUtil import YamlUtil from common import CommonUtil from common.LoggerUtil import MyLog log = MyLog.get_log() _yaml_content = YamlUtil.get_yaml() _host = CommonUtil.get_value_by_navigate_from_dict(_yaml_content, "mysql.host", "localhost") _port = CommonUtil.get_value_by_navigate_from_dict(_yaml_content, "mysql.port", 3306) _user = CommonUtil.get_value_by_navigate_from_dict(_yaml_content, "mysql.user", "root") _password = str( CommonUtil.get_value_by_navigate_from_dict(_yaml_content, "mysql.password", "123456")) _charset = CommonUtil.get_value_by_navigate_from_dict(_yaml_content, "mysql.charset", "utf8") _database = CommonUtil.get_value_by_navigate_from_dict(_yaml_content, "mysql.database", "utf8") ''' 注意:密码是字符串类型,charset设置为utf8(在mysql中utf8是utf-8的实现,两者是不同的) 游标读到的数据默认以tuple呈现,可以通过cursorclass=pymysql.cursors.DictCursor将返回值类型改为字典(其实是list<dict>) ''' class HandleMysql: def __init__(self): try: self._conn = pymysql.connect(
def urlencode(value, encoding='utf-8'): value = CommonUtil.to_string(value) return urllib.quote(value)
import unittest from common import CommonUtil from common.ConfigUtil import YamlUtil from BeautifulReport import BeautifulReport # 获得配置 yaml_config = YamlUtil.get_yaml() # 获得测试用例目录 testcase_dir = CommonUtil.get_value_by_navigate_from_dict(YamlUtil.get_yaml(), "testcase.dir") testcase_dir = CommonUtil.get_abspath(testcase_dir) loader = unittest.TestLoader().discover(testcase_dir) reportDir = CommonUtil.get_value_by_navigate_from_dict(YamlUtil.get_yaml(), "testcase.reportDir") reportDir = CommonUtil.get_abspath(reportDir) br = BeautifulReport(loader) br.report("desc", "beautifullReport.html", reportDir) # with open(reportDir + "report.txt", "w") as f: # runner = unittest.TextTestRunner(f) # runner.run(loader)
def common_test(): from common import CommonUtil print CommonUtil.to_string(u'你好,世界') print CommonUtil.to_string(u'你好,世界', 'GBK') print CommonUtil.to_string(object) print CommonUtil.to_string(1024) print CommonUtil.to_unicode(u'你好,世界') print CommonUtil.to_unicode(CommonUtil.to_string(u'你好,世界', 'GBK'), 'GBK') s = 'jo4MDgwIinBhcmFt\t0ZWxpZCI6ICI0\ngsICJobATU4Iiwg\nInRhc2tb\x20DM2NDX19' print s print id(s) s_ = CommonUtil.re_str_replace(s, '\s', '') s = s.replace('\t', '') print s print id(s) print id(s_) print s_ print CommonUtil.html_unescape('<node> value </node>') print CommonUtil.re_str_replace('line0<br>newline', '\<br\>', '\n') print CommonUtil.re_str_replace(' encoding="utf-16"?>', 'encoding=\"utf-16\"', 'encoding="utf-8"') print CommonUtil.fibonacci_num(0) print CommonUtil.fibonacci_num(1) print CommonUtil.fibonacci_num(2) print CommonUtil.fibonacci_num(3) print CommonUtil.fibonacci_num(4) print CommonUtil.fibonacci_num(5) print CommonUtil.fibonacci_num(6) print CommonUtil.fibonacci_num(7) print CommonUtil.fibonacci_num(8) print CommonUtil.fibonacci_num(9) print CommonUtil.fibonacci_num(10) # round robin dic = { 'a': { 'w': 5, 'cc': 0, 'sc': 0, }, 'b': { 'w': 3, 'cc': 0, 'sc': 0, }, 'c': { 'w': 2, 'cc': 0, 'sc': 0, }, } for i in xrange(10): print CommonUtil.wdrr_schedule(dic, need_sc=True) print dic pass
def urldecode(value): value = CommonUtil.to_string(value) return urllib.unquote(value)