V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  WilliamHL  ›  全部回复第 1 页 / 共 2 页
回复总数  22
1  2  
感觉生产环境 virtualenv 更习惯
环境隔离,项目 a 需要用到 numpy-A 版本,项目 b 需要用到 numpy-B 版本,这样可以方便进行环境管理和切换
2021-02-05 14:43:20 +08:00
回复了 WilliamHL 创建的主题 Python 如何优化 Python 计算超大字典的问题
@kele1997 感谢,我去尝试一下。多进程是后来改写的,还没有验证,数据上都是之前单进程执行出现的
2021-02-05 14:41:23 +08:00
回复了 WilliamHL 创建的主题 Python 如何优化 Python 计算超大字典的问题
@DoctorCat 尝试了一下 del 确实比 pop 占用多一些内存。感觉峰值内存还是在切片和推导上
2021-02-05 11:31:07 +08:00
回复了 WilliamHL 创建的主题 Python 如何优化 Python 计算超大字典的问题
@Wincer 是这样的但是不知道有没有什么好的办法,最后坏的办法就是进行分次读取,但是感觉多 db 读取,会造成程序执行时间过长
2021-02-05 11:27:07 +08:00
回复了 WilliamHL 创建的主题 Python 如何优化 Python 计算超大字典的问题
@DoctorCat 大概就是 19 楼和 20 提到的问题,value 计算了很多中间层的 list 和 str 造成的,但是目前这些都是需要进行的中间层计算,暂时没有想到好的办法
2021-02-04 19:25:06 +08:00
回复了 WilliamHL 创建的主题 Python 如何优化 Python 计算超大字典的问题
@skinny 考虑过采用 array 实现,稍微看了下貌似能降低内存占用
2021-02-04 19:10:14 +08:00
回复了 WilliamHL 创建的主题 Python 如何优化 Python 计算超大字典的问题
@laqow 你竟然能看懂,我还没排版
2021-02-04 19:09:34 +08:00
回复了 WilliamHL 创建的主题 Python 如何优化 Python 计算超大字典的问题
@laqow Pool 是我新更新的,之前是单进程的
2021-02-04 18:52:58 +08:00
回复了 WilliamHL 创建的主题 Python 如何优化 Python 计算超大字典的问题
@linw1995 感谢啊,周末排查一下
2021-02-04 18:52:17 +08:00
回复了 WilliamHL 创建的主题 Python 如何优化 Python 计算超大字典的问题
@TimePPT 觉得来回读写消耗性能
2021-02-04 18:49:55 +08:00
回复了 WilliamHL 创建的主题 Python 如何优化 Python 计算超大字典的问题
@Wincer 在 8 楼贴了一下代码
2021-02-04 18:49:29 +08:00
回复了 WilliamHL 创建的主题 Python 如何优化 Python 计算超大字典的问题
``` python

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import copy
import json
import logging
import pymysql
import itertools
import pandas as pd
import multiprocessing as mp

PWD = os.path.dirname(os.path.realpath(__file__))
LOGPATH = os.path.join(PWD, './info.log')
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s',
filename=LOGPATH)


class FuncTactic:

def __init__(self, task_id):
self.task_id = task_id

def tactic(self):
case_id_path = "/Users/xxxx/Downloads/" + str(self.task_id) + ".log"
with open(case_id_path, 'r', encoding='utf-8') as content:
result_list = json.load(content)
content.close()
case_id_func = {} # 这个是 id 对应的方法变更
for i in result_list:
case_id_func[str(i.get('c_id'))] = i.get('c_c_func_origin_list')

conn = self.connect_db("localhost", 3306, "root", "root1234", "code_trees")
table_name = "android_code_tree"
sql = "SELECT case_id, trees FROM %s where case_id in (%s)" % (table_name, str(list(case_id_func.keys()))[
1:-1])
covs_pd = pd.read_sql(sql, conn)
funcs_dict = {} # 这个是 id 对应的关系树
for row in covs_pd.itertuples():
id = int(getattr(row, "case_id"))
funcs = getattr(row, "trees")
if funcs is None or len(funcs) < 1:
continue
else:
funcs_dict[id] = funcs
conn.close()

# 读取文件获取方法变更和对应的 case
func_case_id = {} # 方法对应的变更 id
conn = self.connect_db("x.x.x.x", 3306, "test", "test", "code_ing")
table_name = "task_diff_case_relation"
sql = "SELECT relation FROM %s where taskid = %s" % (table_name, self.task_id)
relation = pd.read_sql(sql, conn)
functions = ""
for row in relation.itertuples():
functions = getattr(row, "relation")
conn.close()
for i in json.loads(functions):
func_case_id[i.get('diff_code')] = i.get('caseid_list')
# 计算基类方法
pool = mp.Pool(mp.cpu_count())
jobs = []
for _dict in self.split_dict(func_case_id, mp.cpu_count()):
jobs.append(pool.apply(self.get_dict_common_func, _dict))

res = [job.get() for job in jobs]
pool.close()

common_funcs = list(itertools.chain(*map(eval, res)))

# 过滤
result_case_id_func = copy.deepcopy(case_id_func)
flitur_result = {}
for id, fun_list in case_id_func.items():
if set(fun_list) < set(common_funcs):
in_list = []
for fun in fun_list:
func_case_id[fun].remove(id)
if len(func_case_id[fun]) > 0:
if id in result_case_id_func:
del result_case_id_func[id]
in_list.append(fun)
flitur_result[id] = in_list
print(result_case_id_func)
print(flitur_result)
logging.info(str(result_case_id_func.keys()))
logging.info(str(flitur_result.keys()))
print(
len(list(flitur_result.keys())) / (len(list(flitur_result.keys())) + len(list(result_case_id_func.keys()))))

def connect_db(self, host, port, user, passwd, db):
try:
conn = pymysql.connect(host=host, port=port, user=user, passwd=passwd, db=db)
return conn
except Exception as e:
logging.error("connect db error : " + str(e))
return None

def get_dict_common_func(self, _dict):
common_funcs = []
for fun, ids in _dict.items():
if len(_dict[fun]) >= 40:
common_funcs.append(fun)
continue
else:
in_list = [self.get_funcs_lines_from_tree(_dict[int(x)], fun) for x in ids]
if len(in_list) > 100:
common_funcs.append(fun)
continue

return common_funcs

def get_funcs_lines_from_tree(self, tree_string, func):
data = tree_string.split("\n")
index_list = [data.index(i) for i in data if func in i]

result_list = []
for i in index_list:
list_in = [data[i].split(" ")[-1].lstrip("L")]
prefix = self.rreplace(data[i].split("L")[0], "| ", "")

for j in data[0:i][::-1]:
if prefix in j:
# print("old new_prefix: ", prefix)
prefix = self.rreplace(prefix, "| ", "")
# print("新建 new_prefix: ", prefix)
list_in.append(j.split(" ")[-1].lstrip("L"))
continue
else:
pass

# 查找完成所有的前向调用,之后查找后向调用,先还原 prefxi
prefix = self.rreplace(data[i].split("L")[0], "| ", "| | ")
list_in = list_in[::-1]
for j in data[i:]:
if prefix in j:
prefix = self.rreplace(prefix, "| ", "| | ")
list_in.append(j.split(" ")[-1].lstrip("L"))
continue
else:
pass
if len(list_in) > 0:
result_list.append(list_in)

if len(result_list) > 0:
return result_list
else:
return

def rreplace(self, s, old, new):
li = s.rsplit(old, 1)
return new.join(li)

def split_dict(self, x, chunks):
i = itertools.cycle(range(chunks))
split = [dict() for _ in range(chunks)]
for k, v in x.items():
split[next(i)][k] = v
return split


if __name__ == "__main__":
fun = FuncTactic(993)
fun.tactic()


```
2020-09-01 09:30:17 +08:00
回复了 w3cfed 创建的主题 Vue.js 有哪些适合新手实战的 Vue 开源项目?
@Elissa 感谢
2020-08-31 14:24:42 +08:00
回复了 w3cfed 创建的主题 Vue.js 有哪些适合新手实战的 Vue 开源项目?
@Elissa 同求一份~ [email protected]
2020-08-31 14:15:02 +08:00
回复了 lidlesseye11 创建的主题 Android 有办法在安卓机上直接跑 adb shell 命令吗?
其实通过无线 adb 就可以实现了,但是楼主说的几个按键我在小米上没实现
adb tcpip 5555
adb connect 172.22.113.8:5555
adb shell settings put global three_Key_mode 1
adb shell settings put global three_Key_mode 2
adb shell settings put global three_Key_mode 3
下面两个都是 OK 的
adb shell settings get secure default_input_method
adb shell settings put system screen_brightness 150
2020-08-31 12:41:51 +08:00
回复了 jdhao 创建的主题 macOS Mac 罗技 M590 设置
@jdhao 暂时好像没啥办法
2020-08-25 10:27:16 +08:00
回复了 jdhao 创建的主题 macOS Mac 罗技 M590 设置
@russellyoung 同,不间断类似断联 catalina 系统
1  2  
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2468 人在线   最高记录 6543   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 19ms · UTC 09:17 · PVG 17:17 · LAX 02:17 · JFK 05:17
Developed with CodeLauncher
♥ Do have faith in what you're doing.