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
65
`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
1.2k
iOS app meets GraphQL
manicmaniac
0
1.7k
Debugging Apollo iOS with Apollo Client Developer Tools
manicmaniac
0
50
Other Decks in Programming
See All in Programming
try! Swift Tokyo 初参加報告LT
hinakko2
0
220
Amazon SQSコンシューマー疎結合への旅 - 出張! #DevelopersIO IT技術ブログの中の人が語る勉強会 #3
quiver
0
260
Node.js v22 で変わること
yosuke_furukawa
PRO
9
3.2k
AWS Application Composerで始める、 サーバーレスなデータ基盤構築 / 20240406-jawsug-hokuriku-shinkansen
kasacchiful
1
260
[技育CAMPアカデミア]アイディアを形に!【超入門】スマホアプリ開発〜リリースまでの流れをご紹介
teamlab
PRO
0
370
PHPはいつから死んでいるかの調査
chiroruxx
1
400
Netty Chicago Java User Group 2024-04-17
sullis
0
170
HUIT新歓2024「競技プログラミング、やってみませんか?」
slephy2784
1
270
Micro Frontends for Java Microservices - Devnexus 2024
mraible
PRO
0
490
Apache Hive 4 on Treasure Data
ryukobayashi
0
200
ゆるい個人開発のススメ
kuroppe1819
10
990
Fragment Composition of GraphQL
quramy
5
860
Featured
See All Featured
Debugging Ruby Performance
tmm1
70
11k
4 Signs Your Business is Dying
shpigford
175
21k
Testing 201, or: Great Expectations
jmmastey
28
6.3k
The Cost Of JavaScript in 2023
addyosmani
16
3.9k
Web development in the modern age
philhawksworth
202
10k
Code Reviewing Like a Champion
maltzj
514
39k
The Illustrated Children's Guide to Kubernetes
chrisshort
31
46k
Creatively Recalculating Your Daily Design Routine
revolveconf
210
11k
Agile that works and the tools we love
rasmusluckow
325
20k
The Language of Interfaces
destraynor
151
23k
Typedesign – Prime Four
hannesfritz
36
2.1k
Building Better People: How to give real-time feedback that sticks.
wjessup
355
18k
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!