(译) Server-Sent Events: the alternative to WebSockets you should be using

原文地址: https://germano.dev/sse-websockets/ 作者: Germano Gabbianelli 当开发实时 web 应用时,WebSockets 可能是我们首先想到的。然而,Server Sent Events...

SSE

使用 mypy 做 type check

前言 完残!😂,最近看之前写的 Python 代码老得琢磨这比变量的类型是啥(Python 无类型系统xxx),不愧是我写的! 看段之前写的实现迭代器模式的代码:

Python

Serialization and Deserialization

序列化与反序列化 Serialization:Data Structure/Object --> Binary String Deserialization:Binary String --> Data Structure/Object Goals:Cross-platform Communication、Persistent Storage and More Python中对象的序列化与反序列化 pickle module pickle 仅可用于 Python,pickle所使用的数据流格式仅可用于 Python pickle 模块可以将复杂对象转换为字节流,也可以将字节流转换为具有相同内部结构的对象。 可被pickling和unpickling的对象:https://docs.python.org/zh-cn/3/library/pickle.html#what-can-be-pickled-and-unpickled pickle提供了优秀的方法方便我们对对象进行pickling(封存)和unpickling(解封)

Python

map-filter-reduce

听说函数式编程很⑥,咱也不知道,咱也不晓得,还没实际用过。emmm。。。。,先mark下Python中和函数式编程有关的部分功能先,又开始水了,立个flag🚩:慢慢完善 map 先看下Python官方文档的说法 map(function, iterable, …),返回一个将 function 应用于 iterable 中每一项并输出其结果的迭代器。 如果传入了额外的 iterable 参数,function 必须接受相同个数的实参并被应用于从所有可迭代对象中并行获取的项。 见识一下 123456789101112>>> def cook(something):... if something == "cow":... return "hamburger"... elif something == "tomato":... return "chips"... elif something == "chicken":... return "ddrumstick"... elif something == "corn":... return "popcorn"...>>> list(map(cook, ["cow", "tomato", "chicken", "corn"]))['hamburger', 'chips', 'ddrumstick', 'popcorn']

函数式编程

简单感受下Python内置数据类型常用操作的性能

生成一个列表的几种方式的性能对比 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152# -*- coding: utf-8 -*-from timeit import Timerimport matplotlib.pyplot as plt# 列表常用操作性能测试# 迭代 + '+'def test1(): l = [] for i in range(1000): l = l + [i]# 迭代 + appenddef test2(): l = [] for i in range(1000): l.append(i)# 列表生成式def test3(): l = [i for i in range(1000)]# list构造函数 + rangedef test4(): l = list(range(1000))t1 = Timer("test1()", "from __main__ import test1")# print("concat %f seconds" % t1.timeit(number=1000))t2 = Timer("test2()", "from __main__ import test2")# print("concat %f seconds" % t2.timeit(number=1000))t3 = Timer("test3()", "from __main__ import test3")# print("concat %f seconds" % t3.timeit(number=1000))t4 = Timer("test4()", "from __main__ import test4")# print("concat %f seconds" % t4.timeit(number=1000))result = [t1.timeit(1000), t2.timeit(1000), t3.timeit(1000), t4.timeit(1000)]method = ["for+ '+'", "for + append", "list comprehension", "list + range"]plt.bar(method, result, color='rgby')# plt.legend('concat time')# print(zip(method, result))for x,y in zip(method, result): plt.text(x, y, "%fs" % y)plt.show()

Python

Python 环境管理与项目依赖管理

个人简单记录下 virtualenv + pip virtualenv是一个用于创建"隔离的ython运行环境"的工具,Docs pip是Python的包管理工具,Docs 1234567891011121314# 安装virtualenvpip install virtualenv# -------------------------------- ## 虚拟环境的创建与使用# 1、在当前工程目录下使用virtualenv创建一套独立的Python运行环境virtualenv venv # 环境名为venv(自由定义)# 2、cd 到创建好的虚拟环境的Scripts目录,执行如下命令可激活或者退出虚拟环境activate # 激活,激活后命令提示符会变成当前工程目录Python环境名deactivate # 退出# 3、激活虚拟环境后可使用pip为当前项目安装依赖,example:pip install numpy# 4、使用pip freeze > requirements.txt 可导出项目依赖到requirements.txt中# 为项目创建一个新的、干净的环境时,可使用 pip install -r requiremen.txt 为项目安装依赖

Python

几个不错的Jupyter Notebook云端展示平台

jupyter nbviewer URL:https://nbviewer.jupyter.org/ 结合Github的示例用法:https://nbviewer.jupyter.org/github/ + <用户名或者用户名/存放ipynb文件的仓库或者Gist ID> 例如:https://nbviewer.jupyter.org/github/yeshan333/JupyterNotebook-Show-sample

Python

pdb && cProfile

pdb https://docs.python.org/zh-cn/3.7/library/pdb.html#module-pdb 使用方式 1、在命令行下直接运行调试 1python -m pdb test.py 2、在需要被调试的代码中添加import pdb、pdb.set_trace()再运行代码进行调试

Python

Python-自定义上下文管理器

上下文管理器 上下文管理器可以帮助我们自动分配和释放资源 上下文管理器需要配合with语句使用 比如进行文件操作的时候我们可能会忘记操作后关闭文件(file close),使用with open(filename, mode) as f不需要我们手动关闭文件,不管处理文件中是否有异常出现,都能保证with语句执行完毕后关闭文件,有效防止资源泄露,安全多了。 123# with 语句的一般格式with context_expression [as target(s)]: with-body 在执行with-body会调用上下文管理器的__enter__方法,执行完with-body之后再调用上下文管理器的__exit__方法

Python

Python协程-asyncio、async/await

看到吐血 (´ཀ`」 ∠) 协程(Coroutine)本质上是一个函数,特点是在代码块中可以将执行权交给其他协程 众所周知,子程序(函数)都是层级调用的,如果在A中调用了B,那么B执行完毕返回后A才能执行完毕。协程与子程序有点类似,但是它在执行过程中可以中断,转而执行其他的协程,在适当的时候再回来继续执行。 协程与多线程相比的最大优势在于:协程是一个线程中执行,没有线程切换的开销;协程由用户决定在哪里交出控制权 这里用到的是asyncio库(Python 3.7),这个库包含了大部分实现协程的魔法工具 使用 async 修饰词声明异步函数 使用 await 语句执行可等待对象(Coroutine、Task、Future) 使用 asyncio.create_task 创建任务,将异步函数(协程)作为参数传入,等待event loop执行 使用 asyncio.run 函数运行协程程序,协程函数作为参数传入

Python
12