从notebook学python
由于项目经常用到jupyter-notebook,所以经常会去看jupyter-notebook的源码。由于python本身的可读性比较强,再加上jupyter-notebook源码的注释写的非常完备,所以还是比较容易看懂的。
通过阅读jupyter-notebook的源码,学习到了很多python的写法和逻辑,在这里分享给大家。
- 未完待续……
# 在类中调用init方法
class VolumeShareSlippage(SlippageModel):
"""
Model slippage as a function of the volume of contracts traded.
"""
def __init__(self, volume_limit=DEFAULT_EQUITY_VOLUME_SLIPPAGE_BAR_LIMIT,
price_impact=0.1):
super(VolumeShareSlippage, self).__init__()
self.volume_limit = volume_limit
self.price_impact = price_impact
# 匿名函数lambda用法,获取函数返回值作为传入值
if not future.done():
self.log.debug("Waiting for pending kernel_info request")
future.add_done_callback(lambda f: self._finish_kernel_info(f.result()))
# del用法,删除列表中的特定元素
now = IOLoop.current().time()
while len(self._iopub_window_byte_queue) > 0:
queued = self._iopub_window_byte_queue[0]
if (now >= queued[0]):
self._iopub_window_byte_count -= queued[1]
self._iopub_window_msg_count -= 1
del self._iopub_window_byte_queue[0]
else:
# This part of the queue hasn't be reached yet, so we can
# abort the loop.
break
# not in 用法
if channel == 'iopub' and msg_type not in {'status', 'comm_open', 'execute_input'}: