Python杂记
Return the canonical path of the specified filename, eliminating any symbolic links encountered in the path (if they are supported by the operating system).
/home/yundongx/bin/instant-markdown-d-py
/home/yundongx/gitroom/instant-markdown-d/instant-markdown-d/instant-markdown-d.py
在instant-markdown-d-py
中运行:
BASE_DIR = os.path.dirname(os.path.realpath(__file__))
print(BASE_DIR)
将会输出:
/home/yundongx/gitroom/instant-markdown-d/instant-markdown-d
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import hashlib
data = b'This a md5 test!'
hash_md5 = hashlib.md5(data)
hash_sha256 = hashlib.sha256(data)
# 输出结果为16进制表示的hash值
# md5结果为128位,所以结果长度就为 128 / 4 = 32
# sha256结果为256位,所以结果长度就为 256 / 4 = 64
md5 = hash_md5.hexdigest()
print("[md5:{}]: {}".format(len(md5), md5))
sha256 = hash_sha256.hexdigest()
print("[sha256:{}]: {}".format(len(sha256), sha256))
## 处理大文件,当处理大文件的时候,由于内存不够用可能会导致程序崩溃
## 此时就要使用 update 方法了
## 通过这个update函数分段去进行计算,最后得到hash值
import hashlib
def get_file_md5(f):
m = hashlib.md5()
while True:
data = f.read(10240)
if not data:
break
m.update(data)
return m.hexdigest()
def get_file_sha256(f):
h = hashlib.sha256()
while True:
data = f.read(4096)
if not data:
break
h.update(data)
return h.hexdigest()
with open('/home/yundongx/Desktop/books/经典书籍/代码整洁之道.pdf', 'rb') as f:
file_md5 = get_file_md5(f)
print(file_md5)
with open('/home/yundongx/Desktop/books/经典书籍/代码整洁之道.pdf', 'rb') as f:
file_sha256 = get_file_sha256(f)
print(file_sha256)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
这部分代码中,解析了 yield from 的用法。
以 main 中调用 gather_tallies, gather_tallies中`yield from accumulate`为例:
+ yield from主要的的功能就是让 main 和 accumulate 直接通过send和next进行交互,
而 gather_tallies 保持休眠,而最终 accumulate 调用完成以后(既这个生成器close了)
返回的值返回给了gather_tallies 函数,gather_tallies 继续执行。
+ 需要注意的是 生成器 通过return语句返回值的时候,是抛出了一个 StopIteration 的
异常,而异常的值就是`return`返回的值。`yield from`自动处理了这部分,能够自动获取值。
"""
def accumulate():
tally = 0
while True:
_next = yield
if _next is None:
return tally
tally += _next
def gather_tallies(tallies):
"""yield from 说明
这里的这个 yield from 的语句的意思就是一直运行子生成器accumulate,
一直到这个自生成器关闭了,这条语句才返回。
"""
while True:
tally = yield from accumulate()
tallies.append(tally)
def main():
""" yield from 简单分析
在这个例子中,gather_tallies 就好像在 main 函数和 accumulate 函数之间的一个透明代理
main 函数一运行,就相当于直接调用了子生成器。
而 main 函数中的next,send函数也是直接和子生成器进行交互的。
和 main 函数直接调用 accumulate 不同的是,main 函数通过 gather_tallies 函数可以不断地关闭,新建生成器。
"""
tallies = []
# 这里一运行直接运行到了子生成器(accumulate)中
acc = gather_tallies(tallies)
# 下面的这些操作都是在和子生成器交互
# 这里初始化子生成器,让它运行到 yield 处
next(acc)
for i in range(4):
acc.send(i)
# send(None)之后,gather_tallies 函数又去重新生成了一个子生成器,
# 运行到了 yield 语句
acc.send(None)
# 这里又启动了一个新的子生成器
for i in range(5):
acc.send(i)
acc.send(None)
print(tallies)
def test_main():
""" main直接调用子生成器
在这个函数中,main函数直接调用子生成器。这里子生成器 return 语句的效果就是
直接向调用者抛出了一个StopIteration的异常,这个异常的值就是返回的值。
参考 tornado 的 `raise gen.Return(retval)`和`return`,应该是3.3中新实现的return语句
"""
g = accumulate()
next(g)
for i in range(4):
g.send(i)
try:
g.send(None)
except StopIteration as e:
print(e)
g = accumulate()
next(g)
for i in range(5):
g.send(i)
try:
g.send(None)
except StopIteration as e:
print(e)
if __name__ == '__main__':
test_main()
main()
def g(x):
# `yield from iterable` 等价于 `for item in iterable: yield item`
yield from range(x, 0, -1)
for item in range(x):
yield item
print(list(g(5)))