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
Catch Up: Go Style Guide Update
andpad
0
200
株式会社 Sun terras カンパニーデック
sunterras
0
250
開発者への寄付をアプリ内課金として実装する時の気の使いどころ
ski
0
360
技術的負債の正体を知って向き合う / Facing Technical Debt
irof
0
120
CSC305 Lecture 02
javiergs
PRO
1
260
『毎日の移動』を支えるGoバックエンド内製開発
yutautsugi
2
210
Goで実践するドメイン駆動開発 AIと歩み始めた新規プロダクト開発の現在地
imkaoru
4
760
Railsだからできる 例外業務に禍根を残さない 設定設計パターン
ei_ei_eiichi
0
370
Django Ninja による API 開発効率化とリプレースの実践
kashewnuts
0
1.1k
Playwrightはどのようにクロスブラウザをサポートしているのか
yotahada3
7
2.3k
Six and a half ridiculous things to do with Quarkus
hollycummins
0
130
Breaking Up with Big ViewModels — Without Breaking Your Architecture (droidcon Berlin 2025)
steliosf
PRO
1
350
Featured
See All Featured
Documentation Writing (for coders)
carmenintech
75
5k
Imperfection Machines: The Place of Print at Facebook
scottboms
269
13k
Bootstrapping a Software Product
garrettdimon
PRO
307
110k
Into the Great Unknown - MozCon
thekraken
40
2.1k
Intergalactic Javascript Robots from Outer Space
tanoku
273
27k
Speed Design
sergeychernyshev
32
1.1k
The Power of CSS Pseudo Elements
geoffreycrofte
79
6k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
53k
Making Projects Easy
brettharned
119
6.4k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
657
61k
Producing Creativity
orderedlist
PRO
347
40k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
29
2.6k
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!