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.4k
iOS app meets GraphQL
manicmaniac
0
4k
Debugging Apollo iOS with Apollo Client Developer Tools
manicmaniac
0
63
Other Decks in Programming
See All in Programming
Redox OS でのネームスペース管理と chroot の実現
isanethen
0
250
OTP を自動で入力する裏技
megabitsenmzq
0
120
AI活用のコスパを最大化する方法
ochtum
0
230
Docコメントで始める簡単ガードレール
keisukeikeda
1
130
AI時代の脳疲弊と向き合う ~言語学としてのPHP~
sakuraikotone
1
130
PHPで TLSのプロトコルを実装してみる
higaki_program
0
230
AI駆動開発の本音 〜Claude Code並列開発で見えたエンジニアの新しい役割〜
hisuzuya
4
520
Windows on Ryzen and I
seosoft
0
320
社内規程RAGの精度を73.3% → 100%に改善した話
oharu121
13
8.2k
maplibre-gl-layers - 地図に移動体たくさん表示したい
kekyo
PRO
0
290
RAGでハマりがちな"Excelの罠"を、データの構造化で突破する
harumiweb
9
2.9k
Fundamentals of Software Engineering In the Age of AI
therealdanvega
2
260
Featured
See All Featured
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
22k
Music & Morning Musume
bryan
47
7.1k
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
9.9k
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
250
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
270
GraphQLの誤解/rethinking-graphql
sonatard
75
11k
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
150
Winning Ecommerce Organic Search in an AI Era - #searchnstuff2025
aleyda
1
1.9k
The SEO identity crisis: Don't let AI make you average
varn
0
420
Ethics towards AI in product and experience design
skipperchong
2
230
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
122
21k
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
0
210
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!