Skip to content

Commit a6c0b6a

Browse files
committed
BangPypers session
1 parent c492f5b commit a6c0b6a

35 files changed

+11395
-0
lines changed

pygotcha2015/.ipynb_checkpoints/Class_Gotchas-checkpoint.ipynb

Lines changed: 938 additions & 0 deletions
Large diffs are not rendered by default.

pygotcha2015/.ipynb_checkpoints/DataStructures_Types_Gotcha-checkpoint.ipynb

Lines changed: 1723 additions & 0 deletions
Large diffs are not rendered by default.

pygotcha2015/.ipynb_checkpoints/Function_Gotchas-checkpoint.ipynb

Lines changed: 924 additions & 0 deletions
Large diffs are not rendered by default.

pygotcha2015/.ipynb_checkpoints/Miscellaneous_Gotchas-checkpoint.ipynb

Lines changed: 1053 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 375 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,375 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"###### Copyright © Anand B Pillai, Anvetsu Technologies Pvt. Ltd (2015)"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"# Modules & Packages"
15+
]
16+
},
17+
{
18+
"cell_type": "markdown",
19+
"metadata": {},
20+
"source": [
21+
"## 1. Forgetting ______init______.py in package folders"
22+
]
23+
},
24+
{
25+
"cell_type": "markdown",
26+
"metadata": {},
27+
"source": [
28+
"### 1.1 Show me the Code !"
29+
]
30+
},
31+
{
32+
"cell_type": "code",
33+
"execution_count": 7,
34+
"metadata": {},
35+
"outputs": [
36+
{
37+
"ename": "ImportError",
38+
"evalue": "No module named pygotcha",
39+
"output_type": "error",
40+
"traceback": [
41+
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mImportError\u001b[0m Traceback (most recent call last)",
42+
"\u001b[1;32m<ipython-input-7-baa2c36fc908>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mpygotcha\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mreload\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpygotcha\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 3\u001b[0m \u001b[0mdir\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpygotcha\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
43+
"\u001b[1;31mImportError\u001b[0m: No module named pygotcha"
44+
]
45+
}
46+
],
47+
"source": [
48+
"import pygotcha\n",
49+
"\n",
50+
"dir(pygotcha)"
51+
]
52+
},
53+
{
54+
"cell_type": "markdown",
55+
"metadata": {},
56+
"source": [
57+
"## Gotcha !!!\n",
58+
"\n",
59+
"#### This is because,\n",
60+
"\n",
61+
" 1. To convert your folder into a package which can be imported as a module in Python, it needs to contain a filed named ________init________.py . You cannot simply import a folder containing Python code as a module.\n",
62+
" 1. The ________init________.py can be an empty file."
63+
]
64+
},
65+
{
66+
"cell_type": "markdown",
67+
"metadata": {},
68+
"source": [
69+
"### 1.2 Show me the Fix !"
70+
]
71+
},
72+
{
73+
"cell_type": "markdown",
74+
"metadata": {},
75+
"source": [
76+
"#### Just create an empty file named ________init________.py in the folder you want to import as a module in Python."
77+
]
78+
},
79+
{
80+
"cell_type": "markdown",
81+
"metadata": {},
82+
"source": [
83+
"## 2. Silent overwrite of module (or keyword) names"
84+
]
85+
},
86+
{
87+
"cell_type": "markdown",
88+
"metadata": {},
89+
"source": [
90+
"#### A very common mistake done by Python programmers. This one trips up even experienced programmers if you are not careful in naming your variables."
91+
]
92+
},
93+
{
94+
"cell_type": "code",
95+
"execution_count": 11,
96+
"metadata": {},
97+
"outputs": [
98+
{
99+
"name": "stdout",
100+
"output_type": "stream",
101+
"text": [
102+
"Folder contents => ['Class_Gotchas.ipynb', 'Function_Gotchas.ipynb', '.ipynb_checkpoints', 'Scoping_Gotcha.ipynb', 'DataStructures_Types_Gotcha.ipynb', 'Module_Gotchas.ipynb', 'pygotcha']\n"
103+
]
104+
},
105+
{
106+
"ename": "AttributeError",
107+
"evalue": "'str' object has no attribute 'listdir'",
108+
"output_type": "error",
109+
"traceback": [
110+
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)",
111+
"\u001b[1;32m<ipython-input-11-70fbf06dccd5>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 13\u001b[0m \u001b[1;31m# After a few lines ...\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 14\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 15\u001b[1;33m \u001b[0mos\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlistdir\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"/tmp\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
112+
"\u001b[1;31mAttributeError\u001b[0m: 'str' object has no attribute 'listdir'"
113+
]
114+
}
115+
],
116+
"source": [
117+
"import os\n",
118+
"\n",
119+
"print \"Folder contents =>\", os.listdir(\".\")\n",
120+
"\n",
121+
"# ...\n",
122+
"# ...\n",
123+
"\n",
124+
"# After a lot of code ...\n",
125+
"\n",
126+
"# Forgot you imported 'os' on top\n",
127+
"os = 'linux'\n",
128+
"\n",
129+
"# After a few lines ...\n",
130+
"\n",
131+
"os.listdir(\"/tmp\")"
132+
]
133+
},
134+
{
135+
"cell_type": "markdown",
136+
"metadata": {},
137+
"source": [
138+
"## Gotcha !!!\n",
139+
"\n",
140+
"#### This is because,\n",
141+
"\n",
142+
" 1. Python doesn't respect its keywords like some other languages do. Python being a very flexible programming language allows developer a lot of leeway in naming variables.\n",
143+
" 1. The downside is that you might overwrite module or keyword names if you don't name your variables properly."
144+
]
145+
},
146+
{
147+
"cell_type": "markdown",
148+
"metadata": {},
149+
"source": [
150+
"### 2.2 Show me the Fix !"
151+
]
152+
},
153+
{
154+
"cell_type": "markdown",
155+
"metadata": {},
156+
"source": [
157+
"Well of course the fix is to choose names of your variables carefully. Make sure you don't reuse names of common Python modules or keywords in your code. \n",
158+
"\n",
159+
"The most well-known variable names that trip up programmers are,\n",
160+
"\n",
161+
"(in no order)\n",
162+
"\n",
163+
" 1. os\n",
164+
" 2. sys\n",
165+
" 3. dir\n",
166+
" 4. type\n",
167+
" 5. str\n",
168+
" 6. string\n",
169+
" 7. list\n",
170+
" 8. dict\n",
171+
" 9. tuple\n",
172+
" 10. int\n",
173+
" 11. file\n",
174+
" ..."
175+
]
176+
},
177+
{
178+
"cell_type": "markdown",
179+
"metadata": {},
180+
"source": [
181+
"# 3. Cyclical Imports"
182+
]
183+
},
184+
{
185+
"cell_type": "markdown",
186+
"metadata": {},
187+
"source": [
188+
"### 3.1 Show me the Code !"
189+
]
190+
},
191+
{
192+
"cell_type": "code",
193+
"execution_count": 13,
194+
"metadata": {},
195+
"outputs": [],
196+
"source": [
197+
"# Assume a module \"a\" as as follows.\n",
198+
"import b\n",
199+
"\n",
200+
"def f(x, y):\n",
201+
" return b.g(x, y)\n",
202+
"\n",
203+
"def h(x, y):\n",
204+
" return x + y\n"
205+
]
206+
},
207+
{
208+
"cell_type": "code",
209+
"execution_count": 12,
210+
"metadata": {},
211+
"outputs": [],
212+
"source": [
213+
"# Assume module \"b\" as follows.\n",
214+
"import a\n",
215+
"\n",
216+
"def g(x, y):\n",
217+
" return a.h(x, y)"
218+
]
219+
},
220+
{
221+
"cell_type": "code",
222+
"execution_count": 16,
223+
"metadata": {},
224+
"outputs": [
225+
{
226+
"data": {
227+
"text/plain": [
228+
"30"
229+
]
230+
},
231+
"execution_count": 16,
232+
"metadata": {},
233+
"output_type": "execute_result"
234+
}
235+
],
236+
"source": [
237+
"import a\n",
238+
"a.f(10, 20)"
239+
]
240+
},
241+
{
242+
"cell_type": "code",
243+
"execution_count": 17,
244+
"metadata": {},
245+
"outputs": [
246+
{
247+
"data": {
248+
"text/plain": [
249+
"30"
250+
]
251+
},
252+
"execution_count": 17,
253+
"metadata": {},
254+
"output_type": "execute_result"
255+
}
256+
],
257+
"source": [
258+
"import b\n",
259+
"b.g(10, 20)"
260+
]
261+
},
262+
{
263+
"cell_type": "code",
264+
"execution_count": 20,
265+
"metadata": {},
266+
"outputs": [
267+
{
268+
"ename": "ImportError",
269+
"evalue": "cannot import name g",
270+
"output_type": "error",
271+
"traceback": [
272+
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mImportError\u001b[0m Traceback (most recent call last)",
273+
"\u001b[1;32m<ipython-input-20-728644ebf265>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# Now assume a module \"c\" as follows\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[1;32mfrom\u001b[0m \u001b[0md\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mg\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 3\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mf\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0mg\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
274+
"\u001b[1;32m/home/anand/Documents/pycon/pybelgaum2015/d.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mfrom\u001b[0m \u001b[0mc\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mh\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mg\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[0mh\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
275+
"\u001b[1;32m/home/anand/Documents/pycon/pybelgaum2015/c.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mfrom\u001b[0m \u001b[0md\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mg\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mf\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0mg\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
276+
"\u001b[1;31mImportError\u001b[0m: cannot import name g"
277+
]
278+
}
279+
],
280+
"source": [
281+
"# Now assume a module \"c\" as follows\n",
282+
"from d import g\n",
283+
"\n",
284+
"def f(x, y):\n",
285+
" return g(x, y)\n",
286+
"\n",
287+
"def h(x, y):\n",
288+
" return x + y"
289+
]
290+
},
291+
{
292+
"cell_type": "code",
293+
"execution_count": 21,
294+
"metadata": {},
295+
"outputs": [
296+
{
297+
"ename": "ImportError",
298+
"evalue": "cannot import name h",
299+
"output_type": "error",
300+
"traceback": [
301+
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mImportError\u001b[0m Traceback (most recent call last)",
302+
"\u001b[1;32m<ipython-input-21-e6e6b4db5842>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# Assume module \"d\" as follows.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[1;32mfrom\u001b[0m \u001b[0mc\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mh\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 3\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mg\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[0mh\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
303+
"\u001b[1;32m/home/anand/Documents/pycon/pybelgaum2015/c.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mfrom\u001b[0m \u001b[0md\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mg\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mf\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0mg\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
304+
"\u001b[1;32m/home/anand/Documents/pycon/pybelgaum2015/d.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mfrom\u001b[0m \u001b[0mc\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mh\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mg\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[0mh\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
305+
"\u001b[1;31mImportError\u001b[0m: cannot import name h"
306+
]
307+
}
308+
],
309+
"source": [
310+
"# Assume module \"d\" as follows.\n",
311+
"from c import h\n",
312+
"\n",
313+
"def g(x, y):\n",
314+
" h(x, y)"
315+
]
316+
},
317+
{
318+
"cell_type": "markdown",
319+
"metadata": {},
320+
"source": [
321+
"## Gotcha !!!\n",
322+
"\n",
323+
"#### This is because,\n",
324+
"\n",
325+
" 1. Cyclical imports are fine in Python as long as you import the full modules - as in __import foo__ .\n",
326+
" 1. Cyclical import fails if you do a __from foo import x__ syntax since now both modules need to be fully defined at the time of import and the cyclical relation makes this impossible - so both the imports fail as seen above."
327+
]
328+
},
329+
{
330+
"cell_type": "markdown",
331+
"metadata": {},
332+
"source": [
333+
"### 3.2 Show me the Fix !"
334+
]
335+
},
336+
{
337+
"cell_type": "markdown",
338+
"metadata": {},
339+
"source": [
340+
"#### Two things\n",
341+
"\n",
342+
" 1. Define your modules in a way as to avoid cyclical imports as much as possible.\n",
343+
" 1. If you have to use cyclical imports, make sure you use the full import of the module to avoid such errors."
344+
]
345+
},
346+
{
347+
"cell_type": "markdown",
348+
"metadata": {},
349+
"source": [
350+
"###### Copyright &copy; Anand B Pillai, Anvetsu Technologies Pvt. Ltd (2015)"
351+
]
352+
}
353+
],
354+
"metadata": {
355+
"kernelspec": {
356+
"display_name": "Python 2",
357+
"language": "python",
358+
"name": "python2"
359+
},
360+
"language_info": {
361+
"codemirror_mode": {
362+
"name": "ipython",
363+
"version": 2
364+
},
365+
"file_extension": ".py",
366+
"mimetype": "text/x-python",
367+
"name": "python",
368+
"nbconvert_exporter": "python",
369+
"pygments_lexer": "ipython2",
370+
"version": "2.7.15rc1"
371+
}
372+
},
373+
"nbformat": 4,
374+
"nbformat_minor": 1
375+
}

0 commit comments

Comments
 (0)