Python代码片段
1 记录函数执行耗时
1 def timeToRunWithParams(level, param_idx_lst): 2 def inner(func): 3 def wrapper(*args, **kwargs): 4 s_date = datetime.datetime.now() 5 result = func(*args, **kwargs) 6 duration = datetime.datetime.now() - s_date 7 cost_ms = round(duration.total_seconds() * 1000, 2) 8 if "DEBUG" == level.upper(): 9 sub_params = [args[idx] for idx in param_idx_lst] 10 log.info('func={} with sub_params={} cost={} milliseconds'.format(func.__name__, sub_params, cost_ms)) 11 else: 12 log.info('func={} cost={} milliseconds'.format(func.__name__, cost_ms)) 13 return result 14 return wrapper 15 return inner 16 17 @timeToRunWithParams(level="info", param_idx_lst=[]) 18 def job(): 19 # to do something 20 pass
2 HTTP请求失败自动重试
def make_request(url, timeout=30, max_retries=3): retry_delay = 1 # 初始重试间隔时间为1秒 for i in range(max_retries + 1): try: response = req.get(url, timeout=timeout) response.raise_for_status() # 检查请求是否成功 return response # 返回成功的响应 except req.exceptions.RequestException as e: if i == max_retries: raise # 如果已达到最大重试次数,则将异常抛出 else: log.error("failed to request: {} \n retrying {} times in {} seconds...".format(url, i+1, retry_delay)) time.sleep(retry_delay) retry_delay *= 2 # 增加重试间隔时间
3 输出日志
def get_logger(path='./', filename='access.log', level='debug'): log_level = { 'debug': logging.DEBUG, 'info': logging.INFO, 'warning': logging.WARNING, 'error': logging.ERROR, 'critical': logging.CRITICAL } level = 'debug' if level not in log_level else level if not os.path.exists(path): os.makedirs(path) log_file = os.path.join(path, filename) formatter = logging.Formatter("%(asctime)s.%(msecs)03d [line:%(lineno)d] %(levelname)s %(message)s", datefmt="%Y-%m-%d %H:%M:%S") # handler = logging.FileHandler(log_file, mode='a') # 创建一个时间轮转日志处理器 handler = TimedRotatingFileHandler( log_file, when="midnight", interval=1, backupCount=32 ) handler.setFormatter(formatter) # logger = logging.getLogger(__name__) logger = logging.getLogger(filename) logger.setLevel(log_level[level]) logger.addHandler(handler) return logger
4 自定义异常
class CustomException(Exception): def __init__(self, message): self.message = message super().__init__(self.message) def __str__(self): return "Custom Exception: {}".format(self.message) raise CustomException("desc of panic")
5 任务执行失败自动重试
import time import datetime import traceback # 自定义重试次数和backoff时间 backoff = [ [1, 1], [2, 2], [3, 4], [4, 8], [5, 16], ] def date2ts(date_str, layout="%Y-%m-%d %H:%M:%S"): date_struct=time.strptime(date_str, layout) return int(time.mktime(date_struct)) def ts2date(ts, layout="%Y-%m-%d %H:%M:%S"): ts = int(ts) return datetime.datetime.fromtimestamp(ts).strftime(layout) def generate_date_pairs(year, month): # 生成指定月份的所有日期 start_date = datetime.datetime(year, month, 1) if month == 12: # 如果是12月,下一个月是下一年的1月 end_date = datetime.datetime(year + 1, 1, 1) else: end_date = datetime.datetime(year, month + 1, 1) date_range = pd.date_range(start_date, end_date, closed='left') date_pairs = [] for i in range(len(date_range) - 1): date_pairs.append((date_range[i].strftime("%Y-%m-%d %H:%M:%S"), date_range[i + 1].strftime("%Y-%m-%d %H:%M:%S"))) # 补上最后一天 last_day = end_date - datetime.timedelta(days=1) date_pairs.append((last_day.strftime("%Y-%m-%d %H:%M:%S"), end_date.strftime("%Y-%m-%d %H:%M:%S"))) return date_pairs if __name__ == "__main__": pairs = generate_date_pairs(2024, 7) for pair in pairs: print(pair) day = ts2date(date2ts(pair[0]), layout="%Y%m%d") for x in backoff: try: do_something(*pair) break except Exception as e: print(traceback.format_exc()) print(f"failed {x[0]} times, sleep {x[1]}") time.sleep(x[1]) else: print(f"{day} failed!!!") continue print(f"{day} OK\n")
6 Dataframe常用操作-生成Excel文件
df = pd.DataFrame(series) # 将column重新排列 column_order = [ "app", "mode", "ppid", "pname", "pid", "name", "comm", "protocol", "bytes_received", "bytes_sent", ] df_reordered = df[column_order] # 按照多个column进行组合排序 df_sorted = df_reordered.sort_values(by=['name', 'pname', "bytes_sent"], ascending=[True, True, False]) # 按照指定column的value进行拆分 df_grouped = df_sorted.groupby("mode") # 拆分的结果输出xlsx文件,每个子dataframe占用一个sheet output_file = 'xxx.xlsx' with pd.ExcelWriter(output_file, engine='xlsxwriter') as writer: for category, sub_df in df2_grouped: sheet_name = "mode={}".format(str(category)) sub_df.to_excel(writer, sheet_name=sheet_name, index=False)
7 Dataframe常用操作-读取Excel文件
xls = pd.ExcelFile("xxx.xlsx", engine='openpyxl') name2df = {} for sheet_name in xls.sheet_names: name2df[sheet_name] = xls.parse(sheet_name)
作者:Standby — 一生热爱名山大川、草原沙漠,还有我们小郭宝贝!
出处:http://www.cnblogs.com/standby/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://www.cnblogs.com/standby/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。