Best Practice 30 of Python, tips and tricks

作者| Erik-Jan van Baaren

Translator | meniscus, Zebian | Tu Min

Exhibition | CSDN (ID: CSDNnews)

The following is the translation:

New Year's Day had finished, we all have returned to their jobs. New year, new weather, I would like to take this article as we offer the Python language 30 best practices, tips and techniques, hoping to help our hard-working programmers, and hope you all success! 

1. Python version

Here I would like to remind you: Since 2020, January 1, Python official is no longer supported Python 2. Many of the examples in this article can only be run in Python 3. If you are still using Python 2.7, please upgrade immediately.

2. Check the minimum version of Python

You can check the version of Python code, to ensure that your users do not have to run a script that is not compatible versions. Check the following manner:

if not sys.version_info > (2, 7):
   # berate your user for running a 10 year
   # python version
elif not sys.version_info >= (3, 5):
   # Kindly tell your user (s)he needs to upgrade
   # because you're using 3.5 features

3. IPython

On IPython is essentially an enhanced version of the shell. Directed at automatically append it is worth a try, and it features more than that, it also has a lot of command so I put it down, for example:

  • % Cd: change the current working directory

  • % Edit: Type the code editor open and close the editor execution

  • % Env: Display the current environment variables

  • % Pip install [pkgs]: without leaving the interactive shell, you can install the package

  • % Time and% timeit: measuring execution time of the Python code

A complete list of commands, please click here (https://ipython.readthedocs.io/en/stable/interactive/magics.html).

There is also a very useful feature: a reference output of a command. In and Out are the actual object. You can [3] in the form of a command using the output of the third through Out.

IPython installation command as follows:

pip3 install ipython

4. List comprehension

You can use list comprehensions, avoid cumbersome when using loop to populate the list. The basic syntax of list comprehensions as follows:

[ expression for item in list if conditional ]

For a basic example: an ordered set of numbers is filled with a list of:

mylist = [i for i in range(10)]
print(mylist)
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

由于可以使用表达式,所以你也可以做一些算术运算:

squares = [x**2 for x in range(10)]
print(squares)
# [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

甚至可以调用外部函数:

def some_function(a):
    return (a + 5) / 2

my_formula = [some_function(i) for i in range(10)]
print(my_formula)
# [2, 3, 3, 4, 4, 5, 5, 6, 6, 7]

最后,你还可以使用 ‘if’ 来过滤列表。在如下示例中,我们只保留能被2整除的数字:

filtered = [i for i in range(20) if i%2==0]
print(filtered)
# [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

5. 检查对象使用内存的状况

你可以利用 sys.getsizeof() 来检查对象使用内存的状况:

import sys

mylist = range(0, 10000)
print(sys.getsizeof(mylist))
# 48

等等,为什么这个巨大的列表仅包含48个字节?

因为这里的 range 函数返回了一个类,只不过它的行为就像一个列表。在使用内存方面,range 远比实际的数字列表更加高效。

你可以试试看使用列表推导式创建一个范围相同的数字列表: 

import sys

myreallist = [x for x in range(0, 10000)]
print(sys.getsizeof(myreallist))
# 87632

6. 返回多个值

Python 中的函数可以返回一个以上的变量,而且还无需使用字典、列表或类。如下所示:

def get_user(id):
    # fetch user from database
    # ....
    return name, birthdate

name, birthdate = get_user(4)

如果返回值的数量有限当然没问题。但是,如果返回值的数量超过3个,那么你就应该将返回值放入一个(数据)类中。

7. 使用数据类

Python从版本3.7开始提供数据类。与常规类或其他方法(比如返回多个值或字典)相比,数据类有几个明显的优势:

  • 数据类的代码量较少

  • 你可以比较数据类,因为数据类提供了 __eq__ 方法

  • 调试的时候,你可以轻松地输出数据类,因为数据类还提供了 __repr__ 方法

  • 数据类需要类型提示,因此可以减少Bug的发生几率 

数据类的示例如下:

from dataclasses import dataclass

@dataclass
class Card:
    rank: str
    suit: str

card = Card("Q", "hearts")

print(card == card)
# True

print(card.rank)
# 'Q'

print(card)
Card(rank='Q', suit='hearts')

详细的使用指南请点击这里(https://realpython.com/python-data-classes/)。

8. 交换变量

如下的小技巧很巧妙,可以为你节省多行代码:

a = 1
b = 2
a, b = b, a
print (a)
# 2
print (b)
# 1

9. 合并字典(Python 3.5以上的版本)

从Python 3.5开始,合并字典的操作更加简单了:

dict1 = { 'a': 1, 'b': 2 }
dict2 = { 'b': 3, 'c': 4 }
merged = { **dict1, **dict2 }
print (merged)
# {'a': 1, 'b': 3, 'c': 4}

如果 key 重复,那么第一个字典中的 key 会被覆盖。

10. 字符串的首字母大写

如下技巧真是一个小可爱:

mystring = "10 awesome python tricks"
print(mystring.title())
'10 Awesome Python Tricks'

11. 将字符串分割成列表

你可以将字符串分割成一个字符串列表。在如下示例中,我们利用空格分割各个单词:

mystring = "The quick brown fox"
mylist = mystring.split(' ')
print(mylist)
# ['The', 'quick', 'brown', 'fox']

12. 根据字符串列表创建字符串

与上述技巧相反,我们可以根据字符串列表创建字符串,然后在各个单词之间加入空格:

mylist = ['The', 'quick', 'brown', 'fox']
mystring = " ".join(mylist)
print(mystring)
# 'The quick brown fox'

你可能会问为什么不是 mylist.join(" "),这是个好问题!

根本原因在于,函数 String.join() 不仅可以联接列表,而且还可以联接任何可迭代对象。将其放在String中是为了避免在多个地方重复实现同一个功能。

13. 表情符

有些人非常喜欢表情符,而有些人则深恶痛绝。我在此郑重声明:在分析社交媒体数据时,表情符可以派上大用场。

首先,我们来安装表情符模块:

pip3 install emoji

安装完成后,你可以按照如下方式使用:

import emoji
result = emoji.emojize('Python is :thumbs_up:')
print(result)
# 'Python is ????'

# You can also reverse this:
result = emoji.demojize('Python is ????')
print(result)
# 'Python is :thumbs_up:'

更多有关表情符的示例和文档,请点击此处(https://pypi.org/project/emoji/)。

14. 列表切片

列表切片的基本语法如下:

a[start:stop:step]

start、stop 和 step 都是可选项。如果不指定,则会使用如下默认值:

  • start:0

  • end:字符串的结尾

  • step:1

示例如下:

# We can easily create a new list from 
# the first two elements of a list:
first_two = [1, 2, 3, 4, 5][0:2]
print(first_two)
# [1, 2]

# And if we use a step value of 2, 
# we can skip over every second number
# like this:
steps = [1, 2, 3, 4, 5][0:5:2]
print(steps)
# [1, 3, 5]

# This works on strings too. In Python,
# you can treat a string like a list of
# letters:
mystring = "abcdefdn nimt"[::2]
print(mystring)
# 'aced it'

15. 反转字符串和列表

你可以利用如上切片的方法来反转字符串或列表。只需指定 step 为 -1,就可以反转其中的元素:

revstring = "abcdefg"[::-1]
print(revstring)
# 'gfedcba'

revarray = [1, 2, 3, 4, 5][::-1]
print(revarray)
# [5, 4, 3, 2, 1]

16. 显示猫猫

我终于找到了一个充分的借口可以在我的文章中显示猫猫了,哈哈!当然,你也可以利用它来显示图片。首先你需要安装 Pillow,这是一个 Python 图片库的分支:

pip3 install Pillow

接下来,你可以将如下图片下载到一个名叫 kittens.jpg 的文件中:

然后,你就可以通过如下 Python 代码显示上面的图片:

from PIL import Image

im = Image.open("kittens.jpg")
im.show()
print(im.format, im.size, im.mode)
# JPEG (1920, 1357) RGB

Pillow 还有很多显示该图片之外的功能。它可以分析、调整大小、过滤、增强、变形等等。完整的文档,请点击这里(https://pillow.readthedocs.io/en/stable/)。

17. map()

Python 有一个自带的函数叫做 map(),语法如下:

map(function, something_iterable)

所以,你需要指定一个函数来执行,或者一些东西来执行。任何可迭代对象都可以。在如下示例中,我指定了一个列表:

def upper(s):
    return s.upper()

mylist = list(map(upper, ['sentence', 'fragment']))
print(mylist)
# ['SENTENCE', 'FRAGMENT']

# Convert a string representation of
# a number into a list of ints.
list_of_ints = list(map(int, "1234567")))
print(list_of_ints)
# [1, 2, 3, 4, 5, 6, 7]

你可以仔细看看自己的代码,看看能不能用 map() 替代某处的循环。

18. 获取列表或字符串中的唯一元素

如果你利用函数 set() 创建一个集合,就可以获取某个列表或类似于列表的对象的唯一元素:

mylist = [1, 1, 2, 3, 4, 5, 5, 5, 6, 6]
print (set(mylist))
# {1, 2, 3, 4, 5, 6}

# And since a string can be treated like a 
# list of letters, you can also get the 
# unique letters from a string this way:
print (set("aaabbbcccdddeeefff"))
# {'a', 'b', 'c', 'd', 'e', 'f'}

19. 查找出现频率最高的值

你可以通过如下方法查找出现频率最高的值:

test = [1, 2, 3, 4, 2, 2, 3, 1, 4, 4, 4]
print(max(set(test), key = test.count))
# 4

你能看懂上述代码吗?想法搞明白上述代码再往下读。

没看懂?我来告诉你吧:

  • max() 会返回列表的最大值。参数 key 会接受一个参数函数来自定义排序,在本例中为 test.count。该函数会应用于迭代对象的每一项。

  • test.count 是 list 的内置函数。它接受一个参数,而且还会计算该参数的出现次数。因此,test.count(1) 将返回2,而 test.count(4) 将返回4。

  • set(test) 将返回 test 中所有的唯一值,也就是 {1, 2, 3, 4}。

因此,这一行代码完成的操作是:首先获取 test 所有的唯一值,即{1, 2, 3, 4};然后,max 会针对每一个值执行 list.count,并返回最大值。

这一行代码可不是我个人的发明。

20. 创建一个进度条

你可以创建自己的进度条,听起来很有意思。但是,更简单的方法是使用 progress 包:

pip3 install progress

接下来,你就可以轻松地创建进度条了:

from progress.bar import Bar

bar = Bar('Processing', max=20)
for i in range(20):
    # Do some work
    bar.next()
bar.finish()

21. 在交互式shell中使用_(下划线运算符)

你可以通过下划线运算符获取上一个表达式的结果,例如在 IPython 中,你可以这样操作:

In [1]: 3 * 3
Out[1]: 9In [2]: _ + 3
Out[2]: 12

Python Shell 中也可以这样使用。另外,在 IPython shell 中,你还可以通过 Out[n] 获取表达式 In[n] 的值。例如,在如上示例中,Out[1] 将返回数字9。

22. 快速创建Web服务器

你可以快速启动一个Web服务,并提供当前目录的内容:

python3 -m http.server

当你想与同事共享某个文件,或测试某个简单的HTML网站时,就可以考虑这个方法。

23. 多行字符串

虽然你可以用三重引号将代码中的多行字符串括起来,但是这种做法并不理想。所有放在三重引号之间的内容都会成为字符串,包括代码的格式,如下所示。

我更喜欢另一种方法,这种方法不仅可以将多行字符串连接在一起,而且还可以保证代码的整洁。唯一的缺点是你需要明确指定换行符。

s1 = """Multi line strings can be put
        between triple quotes. It's not ideal
        when formatting your code though"""

print (s1)
# Multi line strings can be put
#         between triple quotes. It's not ideal
#         when formatting your code though

s2 = ("You can also concatenate multiple\n" +
        "strings this way, but you'll have to\n"
        "explicitly put in the newlines")

print(s2)
# You can also concatenate multiple
# strings this way, but you'll have to
# explicitly put in the newlines

24. 条件赋值中的三元运算符

这种方法可以让代码更简洁,同时又可以保证代码的可读性:

[on_true] if [expression] else [on_false]

示例如下:

x = "Success!" if (y == 2) else "Failed!"

25. 统计元素的出现次数

你可以使用集合库中的 Counter 来获取列表中所有唯一元素的出现次数,Counter 会返回一个字典:

from collections import Counter

mylist = [1, 1, 2, 3, 4, 5, 5, 5, 6, 6]
c = Counter(mylist)
print(c)
# Counter({1: 2, 2: 1, 3: 1, 4: 1, 5: 3, 6: 2})

# And it works on strings too:
print(Counter("aaaaabbbbbccccc"))
# Counter({'a': 5, 'b': 5, 'c': 5})

26. 比较运算符的链接

你可以在 Python 中将多个比较运算符链接到一起,如此就可以创建更易读、更简洁的代码:

x = 10

# Instead of:
if x > 5 and x < 15:
    print("Yes")
# yes

# You can also write:
if 5 < x < 15:
    print("Yes")
# Yes

27. 添加颜色

你可以通过 Colorama,设置终端的显示颜色:

from colorama import Fore, Back, Style

print(Fore.RED + 'some red text')
print(Back.GREEN + 'and with a green background')
print(Style.DIM + 'and in dim text')
print(Style.RESET_ALL)
print('back to normal now')

28. 日期的处理

python-dateutil 模块作为标准日期模块的补充,提供了非常强大的扩展,你可以通过如下命令安装: 

pip3 install python-dateutil 

你可以利用该库完成很多神奇的操作。在此我只举一个例子:模糊分析日志文件中的日期:

from dateutil.parser import parse

logline = 'INFO 2020-01-01T00:00:01 Happy new year, human.'
timestamp = parse(log_line, fuzzy=True)
print(timestamp)
# 2020-01-01 00:00:01

你只需记住:当遇到常规 Python 日期时间功能无法解决的问题时,就可以考虑 python-dateutil !

29.整数除法

在 Python 2 中,除法运算符(/)默认为整数除法,除非其中一个操作数是浮点数。因此,你可以这么写:

# Python 2
5 / 2 = 2
5 / 2.0 = 2.5

在 Python 3 中,除法运算符(/)默认为浮点除法,而整数除法的运算符为 //。因此,你需要这么写:

Python 3
5 / 2 = 2.5
5 // 2 = 2

这项变更背后的动机,请参阅 PEP-0238(https://www.python.org/dev/peps/pep-0238/)。

30. 通过chardet 来检测字符集

你可以使用 chardet 模块来检测文件的字符集。在分析大量随机文本时,这个模块十分实用。安装方法如下:

pip install chardet

安装完成后,你就可以使用命令行工具 chardetect 了,使用方法如下:

chardetect somefile.txt
somefile.txt: ascii with confidence 1.0

你也可以在编程中使用该库,完整的文档请点击这里(https://chardet.readthedocs.io/en/latest/usage.html)。

如上就是我为各位奉上的新年礼物,希望各位喜欢!如果你有其他的技巧、贴士和实践,请在下方留言!

原文:https://towardsdatascience.com/30-python-best-practices-tips-and-tricks-caefb9f8c5f5

本文为 CSDN 翻译,转载请注明来源出处。

热 文 推 荐 

小米MIX Alpha获得百万美金技术大奖;索尼或将推出无边框手机;Linus 不建议用 ZFS | 极客头条

每位初级开发都应该知道的六件大事

Rust 入坑指南:鳞次栉比 | CSDN 博文精选

2019 互联网大事记:谁是最后的赢家?

中国程序员在美遭抢劫电脑遇害,数百人悼念

2019,不可错过的NLP“高光时刻”

详解CPU几个重点基础知识

在以太坊上开发 Dapp 的瓶颈和门槛有哪些?| 博文精选

你点的每个“在看”,我都认真当成了喜欢

发布了1640 篇原创文章 · 获赞 4万+ · 访问量 1324万+

Guess you like

Origin blog.csdn.net/csdnnews/article/details/103942674