Skip to content

Commit bcbf529

Browse files
committed
feat: update python ver
1 parent b6cffc7 commit bcbf529

19 files changed

+271
-179
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ package-lock.json
66
node_modules/
77
dist/
88
/readme_sync/
9+
/readme_snatcher/python-cheatsheet.md

README-zh-cn.md

Lines changed: 41 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
# Python标准库速查表
1+
# Python Standard Libraries Cheatsheet
22

3-
依赖Python v3.9.8
3+
Depend on Python v3.10.11
44

5-
所有代码片段都经过测试确保可以正常运行
5+
All code snippets have been tested to ensure they work properly.
66

7-
[GitHub](https://github.com/pynickle/python-cheatsheet)上fork这个仓库吧
7+
Fork me on [GitHub](https://github.com/pynickle/python-cheatsheet-redefined).
88

99
- [中文](README-zh-cn.md)
1010
- [English](README.md)
1111

12-
**注意**:
13-
- **这里的每个代码片段都可以独立运行 (一些需要这个仓库提供的文件)**
14-
- **你可以通过web文件夹中的flask项目得到可以切换代码主题和语言的网页版本**
15-
- **你可以使用GETREADME.py来从仓库中下载README.md(中英文,命令行前缀的有无皆可选择)**
12+
**Notes**:
1613

17-
## 目录
14+
- **Every code snippet here can run independently (some need the files provided by this repo)**
15+
- **You can use readme_snatcher.py to download README.md from the repository (Chinese or English, with or not with command line prefixes is up to you!)**
16+
17+
## Contents
1818

1919
**文本处理**: [``string``](#string), [``re``](#re), [``difflib``](#difflib),
2020
[``textwrap``](#textwrap), [``unicodedata``](#unicodedata), [``readline``](#readline)
@@ -126,6 +126,7 @@
126126
'Hello nick'
127127
>>> t.substitute(World = "nick") # use args
128128
'Hello nick'
129+
>>>
129130
>>> class MyTemplate(string.Template):
130131
... delimiter = "^"
131132
...
@@ -144,11 +145,17 @@
144145
>>> strcmp = "www.baidu.com"
145146
>>> re.match("www", strcmp).span() # span function to get index
146147
(0, 3)
147-
>>> re.match("baidu", strcmp) # re.match only match from the beginning of the string
148-
>>> re.search("baidu", strcmp).span() # re.search search from all string and return the first
148+
>>>
149+
>>> # re.match only match from the beginning of the string
150+
>>> re.match("baidu", strcmp)
151+
>>>
152+
>>> # re.search search from all string and return the first
153+
>>> re.search("baidu", strcmp).span()
149154
(4, 9)
150155
>>> strcmp = "baidu.com/runoob.com"
151-
>>> re.findall("com", strcmp) # re.findall find all results and return
156+
>>>
157+
>>> # re.findall find all results and return
158+
>>> re.findall("com", strcmp)
152159
['com', 'com']
153160
>>> re.findall("b(.*?).", strcmp)
154161
['', '']
@@ -162,7 +169,9 @@
162169
>>> import re
163170
>>> re.split(r"\W", "hello,world") # use regular expression
164171
['hello', 'world']
165-
>>> re.sub(r"Boy|Girl", "Human", "boy and girl", flags = re.I) # re.I means ignoring apitalization
172+
>>>
173+
>>> # re.I means ignoring capitalization
174+
>>> re.sub(r"Boy|Girl", "Human", "boy and girl", flags = re.I)
166175
'Human and Human'
167176
>>> re.escape(r"#$&*+-.^|~")
168177
'\\#\\$\\&\\*\\+\\-\\.\\^\\|\\~'
@@ -172,26 +181,6 @@
172181

173182
#### Differ
174183

175-
```python
176-
>>> import difflib
177-
>>> d = difflib.Differ()
178-
>>> text1 = """difflib
179-
... python version 3.7.4
180-
... difflib version 3.7.4
181-
... this is difflib document
182-
... """
183-
>>> text2 = """difflib
184-
... python version 3.7.3
185-
... this is difflib document
186-
... feature: diff in linux
187-
... """
188-
>>> text1_lines = text1.splitlines()
189-
>>> text2_lines = text2.splitlines()
190-
>>>
191-
>>> list(d.compare(text1_lines, text2_lines))
192-
[' difflib', '- python version 3.7.4', '? ^\n', '+ python version 3.7.3', '? ^\n', '- difflib version 3.7.4', ' this is difflib document', '+ feature: diff in linux']
193-
```
194-
195184
#### HtmlDiff
196185

197186
```python
@@ -477,17 +466,18 @@ ValueError: duplicate values found in <enum 'Unique'>: Jack -> Nick
477466
0
478467
>>> bisect.bisect_right(a, 1) # if it has the same, choose right
479468
1
480-
>>> bisect.bisect(a, 1)
469+
>>> bisect.bisect(a, 1) # same to bisect_right
481470
1
482-
>>> bisect.insort(a, 1) # bisect and insert
471+
>>> bisect.insort(a, 1) # bisect and insert (same to insort_right)
483472
>>> a
484473
[1, 1, 2, 4, 5]
485474
>>> bisect.insort_left(a, 2)
486475
>>> a
487476
[1, 1, 2, 2, 4, 5]
488-
>>> bisect.insort_right(a, 4)
489-
>>> a
490-
[1, 1, 2, 2, 4, 4, 5]
477+
>>>
478+
>>> data = [('red', 5), ('blue', 1), ('yellow', 8), ('black', 0)]
479+
>>> bisect.bisect_left(data, 3, key = lambda x: x[1]) # use key to choose the index
480+
2
491481
```
492482

493483
## heapq
@@ -799,7 +789,7 @@ b'a'
799789

800790
```python
801791
>>> import filecmp
802-
>>> filecmp.cmp("examples/cmp1.txt", "examples/cmp2.txt")
792+
>>> filecmp.cmp("test_env/cmp1.txt", "test_env/cmp2.txt")
803793
True
804794
```
805795

@@ -820,8 +810,6 @@ examples/cmp1.txt | Line Number: 3 |: 3
820810
examples/cmp1.txt | Line Number: 4 |: 4
821811

822812
examples/cmp1.txt | Line Number: 5 |: 5
823-
824-
825813
```
826814

827815
## shutil
@@ -830,10 +818,10 @@ examples/cmp1.txt | Line Number: 5 |: 5
830818

831819
```python
832820
>>> import shutil
833-
>>> shutil.copyfile("examples/song.wav", "examples/copysong.wav")
821+
>>> shutil.copyfile("test_env/song.wav", "test_env/copysong.wav")
834822
'examples/copysong.wav'
835823
>>> shutil.rmtree("shutil_tree") # can delete tree has contents, os.remove can't
836-
>>> shutil.move("examples/copysong.wav", "myapp/copysong.wav")
824+
>>> shutil.move("test_env/copysong.wav", "myapp/copysong.wav")
837825
'myapp/copysong.wav'
838826
```
839827

@@ -843,7 +831,7 @@ examples/cmp1.txt | Line Number: 5 |: 5
843831

844832
```python
845833
>>> import linecache
846-
>>> linecache.getline("examples/readme_snatcher.py", 1) # start from one, not zero
834+
>>> linecache.getline("test_env/readme_snatcher.py", 1) # start from one, not zero
847835
'from sys import exit\n'
848836
```
849837

@@ -917,7 +905,7 @@ b'Hello, python3!'
917905

918906
```python
919907
>>> import zipfile
920-
>>> with zipfile.ZipFile("examples/g.zip") as f: # extract zip file
908+
>>> with zipfile.ZipFile("test_env/g.zip") as f: # extract zip file
921909
... f.extractall()
922910
...
923911
>>> with zipfile.ZipFile("a.zip", "a") as zip: # create zip file
@@ -1086,12 +1074,12 @@ Password:
10861074
... res = os.popen(command)
10871075
... print(res.read())
10881076
...
1089-
>>> cmd("python examples/argparse_example.py -a 1 -b 2 --sum 1 2 3 4 -r 10 -t")
1077+
>>> cmd("python argparse_example.py -a 1 -b 2 --sum 1 2 3 4 -r 10 -t")
10901078
Namespace(a=1, b=2, sum=[1, 2, 3, 4], required='10', true=True)
10911079
3
10921080
10
10931081

1094-
>>> cmd("python examples/argparse_example.py --help")
1082+
>>> cmd("python argparse_example.py --help")
10951083
usage: argparse_example.py [-h] [-a A] [-b B] [-s SUM [SUM ...]] -r REQUIRED
10961084
[-t]
10971085

@@ -1104,7 +1092,6 @@ optional arguments:
11041092
-s SUM [SUM ...], --sum SUM [SUM ...]
11051093
-r REQUIRED, --required REQUIRED
11061094
-t, --true
1107-
11081095
```
11091096

11101097
## errno
@@ -1177,8 +1164,8 @@ Received b'Hello, world'
11771164
```python
11781165
>>> import html
11791166
>>> html.escape("As we all know, 2>1")
1180-
'As we all know, 2&gt;1'
1181-
>>> html.unescape('As we all know, 2&gt;1')
1167+
'As we all know, 2>1'
1168+
>>> html.unescape('As we all know, 2>1')
11821169
'As we all know, 2>1'
11831170
```
11841171

@@ -1202,7 +1189,7 @@ True
12021189

12031190
```python
12041191
>>> import wave
1205-
>>> f = wave.open("examples/song.wav", "rb")
1192+
>>> f = wave.open("test_env/song.wav", "rb")
12061193
>>> f.getparams()
12071194
_wave_params(nchannels=2, sampwidth=2, framerate=44100, nframes=442368, comptype='NONE', compname='not compressed')
12081195
```
@@ -1401,7 +1388,7 @@ python -m ensurepip --upgrade # upgrade pip
14011388

14021389
```python
14031390
>>> import zipapp
1404-
>>> zipapp.create_archive("myapp", "examples/myapp.pyz")
1391+
>>> zipapp.create_archive("myapp", "test_env/myapp.pyz")
14051392
>>> __import__("os").popen("python examples/myapp.pyz").read() # pyz file can also be executed
14061393
'Hello, everyone!\n'
14071394
```
@@ -1611,7 +1598,7 @@ True
16111598

16121599
```python
16131600
>>> import zipimport
1614-
>>> zip = zipimport.zipimporter("examples/g.zip")
1601+
>>> zip = zipimport.zipimporter("test_env/g.zip")
16151602
>>> zip.archive
16161603
'examples\\g.zip'
16171604
>>>
@@ -1761,7 +1748,7 @@ Names:
17611748
```python
17621749
>>> import tabnanny
17631750
>>> tabnanny.verbose = True
1764-
>>> tabnanny.check("examples/tabnanny_example.py")
1751+
>>> tabnanny.check("test_env/tabnanny_example.py")
17651752
'examples/tabnanny_example.py': *** Line 3: trouble in tab city! ***
17661753
offending line: '\tprint(i)'
17671754
indent not greater e.g. at tab sizes 1, 2, 3, 4

0 commit comments

Comments
 (0)