Ethan's Blog


  • Home

  • Archives

  • Tags

  • Search

场景:消除条件分支

Posted on 2022-03-06

使用 bisect 优化范围类分支判断

1
2
3
4
5
6
7
8
9
10
11
12
def rank(rating):
rating_num = float(rating)
if rating_num >= 8.5:
return 'S'
elif rating_num >= 8:
return 'A'
elif rating_num >= 7:
return 'B'
elif rating_num >= 6:
return 'C'
else:
return 'D'

要优化这段代码,我们得把所有分界点收集起来,放在一个元组里。因为 breakpoints 已经是一个排好序的元组,所以我们可以直接使用 bisect 模块来实现查找功能:

1
2
3
4
5
6
7
8
import bisect

def rank(rating):
breakpoints = (6, 7, 8, 8.5)
grades = ('D', 'C', 'B', 'A', 'S')

index = bisect.bisect(breakpoints, float(rating))
return grades[index]
Read more »

语法结构

Posted on 2022-03-05

条件分支控制流

掌握如何写出好的条件分支代码非常重要,它可以帮助我们用更简洁、更清晰的代码来表达复杂逻辑。

分支惯用写法

当某个对象作为主角出现在 if 分支里时,解释器会主动对它进行真值测试,也就是调用 bool() 函数获取它的布尔值:

1
2
>>> bool([]), bool([1, 2, 3])
(False, True)

不要因为过度追求简写而引入其他逻辑问题:

1
2
3
4
5
6
7
# 更精准:只有为 0 的时候,才会满足分支条件
if containers_count == 0:
pass

# 更宽泛:为 0、None、空字符串等时,都可以满足分支条件
if not containers_count:
pass
Read more »

蟒蛇军火库

Posted on 2022-03-05

this

Python 之禅:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
>>> 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!
Read more »

变量与基础类型

Posted on 2022-03-04

变量与注释

当我们看到一段代码时,最先注意到的,不是代码有几层循环,用了什么模式,而是变量与注释,因为它们是代码里最接近自然语言的东西,最容易被大脑消化、理解。

变量常见用法

Python 支持灵活的动态解包语法,只要用星号表达式(*variables)作为变量名,它便会贪婪地捕获多个值对象,并将捕获到的内容作为列表赋值给 variables:

1
2
3
4
>>> data = ['ethan', 'apple', 'orange', 'banana', 100]
>>> username, *fruits, score = data
>>> fruits
['apple', 'orange', 'banana', 100]

在 Python 交互式命令行里,_ 变量还有一层特殊含义——默认保存我们输入的上个表达式的返回值:

1
2
3
4
>>> 'foo'.upper()
'FOO'
>>> print(_)
FOO
Read more »

重新认识 Docker 容器

Posted on 2021-05-28

剖析 Linux 容器的核心实现原理:

  1. 基于 Namespace 的视图隔离
  2. 基于 Cgroups 的资源限制
  3. 基于 rootfs 的文件系统

Docker 容器的核心功能

在开始实践之前,你需要准备一台 Linux 机器,并安装 Docker。这一次,我要用 Docker 部署一个用 Python 编写的 Web 应用。这个应用的代码部分(app.py)非常简单:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
from flask import Flask
import socket
import os

app = Flask(__name__)

@app.route('/')
def hello():
html = "<h3>Hello {name}!</h3>" \
"<b>Hostname:</b> {hostname}<br/>"
return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname())

if __name__ == "__main__":
app.run(host='0.0.0.0', port=80)
Read more »
1…121314…55
necusjz

necusjz

271 posts
16 tags
© 2016 - 2026 necusjz
Powered by Hexo
Theme - NexT.Mist