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
110
`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
2.2k
iOS app meets GraphQL
manicmaniac
0
2.2k
Debugging Apollo iOS with Apollo Client Developer Tools
manicmaniac
0
60
Other Decks in Programming
See All in Programming
CSC305 Lecture 08
javiergs
PRO
0
270
Android16 Migration Stories ~Building a Pattern for Android OS upgrades~
reoandroider
0
130
pnpm に provenance のダウングレード を検出する PR を出してみた
ryo_manba
1
150
実践Claude Code:20の失敗から学ぶAIペアプログラミング
takedatakashi
16
6.5k
組込みだけじゃない!TinyGo で始める無料クラウド開発入門
otakakot
2
350
オープンソースソフトウェアへの解像度🔬
utam0k
17
3.1k
overlayPreferenceValue で実現する ピュア SwiftUI な AdMob ネイティブ広告
uhucream
0
200
チームの境界をブチ抜いていけ
tokai235
0
210
CSC509 Lecture 07
javiergs
PRO
0
240
品質ワークショップをやってみた
nealle
0
600
Six and a half ridiculous things to do with Quarkus
hollycummins
0
210
CSC305 Lecture 09
javiergs
PRO
0
300
Featured
See All Featured
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
285
14k
Java REST API Framework Comparison - PWX 2021
mraible
34
8.9k
Making Projects Easy
brettharned
120
6.4k
4 Signs Your Business is Dying
shpigford
185
22k
RailsConf 2023
tenderlove
30
1.3k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
46
7.7k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.2k
Fashionably flexible responsive web design (full day workshop)
malarkey
407
66k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
15k
Measuring & Analyzing Core Web Vitals
bluesmoon
9
630
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.2k
Producing Creativity
orderedlist
PRO
347
40k
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!