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.4k
iOS app meets GraphQL
manicmaniac
0
4.1k
Debugging Apollo iOS with Apollo Client Developer Tools
manicmaniac
0
78
Other Decks in Programming
See All in Programming
Contextとはなにか
chiroruxx
1
320
不変条件と整合性境界—ビジネスが決める設計判断と実現パターン / Invariants and Consistency Boundaries
nrslib
13
4.7k
さぁV100、メモリをお食べ・・・
nilpe
0
140
TSKaigi Night Talks 2026_TypeScriptでサプライチェーンの整合性を型に閉じ込める
geekplus_tech
0
350
The ROI of Quarkus for Spring Boot Applications
hollycummins
0
120
技術記事、 専門家としてのプログラマ、 言語化
mizchi
13
6k
Go1.27で導入されるジェネリクスメソッドでできること
mackee
0
120
Webフレームワークの ベンチマークについて
yusukebe
0
170
Claspは野良GASの夢をみるか
takter00
0
190
フロントエンドとバックエンドで「1文字」を揃えよう
youkidearitai
PRO
0
690
Composerを使ったサプライチェーン攻撃の様子を眺めてみる #phpstudy
o0h
PRO
2
250
Oxcを導入して開発体験が向上した話
yug1224
4
310
Featured
See All Featured
We Are The Robots
honzajavorek
0
250
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
360
30k
Java REST API Framework Comparison - PWX 2021
mraible
34
9.4k
Test your architecture with Archunit
thirion
1
2.3k
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
200
So, you think you're a good person
axbom
PRO
2
2.1k
GraphQLとの向き合い方2022年版
quramy
50
15k
We Have a Design System, Now What?
morganepeng
55
8.2k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.3k
Are puppies a ranking factor?
jonoalderson
1
3.6k
Crafting Experiences
bethany
1
180
Color Theory Basics | Prateek | Gurzu
gurzu
0
360
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!