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
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
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.3k
iOS app meets GraphQL
manicmaniac
0
2.3k
Debugging Apollo iOS with Apollo Client Developer Tools
manicmaniac
0
62
Other Decks in Programming
See All in Programming
CSC307 Lecture 09
javiergs
PRO
1
830
AIエージェント、”どう作るか”で差は出るか? / AI Agents: Does the "How" Make a Difference?
rkaga
4
2k
Grafana:建立系統全知視角的捷徑
blueswen
0
330
2026年 エンジニアリング自己学習法
yumechi
0
130
疑似コードによるプロンプト記述、どのくらい正確に実行される?
kokuyouwind
0
380
[KNOTS 2026登壇資料]AIで拡張‧交差する プロダクト開発のプロセス および携わるメンバーの役割
hisatake
0
250
Implementation Patterns
denyspoltorak
0
280
MDN Web Docs に日本語翻訳でコントリビュート
ohmori_yusuke
0
640
副作用をどこに置くか問題:オブジェクト指向で整理する設計判断ツリー
koxya
1
590
例外処理とどう使い分ける?Result型を使ったエラー設計 #burikaigi
kajitack
16
6k
それ、本当に安全? ファイルアップロードで見落としがちなセキュリティリスクと対策
penpeen
7
2.4k
OCaml 5でモダンな並列プログラミングを Enjoyしよう!
haochenx
0
140
Featured
See All Featured
What does AI have to do with Human Rights?
axbom
PRO
0
2k
SEO for Brand Visibility & Recognition
aleyda
0
4.2k
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
420
Being A Developer After 40
akosma
91
590k
How to train your dragon (web standard)
notwaldorf
97
6.5k
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
110
Side Projects
sachag
455
43k
Marketing to machines
jonoalderson
1
4.6k
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
72
Skip the Path - Find Your Career Trail
mkilby
0
53
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
49
9.8k
Building AI with AI
inesmontani
PRO
1
680
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!