Mercurial > cpython-lru_cache-weakref
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 |
| rev | line source |
|---|---|
| 945 | 1 # A simple vector class |
| 2 | |
| 3 | |
| 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 | 6 |
| 7 | |
| 8 class Vec: | |
| 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 | 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 | 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 | 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 | 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 | 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 | 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 | 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 | 42 |
| 43 | |
| 44 | |
| 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 | 53 |
| 54 test() |
