2 Commits

Author SHA1 Message Date
c2b09ece24 magic-python add one-line code 2021-07-29 21:56:50 +08:00
47df4cda71 magic-python 2021-07-29 21:23:41 +08:00

View File

@@ -12,12 +12,307 @@
<!-- Theme used for syntax highlighted code -->
<link rel="stylesheet" href="plugin/highlight/monokai.css">
<style>
big {
font-size: 139%;
}
</style>
</head>
<body>
<div class="reveal">
<div class="slides">
<section>Slide 1</section>
<section>Slide 2</section>
<section>
<section>
<h2>代码如诗</h2>
<small>Python 代码风格规范 (PEP8)</small>
</section>
</section>
<section>
<section data-markdown>
### 一般注释
```python
some_code()
more_code_here()
# One sentence explaining
if not data:
do_something_here()
other_code()
```
</section>
<section data-markdown>
### 行内注释
```python
words = 'Hello, World!' # Some comment here
something = 'Hahaha' # 注释不需要对齐
```
</section>
<section data-markdown>
#### 块注释
```python
# 通常在需要 review 的晦涩难懂的代码前
# 留下 TODO、待 review 的说明
# 不要在这里流水帐式描述你的代码
# 如: 「此处判定列表各项均为有效值,然后我们...」
# 要假定 reviewer 比你更懂 Python
if 0 in [!int(node) for node in li]: # 恰到好处的注释
```
> 如果代码不够清晰以至于需要一个注释,那么或许它应该被重写。
</section>
<section data-markdown>
#### 函数注释
```python
def print(arg):
"""将 arg 打印在屏幕上
这个函数会调用内置方法讲 arg 输出到屏幕上,
注意这个函数不是线程安全的。
参数:
arg: 字符串或可以调用 arg.__str__() 的对象
返回值:
返回一个整数,代表输出的字符串长度
抛出异常:
NameError: 参数没有 __str__() 方法供转化成字符串。
"""
do_some_thing()
```
</section>
<section data-markdown>
#### 类注释
```python
class SampleClass:
"""简单描述这个类
详细说明这个类...
可以有很多行说明...
类成员说名:
Blablabla
"""
def __init__(self, *args):
"""类方法说明"""
```
</section>
</section>
<section>
<section><h1><code>String</code></h1></section>
<section data-markdown=>
需要拼接有空格的字符串,运用占位符
```python
x = '%s, %s!' % (a, b)
x = '{}, {}!'.format(a, b)
x = ', '.join([a, b])
```
直接拼接的字符串
```python
x = a + b
```
</section>
<section data-markdown>
> 注意:使用加法循环拼接字符串,可能导致二次而非线性的运行时间。
BAD
```python
li = ['a', 'very', 'long', 'list']
result = ''
for i in li:
result = result + i
print(result)
```
GOOD
```python
result = ''.join(li)
print(result)
```
</section>
<section data-markdown>
当字符串过长时,使用 `\` 漂亮地分割字符串
```python
s = '这是一个' \
'很长的字符串'
```
</section>
</section>
<section>
<section>其他乱七八糟但很重要的东西</section>
<section>
<p>类名以<big></big>写字母开头 <code>ClassName</code></p>
<p>驼峰<big></big>名法 <code>someFunctionName</code></p>
<p>下划线_命名法 <code>some_variable_name</code></p>
</section>
<section data-markdown>
### 遍历同时操作数组
> 不规范的行为
```python
li = list(range(10))
for i in li:
li.append(i)
```
</section>
<section data-markdown>
### 传参传字典或列表
```python
def foo(i, arg=[]): # 正确做法 arg=None
arg.append(i)
print(arg)
foo('a')
# Output: ['a']
foo('b')
# Output: ['a', 'b']
```
</section>
<section data-markdown>
### 文件中读入的字符莫名消失
复现代码
```python
raw = '第一行\n\r第二行'
for i in raw.split('\n'):
print('---start', i, 'end---')
# Output:
# ---start 第一行 end---
# 第二行 end---
```
</section>
<section data-markdown>
GOOD
```python
if age < 18:
return 'You are too small'
do_something_here()
```
BAD
```python
if age > 18:
do_something_here()
else:
return 'You are too small'
```
</section>
<section data-markdown>
#### 善用 if
GOOD
```python
if data:
break
```
BAD
```python
if data != []:
break
```
</section>
</section>
<section>
<section data-markdown>
## 忍者代码
</section>
<section data-markdown>
#### 没人知道这个变量是什么
- a, b, j, k, l
- str, string, number, list, dict
- data
- data1
- data114514
以及见鬼的缩写
- `ObjStuAre_id`: object_student_area_id
</section>
<section data-markdown>
> 一个变量一杯茶一个bug修一天
- `data`
- `date`
- `l`
- `I`
</section>
<section data-markdown>
#### 没人知道这两是不是同一个东西
- `showMessage`
- `displayMessage`
- `printMessage`
</section>
<section data-markdown>
#### 夸张的变量名
- `very_super_powerful_print_function`
- `lovely_variable`
- `nice_list`
</section>
<section data-markdown>
#### 重叠外部命名空间变量
```python
id = 39
def foo():
id += 1
# 一堆代码
print(id)
```
</section>
<section data-markdown>
#### Powerful Function !
例如某个 `check_empty_in_list()`,按字面意思会检查列表中是否有空项,可这个函数在检查到空项的同时还会「智能地」帮你删掉空项。然而使用函数的人对此一无所知,函数作者也忘了有这么回事。
</section>
<section>
<p>一行!展现你的聪明才智!</p>
<code>
result = not os.system(' '.join(['proxychains', '-q', 'pandoc', 'README.md', 'SUMMARY.md', ' '.join([str(i)+'.md' for i in range(3, 109+1)]), '-o', '/tmp/out.epub']) or print("Failed")
</code>
</section>
</section>
<section>
<section style="text-align: left;" data-markdown>
#### References
- [现代 JavaScript 教程 - 忍者代码](https://zh.javascript.infoi/ninja-code)
- [Google Python Style Guide](https://google.github.io/styleguide/pyguide.html)
- [水族馆](https://aquarium39.moe)
- [你所不知道的Python冷知識](https://codertw.com/%E7%A8%8B%E5%BC%8F%E8%AA%9E%E8%A8%80/649905/)
</section>
</section>
</div>
</div>