/python-fragments

python编程实践代码示例

Primary LanguageHTMLApache License 2.0Apache-2.0

python-fragments

python相关的代码片段 大部分可在python3.6运行,少数代码需要在python3.7+运行。

python之禅

执行命令: python -c 'import this'

The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

代码导航

一、基础模块相关

1、解析命令行参数(argparse和optparse)

2、异步IO的应用(asyncio)python3.7+

3、asyncio与threading结合的使用

4、字节处理(bytes)

5、class类定义与应用

6、列表排序及collections的使用

7、读取静态配置(configparser)

8、查看代码执行效率(cProfile)

9、字符的编码解码,编码类型检测(encoding、chardet)

10、异常处理与堆栈信息

11、格式化输出(format)

12、函数与装饰器

13、函数耗时统计

14、yield关键字与生成器

15、python的自省(inspect)

16、日志工具的编写(logging)

17、python的序列化(pickle)

18、图像处理(pil)

19、RSA加密算法的实现与应用

20、python执行shell命令的几种方法

21、字符串处理及内置方法的使用

22、多线程实现生产者消费者模型(threading)

23、asyncio实现生产者消费者模型

24、比较两种生产者与消费者模型(threading与asyncio)

25、多进程实现生产者消费者模型(multiprocessing)

26、多进程共享内存的应用(Process和Manager)

二、数据结构与算法

1、排序算法:冒泡排序

2、排序算法:快速排序

3、查找算法:BruteForce暴力查找

4、查找算法:KMP查找

5、搜索算法:Dijkstra最短路径算法

6、数据结构:最大堆

7、排序算法:堆排序-基于最大堆

8、数据结构:最小堆

9、排序算法:堆排序-基于最小堆

10、排序算法:插入排序

11、排序算法:希尔排序

12、排序算法:归并排序

13、图的遍历:BFS广度优先遍历

14、图的遍历:DFS深度优先遍历

15、查找算法:二分查找

16、数据结构:二叉查找树的python实现

17、数据结构:Trie字典树的python实现

18、数据结构:Huffman哈夫曼树的python实现

19、压缩算法:利用哈夫曼树实现数据压缩与解压缩

20、搜索算法:利用Trie实现多模字符串的匹配(类AC自动机)

21、排序算法:计数排序(支持负数)

22、排序算法:基数排序(支持负数)

23、搜索算法:Dijkstra最短路径算法的另一种python实现

24、数据结构:SkipList跳跃链表的python实现

25、排序算法:桶排序

26、搜索算法:利用python原生库实现bloom过滤器

27、搜索算法:利用mmh3+bitarray实现BloomFilter

28、数据结构:DoubleArrayTrie双数组前缀树的python实现

29、压缩算法:算术编码的python实现

30、随机算法:加权随机的python实现

三、扩展模块相关

1、folium包的地图应用

2、lxml处理html和xml

3、matplotlib绘制2D坐标图

4、matplotlib的更多应用(曲线图、直方图、散点图、3D图)

5、numpy进行科学计算(精度、排序、矩阵、线性代数)

6、opencv的基本应用(通道处理)

7、opencv的更多应用(图像特性处理:怀旧、素描、光照、滤镜)

8、openpyxl处理excel文档(读取、修改、创建)

9、reportlab制作pdf文件

10、websockets的应用(配合asyncio实现)

11、webscokets的多线程应用

12、PIL的基本应用

四、机器学习-NLP相关

1、聚类-基于密度的噪声应用空间聚类(DBSCAN)

2、分类-朴素贝叶斯(Naive Bayes)

3、分类-基于gensim的word2vec应用

4、分类-基于gensim的doc2vec应用

5、聚类-k-means

6、搜索相关性算法-BM25

7、搜索网页去重算法-IMatch

8、搜索网页去重算法-Shingling

9、搜索网页去重算法-SimHash

五、工具脚本

1、贷款本息计算

2、个税计算

3、投资年化收益计算