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
110
0
Share
`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.4k
iOS app meets GraphQL
manicmaniac
0
4k
Debugging Apollo iOS with Apollo Client Developer Tools
manicmaniac
0
73
Other Decks in Programming
See All in Programming
Lightning-Fast Method Calls with Ruby 4.1 ZJIT / RubyKaigi 2026
k0kubun
3
3.1k
Claude CodeでETLジョブ実行テストを自動化してみた
yoshikikasama
0
1.2k
(Re)make Regexp in Ruby: Democratizing internals for the JIT
makenowjust
3
1.1k
PicoRuby for IoT: Connecting to the Cloud with MQTT
yuuu
2
770
実用!Hono RPC2026
yodaka
2
310
いつか誰かが、と思っていた フロントエンド刷新5年間の実践知
kiichisugihara
1
280
AI時代のエンジニアリングの原則 / Engineering Principles in the AI Era
haru860
0
1.2k
Kubernetesを使わない環境にもCloud Nativeなデプロイを実現する / Enabling Cloud Native deployments without the complexity of Kubernetes
linyows
3
400
Sans tests, vos agents ne sont pas fiables
nabondance
0
120
空間オーディオの活用
objectiveaudio
0
150
Agent Skills を社内で育てる仕組み作り
jackchuka
1
2k
検索設計から 推論設計への重心移動と Recall-First Retrieval
po3rin
5
1.7k
Featured
See All Featured
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
360
Lightning Talk: Beautiful Slides for Beginners
inesmontani
PRO
1
540
From π to Pie charts
rasagy
0
180
XXLCSS - How to scale CSS and keep your sanity
sugarenia
250
1.3M
How GitHub (no longer) Works
holman
316
150k
B2B Lead Gen: Tactics, Traps & Triumph
marketingsoph
0
120
SEO Brein meetup: CTRL+C is not how to scale international SEO
lindahogenes
1
2.6k
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
370
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
65
55k
Avoiding the “Bad Training, Faster” Trap in the Age of AI
tmiket
0
150
Into the Great Unknown - MozCon
thekraken
41
2.5k
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
140
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!