Skip to content

Commit 920f62a

Browse files
committed
first exercise set
1 parent 3271222 commit 920f62a

File tree

8 files changed

+459
-258
lines changed

8 files changed

+459
-258
lines changed

docs/exercise_1.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
=============================
2+
Exercise #1: Vector addition
3+
=============================
4+
5+
A. Preparation
6+
---------------
7+
8+
1. Install ``py.test``:: ``pip install pytest`
9+
10+
2. Clone or download repo at https://github.com/fluentpython/pythonic-api
11+
12+
13+
B. Vector unary ``-``
14+
---------------------
15+
16+
1. Go to ``exercises/vector`` directory
17+
18+
2. Run the tests: ``py.test`
19+
20+
3. Open the ``vector/vector.py``.

docs/index.rst

Lines changed: 17 additions & 256 deletions
Original file line numberDiff line numberDiff line change
@@ -1,266 +1,27 @@
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.
1245
6+
Welcome to Pythonic APIs's documentation!
7+
=========================================
1258

126-
Basic tests of operator ``*``::
9+
Contents:
12710

128-
>>> v1 = Vector([1, 2, 3])
129-
>>> v1 * 10
130-
Vector([10.0, 20.0, 30.0])
11+
.. toctree::
12+
:maxdepth: 2
13113

14+
inspiration
15+
vector
16+
exercise_1
13217

133-
Tests of ``*`` with unusual but valid operands::
13418

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])
14019

141-
This version has a problem::
14220

21+
Indices and tables
22+
==================
14323

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`
26527

266-
.. literalinclude:: ../examples/vector_v6.py

docs/inspiration.rst

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
=============
2+
Inspiration
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+
The ``requests`` library
24+
------------------------
25+
26+
Using ``requests``::
27+
28+
import requests
29+
30+
r = requests.get('https://api.github.com', auth=('user', 'pass'))
31+
32+
print r.status_code
33+
print r.headers['content-type']
34+
35+
# ------
36+
# 200
37+
# 'application/json'
38+
39+
40+
Using ``urllib2``::
41+
42+
import urllib2
43+
44+
gh_url = 'https://api.github.com'
45+
46+
req = urllib2.Request(gh_url)
47+
48+
password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
49+
password_manager.add_password(None, gh_url, 'user', 'pass')
50+
51+
auth_manager = urllib2.HTTPBasicAuthHandler(password_manager)
52+
opener = urllib2.build_opener(auth_manager)
53+
54+
urllib2.install_opener(opener)
55+
56+
handler = urllib2.urlopen(req)
57+
58+
print handler.getcode()
59+
print handler.headers.getheader('content-type')
60+
61+
# ------
62+
# 200
63+
# 'application/json'

0 commit comments

Comments
 (0)