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
98
`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.1k
iOS app meets GraphQL
manicmaniac
0
2.1k
Debugging Apollo iOS with Apollo Client Developer Tools
manicmaniac
0
54
Other Decks in Programming
See All in Programming
On-the-fly Suggestions of Rewriting Method Deprecations
ohbarye
1
4.8k
インプロセスQAにおいて大事にしていること / In-process QA Meetup
medley
0
140
オープンソースコントリビュート入門
_katsuma
0
120
Bedrock × Confluenceで簡単(?)社内RAG
iharuoru
1
110
Empowering Developers with HTML-Aware ERB Tooling @ RubyKaigi 2025, Matsuyama, Ehime
marcoroth
2
960
Improve my own Ruby
sisshiki1969
0
100
Golangci-lint v2爆誕: 君たちはどうすべきか
logica0419
1
230
The Nature of Complexity in John Ousterhout’s Philosophy of Software Design
philipschwarz
PRO
0
160
Thank you <💅>, What's the Next?
ahoxa
1
590
State of Namespace
tagomoris
5
2.4k
ASP.NETアプリケーションのモダナイゼーションについて
tomokusaba
0
240
The New Developer Workflow: How AI Transforms Ideas into Code
danielsogl
0
110
Featured
See All Featured
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
13
830
Documentation Writing (for coders)
carmenintech
71
4.7k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
14
1.4k
The Cult of Friendly URLs
andyhume
78
6.3k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
12k
Fashionably flexible responsive web design (full day workshop)
malarkey
407
66k
Speed Design
sergeychernyshev
29
920
Large-scale JavaScript Application Architecture
addyosmani
512
110k
StorybookのUI Testing Handbookを読んだ
zakiyama
30
5.7k
The World Runs on Bad Software
bkeepers
PRO
68
11k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.3k
Testing 201, or: Great Expectations
jmmastey
42
7.5k
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!