Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
`shell invocation` in Python 2
Search
Ryosuke Ito
December 16, 2019
Programming
0
93
`shell invocation` in Python 2
Presentation for Quipper Alumni Meetup held in 2019.
Ryosuke Ito
December 16, 2019
Tweet
Share
More Decks by Ryosuke Ito
See All by Ryosuke Ito
React Native 卒業後の「スタディサプリ」の進路
manicmaniac
2
2k
iOS app meets GraphQL
manicmaniac
0
2k
Debugging Apollo iOS with Apollo Client Developer Tools
manicmaniac
0
51
Other Decks in Programming
See All in Programming
DMMオンラインサロンアプリのSwift化
hayatan
0
270
はてなにおけるfujiwara-wareの活用やecspressoのCI/CD構成 / Fujiwara Tech Conference 2025
cohalz
3
3.2k
BEエンジニアがFEの業務をできるようになるまでにやったこと
yoshida_ryushin
0
260
Fibonacci Function Gallery - Part 2
philipschwarz
PRO
0
230
Rubyでつくるパケットキャプチャツール
ydah
0
540
React 19でお手軽にCSS-in-JSを自作する
yukukotani
5
600
ペアーズでの、Langfuseを中心とした評価ドリブンなリリースサイクルのご紹介
fukubaka0825
1
200
ASP.NET Core の OpenAPIサポート
h455h1
0
160
盆栽転じて家具となる / Bonsai and Furnitures
aereal
0
2.2k
Scaling your build logic
antalmonori
1
150
SRE、開発、QAが協業して挑んだリリースプロセス改革@SRE Kaigi 2025
nealle
1
3.3k
自動で //nolint を挿入する取り組み / Gopher's Gathering
utgwkk
1
170
Featured
See All Featured
Building Flexible Design Systems
yeseniaperezcruz
328
38k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
98
18k
Done Done
chrislema
182
16k
Fireside Chat
paigeccino
34
3.2k
Building Better People: How to give real-time feedback that sticks.
wjessup
366
19k
KATA
mclloyd
29
14k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.4k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
Adopting Sorbet at Scale
ufuk
74
9.2k
Become a Pro
speakerdeck
PRO
26
5.1k
Typedesign – Prime Four
hannesfritz
40
2.5k
4 Signs Your Business is Dying
shpigford
182
22k
Transcript
`shell invocation` in Python 2 @manicmaniac
Ryosuke Ito • iOS software engineer • Working for Quipper
Ltd. since Apr. 2019 • Loves Python @manicmaniac
Python 2
EOL of Python 2 2020-01-01 00:00:00 Only 15 days left!
Shell invocation
In Ruby puts `date`
In Perl print `date`
In Python 2.7 import subprocess print subprocess.check_output([‘date’]),
In Python <= 2.6 import subprocess process = subprocess.Popen(['date'], stdout=subprocess.PIPE),
stdout, _ = process.communicate() print stdout,
How can I `date`?
Backquotes in Python 2 `date` == repr(date) == date.__repr__()
Naive implementation import subprocess class date(object): def __repr__(self): return subprocess.check_output(‘date’)
date = date() print `date`
Using meta class import subprocess def register_command(name): def __repr__(self): return
subprocess.check_output( self.__class__.__name__ ) globals()[name] = type( name, (object,), dict(__repr__=__repr__) )() register_command('date') print `date`
But how about other commands?
import backquotes print `date`
None
How it works?
Preprocessing import tokenize def preprocess(filename, readline): for token in tokenize.generate_tokens(readline):
... if inside_backquotes: tokens.extend([ (tokenize.NAME, 'backquotes'), (tokenize.OP, '.'), (tokenize.NAME, 'shell'), (tokenize.OP, '('), ]) ...
At Runtime frame = inspect.currentframe().f_back while frame.f_code.co_filename.startswith('<frozen importlib'): frame =
frame.f_back execfile(source, frame.f_globals, frame.f_locals)
print `date`
Limitations • Doesn’t work on REPL • Doesn’t work on
Python 3 • Really buggy
Backquotes in Python 3 >>> `date` File "<stdin>", line 1
`date` ^ SyntaxError: invalid syntax
Don’t use backquotes
R.I.P Python 2 Thank you for listening!