summaryrefslogtreecommitdiff
path: root/docs/pycon2010
diff options
context:
space:
mode:
authorFederico Ceratto <federico.ceratto@gmail.com>2017-01-06 12:23:48 +0000
committerGitHub <noreply@github.com>2017-01-06 12:23:48 +0000
commit46d1028a330ffe4d344b99739d1d4b607654ade5 (patch)
treea9d266360cc23d383335c69195a2b65cc8cba77f /docs/pycon2010
parentef074cc2ffb64b2c5281757820f6904bd368d51e (diff)
parent01a8ab20a7ce3544da7c3e69dfc5d4cb0ad257ae (diff)
downloadcmd2-git-46d1028a330ffe4d344b99739d1d4b607654ade5.tar.gz
Merge pull request #29 from tleonhardt/master
Refactored to create a unified Python 2/3 codebase based on six which doesn't require 2to3
Diffstat (limited to 'docs/pycon2010')
-rw-r--r--docs/pycon2010/fileutil.py12
-rw-r--r--docs/pycon2010/graph.py41
-rw-r--r--docs/pycon2010/pirate.py5
-rw-r--r--docs/pycon2010/pirate2.py6
-rw-r--r--docs/pycon2010/pirate3.py12
-rw-r--r--docs/pycon2010/pirate4.py18
-rw-r--r--docs/pycon2010/pirate5.py19
-rw-r--r--docs/pycon2010/pirate6.py20
-rw-r--r--docs/pycon2010/pirate7.py22
-rw-r--r--docs/pycon2010/pirate8.py25
-rw-r--r--docs/pycon2010/pycon2010.rst64
-rw-r--r--docs/pycon2010/schematic.py32
12 files changed, 130 insertions, 146 deletions
diff --git a/docs/pycon2010/fileutil.py b/docs/pycon2010/fileutil.py
deleted file mode 100644
index 5e754b5a..00000000
--- a/docs/pycon2010/fileutil.py
+++ /dev/null
@@ -1,12 +0,0 @@
-import glob
-import os.path
-
-for fullfilename in glob.glob('/home/cat/proj/cmd2/*.py'):
- (dirpath, fname) = os.path.split(fullfilename)
- stats = os.stat(fullfilename)
- binds['path'] = dirpath
- binds['name'] = fname
- binds['bytes'] = stats.st_size
- cmd("""INSERT INTO cat.files (path, name, bytes)
- VALUES (%(path)s, %(name)s, %(bytes)s)""")
-quit()
diff --git a/docs/pycon2010/graph.py b/docs/pycon2010/graph.py
deleted file mode 100644
index 96ffde72..00000000
--- a/docs/pycon2010/graph.py
+++ /dev/null
@@ -1,41 +0,0 @@
-from turtle import *
-pu()
-goto(-400,-400)
-
-def label(txt):
- write(txt, font=('Arial', 20, 'italic'))
-hideturtle()
-width(6)
-
-def line(len, _label):
- start = pos()
- pd()
- forward(len)
- pu()
- forward(30)
- pd()
- label(_label)
- pu()
- goto(start)
-
-def tech(x, y, _label):
- pu()
- goto(x, y)
- pd()
- write(_label, font=('Arial', 40, 'bold'))
- pu()
-
-line(600, "Easy to write")
-left(90)
-line(600, "Easy to use")
-
-tech(-360, 160, 'GUI')
-tech(-390, 100, 'AJAX')
-tech(-300, -10, 'webapp')
-tech(190, -380, 'CLU')
-tech(60, -320, 'TUI')
-tech(100, -210, 'cmd')
-tech(80, -80, 'cmd2')
-
-while True:
- pass \ No newline at end of file
diff --git a/docs/pycon2010/pirate.py b/docs/pycon2010/pirate.py
index 98db50ea..bd8b5170 100644
--- a/docs/pycon2010/pirate.py
+++ b/docs/pycon2010/pirate.py
@@ -1,7 +1,10 @@
+# coding=utf-8
from cmd import Cmd
+
class Pirate(Cmd):
pass
+
pirate = Pirate()
-pirate.cmdloop() \ No newline at end of file
+pirate.cmdloop()
diff --git a/docs/pycon2010/pirate2.py b/docs/pycon2010/pirate2.py
index e2c49609..343f94ff 100644
--- a/docs/pycon2010/pirate2.py
+++ b/docs/pycon2010/pirate2.py
@@ -1,18 +1,24 @@
+# coding=utf-8
from cmd import Cmd
+
+
# using ``do_`` methods
class Pirate(Cmd):
gold = 3
+
def do_loot(self, arg):
'Seize booty from a passing ship.'
self.gold += 1
print('Now we gots {0} doubloons'
.format(self.gold))
+
def do_drink(self, arg):
'Drown your sorrrows in rrrum.'
self.gold -= 1
print('Now we gots {0} doubloons'
.format(self.gold))
+
pirate = Pirate()
pirate.cmdloop()
diff --git a/docs/pycon2010/pirate3.py b/docs/pycon2010/pirate3.py
index 7977a8dc..ecc70f3f 100644
--- a/docs/pycon2010/pirate3.py
+++ b/docs/pycon2010/pirate3.py
@@ -1,21 +1,29 @@
+# coding=utf-8
from cmd import Cmd
+
+
# using hook
class Pirate(Cmd):
gold = 3
+
def do_loot(self, arg):
'Seize booty from a passing ship.'
self.gold += 1
+
def do_drink(self, arg):
- 'Drown your sorrrows in rrrum.'
+ 'Drown your sorrrows in rrrum.'
self.gold -= 1
+
def precmd(self, line):
self.initial_gold = self.gold
return line
- def postcmd(self, stop, line):
+
+ def postcmd(self, stop, line):
if self.gold != self.initial_gold:
print('Now we gots {0} doubloons'
.format(self.gold))
+
pirate = Pirate()
pirate.cmdloop()
diff --git a/docs/pycon2010/pirate4.py b/docs/pycon2010/pirate4.py
index 5de9c212..a4e4816d 100644
--- a/docs/pycon2010/pirate4.py
+++ b/docs/pycon2010/pirate4.py
@@ -1,27 +1,35 @@
+# coding=utf-8
from cmd import Cmd
+
+
# using arguments
class Pirate(Cmd):
gold = 3
+
def do_loot(self, arg):
'Seize booty from a passing ship.'
self.gold += 1
+
def do_drink(self, arg):
'''Drown your sorrrows in rrrum.
-
- drink [n] - drink [n] barrel[s] o' rum.'''
+
+ drink [n] - drink [n] barrel[s] o' rum.'''
try:
self.gold -= int(arg)
except:
if arg:
print('''What's "{0}"? I'll take rrrum.'''.format(arg))
- self.gold -= 1
+ self.gold -= 1
+
def precmd(self, line):
self.initial_gold = self.gold
return line
- def postcmd(self, stop, line):
+
+ def postcmd(self, stop, line):
if self.gold != self.initial_gold:
print('Now we gots {0} doubloons'.format(self.gold))
+
pirate = Pirate()
-pirate.cmdloop() \ No newline at end of file
+pirate.cmdloop()
diff --git a/docs/pycon2010/pirate5.py b/docs/pycon2010/pirate5.py
index 7add4635..2167c7f4 100644
--- a/docs/pycon2010/pirate5.py
+++ b/docs/pycon2010/pirate5.py
@@ -1,25 +1,32 @@
+# coding=utf-8
from cmd import Cmd
+
+
# quitting
class Pirate(Cmd):
gold = 3
+
def do_loot(self, arg):
'Seize booty from a passing ship.'
self.gold += 1
+
def do_drink(self, arg):
'''Drown your sorrrows in rrrum.
-
- drink [n] - drink [n] barrel[s] o' rum.'''
+
+ drink [n] - drink [n] barrel[s] o' rum.'''
try:
self.gold -= int(arg)
except:
if arg:
print('''What's "{0}"? I'll take rrrum.'''.format(arg))
- self.gold -= 1
+ self.gold -= 1
+
def precmd(self, line):
self.initial_gold = self.gold
return line
- def postcmd(self, stop, line):
+
+ def postcmd(self, stop, line):
if self.gold != self.initial_gold:
print('Now we gots {0} doubloons'
.format(self.gold))
@@ -27,9 +34,11 @@ class Pirate(Cmd):
print("Off to debtorrr's prison.")
stop = True
return stop
+
def do_quit(self, arg):
print("Quiterrr!")
- return True
+ return True
+
pirate = Pirate()
pirate.cmdloop()
diff --git a/docs/pycon2010/pirate6.py b/docs/pycon2010/pirate6.py
index 4a03fed4..a90c2b52 100644
--- a/docs/pycon2010/pirate6.py
+++ b/docs/pycon2010/pirate6.py
@@ -1,29 +1,37 @@
+# coding=utf-8
from cmd2 import Cmd
+
+
# prompts and defaults
class Pirate(Cmd):
gold = 3
prompt = 'arrr> '
+
def default(self, line):
print('What mean ye by "{0}"?'
.format(line))
+
def do_loot(self, arg):
'Seize booty from a passing ship.'
self.gold += 1
+
def do_drink(self, arg):
'''Drown your sorrrows in rrrum.
-
- drink [n] - drink [n] barrel[s] o' rum.'''
+
+ drink [n] - drink [n] barrel[s] o' rum.'''
try:
self.gold -= int(arg)
except:
if arg:
print('''What's "{0}"? I'll take rrrum.'''.format(arg))
- self.gold -= 1
+ self.gold -= 1
+
def precmd(self, line):
self.initial_gold = self.gold
return line
- def postcmd(self, stop, line):
+
+ def postcmd(self, stop, line):
if self.gold != self.initial_gold:
print('Now we gots {0} doubloons'
.format(self.gold))
@@ -31,9 +39,11 @@ class Pirate(Cmd):
print("Off to debtorrr's prison.")
stop = True
return stop
+
def do_quit(self, arg):
print("Quiterrr!")
- return True
+ return True
+
pirate = Pirate()
pirate.cmdloop()
diff --git a/docs/pycon2010/pirate7.py b/docs/pycon2010/pirate7.py
index 25ff5822..a333070c 100644
--- a/docs/pycon2010/pirate7.py
+++ b/docs/pycon2010/pirate7.py
@@ -1,28 +1,36 @@
+# coding=utf-8
from cmd2 import Cmd
+
+
# prompts and defaults
class Pirate(Cmd):
gold = 3
prompt = 'arrr> '
+
def default(self, line):
print('What mean ye by "{0}"?'.format(line))
+
def do_loot(self, arg):
'Seize booty from a passing ship.'
self.gold += 1
+
def do_drink(self, arg):
'''Drown your sorrrows in rrrum.
-
- drink [n] - drink [n] barrel[s] o' rum.'''
+
+ drink [n] - drink [n] barrel[s] o' rum.'''
try:
self.gold -= int(arg)
except:
if arg:
print('''What's "{0}"? I'll take rrrum.'''.format(arg))
- self.gold -= 1
+ self.gold -= 1
+
def precmd(self, line):
self.initial_gold = self.gold
return line
- def postcmd(self, stop, line):
+
+ def postcmd(self, stop, line):
if self.gold != self.initial_gold:
print('Now we gots {0} doubloons'
.format(self.gold))
@@ -30,17 +38,21 @@ class Pirate(Cmd):
print("Off to debtorrr's prison.")
stop = True
return stop
+
def do_quit(self, arg):
print("Quiterrr!")
- return True
+ return True
+
default_to_shell = True
multilineCommands = ['sing']
terminators = Cmd.terminators + ['...']
songcolor = 'blue'
settable = Cmd.settable + 'songcolor Color to ``sing`` in (red/blue/green/cyan/magenta, bold, underline)'
Cmd.shortcuts.update({'~': 'sing'})
+
def do_sing(self, arg):
print(self.colorize(arg, self.songcolor))
+
pirate = Pirate()
pirate.cmdloop()
diff --git a/docs/pycon2010/pirate8.py b/docs/pycon2010/pirate8.py
index 3e80b241..55d6df5c 100644
--- a/docs/pycon2010/pirate8.py
+++ b/docs/pycon2010/pirate8.py
@@ -1,28 +1,36 @@
+# coding=utf-8
from cmd2 import Cmd, options, make_option
+
+
# prompts and defaults
class Pirate(Cmd):
gold = 3
prompt = 'arrr> '
+
def default(self, line):
print('What mean ye by "{0}"?'.format(line))
+
def do_loot(self, arg):
'Seize booty from a passing ship.'
self.gold += 1
+
def do_drink(self, arg):
'''Drown your sorrrows in rrrum.
-
- drink [n] - drink [n] barrel[s] o' rum.'''
+
+ drink [n] - drink [n] barrel[s] o' rum.'''
try:
self.gold -= int(arg)
except:
if arg:
print('''What's "{0}"? I'll take rrrum.'''.format(arg))
- self.gold -= 1
+ self.gold -= 1
+
def precmd(self, line):
self.initial_gold = self.gold
return line
- def postcmd(self, stop, line):
+
+ def postcmd(self, stop, line):
if self.gold != self.initial_gold:
print('Now we gots {0} doubloons'
.format(self.gold))
@@ -30,21 +38,25 @@ class Pirate(Cmd):
print("Off to debtorrr's prison.")
stop = True
return stop
+
def do_quit(self, arg):
print("Quiterrr!")
- return True
+ return True
+
default_to_shell = True
multilineCommands = ['sing']
terminators = Cmd.terminators + ['...']
songcolor = 'blue'
settable = Cmd.settable + 'songcolor Color to ``sing`` in (red/blue/green/cyan/magenta, bold, underline)'
Cmd.shortcuts.update({'~': 'sing'})
+
def do_sing(self, arg):
print(self.colorize(arg, self.songcolor))
+
@options([make_option('--ho', type='int', default=2,
help="How often to chant 'ho'"),
make_option('-c', '--commas',
- action="store_true",
+ action="store_true",
help="Intersperse commas")])
def do_yo(self, arg, opts):
chant = ['yo'] + ['ho'] * opts.ho
@@ -53,5 +65,6 @@ class Pirate(Cmd):
print('{0} and a bottle of {1}'
.format(chant, arg))
+
pirate = Pirate()
pirate.cmdloop()
diff --git a/docs/pycon2010/pycon2010.rst b/docs/pycon2010/pycon2010.rst
index 0b3b7a46..6c3af676 100644
--- a/docs/pycon2010/pycon2010.rst
+++ b/docs/pycon2010/pycon2010.rst
@@ -11,7 +11,7 @@ Web 2.0
.. image:: web-2-0-logos.gif
:height: 350px
-
+
But first...
============
@@ -20,10 +20,10 @@ But first...
.. image:: akkad.png
:height: 250px
-
+
Sargon the Great
Founder of Akkadian Empire
-
+
.. twenty-third century BC
In between
@@ -31,16 +31,16 @@ In between
.. image:: apple.jpg
:height: 250px
-
+
Command-Line Interface
- Unlike the Akkadian Empire,
+ Unlike the Akkadian Empire,
the CLI will never die.
Defining CLI
============
Also known as
-
+
- "Line-oriented command interpreter"
- "Command-line interface"
- "Shell"
@@ -85,24 +85,24 @@ Examples
.. image:: urwid.png
:height: 250px
-
+
Decide your priorities
======================
.. image:: strategy.png
:height: 350px
-
+
A ``cmd`` app: pirate.py
========================
::
from cmd import Cmd
-
+
class Pirate(Cmd):
pass
-
+
pirate = Pirate()
pirate.cmdloop()
@@ -114,9 +114,9 @@ Fundamental prrrinciple
=======================
.. class:: huge
-
- ``(Cmd) foo a b c``
-
+
+ ``(Cmd) foo a b c``
+
becomes
``self.do_foo('a b c')``
@@ -139,7 +139,7 @@ Fundamental prrrinciple
print('Now we gots {0} doubloons'
.format(self.gold))
-.. do_methods; more help
+.. do_methods; more help
Hooks
=====
@@ -163,16 +163,16 @@ Hooks: pirate3.py
'Seize booty from a passing ship.'
self.gold += 1
def do_drink(self, arg):
- 'Drown your sorrrows in rrrum.'
+ 'Drown your sorrrows in rrrum.'
self.gold -= 1
def precmd(self, line):
self.initial_gold = self.gold
return line
- def postcmd(self, stop, line):
+ def postcmd(self, stop, line):
if self.gold != self.initial_gold:
print('Now we gots {0} doubloons'
.format(self.gold))
-
+
Arguments: pirate4.py
=====================
@@ -180,22 +180,22 @@ Arguments: pirate4.py
def do_drink(self, arg):
'''Drown your sorrrows in rrrum.
-
- drink [n] - drink [n] barrel[s] o' rum.'''
+
+ drink [n] - drink [n] barrel[s] o' rum.'''
try:
self.gold -= int(arg)
except:
if arg:
print('''What's "{0}"? I'll take rrrum.'''
.format(arg))
- self.gold -= 1
-
+ self.gold -= 1
+
quitting: pirate5.py
====================
::
- def postcmd(self, stop, line):
+ def postcmd(self, stop, line):
if self.gold != self.initial_gold:
print('Now we gots {0} doubloons'
.format(self.gold))
@@ -205,7 +205,7 @@ quitting: pirate5.py
return stop
def do_quit(self, arg):
print("Quiterrr!")
- return True
+ return True
prompts, defaults: pirate6.py
=============================
@@ -227,7 +227,7 @@ Other CLI packages
* CMdO
* pycopia
* cmdlin
- * cmd2
+ * cmd2
Demo
====
@@ -258,7 +258,7 @@ Script files
Commands at invocation
-Output redirection
+Output redirection
Python
@@ -273,9 +273,9 @@ But wait, there's more
* Timing
* Echo
* Debug
-
+
Minor changes: pirate7.py
-=========================
+=========================
::
@@ -287,18 +287,18 @@ Minor changes: pirate7.py
Cmd.shortcuts.update({'~': 'sing'})
def do_sing(self, arg):
print(self.colorize(arg, self.songcolor))
-
+
Now how much would you pay?
===========================
options / flags
-Quiet (suppress feedback)
+Quiet (suppress feedback)
BASH-style ``select``
Parsing: terminators, suffixes
-
+
Options: pirate8.py
===================
@@ -307,13 +307,13 @@ Options: pirate8.py
@options([make_option('--ho', type='int', default=2,
help="How often to chant 'ho'"),
make_option('-c', '--commas',
- action="store_true",
+ action="store_true",
help="Intersperse commas")])
def do_yo(self, arg, opts):
chant = ['yo'] + ['ho'] * opts.ho
separator = ', ' if opts.commas else ' '
chant = separator.join(chant)
- print('{0} and a bottle of {1}'
+ print('{0} and a bottle of {1}'
.format(chant, arg))
Serious example: sqlpython
diff --git a/docs/pycon2010/schematic.py b/docs/pycon2010/schematic.py
deleted file mode 100644
index 80774859..00000000
--- a/docs/pycon2010/schematic.py
+++ /dev/null
@@ -1,32 +0,0 @@
-from turtle import *
-hideturtle()
-width(6)
-pensize = 10
-pu()
-goto(0,-400)
-
-def rectangle(x, y, _label):
- pu()
- seth(0)
- backward(x / 2)
- fontsize = 40
- pd()
- for i in range(2):
- forward(x)
- left(90)
- forward(y)
- left(90)
- pu()
- forward(x / 2)
- left(90)
- forward(y / 2 - fontsize)
- pd()
- write(_label, align='center', font=('Arial', fontsize, 'bold'))
-
-rectangle(800, 80, 'cmd')
-pu()
-forward(80)
-rectangle(200, 400, 'cmd2')
-
-while True:
- pass