# -*- coding: utf-8 -*- """ Created on 2018-09-06 14:21 --------- @summary: 工具 --------- @author: Boris @email: boris_liu@foxmail.com """ import datetime import json import re from pprint import pformat print('sssssssss') _regexs = {} def get_info(html, regexs, allow_repeat=True, fetch_one=False, split=None): regexs = isinstance(regexs, str) and [regexs] or regexs infos = [] for regex in regexs: if regex == "": continue if regex not in _regexs.keys(): _regexs[regex] = re.compile(regex, re.S) if fetch_one: infos = _regexs[regex].search(html) if infos: infos = infos.groups() else: continue else: infos = _regexs[regex].findall(str(html)) if len(infos) > 0: # print(regex) break if fetch_one: infos = infos if infos else ("",) return infos if len(infos) > 1 else infos[0] else: infos = allow_repeat and infos or sorted(set(infos), key=infos.index) infos = split.join(infos) if split else infos return infos def get_json(json_str): """ @summary: 取json对象 --------- @param json_str: json格式的字符串 --------- @result: 返回json对象 """ try: return json.loads(json_str) if json_str else {} except Exception as e1: try: json_str = json_str.strip() json_str = json_str.replace("'", '"') keys = get_info(json_str, "(\w+):") for key in keys: json_str = json_str.replace(key, '"%s"' % key) return json.loads(json_str) if json_str else {} except Exception as e2: print( """ e1: %s format json_str: %s e2: %s """ % (e1, json_str, e2) ) return {} def dumps_json(json_, indent=4, sort_keys=False): """ @summary: 格式化json 用于打印 --------- @param json_: json格式的字符串或json对象 --------- @result: 格式化后的字符串 """ try: if isinstance(json_, str): json_ = get_json(json_) json_ = json.dumps( json_, ensure_ascii=False, indent=indent, skipkeys=True, sort_keys=sort_keys ) except Exception as e: print(e) json_ = pformat(json_) return json_ def get_current_date(date_format="%Y-%m-%d %H:%M:%S"): return datetime.datetime.now().strftime(date_format) # return time.strftime(date_format, time.localtime(time.time())) def key2hump(key): """ 下划线试变成首字母大写 """ return key.title().replace("_", "")