Source Codehtml
Welcome to Python.orgpython
3.6.3rc1 Documentationlinux
PEP 8 -- Style Guide for Python Code | Python.orggit
Python_百度百科程序員
- https://baike.baidu.com/item/Python/407313?fr=aladdin#reference-[12]-21087-wrap
- 自從20世紀90年代初Python語言誕生至今,它已被逐漸普遍應用於系統管理任務的處理和Web編程。
- Python是徹底面向對象的語言。函數、模塊、數字、字符串都是對象。而且徹底支持繼承、重載、派生、多繼承,有益於加強源代碼的複用性。Python支持重載運算符和動態類型。
- Python容許像數學的經常使用寫法那樣連着寫兩個比較運行符。好比a < b < c與a < b and b < c等價。C++的結果與Python不同,首先它會先計算a < b,根據二者的大小得到0或者1兩個值之一,而後再與c進行比較。
- Python擁有一個強大的標準庫。Python語言的核心只包含數字、字符串、列表、字典、文件等常見類型和函數,而由Python標準庫提供了系統管理、網絡通訊、文本處理、數據庫接口、圖形系統、XML處理等額外的功能。
學習筆記之Python IDE ( Anaconda / PyCharm ) - 浩然119 - 博客園github
學習筆記之Python 3web
面試總結之Python - 浩然119 - 博客園面試
學習筆記之盤一盤 Python 系列 1 & 2 - 入門篇 - 浩然119 - 博客園正則表達式
學習筆記之Python爬蟲 - 浩然119 - 博客園算法
學習筆記之Django - 浩然119 - 博客園
Python3 教程 | 菜鳥教程
Python 入門指南 — Python tutorial 3.6.0 documentation
在 Windows 上使用 Python 進行開發 - Windows apps | Microsoft Docs
網絡課程 python 網絡教育-百度傳課
Python Example
一文總結學習 Python 的 14 張思惟導圖 - 人工智能與大數據技術
- https://mp.weixin.qq.com/s/yuxqP0cNsUgYnfLlStEQ5Q
- https://woaielf.github.io/2017/06/13/python3-all/
- 本文主要涵蓋了 Python 編程的核心知識(暫不包括標準庫及第三方庫,後續會發布相應專題的文章)。
- 第1張圖
- 基礎知識圖一包括了基本規則、Python語言特色、計算機語言、如何運行Python、變量賦值五個方面,輔助你快速掌握Python編程的基底知識。
- 第2張圖
- 基礎知識圖二包含了模塊結構、佈局、IO編程流程、標識符、Python對象、內存管理、動態類型六大模塊,兩張基礎知識導圖能夠幫助你區域化了解Python的組成部分及基本操做。
- 第3張圖
- 學習Python少不了對數據的瞭解,這張圖整理了數據類型的分類、做用、空值、標準數據、if語句等等模塊。
- 第4張圖
- 這張圖整理了序列的有序排列、標準操做符與序列類型操做符的重點知識,以及可操做性的BIF。
- 第5張圖
- 字符串是個比較龐大而精細的部分,接着上圖的BIF可分爲標準類型、序列類型、字符串類型,字符串可分爲五種操做符類型,此圖還整理了序列的獨特特性以及編碼問題,能夠說很詳細了。
- 第6張圖
- 關於列表|元素,首先說拷貝問題,分深淺拷貝兩種形式。tuple的內建函數、特殊特性與list的操做符、內建函數是重點部分。
- 第7張圖
- 這張圖主要整理了字典|集合中set、dict的功能、分類、BIF、操做問題。
- 第8張圖
- 條件|循環包含生成器、迭代器、列表解析的使用、拓展,相關BIF、if語句循環控制也可以快速掌握重點。
- 第9張圖
- 關於文件對象內建方法、內建函數、內建屬性都有具體內容,文件迭代的運用,標準文件對象如何輸入輸出以及分隔符的運用都在導圖中詳細標明。
- 第10張圖
- 錯誤|異常這張圖的點介紹瞭如何調試、處理異常狀況。
- 第11張圖
- 函數一介紹了函數概述,注意vs函數的引用、調用,裝飾器的定義、「堆疊」。參數具備本身的完整語法以及本身的傳遞方式。
- 第12張圖
- 函數二圖整理了遞歸函數、返回(回調)函數、變量做用域、偏函數、函數式編程、匿名函數、高階函數BIF的詳細介紹。
- 第13張圖
- 這張圖的重點是模塊的標準區域、名稱空間以及模塊的做用域(三種變量的運用)。
- 第14張圖
- 最後一張圖整理了面向對象編程,弄清楚面向對象的基本概念,繼承與多態、結構組織以及對象的性質、訪問限制等重點,對於python就算是入門了。
學習Python必備的8本書 - Python編程
Python 語法速覽與實戰清單 - Python編程
Python 教程:從零到大師 - Python編程
Python三十年技術演變史
BASIC
Python 字符串拼接總結 - Python編程
Python字符串用法大全
Python字符串處理的8招祕籍
如何優雅的操做Python字典 - 程序員大咖
乾貨|理解Python列表和元組
圖解 Python 函數
ADVANCE
5張圖理解Python中的淺拷貝與深拷貝
Python 程序員如何防止數據被修改?
Python 進階:全面解讀高級特性之切片
迭代器與生成器
Python 迭代器與生成器 - Python編程
- https://mp.weixin.qq.com/s/cT0vqLAvqOc5FNnx6Iu_iw
- http://www.langzi.fun/%E8%BF%AD%E4%BB%A3%E5%99%A8%E4%B8%8E%E7%94%9F%E6%88%90%E5%99%A8.html
Python 迭代器和 C++ 迭代器最大的不一樣
帶你完全搞懂Python生成器
完全理解Python中的yield
模塊
深刻探討Python的import機制:實現遠程導入模塊 | CSDN博文精選
Python Logging 模塊徹底解讀
四種高性能數據類型,Python collections助你優化代碼、簡潔任務
IO / FILE
Python 數據形態及IO操做
Python處理CSV、JSON和XML數據的簡便方法
錯誤和異常
學習筆記之Python Debug ( pdb ) - 浩然119 - 博客園
一文教你讀懂 Python 中的異常信息
面向對象
Python 面向對象編程
- https://mp.weixin.qq.com/s/IFRloykz9Nnb3N94CQpieg
- http://www.langzi.fun/Python%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E7%BC%96%E7%A8%8B.html
- 面向過程編程
- 面向對象編程
- 類的基本用法
- 類與實例
- 調用類的三種方法
- 類的特性
- 魔法方法
- __doc__
- __init__
- __module__ / __class__
- module 表示當前操做的對象在屬於哪一個模塊。
- class 表示當前操做的對象屬於哪一個類。
- 這二者也是Python內建,無需自定義。
- __del__
- 析構方法,當對象在內存中被釋放時,自動觸發此方法。
- 注:此方法通常無須自定義,由於Python自帶內存分配和釋放機制,除非你須要在釋放的時候指定作一些動做。析構函數的調用是由解釋器在進行垃圾回收時自動觸發執行的。
- __call__
- 若是爲一個類編寫了該方法,那麼在該類的實例後面加括號,可會調用這個方法。
- 注:構造方法的執行是由類加括號執行的,即:對象 = 類名(),而對於call() 方法,是由對象後加括號觸發的,即:對象() 或者 類()()
- 能夠用Python內建的callable()函數進行測試,判斷一個對象是否能夠被執行。
- __dict__
- 列出類或對象中的全部成員!很是重要和有用的一個屬性,Python自建,無需用戶本身定義。
- __str__
- 若是一個類中定義了str()方法,那麼在打印對象時,默認輸出該方法的返回值。這也是一個很是重要的方法,須要用戶本身定義。
- __getitem__ / __setitem__ / __delitem__
- 取值、賦值、刪除這「三劍客」的套路,在Python中,咱們已經見過不少次了,好比前面的@property裝飾器。
- Python中,標識符後面加圓括號,一般表明執行或調用方法的意思。而在標識符後面加中括號[],一般表明取值的意思。Python設計了getitem()、setitem()、delitem()這三個特殊成員,用於執行與中括號有關的動做。它們分別表示取值、賦值、刪除數據。
- 若是有一個類同時定義了這三個魔法方法,那麼這個類的實例的行爲看起來就像一個字典同樣
- __iter__
- 這是迭代器方法!列表、字典、元組之因此能夠進行for循環,是由於其內部定義了 iter()這個方法。若是用戶想讓自定義的類的對象能夠被迭代,那麼就須要在類中定義這個方法,而且讓該方法的返回值是一個可迭代的對象。當在代碼中利用for循環遍歷對象時,就會調用類的這個iter()方法。
- __len__
- 在Python中,若是你調用內置的len()函數試圖獲取一個對象的長度,在後臺,實際上是去調用該對象的len()方法
- Python的list、dict、str等內置數據類型都實現了該方法,可是你自定義的類要實現len方法須要好好設計。
- __repr__
- 這個方法的做用和str()很像,二者的區別是str()返回用戶看到的字符串,而repr()返回程序開發者看到的字符串,也就是說,repr()是爲調試服務的。一般二者代碼同樣。
- __add__ / __sub__ / __mul__ / __div__ / __mod__ / __pow__
- 這些都是算術運算方法,須要你本身爲類設計具體運算代碼。有些Python內置數據類型,好比int就帶有這些方法。Python支持運算符的重載,也就是重寫。
- __cmp__
- __author__
- __slots__
- Python做爲一種動態語言,能夠在類定義完成和實例化後,給類或者對象繼續添加隨意個數或者任意類型的變量或方法,這是動態語言的特性。
- 可是!若是我想限制實例能夠添加的變量怎麼辦?可使slots限制實例的變量,好比,只容許Foo的實例添加name和age屬性。
- 須要提醒的是,slots定義的屬性僅對當前類的實例起做用,對繼承了它的子類是不起做用的。想一想也是這個道理,若是你繼承一個父類,卻莫名其妙發現有些變量沒法定義,那不是大問題麼?若是非要子類也被限制,除非在子類中也定義slots,這樣,子類實例容許定義的屬性就是自身的slots加上父類的slots。
- 成員保護與訪問機制
- 私有成員
- 使用get-set-del方法操做私有成員
- Propety裝飾器
- 經常使用的調用方法
- 使用裝飾器
- 更加減半的使用property()函數
簡單理解python面向對象及裝飾器
如何理解 Python 中的面向對象編程?
沒看完這11 條,別說你精通 Python 裝飾器
Python中的元編程:一個關於修飾器和元類的簡單教程
多線程
淺談 Python 中的多線程 - Python編程
- https://mp.weixin.qq.com/s/7dLKlCUumx9NS-Zmv8iSvA
理解python多線程和多進程
深刻理解python多線程和多進程
入門 | 三行Python代碼,讓數據預處理速度提升2到6倍
編碼
一文透徹掌握 Python 編碼問題
一圖看懂 Python 2 / Python 3 編碼 | CSDN 博文精選
PYTHONIC STYLE
STYLE GUIDE
學習筆記之Python最簡編碼規範 - 浩然119 - 博客園
代碼整潔之道-編寫 Pythonic 代碼
Python 編碼風格指南
Python 簡潔編碼之道
18式優雅你的Python
改善Python程序的91個建議 - Python編程
符合語言習慣的 Python 優雅編程技巧 - 超級數學建模
優雅編寫Python3的62個小貼士
TIPS
@Python 程序員,如何最大化提高編碼效率?
這些Python代碼技巧,你確定還不知道
wtfPython—Python中一些奇妙的代碼
15個Pythonic的代碼示例
7個案例15分鐘讓你瞭解Python套路
Python帶我飛:50個有趣而又不爲人知的Python特性
Python中實用卻不常見的小技巧
Python 開發中有哪些高級技巧?
18 個 Python 高效編程技巧
10招玩轉Python
學Python,從列表推導到zip()函數,這五種技巧應知應會
10 個鮮爲人知的Python冷知識
Python的高級特徵你知多少
26個Python實用技巧
Python 有哪些不同的技巧
即學即用的30段Python實用代碼
每30秒學會一個Python小技巧
Python的 5 種高級用法
Python 的 20 個操做
Python技巧小貼士
Python 十大語法
20 個 Python 技巧
BEST PRACTICE
Python編寫循環的兩個建議 | 鵝廠實戰
8個Python高效數據分析的技巧
7 個 Python 特殊技巧,有效提高數分效率!
使用類型註解讓 Python 代碼更易讀
分享8點有用的Python編程建議
爲何Python不用設計模式?
動態類型一時爽,代碼重構火葬場?
Python 10大謬論
坑
Python 初學者常犯的5個錯誤,布爾型竟是整型的子類
Python 常見的17個錯誤分析
運行速度
爲何Python這麼慢? - Python編程
一行代碼讓 Python 的運行速度提升100倍
24式加速你的Python
內存
Python技巧 | 一行代碼減小一半內存佔用
如何下降 Python 的內存消耗量?
Python 內存分配時的小祕密
資源庫工具
Python算法實現資源彙總
那些有趣/用的 Python 庫
140種Python標準庫、第三方庫和外部工具都有了
介紹幾款 Python 類型檢查工具
Python中的兩個測試工具
Python 中更優雅的日誌記錄方案
如何編寫完美的 Python 命令行程序?
APPLICATION
用 Python 打包本身的庫到 PYPI
用 Python 寫一個安卓 APP
用Python把Linux命令寫一遍
「堆」的 Python 實現與應用總結
如何用 Python 快速開發一個區塊鏈數據結構?
一文讀懂Python複雜網絡分析庫networkx | CSDN博文精選
開發必學的驗證碼,教你從零寫一個驗證碼
萬字乾貨 | Python後臺開發的高併發場景優化解決方案
Python實現五子棋人機對戰 | CSDN博文精選
用 Python 偷偷抓取了她的行蹤
誰偷偷刪了你的微信?別慌!Python 幫你都揪出來了
AI
不足 20 行 Python 代碼,高效實現 k-means 均值聚類算法!
使用Python進行機器學習的假設檢驗(附連接&代碼)
Python 天然語言處理:輕鬆上手文本分類
用 Python 自制序列標註平臺
用 Python 實現英文單詞糾錯功能
利用 50 行 Python 代碼構建一個在線文本生成器!
如何建立一個百分百懂你的產品推薦系統 | 深度教程(附代碼詳解)
如何經過 Python 和 OpenCV 實現目標數量監控?
DATA SCIENCE
【實戰】使用 Python 分析 14 億條數據
用 Python 繪製污染物玫瑰圖
用 Python 測算氣象預報的空報率與漏報率
FILE
利用 tornado 實現表格文件預覽
Python 處理分析 128 張 Excel 表格竟不到3秒?| 附數據集
我用Python作了一份PDF報告 - Python編程
將Python字符串生成PDF
用 Python 操做 Word 文檔
爬蟲分析
基於豆瓣影評數據的完整文本分析
如何經過 Python 分析中國演員排名?
Python 爬取 3000 部電影,最具人氣爛片排行榜出爐!
基於微博數據的人物性格分類系統
基於詞典和弱標註信息的電影評論情感分析系統
分析了16年的福利彩票記錄,原來能夠用Python這麼買彩票
Python 分析國慶熱門旅遊景點
用 Python 總結分析男籃世界盃
用 Python 分析各國人口性別比例
用Python分析全國高等教育分佈狀況
Python 分析中國城市夜間燈光數據
教你如何用Python自動下載抖音小姐姐
用 Python 全自動下載抖音視頻
《Python入門必備指南》之如何系統地自學 Python?_騰訊課堂
- https://ke.qq.com/course/217064
- 經過實例知道下list,dict實際使用中一些技巧
- 瞭解web編程的學習線路圖,知識網絡
get
(key[, default])
- https://docs.python.org/3/library/stdtypes.html?highlight=get#dict.get
- Return the value for key if key is in the dictionary, else default. If default is not given, it defaults to
None
, so that this method never raises a KeyError
.
- sorted(iterable, *, key=None, reverse=False)
- https://docs.python.org/3/library/functions.html?highlight=sorted#sorted
- Return a new sorted list from the items in iterable.
items
()
- https://docs.python.org/3/library/stdtypes.html?highlight=items#dict.items
- Return a new view of the dictionary’s items (
(key, value)
pairs). See the documentation of view objects.
- 4.7.5. Lambda Expressions
- https://docs.python.org/3/tutorial/controlflow.html?highlight=lambda#lambda-expressions
- Small anonymous functions can be created with the lambda keyword.
1 #!/usr/bin/python3
2
3 res = {}
4 with open('demo.txt') as f:
5 for ch in f.read().replace(' ', ''):
6 res[ch] = res.get(ch, 0) + 1
7
8 # lambda x[1] stands for value in dictionary, x[0] stands for key in dictionary
9 for char, num in sorted(res.items(), key=lambda x: x[1], reverse=True)[:3]:
10 print('char %s count is %d' % (char, num))
View Code
深刻淺出帶你學Python衝擊年薪30萬【馬哥教育】_騰訊課堂
- https://ke.qq.com/course/134017
- Python哲學
- Python使用引用計數記錄全部變量的引用數
- 當變量引用數變爲0,它就能夠被垃圾回收GC。
- 計數增長:賦值給其餘變量就增長引用計數。E.g. x = 3; y = x;
- 計數減小:
- 函數運行結束時,局部變量就被自動銷燬,對象引用計數減小;
- 變量被賦值給其餘變量。x = 3; y = x; x = 4;
【Python入門只需20分鐘】從安裝到數據抓取、存儲原來這麼簡單 - 旺旺筆記 - 博客園
- https://www.cnblogs.com/zhaww/p/9517514.html
What's package and module ?
- Python 提供了一個辦法,把這些定義存放在文件中,爲一些腳本或者交互式的解釋器實例使用,這個文件被稱爲模塊。模塊是一個包含全部你定義的函數和變量的文件,其後綴名是.py。模塊能夠被別的程序引入,以使用該模塊中的函數等功能。這也是使用 python 標準庫的方法。
- 包是一種管理 Python 模塊命名空間的形式,採用"點模塊名稱"。在導入一個包的時候,Python 會根據 sys.path 中的目錄來尋找這個包中包含的子目錄。目錄只有包含一個叫作 __init__.py 的文件纔會被認做是一個包,主要是爲了不一些濫俗的名字(好比叫作 string)不當心的影響搜索路徑中的有效模塊。
Abstract base class v.s. Interface ?
- Interfaces in Python: Protocols and ABCs · Abu Ashraf Masnun
- There’s no
interface
keyword in Python. The Java / C# way of using interfaces is not available here. In the dynamic language world, things are more implicit. We’re more focused on how an object behaves, rather than it’s type/class.
- PEP 3119 -- Introducing Abstract Base Classes | Python.org
- collections.abc — Abstract Base Classes for Containers — Python 3.8.0 documentation
- 3. Data model — Python 3.8.0 documentation
- Built-in Types — Python 3.8.0 documentation
- Duck typing - Wikipedia
- https://en.wikipedia.org/wiki/Duck_typing
- Duck typing in computer programming is an application of the duck test—"If it walks like a duck and it quacks like a duck, then it must be a duck"—to determine if an object can be used for a particular purpose. With normal typing, suitability is determined by an object's type. In duck typing, an object's suitability is determined by the presence of certain methods and properties, rather than the type of the object itself.[1]
- oop - How do I implement interfaces in python? - Stack Overflow
- Difference between abstract class and interface in Python - Stack Overflow
如何多行字符串拼接?
1 # -*- coding: utf-8 -*-
2 """
3 @author: Hao
4 """
5
6 start_timestamp = "2018-01-01 00:00:00"
7 end_timestamp = "2018-01-02 00:00:00"
8
9 # =============================================================================
10 # SELECT "timestamp", col1
11 # FROM tbl
12 # WHERE "timestamp" >= '2018-01-01 00:00:00' AND "timestamp" <= '2018-01-02 00:00:00'
13 # ORDER BY "timestamp" ASC
14 # =============================================================================
15 query = """
16 SELECT "timestamp", col1
17 FROM tbl
18 WHERE "timestamp" >= '""" \
19 + start_timestamp + \
20 """' AND "timestamp" <= '""" \
21 + end_timestamp + \
22 """'
23 ORDER BY "timestamp" ASC
24 """
25
26 print(query)
27
28 # =============================================================================
29 # SELECT "timestamp", col1
30 # FROM tbl
31 # WHERE "timestamp" >= '2018-01-01 00:00:00' AND "timestamp" <= '2018-01-02 00:00:00'
32 # ORDER BY "timestamp" ASC
33 # =============================================================================
34 query = ("""
35 SELECT "timestamp", col1
36 FROM tbl
37 WHERE "timestamp" >= '"""
38 + start_timestamp +
39 """' AND "timestamp" <= '"""
40 + end_timestamp +
41 """'
42 ORDER BY "timestamp" ASC
43 """)
44
45 print(query)
View Code
Comparisons
- 6. Expressions — Python 3.7.4 documentation
- https://docs.python.org/3/reference/expressions.html#comparisons
- Unlike C, all comparison operations in Python have the same priority, which is lower than that of any arithmetic, shifting or bitwise operation. Also unlike C, expressions like
a < b < c
have the interpretation that is conventional in mathematics
- Comparisons can be chained arbitrarily, e.g.,
x < y <= z
is equivalent to x < y and y <= z
, except that y
is evaluated only once (but in both cases z
is not evaluated at all when x < y
is found to be false).
Conditional Expressions
- 6. Expressions — Python 3.7.0 documentation
- https://docs.python.org/3/reference/expressions.html?highlight=conditional%20expressions#conditional-expressions
- x = 1 if y == 1 else 0
- 注意Python中沒有三元運算符 y == 1 ? 1 : 0
- 1 PEP 308: Conditional Expressions
- https://docs.python.org/2.5/whatsnew/pep-308.html
How to display a decimal in scientific notation ?
- '{:.2e}'.format(0.456) = '4.56e-01'
- '{:.2f}'.format(0.456) = '0.46'
- python - Display a decimal in scientific notation - Stack Overflow
- https://stackoverflow.com/questions/6913532/display-a-decimal-in-scientific-notation
What's the infinity number ?
- float('inf')
- Built-in Types — Python 3.7.4 documentation
zip
- https://docs.python.org/3/library/2to3.html?highlight=zip#2to3fixer-zip
- Wraps
zip()
usage in a list
call. This is disabled when from future_builtins import zip
appears.
How to convert dictionary to list ?
- Converting Python Dictionary to List - Stack Overflow
- https://stackoverflow.com/questions/1679384/converting-python-dictionary-to-list
- 4. Built-in Types — Python 3.6.6rc1 documentation
- https://docs.python.org/3/library/stdtypes.html?highlight=items#dict.items
- https://docs.python.org/3/library/stdtypes.html?highlight=items#dictionary-view-objects
How to convert list to string ?
- stest = str(['test1', 'test2', 'test3']).strip('[]')
- 4. Built-in Types — Python 3.6.6rc1 documentation
- https://docs.python.org/3/library/stdtypes.html?highlight=str#text-sequence-type-str
- https://docs.python.org/3/library/stdtypes.html?highlight=str#str.strip
- python - TypeError: cannot concatenate 'str' and 'list' objects in email - Stack Overflow
- https://stackoverflow.com/questions/26521899/typeerror-cannot-concatenate-str-and-list-objects-in-email
How to convert list to tuple ?
- tuple( list_obj )
- Python | Convert a list into a tuple - GeeksforGeeks
How to check if a list contains elements of another list ?
- check = all(item in List1 for item in List2)
- check = any(item in List1 for item in List2)
- Check if Python List Contains Elements of Another List
- Built-in Functions — Python 3.7.4 documentation
How to check if substring exists ?
- if "substring" in test_string:
- if s.startswith(("a", "b")):
- 6. Expressions — Python 3.7.2rc1 documentation - Membership test operations
- https://docs.python.org/3/reference/expressions.html#membership-test-details
- Built-in Types — Python 3.7.2rc1 documentation
- str.startswith(prefix[, start[, end]])
- Return True if string starts with the prefix, otherwise return False. prefix can also be a tuple of prefixes to look for. With optional start, test string beginning at that position. With optional end, stop comparing string at that position.
- Does Python have a string 'contains' substring method? - Stack Overflow
- https://stackoverflow.com/questions/3437059/does-python-have-a-string-contains-substring-method
- if "blah" not in somestring:
How to replace characters / substring in a string ?
- 'www.example.com'.strip('cmowz.')
- str.replace('html', 'log')
- Pay attention that strip will only remove the leading and trailing characters.
- Built-in Types — Python 3.7.1 documentation - str.strip([chars])
- https://docs.python.org/3/library/stdtypes.html?highlight=strip#str.strip
- Return a copy of the string with the leading and trailing characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or
None
, the chars argument defaults to removing whitespace. The chars argument is not a prefix or suffix; rather, all combinations of its values are stripped
- str.replace(old, new[, count])
- https://docs.python.org/3/library/stdtypes.html?highlight=replace#str.replace
- Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.
Two types usage of for loop ?
- python - "for loop" with two variables? - Stack Overflow
- https://stackoverflow.com/questions/18648626/for-loop-with-two-variables
datetime operation
- Get date of the datetime instance
- 8.1. datetime — Basic date and time types — Python 3.6.6rc1 documentation
- https://docs.python.org/3/library/datetime.html#datetime.date
- timedelta Objects - datetime — Basic date and time types — Python 3.7.2 documentation
- https://docs.python.org/3/library/datetime.html#timedelta-objects
- class datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)
- Python datetime to string without microsecond component - Stack Overflow
- https://stackoverflow.com/questions/7999935/python-datetime-to-string-without-microsecond-component
- 8.1. datetime — Basic date and time types — Python 3.3.7 documentation
- https://docs.python.org/3.3/library/datetime.html#datetime.datetime.strftime
- How to increment the day in datetime? Python - Stack Overflow
- https://stackoverflow.com/questions/3240458/how-to-increment-the-day-in-datetime-python
- date += datetime.timedelta(days=1)
How to find the min value in dictionary ?
- min(d.items(), key=lambda x: x[1])
- min(d.items(), key=d.get)
- min(d.values())
- min(d.keys())
- python - Get the key corresponding to the minimum value within a dictionary - Stack Overflow
- https://stackoverflow.com/questions/3282823/get-the-key-corresponding-to-the-minimum-value-within-a-dictionary
- min(d, key=d.get)
- python - Getting key with maximum value in dictionary? - Stack Overflow
- https://stackoverflow.com/questions/268272/getting-key-with-maximum-value-in-dictionary
- 2. Built-in Functions — Python 3.7.0 documentation
- https://docs.python.org/3/library/functions.html?highlight=min#min
- https://docs.python.org/3/library/stdtypes.html?highlight=dictionary#dict.get
- Python3 min() 函數 | 菜鳥教程
- http://www.runoob.com/python3/python3-func-number-min.html
- Python3 字典 get() 方法 | 菜鳥教程
- http://www.runoob.com/python3/python3-att-dictionary-get.html
How to check if dictionary/list/string/tuple is empty ?
- PEP 8 -- Style Guide for Python Code | Python.org
- https://www.python.org/dev/peps/pep-0008/
- For sequences, (strings, lists, tuples), use the fact that empty sequences are false.
- Yes: if not seq: / if seq:
- No: if len(seq): / if not len(seq):
- Python: Checking if a 'Dictionary' is empty doesn't seem to work - Stack Overflow
- https://stackoverflow.com/questions/23177439/python-checking-if-a-dictionary-is-empty-doesnt-seem-to-work
- python - How do I check if a list is empty? - Stack Overflow
- https://stackoverflow.com/questions/53513/how-do-i-check-if-a-list-is-empty
How to print lists ?
- Print lists in Python (4 Different Ways) - GeeksforGeeks
- https://www.geeksforgeeks.org/print-lists-in-python-4-different-ways/
1 # -*- coding: utf-8 -*-
2 """
3 Created on Thu Nov 22 11:05:55 2018
4
5 @author: h.tang
6 """
7
8 # using for loop
9 a = [1, 2, 3, 4, 5]
10
11 # =============================================================================
12 # 1. Using for loop : Traverse from 0 to len(list) and print all elements of the list one by one uisng a for loop,
13 # this is the standard practice of doing it.
14 # =============================================================================
15 #1
16 #2
17 #3
18 #4
19 #5
20 #1 2 3 4 5
21
22 # printing the list using loop
23 for x in range(len(a)):
24 print a[x]
25
26 for x in range(len(a)):
27 print a[x],
28
29 # =============================================================================
30 # 2. Without using loops: * symbol is use to print the list elements in a single line with space.
31 # To print all elements in new lines or separated by space use sep=」\n」 or sep=」, 」 respectively.
32 # =============================================================================
33 # Python program to print list
34 # without using loop
35
36 a = [1, 2, 3, 4, 5]
37
38 #1 2 3 4 5
39 #printing lists separated by commas
40 #1, 2, 3, 4, 5
41 #printing lists in new line
42 #1
43 #2
44 #3
45 #4
46 #5
47
48 # printing the list using * operator separated
49 # by space
50 print(*a)
51
52 # printing the list using * and sep operator
53 print("printing lists separated by commas")
54
55 print(*a, sep = ", ")
56
57 # print in new line
58 print("printing lists in new line")
59
60 print(*a, sep = "\n")
61
62 # =============================================================================
63 # 3. Convert a list to a string for display : If it is a list of strings we can simply join them using join() function,
64 # but if the list contains integers then convert it into string and then use join() function to join them to a string and print the string.
65 # =============================================================================
66 # Python program to print list
67 # by Converting a list to a
68 # string for display
69 a =["Geeks", "for", "Geeks"]
70
71 #Geeks for Geeks
72 #1, 2, 3, 4, 5
73
74 # print the list using join function()
75 print(' '.join(a))
76
77 # print the list by converting a list of
78 # integers to string
79 a = [1, 2, 3, 4, 5]
80
81 print str(a)[1:-1]
82
83 # =============================================================================
84 # 4. Using map : Use map() to convert each item in the list to a string if list is not a string, and then join them
85 # =============================================================================
86 # Python program to print list
87 # print the list by converting a list of
88 # integers to string using map
89 a = [1, 2, 3, 4, 5]
90 #1 2 3 4 5
91 #in new line
92 #1
93 #2
94 #3
95 #4
96 #5
97 print(' '.join(map(str, a)))
98
99 print"in new line"
100 print('\n'.join(map(str, a)))
View Code
How to create and initialise list with repeated N times ?
- x = [5]
- print(x * 5) // [5, 5, 5, 5, 5]
- print([x] * 5) // [[5], [5], [5], [5], [5]]
- Create List of Single Item Repeated n Times in Python - Stack Overflow
- https://stackoverflow.com/questions/3459098/create-list-of-single-item-repeated-n-times-in-python/3459131
- [e] * n
- [ [ 1 for x in range(n) ] for x in range(m) ]
How to remove duplicates in lists ?
- python - Removing duplicates in lists - Stack Overflow
- https://stackoverflow.com/questions/7961363/removing-duplicates-in-lists
- list(set(t))
- 5. Data Structures — Python 3.7.0 documentation
- https://docs.python.org/3/tutorial/datastructures.html#sets
- Python also includes a data type for sets. A set is an unordered collection with no duplicate elements. Basic uses include membership testing and eliminating duplicate entries. Set objects also support mathematical operations like union, intersection, difference, and symmetric difference.
How to print dictionary / list on multiple lines with pprint?
- pprint — Data pretty printer — Python 3.7.4 documentation
- python - pprint dictionary on multiple lines - Stack Overflow
- 如何美觀地打印 Python 對象?這個標準庫能夠簡單實現
What's namedtuple ?
- collections — Container datatypes — Python 3.8.0 documentation
- https://docs.python.org/3/library/collections.html#collections.namedtuple
collections.
namedtuple
(typename, field_names, *, rename=False, defaults=None, module=None)
- Returns a new tuple subclass named typename. The new subclass is used to create tuple-like objects that have fields accessible by attribute lookup as well as being indexable and iterable. Instances of the subclass also have a helpful docstring (with typename and field_names) and a helpful
__repr__()
method which lists the tuple contents in a name=value
format.
How to check type of object ?
- x = isinstance(5, int)
- Built-in Functions — Python 3.7.4 documentation
- Python isinstance() Function
How to check if file exists ?
- os.path — Common pathname manipulations — Python 3.7.2 documentation
- https://docs.python.org/3/library/os.path.html?highlight=isfile#os.path.isfile
- os.path.isfile(path)
- Return True if path is an existing regular file. This follows symbolic links, so both islink() and isfile() can be true for the same path.
- Python: Check if a File or Directory Exists
- https://stackabuse.com/python-check-if-a-file-or-directory-exists/
- Checking if a File Exists
- Checking if a Directory Exists
- Checking if Either Exist
How to iterate directory for files ?
- os — Miscellaneous operating system interfaces — Python 3.7.4 documentation
- Python3 OS 文件/目錄方法 | 菜鳥教程
- Python list directory, subdirectory, and files - Stack Overflow
1 import os
2
3 root = '.'
4
5 for path, subdirs, files in os.walk(root):
6 for name in files:
7 if name.endswith('.json'):
8 filename = os.path.join(path, name)
9 print(filename) # '.\path\test.json'
View Code
How to input and output file ?
- 7. Input and Output — Python 3.7.4 documentation
- Python3 File 方法 | 菜鳥教程
- python - How to read a file line-by-line into a list? - Stack Overflow
- Python dump dict to json file - Stack Overflow
- Python Dictionary to CSV - Stack Overflow
1 with open( filename, 'r' ) as f:
2 for line in f:
3 print(line, end='')
4
5 import json
6 with open( filename, 'w' ) as f:
7 json.dump(text, f)
8
9 with open(filename, 'r') as f:
10 x = json.load(f)
View Code
How to use try ... except ... finally statement for exception ?
- 8.4. The try statement - 8. Compound statements — Python 3.7.4 documentation
- Manually raising (throwing) an exception in Python - Stack Overflow
How to define custom exception ?
- How to Define Custom Exceptions in Python? (With Examples)
How to use Regular Expression 正則表達式 ?
- Python3 正則表達式 | 菜鳥教程
- re — Regular expression operations — Python 3.7.4 documentation
- Regular Expression HOWTO — Python 3.7.4 documentation
- regex - Get all unnamed groups in a Python match object - Stack Overflow
- regex - Extracting 2 strings from regular expression Python - Stack Overflow
- regex - Python extract pattern matches - Stack Overflow
- Python 正則表達式 re 模塊簡明筆記
- Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript
1 import re
2
3 filename = 'test.log'
4 pattern = re.compile('FinishedMessage from (?P<truck>.*):.*d=(?P<dump>.*)')
5 dumps = {}
6
7 with open( filename, 'r' ) as f:
8 for line in f:
9 print(line)
10
11 match = re.search(pattern, line)
12 print(match)
13
14 if match:
15 print(match.group(0))
16 print(match.group(1))
17 print(match.group(2))
18 print(match.groupdict())
19 print(match.groupdict()['truck'])
20 print(match.groupdict()['dump'])
21
22 dump = match.groupdict()['dump']
23
24 if dump not in dumps:
25 dumps[ dump ] = [ match.groupdict()['truck'] ]
26 else:
27 dumps[ dump ].append(match.groupdict()['truck'])
28 print()
29
30 import pprint
31 pprint.pprint(dumps, width=1)
32
33 # =============================================================================
34 # [1] FinishedMessage from t1: group=[1] d=D1
35 #
36 # <re.Match object; span=(4, 43), match='FinishedMessage from t1: group=[1] d=D1'>
37 # FinishedMessage from t1: group=[1] d=D1
38 # t1
39 # D1
40 # {'truck': 't1', 'dump': 'D1'}
41 # t1
42 # D1
43 #
44 # [2] FinishedMessage from t2: group=[2] d=D2
45 #
46 # <re.Match object; span=(4, 43), match='FinishedMessage from t2: group=[2] d=D2'>
47 # FinishedMessage from t2: group=[2] d=D2
48 # t2
49 # D2
50 # {'truck': 't2', 'dump': 'D2'}
51 # t2
52 # D2
53 #
54 # [3] FinishedMessage from t3: group=[3] d=D2
55 # <re.Match object; span=(4, 43), match='FinishedMessage from t3: group=[3] d=D2'>
56 # FinishedMessage from t3: group=[3] d=D2
57 # t3
58 # D2
59 # {'truck': 't3', 'dump': 'D2'}
60 # t3
61 # D2
62 #
63 # {'D1': ['t1'],
64 # 'D2': ['t2',
65 # 't3']}
66 # =============================================================================
View Code
How to use logging ?
- Logging HOWTO — Python 3.7.0 documentation
- https://docs.python.org/3.7/howto/logging.html#basic-logging-tutorial
- 16.6. logging — Logging facility for Python — Python 3.7.0 documentation
- https://docs.python.org/3.7/library/logging.html#logging.debug
- https://docs.python.org/3.7/library/logging.html#logging.info
- https://docs.python.org/3.7/library/logging.html#logging.basicConfig
- https://docs.python.org/3.7/library/logging.html?highlight=shutdown#logging.shutdown
- logging.basicConfig( filename=output.replace('html', 'log'), filemode='w',
format='[%(asctime)s] \n%(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
level=logging.DEBUG )
- Logging Cookbook — Python 3.7.0 documentation
- https://docs.python.org/3/howto/logging-cookbook.html
- Good logging practice in Python – Fang's coding note
- https://fangpenlin.com/posts/2012/08/26/good-logging-practice-in-python/
How to measure execution time of code ?
- timeit — Measure execution time of small code snippets — Python 3.8.0 documentation
How to import module from parent directory ?
- import sys
- sys.path.append('..')
- from A import B
- python - Importing modules from parent folder - Stack Overflow
How to parse arguments for command-line options ?
- 15.4. argparse — Parser for command-line options, arguments and sub-commands — Python 2.7.16 documentation
- python - What's the best way to parse command line arguments? - Stack Overflow
- python - Why use argparse rather than optparse? - Stack Overflow
1 import argparse
2
3 if __name__ == "__main__":
4 try:
5 parser = argparse.ArgumentParser(prog='test',
6 description='Test class A')
7
8 parser.add_argument('-c',
9 '--config',
10 metavar='file',
11 help='Path to file',
12 required=True)
13
14 args = parser.parse_args()
15
16 print("args.config = {0}\n".format(args.config))
17
18 except (AttributeError, TypeError, RuntimeError) as err:
19 logger.logError(err.message)
20
21 except Exception as err:
22 logger.logException(err.message)
View Code
How to fix AttributeError: MyBokeh instance has no attribute 'plot_all' ?
- Check the indentation for other class member functions prior to plot_all()
How to fix TypeError: slice indices must be integers or None or have an __index__ method ?
- b = ['a', 'aa', 'aaa', 'b', 'c']
- d = [c for c in b if c.startswith( 'a', 'b' )]
- It's due to lack of parenthese. Change to
- d = [c for c in b if c.startswith( ('a', 'b') )]
How to fix sqlite3.OperationalError: database is locked ?
- SQLite is lightweight database and need to use, e.g. PostgrsSQL, for large number of connections. If the cache db file is in locked even if with one job, use the below cmds to recover it.
- sqlite3 — DB-API 2.0 interface for SQLite databases — Python 3.7.4 documentation
- https://docs.python.org/3.7/library/sqlite3.html
- SQLite is a C library that provides a lightweight disk-based database that doesn’t require a separate server process and allows accessing the database using a nonstandard variant of the SQL query language. Some applications can use SQLite for internal data storage. It’s also possible to prototype an application using SQLite and then port the code to a larger database such as PostgreSQL or Oracle.
- SQLite Frequently Asked Questions
- Python SQLite: database is locked - Stack Overflow