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
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
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
Claude Codeログ基盤の構築
giginet
PRO
7
3.4k
What Spring Developers Should Know About Jakarta EE
ivargrimstad
0
580
脱 雰囲気実装!AgentCoreを良い感じにWEBアプリケーションに組み込むために
takuyay0ne
1
260
守る「だけ」の優しいEMを抜けて、 事業とチームを両方見る視点を身につけた話
maroon8021
3
1k
OTP を自動で入力する裏技
megabitsenmzq
0
110
AIに任せる範囲を安全に広げるためにやっていること
fukucheee
0
140
Windows on Ryzen and I
seosoft
0
300
最初からAWS CDKで技術検証してもいいんじゃない?
akihisaikeda
4
160
AI時代のシステム設計:ドメインモデルで変更しやすさを守る設計戦略
masuda220
PRO
5
1k
SourceGeneratorのマーカー属性問題について
htkym
0
200
AI Assistants for Your Angular Solutions
manfredsteyer
PRO
0
140
ふつうのRubyist、ちいさなデバイス、大きな一年 / Ordinary Rubyists, Tiny Devices, Big Year
chobishiba
1
460
Featured
See All Featured
Build your cross-platform service in a week with App Engine
jlugia
234
18k
Mind Mapping
helmedeiros
PRO
1
120
Lightning Talk: Beautiful Slides for Beginners
inesmontani
PRO
1
480
Reality Check: Gamification 10 Years Later
codingconduct
0
2k
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
290
Making Projects Easy
brettharned
120
6.6k
The browser strikes back
jonoalderson
0
810
Leo the Paperboy
mayatellez
4
1.5k
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
0
210
SEO Brein meetup: CTRL+C is not how to scale international SEO
lindahogenes
1
2.4k
Code Reviewing Like a Champion
maltzj
528
40k
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
640
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!