Skip to content

Commit 82d83ac

Browse files
committed
first exercise
1 parent 0fa487f commit 82d83ac

File tree

2 files changed

+318
-11
lines changed

2 files changed

+318
-11
lines changed

examples/vector0.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'''
2+
Vector: classe vetor euclideano
3+
4+
>>> v1 = Vector([1, 2])
5+
>>> v1
6+
Vector([1.0, 2.0])
7+
8+
'''
9+
10+
from array import array
11+
import math
12+
13+
class Vector:
14+
15+
typecode = 'd'
16+
17+
def __init__(self, components):
18+
self._components = array(self.typecode, components)
19+
20+
def __len__(self):
21+
return len(self._components)
22+
23+
def __iter__(self):
24+
return iter(self._components)
25+
26+
def __abs__(self):
27+
return math.sqrt(sum(x * x for x in self))
28+
29+
def __eq__(self, other):
30+
return (len(self) == len(other) and
31+
all(a == b for a, b in zip(self, other)))
32+
33+
def __repr__(self):
34+
return 'Vector({})'.format(list(self._components))

pythonic-api-notebook.ipynb

Lines changed: 284 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,76 @@
2626
"* Wrap-up\n"
2727
]
2828
},
29+
{
30+
"cell_type": "markdown",
31+
"metadata": {},
32+
"source": [
33+
"## What is *Pythonic*?\n",
34+
"\n",
35+
"Pythonic code is concise and expressive. It leverages Python features and idioms to acomplish maximum effect with minimum effort, without being unreadable. It uses the language as it's designed to be used, so it is most readable to the fluent Pythonista."
36+
]
37+
},
38+
{
39+
"cell_type": "markdown",
40+
"metadata": {},
41+
"source": [
42+
"### Real example 1: the `requests` API\n",
43+
"\n",
44+
"`requests` is pleasant HTTP client library. It's great but it would be awesome if it was asynchronous (but could it be pleasant **and** asynchronous at the same time?). The examples below are from Kenneth Reitz, the author of `requests` ([source](https://gist.github.com/kennethreitz/973705)).\n",
45+
"\n",
46+
"#### Pythonic, using `requests`\n",
47+
"\n",
48+
"```python\n",
49+
"import requests\n",
50+
"\n",
51+
"r = requests.get('https://api.github.com', auth=('user', 'pass'))\n",
52+
"\n",
53+
"print r.status_code\n",
54+
"print r.headers['content-type']\n",
55+
"\n",
56+
"# ------\n",
57+
"# 200\n",
58+
"# 'application/json'\n",
59+
"```\n",
60+
"\n",
61+
"#### Unpythonic, using `urllib2`\n",
62+
"\n",
63+
"```python\n",
64+
"import urllib2\n",
65+
"\n",
66+
"gh_url = 'https://api.github.com'\n",
67+
"\n",
68+
"req = urllib2.Request(gh_url)\n",
69+
"\n",
70+
"password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm()\n",
71+
"password_manager.add_password(None, gh_url, 'user', 'pass')\n",
72+
"\n",
73+
"auth_manager = urllib2.HTTPBasicAuthHandler(password_manager)\n",
74+
"opener = urllib2.build_opener(auth_manager)\n",
75+
"\n",
76+
"urllib2.install_opener(opener)\n",
77+
"\n",
78+
"handler = urllib2.urlopen(req)\n",
79+
"\n",
80+
"print handler.getcode()\n",
81+
"print handler.headers.getheader('content-type')\n",
82+
"\n",
83+
"# ------\n",
84+
"# 200\n",
85+
"# 'application/json'\n",
86+
"\n",
87+
"```\n"
88+
]
89+
},
90+
{
91+
"cell_type": "code",
92+
"execution_count": null,
93+
"metadata": {
94+
"collapsed": true
95+
},
96+
"outputs": [],
97+
"source": []
98+
},
2999
{
30100
"cell_type": "markdown",
31101
"metadata": {},
@@ -39,7 +109,7 @@
39109
},
40110
{
41111
"cell_type": "code",
42-
"execution_count": 9,
112+
"execution_count": 19,
43113
"metadata": {
44114
"collapsed": false
45115
},
@@ -49,26 +119,128 @@
49119
"output_type": "stream",
50120
"text": [
51121
"['F', 'l', 'u', 'e', 'n', 't']\n",
52-
"(10, 20, 30)\n",
53-
"10\n",
54-
"20\n",
55-
"30\n",
56-
"40\n",
57-
"50\n"
122+
"(10, 20, 50)\n",
123+
"10 20 30 40 50 "
58124
]
59125
}
60126
],
61127
"source": [
62128
"s = 'Fluent'\n",
63-
"l = [10, 20, 30, 40, 50]\n",
129+
"L = [10, 20, 30, 40, 50]\n",
64130
"\n",
65131
"print(list(s)) # list constructor iterates over its argument\n",
66132
"\n",
67-
"a, b, c, *rest = l # tuple unpacking iterates over right side\n",
133+
"a, b, *middle, c = L # tuple unpacking iterates over right side\n",
68134
"print((a, b, c))\n",
69135
"\n",
70-
"for i in l:\n",
71-
" print(i)"
136+
"for i in L:\n",
137+
" print(i, end=' ')"
138+
]
139+
},
140+
{
141+
"cell_type": "markdown",
142+
"metadata": {},
143+
"source": [
144+
"## Sizing with `len()`"
145+
]
146+
},
147+
{
148+
"cell_type": "code",
149+
"execution_count": 3,
150+
"metadata": {
151+
"collapsed": false
152+
},
153+
"outputs": [
154+
{
155+
"data": {
156+
"text/plain": [
157+
"(6, 5)"
158+
]
159+
},
160+
"execution_count": 3,
161+
"metadata": {},
162+
"output_type": "execute_result"
163+
}
164+
],
165+
"source": [
166+
"len(s), len(L)"
167+
]
168+
},
169+
{
170+
"cell_type": "code",
171+
"execution_count": 4,
172+
"metadata": {
173+
"collapsed": false
174+
},
175+
"outputs": [
176+
{
177+
"data": {
178+
"text/plain": [
179+
"(6, 5)"
180+
]
181+
},
182+
"execution_count": 4,
183+
"metadata": {},
184+
"output_type": "execute_result"
185+
}
186+
],
187+
"source": [
188+
"s.__len__(), L.__len__()"
189+
]
190+
},
191+
{
192+
"cell_type": "markdown",
193+
"metadata": {},
194+
"source": [
195+
"### Arithmetic"
196+
]
197+
},
198+
{
199+
"cell_type": "code",
200+
"execution_count": 10,
201+
"metadata": {
202+
"collapsed": false
203+
},
204+
"outputs": [
205+
{
206+
"data": {
207+
"text/plain": [
208+
"(6, 6)"
209+
]
210+
},
211+
"execution_count": 10,
212+
"metadata": {},
213+
"output_type": "execute_result"
214+
}
215+
],
216+
"source": [
217+
"a = 2\n",
218+
"b = 3\n",
219+
"a * b, a.__mul__(b)"
220+
]
221+
},
222+
{
223+
"cell_type": "code",
224+
"execution_count": 24,
225+
"metadata": {
226+
"collapsed": false
227+
},
228+
"outputs": [
229+
{
230+
"data": {
231+
"text/plain": [
232+
"[1, 2, 3, [...]]"
233+
]
234+
},
235+
"execution_count": 24,
236+
"metadata": {},
237+
"output_type": "execute_result"
238+
}
239+
],
240+
"source": [
241+
"L = [1, 2, 3]\n",
242+
"L.append(L)\n",
243+
"L"
72244
]
73245
},
74246
{
@@ -81,6 +253,107 @@
81253
{
82254
"cell_type": "markdown",
83255
"metadata": {},
256+
"source": [
257+
"## String formatting mini-language"
258+
]
259+
},
260+
{
261+
"cell_type": "code",
262+
"execution_count": 27,
263+
"metadata": {
264+
"collapsed": false
265+
},
266+
"outputs": [
267+
{
268+
"data": {
269+
"text/plain": [
270+
"1.4142135623730951"
271+
]
272+
},
273+
"execution_count": 27,
274+
"metadata": {},
275+
"output_type": "execute_result"
276+
}
277+
],
278+
"source": [
279+
"x = 2**.5\n",
280+
"x"
281+
]
282+
},
283+
{
284+
"cell_type": "code",
285+
"execution_count": 29,
286+
"metadata": {
287+
"collapsed": false
288+
},
289+
"outputs": [
290+
{
291+
"data": {
292+
"text/plain": [
293+
"'1.414'"
294+
]
295+
},
296+
"execution_count": 29,
297+
"metadata": {},
298+
"output_type": "execute_result"
299+
}
300+
],
301+
"source": [
302+
"format(x, '.3f')"
303+
]
304+
},
305+
{
306+
"cell_type": "code",
307+
"execution_count": 31,
308+
"metadata": {
309+
"collapsed": false
310+
},
311+
"outputs": [
312+
{
313+
"name": "stdout",
314+
"output_type": "stream",
315+
"text": [
316+
"2016-05-11 20:53:37.210333\n",
317+
"20:53\n"
318+
]
319+
}
320+
],
321+
"source": [
322+
"from datetime import datetime\n",
323+
"agora = datetime.now()\n",
324+
"print(agora)\n",
325+
"print(format(agora, '%H:%M'))"
326+
]
327+
},
328+
{
329+
"cell_type": "code",
330+
"execution_count": 34,
331+
"metadata": {
332+
"collapsed": false
333+
},
334+
"outputs": [
335+
{
336+
"data": {
337+
"text/plain": [
338+
"'20... 1.414!'"
339+
]
340+
},
341+
"execution_count": 34,
342+
"metadata": {},
343+
"output_type": "execute_result"
344+
}
345+
],
346+
"source": [
347+
"'{1:%H}... {0:.3f}!'.format(x, agora)"
348+
]
349+
},
350+
{
351+
"cell_type": "code",
352+
"execution_count": null,
353+
"metadata": {
354+
"collapsed": true
355+
},
356+
"outputs": [],
84357
"source": []
85358
}
86359
],

0 commit comments

Comments
 (0)