|
1 | | -================== |
2 | | -Pythonic APIs |
3 | | -================== |
4 | | - |
5 | | -Consistency |
6 | | ------------- |
7 | | - |
8 | | -Python:: |
9 | | - |
10 | | - len(text) # string |
11 | | - len(rate) # array of floats |
12 | | - len(names) # list |
13 | | - |
14 | | -Java: |
15 | | - |
16 | | -.. code-block:: Java |
17 | | -
|
18 | | - texto.length() // String |
19 | | - pesos.length // array of floats |
20 | | - nomes.size() // ArrayList |
21 | | -
|
22 | | -
|
23 | | -``vector_v0.py`` |
24 | | ----------------- |
25 | | - |
26 | | -Importing:: |
27 | | - |
28 | | - >>> from examples.vector_v0 import Vector |
29 | | - |
30 | | -Tests with 2 dimensions:: |
31 | | - |
32 | | - >>> v1 = Vector([3, 4]) |
33 | | - >>> len(v1) |
34 | | - 2 |
35 | | - >>> abs(v1) |
36 | | - 5.0 |
37 | | - >>> v1 == Vector((3.0, 4.0)) |
38 | | - True |
39 | | - >>> x, y = v1 |
40 | | - >>> x, y |
41 | | - (3.0, 4.0) |
42 | | - >>> list(v1) |
43 | | - [3.0, 4.0] |
44 | | - |
45 | | -Tests with 3 dimensions:: |
46 | | - |
47 | | - >>> v1 = Vector([3, 4, 5]) |
48 | | - >>> x, y, z = v1 |
49 | | - >>> x, y, z |
50 | | - (3.0, 4.0, 5.0) |
51 | | - >>> abs(v1) # doctest:+ELLIPSIS |
52 | | - 7.071067811... |
53 | | - |
54 | | - |
55 | | -Tests with more dimensions:: |
56 | | - |
57 | | - >>> v7 = Vector(range(7)) |
58 | | - >>> tuple(v7) |
59 | | - (0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0) |
60 | | - >>> abs(v7) # doctest:+ELLIPSIS |
61 | | - 9.53939201... |
62 | | - |
63 | | - |
64 | | - |
65 | | -.. literalinclude:: ../examples/vector_v0.py |
66 | | - |
67 | | - |
68 | | -``vector_v1.py`` |
69 | | ----------------- |
70 | | - |
71 | | - |
72 | | -Importing:: |
73 | | - |
74 | | - >>> from examples.vector_v1 import Vector |
75 | | - |
76 | | - |
77 | | -Vector representations:: |
78 | | - |
79 | | - >>> Vector([3.1, 4.2]) |
80 | | - Vector([3.1, 4.2]) |
81 | | - >>> Vector(range(10)) |
82 | | - Vector([0.0, 1.0, 2.0, 3.0, 4.0, ...]) |
83 | | - >>> v3 = Vector([3, 4, 5]) |
84 | | - >>> v3 |
85 | | - Vector([3.0, 4.0, 5.0]) |
86 | | - >>> v3_clone = eval(repr(v3)) |
87 | | - >>> v3_clone == v3 |
88 | | - True |
89 | | - >>> print(v3_clone) |
90 | | - (3.0, 4.0, 5.0) |
91 | | - |
92 | | - |
93 | | -.. literalinclude:: ../examples/vector_v1.py |
94 | | - |
95 | | - |
96 | | -``vector_v2.py`` |
97 | | ----------------- |
98 | | - |
99 | | -Importing:: |
100 | | - |
101 | | - >>> from examples.vector_v2 import Vector |
102 | | - |
103 | | - |
104 | | -Emulating sequences:: |
105 | | - |
106 | | - >>> v = Vector([10, 20, 30, 40, 50]) |
107 | | - >>> v[0] |
108 | | - 10.0 |
109 | | - >>> v[-1] |
110 | | - 50.0 |
111 | | - >>> v[:3] |
112 | | - Vector([10.0, 20.0, 30.0]) |
113 | | - |
114 | | - |
115 | | -.. literalinclude:: ../examples/vector_v2.py |
116 | | - |
117 | | - |
118 | | -``vector_v3.py`` |
119 | | ----------------- |
120 | | - |
121 | | -Importing:: |
122 | | - |
123 | | - >>> from examples.vector_v3 import Vector |
| 1 | +.. Pythonic APIs documentation master file, created by |
| 2 | + sphinx-quickstart on Tue May 17 06:09:39 2016. |
| 3 | + You can adapt this file completely to your liking, but it should at least |
| 4 | + contain the root `toctree` directive. |
124 | 5 |
|
| 6 | +Welcome to Pythonic APIs's documentation! |
| 7 | +========================================= |
125 | 8 |
|
126 | | -Basic tests of operator ``*``:: |
| 9 | +Contents: |
127 | 10 |
|
128 | | - >>> v1 = Vector([1, 2, 3]) |
129 | | - >>> v1 * 10 |
130 | | - Vector([10.0, 20.0, 30.0]) |
| 11 | +.. toctree:: |
| 12 | + :maxdepth: 2 |
131 | 13 |
|
| 14 | + inspiration |
| 15 | + vector |
| 16 | + exercise_1 |
132 | 17 |
|
133 | | -Tests of ``*`` with unusual but valid operands:: |
134 | 18 |
|
135 | | - >>> v1 * True |
136 | | - Vector([1.0, 2.0, 3.0]) |
137 | | - >>> from fractions import Fraction |
138 | | - >>> v1 * Fraction(1, 3) # doctest:+ELLIPSIS |
139 | | - Vector([0.3333..., 0.6666..., 1.0]) |
140 | 19 |
|
141 | | -This version has a problem:: |
142 | 20 |
|
| 21 | +Indices and tables |
| 22 | +================== |
143 | 23 |
|
144 | | - >>> 10 * v1 |
145 | | - Traceback (most recent call last): |
146 | | - ... |
147 | | - TypeError: unsupported operand type(s) for *: 'int' and 'Vector' |
148 | | - |
149 | | - |
150 | | -.. literalinclude:: ../examples/vector_v3.py |
151 | | - |
152 | | - |
153 | | - |
154 | | -In Python, you can compute compound interest using a formula written like this:: |
155 | | - |
156 | | - interest = principal * ((1 + rate) ** periods - 1) |
157 | | - |
158 | | -The Python formula works with any numeric type that overloads the arithmetic operators. |
159 | | - |
160 | | -In Java, only the primitive types support the arithmentic operators. If a financial app needs to use BigDecimal for enhanced precision, the compound interest formula has to be coded with method calls, like this: |
161 | | - |
162 | | -.. code-block:: Java |
163 | | -
|
164 | | - interest = principal.multiply(BigDecimal.ONE.add(rate).pow(periods).subtract(BigDecimal.ONE)); |
165 | | -
|
166 | | -
|
167 | | -.. literalinclude:: ../examples/vector_v3.py |
168 | | - |
169 | | - |
170 | | -``vector_v4.py`` |
171 | | ----------------- |
172 | | - |
173 | | -Importing:: |
174 | | - |
175 | | - >>> from examples.vector_v4 import Vector |
176 | | - |
177 | | - |
178 | | -The reversed operator:: |
179 | | - |
180 | | - >>> v1 = Vector([1, 2, 3]) |
181 | | - >>> 10 * v1 |
182 | | - Vector([10.0, 20.0, 30.0]) |
183 | | - |
184 | | - |
185 | | -.. literalinclude:: ../examples/vector_v4.py |
186 | | - |
187 | | - |
188 | | -``vector_v5.py`` |
189 | | ----------------- |
190 | | - |
191 | | -Importing:: |
192 | | - |
193 | | - >>> from examples.vector_v5 import Vector |
194 | | - |
195 | | -Tests for operator `@` (Python >= 3.5), computing the dot product:: |
196 | | - |
197 | | - >>> va = Vector([1, 2, 3]) |
198 | | - >>> vz = Vector([5, 6, 7]) |
199 | | - >>> va @ vz == 38.0 # 1*5 + 2*6 + 3*7 |
200 | | - True |
201 | | - >>> [10, 20, 30] @ vz |
202 | | - 380.0 |
203 | | - >>> va @ 3 |
204 | | - Traceback (most recent call last): |
205 | | - ... |
206 | | - TypeError: unsupported operand type(s) for @: 'Vector' and 'int' |
207 | | - |
208 | | -.. literalinclude:: ../examples/vector_v5.py |
209 | | - |
210 | | - |
211 | | -``vector_v5.py`` |
212 | | ----------------- |
213 | | - |
214 | | -Importing:: |
215 | | - |
216 | | - >>> from examples.vector_v5 import Vector |
217 | | - |
218 | | -Tests for operator `@` (Python >= 3.5), computing the dot product:: |
219 | | - |
220 | | - >>> va = Vector([1, 2, 3]) |
221 | | - >>> vz = Vector([5, 6, 7]) |
222 | | - >>> va @ vz == 38.0 # 1*5 + 2*6 + 3*7 |
223 | | - True |
224 | | - >>> [10, 20, 30] @ vz |
225 | | - 380.0 |
226 | | - >>> va @ 3 |
227 | | - Traceback (most recent call last): |
228 | | - ... |
229 | | - TypeError: unsupported operand type(s) for @: 'Vector' and 'int' |
230 | | - |
231 | | -.. literalinclude:: ../examples/vector_v5.py |
232 | | - |
233 | | - |
234 | | - |
235 | | -``vector_v6.py`` |
236 | | ----------------- |
237 | | - |
238 | | -Importing:: |
239 | | - |
240 | | - >>> from examples.vector_v6 import Vector |
241 | | - |
242 | | -Tests of hashing:: |
243 | | - |
244 | | - >>> v1 = Vector([3, 4]) |
245 | | - >>> hash(v1) == 3 ^ 4 |
246 | | - True |
247 | | - >>> v3 = Vector([3, 4, 5]) |
248 | | - >>> hash(v3) == 3 ^ 4 ^ 5 |
249 | | - True |
250 | | - >>> v6 = Vector(range(6)) |
251 | | - >>> hash(v6) == 0 ^ 1 ^ 2 ^ 3 ^ 4 ^ 5 |
252 | | - True |
253 | | - >>> v2 = Vector([3.1, 4.2]) |
254 | | - >>> hash(v2) == hash(3.1) ^ hash(4.2) |
255 | | - True |
256 | | - >>> {v1, v2, v3, v6} |
257 | | - {Vector([0.0, 1.0, 2.0, 3.0, 4.0, ...]), Vector([3.0, 4.0, 5.0]), Vector([3.0, 4.0]), Vector([3.1, 4.2])} |
258 | | - |
259 | | -Most hash values of non-integers vary from a 32-bit to 64-bit CPython build:: |
260 | | - |
261 | | - >>> import sys |
262 | | - >>> hash(v2) == (384307168202284039 if sys.maxsize > 2**32 else 357915986) |
263 | | - True |
264 | | - |
| 24 | +* :ref:`genindex` |
| 25 | +* :ref:`modindex` |
| 26 | +* :ref:`search` |
265 | 27 |
|
266 | | -.. literalinclude:: ../examples/vector_v6.py |
0 commit comments