Skip to content

Commit 856898b

Browse files
committed
Harmonize docstrings. Move redemo from Tools/scripts to Tools/demo. Add a README file to Tools/demo.
1 parent a3fe8e0 commit 856898b

15 files changed

Lines changed: 196 additions & 151 deletions

File tree

Tools/demo/README

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
This directory contains a collection of demonstration scripts for
2+
various aspects of Python programming.
3+
4+
beer.py Well-known programming example: Bottles of beer.
5+
eiffel.py Python advanced magic: A metaclass for Eiffel post/preconditions.
6+
hanoi.py Well-known programming example: Towers of Hanoi.
7+
life.py Curses programming: Simple game-of-life.
8+
markov.py Algorithms: Markov chain simulation.
9+
mcast.py Network programming: Send and receive UDP multicast packets.
10+
queens.py Well-known programming example: N-Queens problem.
11+
redemo.py Regular Expressions: GUI script to test regexes.
12+
rpython.py Network programming: Small client for remote code execution.
13+
rpythond.py Network programming: Small server for remote code execution.
14+
sortvisu.py GUI programming: Visualization of different sort algorithms.
15+
ss1.py GUI/Application programming: A simple spreadsheet application.
16+
vector.py Python basics: A vector class with demonstrating special methods.

Tools/demo/beer.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
#! /usr/bin/env python3
1+
#!/usr/bin/env python3
22

3-
# By GvR, demystified after a version by Fredrik Lundh.
3+
"""
4+
A Python version of the classic "bottles of beer on the wall" programming
5+
example.
6+
7+
By Guido van Rossum, demystified after a version by Fredrik Lundh.
8+
"""
49

510
import sys
611

Lines changed: 65 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
1-
"""Support Eiffel-style preconditions and postconditions."""
1+
#!/usr/bin/env python3
22

3+
"""
4+
Support Eiffel-style preconditions and postconditions for functions.
5+
6+
An example for Python metaclasses.
7+
"""
8+
9+
import unittest
310
from types import FunctionType as function
411

512
class EiffelBaseMetaClass(type):
613

714
def __new__(meta, name, bases, dict):
815
meta.convert_methods(dict)
9-
return super(EiffelBaseMetaClass, meta).__new__(meta, name, bases,
10-
dict)
16+
return super(EiffelBaseMetaClass, meta).__new__(
17+
meta, name, bases, dict)
1118

1219
@classmethod
1320
def convert_methods(cls, dict):
@@ -31,6 +38,7 @@ def convert_methods(cls, dict):
3138
if pre or post:
3239
dict[k] = cls.make_eiffel_method(dict[m], pre, post)
3340

41+
3442
class EiffelMetaClass1(EiffelBaseMetaClass):
3543
# an implementation of the "eiffel" meta class that uses nested functions
3644

@@ -39,16 +47,17 @@ def make_eiffel_method(func, pre, post):
3947
def method(self, *args, **kwargs):
4048
if pre:
4149
pre(self, *args, **kwargs)
42-
x = func(self, *args, **kwargs)
50+
rv = func(self, *args, **kwargs)
4351
if post:
44-
post(self, x, *args, **kwargs)
45-
return x
52+
post(self, rv, *args, **kwargs)
53+
return rv
4654

4755
if func.__doc__:
4856
method.__doc__ = func.__doc__
4957

5058
return method
5159

60+
5261
class EiffelMethodWrapper:
5362

5463
def __init__(self, inst, descr):
@@ -58,7 +67,8 @@ def __init__(self, inst, descr):
5867
def __call__(self, *args, **kwargs):
5968
return self._descr.callmethod(self._inst, args, kwargs)
6069

61-
class EiffelDescriptor(object):
70+
71+
class EiffelDescriptor:
6272

6373
def __init__(self, func, pre, post):
6474
self._func = func
@@ -79,63 +89,58 @@ def callmethod(self, inst, args, kwargs):
7989
self._post(inst, x, *args, **kwargs)
8090
return x
8191

92+
8293
class EiffelMetaClass2(EiffelBaseMetaClass):
8394
# an implementation of the "eiffel" meta class that uses descriptors
8495

8596
make_eiffel_method = EiffelDescriptor
8697

87-
def _test(metaclass):
88-
class Eiffel(metaclass=metaclass):
89-
pass
90-
91-
class Test(Eiffel):
92-
93-
def m(self, arg):
94-
"""Make it a little larger"""
95-
return arg + 1
96-
97-
def m2(self, arg):
98-
"""Make it a little larger"""
99-
return arg + 1
100-
101-
def m2_pre(self, arg):
102-
assert arg > 0
103-
104-
def m2_post(self, result, arg):
105-
assert result > arg
106-
107-
class Sub(Test):
108-
def m2(self, arg):
109-
return arg**2
110-
def m2_post(self, Result, arg):
111-
super(Sub, self).m2_post(Result, arg)
112-
assert Result < 100
113-
114-
t = Test()
115-
t.m(1)
116-
t.m2(1)
117-
try:
118-
t.m2(0)
119-
except AssertionError:
120-
pass
121-
else:
122-
assert False
123-
124-
s = Sub()
125-
try:
126-
s.m2(1)
127-
except AssertionError:
128-
pass # result == arg
129-
else:
130-
assert False
131-
try:
132-
s.m2(10)
133-
except AssertionError:
134-
pass # result == 100
135-
else:
136-
assert False
137-
s.m2(5)
98+
99+
class Tests(unittest.TestCase):
100+
101+
def testEiffelMetaClass1(self):
102+
self._test(EiffelMetaClass1)
103+
104+
def testEiffelMetaClass2(self):
105+
self._test(EiffelMetaClass2)
106+
107+
def _test(self, metaclass):
108+
class Eiffel(metaclass=metaclass):
109+
pass
110+
111+
class Test(Eiffel):
112+
def m(self, arg):
113+
"""Make it a little larger"""
114+
return arg + 1
115+
116+
def m2(self, arg):
117+
"""Make it a little larger"""
118+
return arg + 1
119+
120+
def m2_pre(self, arg):
121+
assert arg > 0
122+
123+
def m2_post(self, result, arg):
124+
assert result > arg
125+
126+
class Sub(Test):
127+
def m2(self, arg):
128+
return arg**2
129+
130+
def m2_post(self, Result, arg):
131+
super(Sub, self).m2_post(Result, arg)
132+
assert Result < 100
133+
134+
t = Test()
135+
self.assertEqual(t.m(1), 2)
136+
self.assertEqual(t.m2(1), 2)
137+
self.assertRaises(AssertionError, t.m2, 0)
138+
139+
s = Sub()
140+
self.assertRaises(AssertionError, s.m2, 1)
141+
self.assertRaises(AssertionError, s.m2, 10)
142+
self.assertEqual(s.m2(5), 25)
143+
138144

139145
if __name__ == "__main__":
140-
_test(EiffelMetaClass1)
141-
_test(EiffelMetaClass2)
146+
unittest.main()

Tools/demo/hanoi.py

100644100755
Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
# Animated Towers of Hanoi using Tk with optional bitmap file in
2-
# background.
3-
#
4-
# Usage: tkhanoi [n [bitmapfile]]
5-
#
6-
# n is the number of pieces to animate; default is 4, maximum 15.
7-
#
8-
# The bitmap file can be any X11 bitmap file (look in
9-
# /usr/include/X11/bitmaps for samples); it is displayed as the
10-
# background of the animation. Default is no bitmap.
1+
#!/usr/bin/env python3
112

12-
# This uses Steen Lumholt's Tk interface
13-
from tkinter import *
3+
"""
4+
Animated Towers of Hanoi using Tk with optional bitmap file in background.
145
6+
Usage: hanoi.py [n [bitmapfile]]
7+
8+
n is the number of pieces to animate; default is 4, maximum 15.
9+
10+
The bitmap file can be any X11 bitmap file (look in /usr/include/X11/bitmaps for
11+
samples); it is displayed as the background of the animation. Default is no
12+
bitmap.
13+
"""
14+
15+
from tkinter import Tk, Canvas
1516

1617
# Basic Towers-of-Hanoi algorithm: move n pieces from a to b, using c
1718
# as temporary. For each move, call report()
@@ -123,7 +124,6 @@ def report(self, i, a, b):
123124
self.pegstate[b].append(i)
124125

125126

126-
# Main program
127127
def main():
128128
import sys
129129

0 commit comments

Comments
 (0)