text stringlengths 0 828 |
|---|
txt = None |
if safe: |
txt = Template(templ_txt).safe_substitute(sub_value) |
else: |
txt = Template(templ_txt).substitute(sub_value) |
write_file(target, txt)" |
1137,"def get_md5(path): |
""""""获取文件的 MD5 值。 |
:param str path: 文件路径。 |
:returns: MD5 值。 |
:rtype: str |
"""""" |
with open(path,'rb') as f: |
md5obj = hashlib.md5() |
md5obj.update(f.read()) |
return md5obj.hexdigest() |
raise FileNotFoundError(""Error when get md5 for %s!""%path)" |
1138,"def create_zip(files, trim_arcname=None, target_file=None, **zipfile_args): |
""""""创建一个 zip 文件。 |
:param list files: 要创建zip 的文件列表。 |
:param int trim_arcname: 若提供这个值,则使用 ZipFile.write(filename, filename[trim_arcname:]) 进行调用。 |
:returns: zip 文件的路径。 |
:rtype: str |
"""""" |
zipname = None |
azip = None |
if not target_file: |
azip = tempfile.NamedTemporaryFile(mode='wb', delete=False) |
zipname = azip.name |
else: |
azip = target_file |
zipname = target_file.name if hasattr(azip, 'read') else azip |
slog.info('Package %d files to ""%s""'%(len(files), azip.name)) |
fileNum = len(files) |
curFile = 0 |
zipfile_args['mode'] = 'w' |
if not zipfile_args.get('compression'): |
zipfile_args['compression'] = zipfile.ZIP_DEFLATED |
with zipfile.ZipFile(azip, **zipfile_args) as zipf: |
for f in files: |
percent = round(curFile/fileNum*100) |
sys.stdout.write('\r%d%%'%(percent)) |
sys.stdout.flush() |
zipf.write(f, f[trim_arcname:] if trim_arcname else None ) |
curFile = curFile+1 |
sys.stdout.write('\r100%\n') |
sys.stdout.flush() |
if hasattr(azip, 'close'): |
azip.close() |
return zipname" |
1139,"def get_max_ver(fmt, filelist): |
""""""有一堆字符串,文件名均包含 %d.%d.%d 形式版本号,返回其中版本号最大的那个。 |
我一般用它来检测一堆发行版中版本号最大的那个文件。 |
:param str fmt: 要检测测字符串形式,例如 rookout-%s.tar.gz ,其中 %s 会被正则替换。 |
:param list files: 字符串列表。 |
:returns: 版本号最大的字符串。 |
:rtype: str |
"""""" |
x, y, z = 0,0,0 |
verpat = fmt%'(\d+).(\d+).(\d+)' |
verre = re.compile(r''+verpat+'', re.M) |
for f in filelist: |
match = verre.search(f) |
if match: |
x1 = int(match.group(1)) |
y1 = int(match.group(2)) |
z1 = int(match.group(3)) |
if x1 >= x and y1 >= y: |
x = x1 |
y = y1 |
z = z1 |
verfmt = fmt%('%d.%d.%d') |
name = verfmt%(x, y, z) |
if x == 0 and y == 0 and z == 0: |
slog.info('Can not find the string ""%s"" !'%name) |
return None |
return name" |
1140,"def merge_dicts(d1, d2): |
""""""合并两个无限深度的 dict |
会自动合并 list 格式 |
:param dict d1: 被合并的 dict |
:param dict d2: 待合并的 dict |
:returns: 一个新的生成器对象 |
:rtype: generator |
"""""" |
for k in set(d1.keys()).union(d2.keys()): |
if k in d1 and k in d2: |
if isinstance(d1[k], dict) and isinstance(d2[k], dict): |
yield (k, dict(merge_dicts(d1[k], d2[k]))) |
elif isinstance(d1[k], list): |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.