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
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
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
64
Other Decks in Programming
See All in Programming
ポーリング処理廃止によるイベント駆動アーキテクチャへの移行
seitarof
3
1.3k
PHP でエミュレータを自作して Ubuntu を動かそう
m3m0r7
PRO
2
170
年間50登壇、単著出版、雑誌寄稿、Podcast出演、YouTube、CM、カンファレンス主催……全部やってみたので面白さ等を比較してみよう / I’ve tried them all, so let’s compare how interesting they are.
nrslib
4
700
AIエージェントで業務改善してみた
taku271
0
410
夢の無限スパゲッティ製造機 -実装篇- #phpstudy
o0h
PRO
0
190
最初からAWS CDKで技術検証してもいいんじゃない?
akihisaikeda
4
180
20260320登壇資料
pharct
0
160
一度始めたらやめられない開発効率向上術 / Findy あなたのdotfilesを教えて!
k0kubun
4
2.8k
AI活用のコスパを最大化する方法
ochtum
0
370
Rethinking API Platform Filters
vinceamstoutz
0
7.9k
存在論的プログラミング: 時間と存在を記述する
koriym
5
770
アーキテクチャモダナイゼーションとは何か
nwiizo
14
3k
Featured
See All Featured
Building a A Zero-Code AI SEO Workflow
portentint
PRO
0
430
Mobile First: as difficult as doing things right
swwweet
225
10k
Done Done
chrislema
186
16k
Collaborative Software Design: How to facilitate domain modelling decisions
baasie
0
190
Building Flexible Design Systems
yeseniaperezcruz
330
40k
Making Projects Easy
brettharned
120
6.6k
Code Reviewing Like a Champion
maltzj
528
40k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
27
3.4k
Deep Space Network (abreviated)
tonyrice
0
100
Paper Plane (Part 1)
katiecoart
PRO
0
6.4k
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
210
Into the Great Unknown - MozCon
thekraken
40
2.3k
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!