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
120
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
`shell invocation` in Python 2
Presentation for Quipper Alumni Meetup held in 2019.
Ryosuke Ito
December 16, 2019
More Decks by Ryosuke Ito
See All by Ryosuke Ito
React Native 卒業後の「スタディサプリ」の進路
manicmaniac
2
2.5k
iOS app meets GraphQL
manicmaniac
0
4.2k
Debugging Apollo iOS with Apollo Client Developer Tools
manicmaniac
0
81
Other Decks in Programming
See All in Programming
AIの中の人になってみる
htkym
0
140
夏だ!祭りだ!祭りとはドメインモデリングでは?
ryugen04
0
440
引き算の組織 ― アウトカムとAIに全振りするために辞めたこと ― / Organization by Subtraction
hirokiyamamoto14
PRO
0
310
Discordを用いたラボオートメーション関連情報収集の自動化
noguhiro2002
0
460
T3DD26: From RAGs to Riches
martinhelmich
0
110
高専、大学編入、そして未踏へ〜プロダクト開発とキャリアの歩み - Technical College, University Transfer, and On to “Mitou” / My Journey in Product Development and Career
pkmiya
0
100
30年振りにコンパイラの定数整数除算を改善した
herumi
9
4.3k
Dockerfile CMD for Node.js
grazie1999
0
140
Claude Code全社展開のためにやったことn選~プラグイン302個・コミッター271人を支えるために~
kenchan
5
1.8k
Deep dive into the select statement (GopherCon UK)
jespino
0
150
KotlinConf Extended South Korea 2026 Keynote
l2hyunwoo
0
120
Detecting Compromised CI with eBPF and Cilium Tetragon
lizrice
0
270
Featured
See All Featured
GitHub's CSS Performance
jonrohan
1033
470k
Building an army of robots
kneath
306
46k
Practical Orchestrator
shlominoach
191
12k
Speed Design
sergeychernyshev
33
2k
The Anti-SEO Checklist Checklist. Pubcon Cyber Week
ryanjones
0
220
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8.3k
Large-scale JavaScript Application Architecture
addyosmani
515
110k
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
500
Ten Tips & Tricks for a 🌱 transition
stuffmc
0
180
Unsuck your backbone
ammeep
672
58k
My Coaching Mixtape
mlcsv
0
300
From π to Pie charts
rasagy
0
330
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!