在Python3.6的发布中,我们看到他们采纳了字符串字面量插值,或者用更通俗的说法:f-string
。
最开始叫我用的时候,我是犹豫的,因为我们已经有很多字符串工具了:
one, two = 1, 2
_format = '{},{}'.format(one, two)
_percent = '%s,%s' % (one, two)
_concatenation = str(one) + ',' + str(two)
_join = ','.join([str(one), str(two)])
assert _format == _percent == _concatenation == _join == _fstring
加入f-string
之后,也不见得特别有用:
_fstring = f'{one},{two}'
assert _format == _percent == _concatenation == _join == _fstring
最开始的时候我很怀疑,但是之后我将f-string应用到一个现实的项目。现在,真香。。。f-string可以让之前版本中,python那些很繁琐的写法简化。
我感觉真香的原因是因为,f-string更加的简洁,同时也更加地易读:
_fstring = f'Total: {one + two}' # Go f-string!
_format = 'Total: {}'.format(one + two)
_percent = 'Total: %s' % (one + two)
_concatenation = 'Total: ' + str(one + two)
assert _fstring == _format == _percent == _concatenation
F-String 让人上瘾
初看起来,f-string只是python的一个小改动,但是实际用下来会发现,这个改动对语言的可读性的加强是巨大的。
现在我对f-string上瘾了。让我使用3.6以前版本的Python,我会总感觉少了什么。
Okay,享受这个上瘾的感觉吧!f-string是个很好的东西。
一个工具脚本的例子
我们最近发布了<<Two Scoops of Django 1.11>>,它是使用Latex写的。就像很多编程数据一样,我们把书里面的代码都整理进了一个repo,供读者使用。
不过,在我们校正代码高亮之后,我们需要重头提取这些代码。这是一个繁琐的工作,然后我使用Python3.6中的f-string,用30分钟的时间写了下面的脚本,完成了工作:
"""Two Scoops of Django 1.11 Code Extractor"""
import os
import shutil
from glob import glob
try:
shututl.rmtree('code')
print('Remove old code directory')
except FileNotFoundError:
pass
os.mkdir('code')
print('Created new code directory')
STAR = '*'
LANGUAGES = """LEGAL TEXT GOES HERE"""
LANGUAGE_START = {
'\begin{python}': '.py',
'\begin{badpython}': '.py',
'\begin{django}': '.html',
'\begin{baddjango}': '.html',
'\begin{plaintext}': '.txt',
'\begin{badplaintext}': '.txt',
'\begin{sql}': '.sql',
'\begin{makefile}': '',
'\begin{json}': '.json',
'\begin{bash}': '.txt',
'\begin{xml}': '.html',
}
LANGUAGE_END = {x.replace('begin', 'end'): y for x, y in LANGUAGE_START.items()}
def is_example(line, SWITCH):
for key in SWITCH:
if line.strip().startswith(key):
return SWITCH[key]
return None
def makefilename(chapter_num, in_example):
return f"code/chapter_{chapter_num}_example_{str(example_num).zfill(2)}{in_example}"
if __name__ == '__main__':
in_example = False
starting = False
for path in glob('chapters/*.tex'):
try:
chapter_num = int(path[9:11])
chapter_num = path[9:11]
except ValueError:
if not path.lower().startswith('appendix'):
print(f'{STAR*40}
{path}
{STAR*40}')
continue
example_num = 1
with open(path) as f:
lines = (x for x in f.readlines())
for line in lines:
if starting:
# Crazy long string interpolation that should probably
# be broken up but remains because it's easy for me to read
filename = f'code/chapter_{chapter_num}_example_{str(example_num).zfill(2)}{in_example}'
dafile = open(filename, 'w')
if in_example in ('.py', '.html'):
dafile.write(f'"""
{LEGALESE}"""
')
else:
dafile.write(f'{LEGALESE}
{STAR*20}
')
print(filename)
if not in_example:
mime = None
in_example = is_example(line, LANGUAGE_START)
if in_example:
starting = True
continue
mime = is_example(line, LANGUAGE_END)
starting = False
if mime:
print(mime)
in_example = False
example_num += 1
dafile.close()
else:
dafile.write(line)