comparison docs/pycon2010/pycon2010.rst @ 374:89e38f922c25

font sizes up
author cat@eee
date Sat, 20 Feb 2010 00:11:34 -0500
parents a381f8dd3a45
children 6cc12ad6e616
comparison
equal deleted inserted replaced
373:a381f8dd3a45 374:89e38f922c25
2 Easy command-line interpreters with cmd and cmd2 2 Easy command-line interpreters with cmd and cmd2
3 ================================================ 3 ================================================
4 4
5 :author: Catherine Devlin 5 :author: Catherine Devlin
6 :date: 2010-02-20 6 :date: 2010-02-20
7 7 :slides: http://pypi.python.org/pypi/cmd2
8 Quit scribbling
9 ===============
10
11 Slides are *already* posted at
12 homepage: http://pypi.python.org/pypi/cmd2
13 8
14 Web 2.0 9 Web 2.0
15 ======= 10 =======
16 11
17 .. image:: web-2-0-logos.gif 12 .. image:: web-2-0-logos.gif
55 3. (repeat) 50 3. (repeat)
56 51
57 Examples 52 Examples
58 ======== 53 ========
59 54
60 * Bash, Korn, zsh 55 .. class:: big
61 * Python shell 56
62 * screen 57 * Bash, Korn, zsh
63 * Zork 58 * Python shell
64 * SQL clients: psql, SQL*\Plus, mysql... 59 * screen
65 * ed 60 * Zork
61 * SQL clients: psql, SQL*\Plus, mysql...
62 * ed
66 63
67 .. ``ed`` proves that CLI is sometimes the wrong answer. 64 .. ``ed`` proves that CLI is sometimes the wrong answer.
68 65
69 != Command Line Utilities 66 != Command Line Utilities
70 ========================= 67 =========================
71 68
72 (``ls``, ``grep``, ``ping``, etc.) 69 .. class:: big
73 70
74 1. Accepts arguments at invocation 71 (``ls``, ``grep``, ``ping``, etc.)
75 2. executes 72
76 3. terminates 73 1. Accept arguments at invocation
77 74 2. execute
78 Use ``sys.argv``, ``optparse`` 75 3. terminate
79 76
80 !="Text User Interface", "Console" 77 Use ``sys.argv``, ``optparse``
81 ================================== 78
79 !="Text User Interface"
80 =======================
82 81
83 * Use entire (session) screen 82 * Use entire (session) screen
84 * I/O is *not* line-by-line 83 * I/O is *not* line-by-line
85 * See ``curses``, ``urwid`` 84 * See ``curses``, ``urwid``
86 85
114 Fundamental prrrinciple 113 Fundamental prrrinciple
115 ======================= 114 =======================
116 115
117 .. class:: huge 116 .. class:: huge
118 117
119 Transform ``(Cmd) foo a b c`` 118 ``(Cmd) foo a b c``
120 119
121 to ``self.do_foo('a b c')`` 120 becomes
121
122 ``self.do_foo('a b c')``
122 123
123 ``do_``-methods: pirate2.py 124 ``do_``-methods: pirate2.py
124 =========================== 125 ===========================
125 126
126 :: 127 ::
128 class Pirate(Cmd): 129 class Pirate(Cmd):
129 gold = 3 130 gold = 3
130 def do_loot(self, arg): 131 def do_loot(self, arg):
131 'Seize booty frrrom a passing ship.' 132 'Seize booty frrrom a passing ship.'
132 self.gold += 1 133 self.gold += 1
133 print('Now we gots {0} doubloons'.format(self.gold)) 134 print('Now we gots {0} doubloons'
135 .format(self.gold))
134 def do_drink(self, arg): 136 def do_drink(self, arg):
135 'Drown your sorrrows in rrrum.' 137 'Drown your sorrrows in rrrum.'
136 self.gold -= 1 138 self.gold -= 1
137 print('Now we gots {0} doubloons'.format(self.gold)) 139 print('Now we gots {0} doubloons'
140 .format(self.gold))
138 141
139 .. do_methods; more help 142 .. do_methods; more help
140 143
141 Hooks 144 Hooks
142 ===== 145 =====
143 146
144 .. image:: hook.jpg 147 .. image:: hook.jpg
145 :height: 250px 148 :height: 250px
146 149
147 preloop, postloop, precmd, postcmd 150 ::
151
152 self.preloop()
153 self.postloop()
154 self.precmd(line)
155 self.postcmd(stop, line)
148 156
149 Hooks: pirate3.py 157 Hooks: pirate3.py
150 ================= 158 =================
151 159
152 :: 160 ::
160 def precmd(self, line): 168 def precmd(self, line):
161 self.initial_gold = self.gold 169 self.initial_gold = self.gold
162 return line 170 return line
163 def postcmd(self, stop, line): 171 def postcmd(self, stop, line):
164 if self.gold != self.initial_gold: 172 if self.gold != self.initial_gold:
165 print('Now we gots {0} doubloons'.format(self.gold)) 173 print('Now we gots {0} doubloons'
174 .format(self.gold))
166 175
167 Arguments: pirate4.py 176 Arguments: pirate4.py
168 ===================== 177 =====================
169 178
170 :: 179 ::
186 195
187 :: 196 ::
188 197
189 def postcmd(self, stop, line): 198 def postcmd(self, stop, line):
190 if self.gold != self.initial_gold: 199 if self.gold != self.initial_gold:
191 print('Now we gots {0} doubloons'.format(self.gold)) 200 print('Now we gots {0} doubloons'
201 .format(self.gold))
192 if self.gold < 0: 202 if self.gold < 0:
193 print("Off to debtorrr's prison. Game overrr.") 203 print("Off to debtorrr's prison.")
194 return True 204 stop = True
195 return stop 205 return stop
196 def do_quit(self, arg): 206 def do_quit(self, arg):
197 print("Quiterrr!") 207 print("Quiterrr!")
198 return True 208 return True
199 209
200 prompts and defaults: pirate6.py 210 prompts, defaults: pirate6.py
201 ================================ 211 =============================
202 212
203 :: 213 ::
204 214
205 prompt = 'arrr> ' 215 prompt = 'arrr> '
206 def default(self, line): 216 def default(self, line):
207 print('What mean ye by "{0}"?' 217 print('What mean ye by "{0}"?'
208 .format(line)) 218 .format(line))
209 219
210 Other CLI packages 220 Other CLI packages
211 ================== 221 ==================
212 222
213 * cmdlin 223 .. class:: big
214 * cmd2 224
225 * CmdLoop
226 * cly
227 * CMdO
228 * pycopia
229 * cmdlin
230 * cmd2
215 231
216 Demo 232 Demo
217 ==== 233 ====
218 234
219 Convert ``cmd`` app to ``cmd2`` 235 .. class:: huge
236
237 Convert ``cmd`` app to ``cmd2``
220 238
221 cmd2 239 cmd2
222 ==== 240 ====
223 241
224 .. image:: schematic.png 242 .. image:: schematic.png
225 :height: 350px 243 :height: 350px
226 244
227 As you wish, Guido 245 As you wish, Guido
228 ================== 246 ==================
229 247
230 Python 3 compatible 248 .. class:: huge
249
250 Python 3 compatible
231 251
232 (um, mostly) 252 (um, mostly)
233 253
234 Absolutely free 254 Absolutely free
235 =============== 255 ===============
236 256
237 * Script files 257 Script files
238 * Commands at invocation 258
239 * Output redirection 259 Commands at invocation
240 * Python 260
241 * Transcript-based testing 261 Output redirection
262
263 Python
264
265 Transcript testing
242 266
243 But wait, there's more 267 But wait, there's more
244 ====================== 268 ======================
245 269
246 * Abbreviated commands 270 * Abbreviated commands
248 * Quitting 272 * Quitting
249 * Timing 273 * Timing
250 * Echo 274 * Echo
251 * Debug 275 * Debug
252 276
253 For a few keystrokes more...
254 ============================
255
256 * Default to shell
257 * Color output
258 * Shortcuts
259 * Multiline commands
260 * Environment variables
261
262 Minor changes: pirate7.py 277 Minor changes: pirate7.py
263 ========================= 278 =========================
264 279
265 :: 280 ::
266 281
274 print(self.colorize(arg, self.songcolor)) 289 print(self.colorize(arg, self.songcolor))
275 290
276 Now how much would you pay? 291 Now how much would you pay?
277 =========================== 292 ===========================
278 293
279 * options / flags 294 options / flags
280 * Quiet (suppress feedback) 295
281 * BASH-style ``select`` 296 Quiet (suppress feedback)
282 * Parsing: terminators, suffixes 297
298 BASH-style ``select``
299
300 Parsing: terminators, suffixes
283 301
284 Options: pirate8.py 302 Options: pirate8.py
285 =================== 303 ===================
286 304
287 :: 305 ::
294 .format(chant, arg)) 312 .format(chant, arg))
295 313
296 Serious example: sqlpython 314 Serious example: sqlpython
297 ========================== 315 ==========================
298 316
299 ``cmd``-based app by Luca Canali @ CERN 317 .. class:: big
300 318
301 Replacement for Oracle SQL\*Plus 319 ``cmd``-based app by Luca Canali @ CERN
302 320
303 Now ``cmd2``-based; postgreSQL; MySQL 321 Replacement for Oracle SQL\*Plus
322
323 Now ``cmd2``-based; postgreSQL; MySQL
304 324
305 sqlpython features 325 sqlpython features
306 ================== 326 ==================
307 327
308 * from ``cmd2``: scripts, redirection, 328 .. class:: big
309 py, etc. 329
310 * multiple connections 330 * from ``cmd2``: scripts, redirection,
311 * UNIX: ls, cat, grep 331 py, etc.
312 * Special output 332 * multiple connections
333 * UNIX: ls, cat, grep
334 * Special output
313 335
314 File reporter 336 File reporter
315 ============= 337 =============
316 338
317 Gather info: Python 339 .. class:: huge
318 340
319 Store: postgresql 341 Gather info: Python
320 342
321 Report: html 343 Store: postgresql
344
345 Report: html
322 346
323 Thank you 347 Thank you
324 ========= 348 =========
325 349
326 pypi.python.org/pypi/cmd2 350 .. class:: big
327 351
328 catherinedevlin.blogspot.com 352 http://pypi.python.org/pypi/cmd2
329 353
330 catherinedevlin.pythoneers.com 354 http://catherinedevlin.blogspot.com
331 355
332 356 http://catherinedevlin.pythoneers.com
357
358