annotate Demo/classes/Vec.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents b8737a31050c
children 7d16b42fcdbd 3355356f1c74
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
945
465b9841770e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 # A simple vector class
465b9841770e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
465b9841770e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
465b9841770e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 def vec(*v):
30407
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
5 return Vec(*v)
945
465b9841770e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
465b9841770e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
7
465b9841770e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 class Vec:
465b9841770e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
9
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 1427
diff changeset
10 def __init__(self, *v):
30407
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
11 self.v = list(v)
945
465b9841770e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
12
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 1427
diff changeset
13 def fromlist(self, v):
30407
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
14 if not isinstance(v, list):
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 1427
diff changeset
15 raise TypeError
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 1427
diff changeset
16 self.v = v[:]
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 1427
diff changeset
17 return self
945
465b9841770e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
18
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 1427
diff changeset
19 def __repr__(self):
30407
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
20 return 'vec(' + repr(self.v)[1:-1] + ')'
945
465b9841770e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
21
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 1427
diff changeset
22 def __len__(self):
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 1427
diff changeset
23 return len(self.v)
945
465b9841770e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
24
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 1427
diff changeset
25 def __getitem__(self, i):
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 1427
diff changeset
26 return self.v[i]
945
465b9841770e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
27
30407
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
28 def __add__(self, other):
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 1427
diff changeset
29 # Element-wise addition
30407
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
30 v = map(lambda x, y: x+y, self, other)
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 1427
diff changeset
31 return Vec().fromlist(v)
945
465b9841770e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
32
30407
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
33 def __sub__(self, other):
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 1427
diff changeset
34 # Element-wise subtraction
30407
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
35 v = map(lambda x, y: x-y, self, other)
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 1427
diff changeset
36 return Vec().fromlist(v)
945
465b9841770e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
37
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 1427
diff changeset
38 def __mul__(self, scalar):
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 1427
diff changeset
39 # Multiply by scalar
30407
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
40 v = map(lambda x: x*scalar, self.v)
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 1427
diff changeset
41 return Vec().fromlist(v)
945
465b9841770e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
42
465b9841770e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
43
465b9841770e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
44
465b9841770e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 def test():
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 1427
diff changeset
46 a = vec(1, 2, 3)
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 1427
diff changeset
47 b = vec(3, 2, 1)
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 1427
diff changeset
48 print a
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 1427
diff changeset
49 print b
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 1427
diff changeset
50 print a+b
30407
b8737a31050c SF patch #803449: modernize demo scripts
Raymond Hettinger <python@rcn.com>
parents: 28788
diff changeset
51 print a-b
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 1427
diff changeset
52 print a*3.0
945
465b9841770e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
53
465b9841770e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 test()