]> git.proxmox.com Git - mirror_edk2.git/blame - AppPkg/Applications/Python/Python-2.7.2/Demo/classes/Vec.py
EmbeddedPkg: Extend NvVarStoreFormattedLib LIBRARY_CLASS
[mirror_edk2.git] / AppPkg / Applications / Python / Python-2.7.2 / Demo / classes / Vec.py
CommitLineData
4710c53d 1class Vec:\r
2 """ A simple vector class\r
3\r
4 Instances of the Vec class can be constructed from numbers\r
5\r
6 >>> a = Vec(1, 2, 3)\r
7 >>> b = Vec(3, 2, 1)\r
8\r
9 added\r
10 >>> a + b\r
11 Vec(4, 4, 4)\r
12\r
13 subtracted\r
14 >>> a - b\r
15 Vec(-2, 0, 2)\r
16\r
17 and multiplied by a scalar on the left\r
18 >>> 3.0 * a\r
19 Vec(3.0, 6.0, 9.0)\r
20\r
21 or on the right\r
22 >>> a * 3.0\r
23 Vec(3.0, 6.0, 9.0)\r
24 """\r
25 def __init__(self, *v):\r
26 self.v = list(v)\r
27\r
28 @classmethod\r
29 def fromlist(cls, v):\r
30 if not isinstance(v, list):\r
31 raise TypeError\r
32 inst = cls()\r
33 inst.v = v\r
34 return inst\r
35\r
36 def __repr__(self):\r
37 args = ', '.join(repr(x) for x in self.v)\r
38 return 'Vec({0})'.format(args)\r
39\r
40 def __len__(self):\r
41 return len(self.v)\r
42\r
43 def __getitem__(self, i):\r
44 return self.v[i]\r
45\r
46 def __add__(self, other):\r
47 # Element-wise addition\r
48 v = [x + y for x, y in zip(self.v, other.v)]\r
49 return Vec.fromlist(v)\r
50\r
51 def __sub__(self, other):\r
52 # Element-wise subtraction\r
53 v = [x - y for x, y in zip(self.v, other.v)]\r
54 return Vec.fromlist(v)\r
55\r
56 def __mul__(self, scalar):\r
57 # Multiply by scalar\r
58 v = [x * scalar for x in self.v]\r
59 return Vec.fromlist(v)\r
60\r
61 __rmul__ = __mul__\r
62\r
63\r
64def test():\r
65 import doctest\r
66 doctest.testmod()\r
67\r
68test()\r