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
110
0
Share
`shell invocation` in Python 2
Presentation for Quipper Alumni Meetup held in 2019.
Ryosuke Ito
December 16, 2019
More Decks by Ryosuke Ito
See All by Ryosuke Ito
React Native 卒業後の「スタディサプリ」の進路
manicmaniac
2
2.4k
iOS app meets GraphQL
manicmaniac
0
4.1k
Debugging Apollo iOS with Apollo Client Developer Tools
manicmaniac
0
76
Other Decks in Programming
See All in Programming
Spec Driven Development | AI Summit Lisbon
danielsogl
PRO
0
130
dRuby over BLE
makicamel
2
300
Oxlintのカスタムルールの現況
syumai
5
980
oxlintはeslint/typescript-eslintを置き換えられるのか
shomafujita
2
310
Lessons from Spec-Driven Development
simas
PRO
0
130
CSC307 Lecture 17
javiergs
PRO
0
310
プロパティの順序で型推論が壊れる!? TypeScript6.0の修正からContext-Sensitivityの仕組みを追う
bicstone
2
1.3k
プラグインで拡張される Context をtype-safe にする難しさと設計判断
kazupon
2
570
TSKaigi Night Talks 2026_TypeScriptでサプライチェーンの整合性を型に閉じ込める
geekplus_tech
0
230
[2026年度第1回ORセミナー] 計画最適化ベンチャーと競技プログラミング人材
terryu16
0
240
AIとASP.NET Coreで雑Webアプリを作った話
mayuki
0
250
TAKTでAI駆動開発の品質を設計する
j5ik2o
6
760
Featured
See All Featured
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
160
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.7k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
62k
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
240
Building the Perfect Custom Keyboard
takai
2
780
The SEO identity crisis: Don't let AI make you average
varn
0
480
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
160
HDC tutorial
michielstock
2
690
Utilizing Notion as your number one productivity tool
mfonobong
4
310
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
1
250
How to Ace a Technical Interview
jacobian
281
24k
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!