Python工具(导包问题、pickle协议问题、argparse用法、configparser用法、assert、difference求差集)
python导包问题:
相对路径(容易报错):
# .同级目录 ..上级目录
from .resource import UserAgents
from .resource import PROXIES```
绝对路径:
from model.resource import UserAgents
from model.resource import PROXIES
pickle协议问题
python3.7不支持pickle协议5:
ValueError: unsupported pickle protocol: 5
如果pkl文件是在python3.8的环境下生成的(协议5),那么3.7的环境下如果打开协议5的pkl文件?
import pickle5 as pkl
pkl.load(文件名)
argparse用法(用来设置运行参数)
import argparse
parser = argparse.ArgumentParser(description='dnn conf')
parser.add_argument(
'--conf_dir', # “--”表示可选参数
dest='conf_dir',
default='conf/settings/',
help='dnn config file dir')
args = parser.parse_args()
# vars()将传入的object转为字典类型
return vars(args)
para = args['conf_dir']
configparser用法(用来读取配置文件conf)
conf文件结构:
[info] # section info
data =2022-03-05 # option
purpose ="mmoe conf"
type = train
[tfrecord] # section tfrecord
value_pairs =
emb =
[features]
emb = ex_zs:70:512:ex_zs:ex#kexia_bz:50000:512:kexia_bz:yian#she_bz:500:512:she_bz:yian#tai_bz:400:512:tai_bz:yian#mai_bz:400:512:mai_bz:yian#ex_age_gender78:60:32:ex_age_gender78:ex
value_pairs = kexia_bz:ex_zs:zxd|tfidf:avg#she_bz:ex_zs:gx|tfidf|avg
sim_emb =
用法:
conf_parser = configparser.ConfigParser()
conf_parser.read(conf_dir + conf_file) # 读取文件
for section in self.conf_parser.sections(): # 读取所有Section
self.conf_sections[section] = self.get_section_conf(section)
注:上面读入进来后全部都是字符串的形式,一般需要进行形式转化:
def reset(self, section, option, f, default):
try:
self.conf_sections[section][option] = f(self.conf_sections[section][option])
if not self.conf_sections[section][option]:
self.conf_sections[section][option] = default
except Exception as e:
print("Reset conf_sections[%s][%s] failed, will use default value. Exception: %s" % (section, option, e))
if section not in self.conf_sections:
self.conf_sections[section] = {}
self.conf_sections[section][option] = default
finally:
pass
assert用法(是否让程序正常执行下去)
assert flag in ['train', 'predict']
如果flag不在这个列表中,则报错,程序停止。
difference求差集
type(target_list)=='list'
type(target_table_set)=='list'
rec_neg = list(target_table_set.differenct(set(target_list)))
rec_pos = target_list