├── .gitattributes
├── .gitignore
├── README.md
├── Untitled0.ipynb
├── Untitled1.ipynb
├── data
├── images
│ └── nb
│ │ ├── 32-bit-unicode.png
│ │ ├── functioncall.png
│ │ ├── mutatingdefault.png
│ │ ├── nbstats_out.png
│ │ └── title_graphic.png
└── templates
│ ├── base.ipynb
│ ├── images
│ ├── markdown-only.ipynb
│ ├── section_base.ipynb
│ └── source_base.ipynb
├── figures
└── masters
│ └── title_graphic.xcf
├── nbsource
├── .gitignore
├── Adventures in Software Engineering 1.ipynb
├── MyDatabase.sql3
├── a-python-limerick.ipynb
├── accessing-ipython-on-the-cloud.ipynb
├── advanced-slicing.ipynb
├── bytearrays-and-bytestrings.ipynb
├── closures.ipynb
├── consuming-json-data-from-a-web-service.ipynb
├── context-managers.ipynb
├── data
│ └── itr-data.csv
├── decoding-to-unicode.ipynb
├── elementary-sql.ipynb
├── encoding-from-unicode.ipynb
├── from-import.ipynb
├── functions-as-function-arguments.ipynb
├── generator-functions.ipynb
├── goodbye.ipynb
├── handling-filenames.ipynb
├── images
├── importing-modules.ipynb
├── introducing-decorators.ipynb
├── introducing-the-ipython-notebook.ipynb
├── iterables-vs-iterators.ipynb
├── list-dict-and-set-comprehensions.ipynb
├── matplotlib.ipynb
├── mock-objects.ipynb
├── more-about-imports.ipynb
├── names-to-avoid-and-why.ipynb
├── namespace-in-python
├── namespaces-in-python.ipynb
├── numpy.ipynb
├── reading-and-writing-files.ipynb
├── regular-expressions.ipynb
├── scipy.ipynb
├── slicing.ipynb
├── string-formatting.ipynb
├── the-function-api-signatures-and-specifications.ipynb
├── the-python-dbapi.ipynb
├── the-python-iteration-protocol.ipynb
├── tuples.ipynb
├── unicode.txt
├── unit-testing.ipynb
├── using-json-data-from-the-web.ipynb
├── what-happens-when-the-interpreter-starts.ipynb
└── what-is-unicode.ipynb
├── nbsource2
├── Adventures in Software Engineering 1.ipynb
├── Introduction.ipynb
├── MyDatabase.sql3
├── MyDatabase.sql3-journal
├── a-python-limerick.ipynb
├── accessing-ipython-on-the-cloud.ipynb
├── advanced-slicing.ipynb
├── binary.dat
├── bytearrays-and-bytestrings.ipynb
├── closures.ipynb
├── consuming-json-data-from-a-web-service.ipynb
├── context-managers.ipynb
├── data
│ ├── itr-data.csv
│ └── videos.txt
├── decoding-to-unicode.ipynb
├── encoding-from-unicode.ipynb
├── functions-and-function-arguments.ipynb
├── generator-functions.ipynb
├── handling-filenames.ipynb
├── images
├── importing-modules.ipynb
├── introducing-decorators.ipynb
├── introducing-the-ipython-notebook.ipynb
├── iterables-vs-iterators.ipynb
├── list-dict-and-set-comprehensions.ipynb
├── matplotlib.ipynb
├── mock-objects.ipynb
├── mod_simple.py
├── mymodule.py
├── names-to-avoid-and-why.ipynb
├── namespace-in-python
├── namespaces-in-python.ipynb
├── numpy.ipynb
├── reading-and-writing-files.ipynb
├── regular-expressions.ipynb
├── sample.txt
├── scipy.ipynb
├── scoping.ipynb
├── secret_sauce.py
├── slicing.ipynb
├── star-spangled.txt
├── string-formatting.ipynb
├── test1.py
├── the-function-api-signatures-and-specifications.ipynb
├── the-python-dbapi.ipynb
├── the-python-iteration-protocol.ipynb
├── tuples.ipynb
├── unicode.txt
├── unit-testing.ipynb
├── using-json-data-from-the-web.ipynb
├── what-happens-when-the-interpreter-starts.ipynb
├── what-is-unicode.ipynb
└── widget.py
├── outline.txt
├── requirements.txt
└── whitehouse_json.py
/.gitattributes:
--------------------------------------------------------------------------------
1 | *.ipynb filter=ipynb-fix
2 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | stash
2 | *.html
3 | *.pyc
4 | *.pyo
5 | *.swp
6 | *.wp?
7 | .ipynb_checkpoints
8 | notebooks
9 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ### Project Outline
2 |
3 | This project aims to produce a set of IPython notebooks to support an Intermediate Python video class of approximately six hours duration. While the class will be commercial the publisher has agreed to the publication of the notebooks under a Creative Commons license allowing both commercial and non-commercial uses.
4 |
5 | Since the project is running to tight timescales assistance in developing the content will be much appreciated.
6 |
7 | __What Can I Do?__
8 | Most urgent at present is the review of the existing notebook content and suggestions for content -
9 | particularly suggestions for what you'd like to see in the dauntingly long list of unstarted topics listed in `unstarted.txt`. Some portions of the outline may have to be cut anyway, as it looks like there is more than six hours material in it, so it will be good to have evidence of demand.
10 |
11 | The notebooks vary in both style and length. Some contain only code. Some are heavily annotated as a preliminary video script. Currently the code is much more important than the annotations. Over time, though, the published notebooks will be increasingly annotated to increase their utility.
12 |
13 | Those in the Portland area can sign up for in-person review sessions that focus on topics needing content and filling in gaps in participant's knowledge through interactive us of the IPython notebook. These notebooks will be shared with the participants after each session. [URL to follow]
14 |
15 | Reviewers showing promise may be invited to become committers on the project, but at present we aren't set up to handle pull requests on this project, which is simply the contents of the latest nightly dump.
16 |
17 | NOTE: This material will be released under a Collective Commons license, allowing free use for both commercial and non-commercial purposes.
18 |
19 | #### Project Structure
20 |
21 | You will find the following items in this project:
22 |
23 | * `outline.txt`, the indented and annotated table of contents
24 | * `unstarted.txt`, a list of unstarted topics
25 | * `nbstats.txt`, some statistics on the notebook content
26 | * A `notebooks` directory containing the most recently-released content
27 | * An `images` subdirectory containing graphical images used in notebooks
28 |
29 | Any other files or directories you find in there are not yet a permanent part of the project structure.
30 |
31 |
32 | ### Running IPython Notebooks in the Cloud
33 |
34 | First you need to have a Google account (which is also your Notebook Cloud Server account - it's an Appspot app) and an Amazon AWS account. None of this costs anything, and even if you aren't helping with the project this is a really easy way to access Python.
35 |
36 | ___Signing Up for Notebook Cloud Services___
37 | All the computing you need to evaluate these notebooks (and a whole bunch more!) can be accomplished with virtuals that qualify for Amazon's free-tier pricing. Free-tier remains free for the first twelve months, and it's unlikely anything you do will exceed the very generous limits. To sign up for an AWS account:
38 |
39 | * Visit the Amazon AWS home page
40 | * Click the “Sign Up” link
41 | * Enter an email address, select “I am a new user” and click “Sign in ..”
42 |
43 | Once you have opened your account (there's the usual "please confirm" email with a link) you need to sign up with Notebook Cloud, a Google AppSpot application that uses AWS services on your behalf to create virtual machines running the IPython Notebook server on demand.
44 |
45 | * Visit the notebook cloud home page
46 | * Take a quick look at the simple documentation
47 | * Click "Log in with Google Account"
48 | * Once logged in, click the "Account Details" button,
49 | enter your AWS credentials (instructions how to get them are shown)
50 | and click "Save"
51 |
52 | NOTE: you can password-protect all your notebooks by entering a password on this form. There is no mechanism to rerieve this password - you have been warned!
53 |
54 | ___Using the Notebook Cloud Service___
55 | You are now looking at the IPython Notebook Cloud server control window. For this purpose you only need to start the very smallest instance.
56 |
57 | * Click the "Micro" button to create a new Notebook Server
58 |
59 | It will take a minute or two to create a new virtual machine. The Cloud server monitors the status of the virtual machine. Wait until it says "Serving" on the status line under the machine description, you are looking for something like
60 |
61 | __Instance id: i-706d2551 Type: t1.micro Started: 01:04:36 ~ 2014-03-11__
62 | __State__: Serving
63 |
64 | Click on the word "serving" and a new window will open up with a notebook server showing an empty list of files. You can then drag Notebook (`.ipynb`) files into the (initially empty) files list, and an upload button appears. After uploading a notebook you can start it. Each notebook appears in its own browser window or tab.
65 |
66 | After updating a notebook you can download a copy with the Notebook Server's "File | Download As ..." menu item. This allows you to download either a Python file (in which all the markdown text appears as comments) or a Notebook file that you can upload to another server if you want.
67 |
68 | Your notebooks persist on the server between sessions. You can choose, though it's not necessary, to shut them down before closing or shutting down the Notebook Server.
69 | When you are done, you might want to shut your Notebook Server down. There's no point leaving the clock ticking when it's not being used, but at the same time it costs hardly anything. I just don't like waste, you could be computing with those cycles. :)
70 |
71 | In the Cloud Server window click the "Stop" button next to your chosen virtual. It takes a while (maybe up to a minute: what do you care, it's free, right?) to shut down a running virtual. The next time you visit your Notebook Cloud account (or even immediately!) you can restart it by clicking on its "Start" button.
72 |
73 | The "Terminate" button removes the VM instance completely, destroying its file store (so better make sure that you have downloaded copies of all Notebooks you may want to keep before you terminate an instance).
74 |
75 | ___Reviewing the Course Content___
76 | Download the project (presumably you will simply clone the repository, but however). Upload them to your Notebook Cloud server and start them.
77 |
78 | You can step through the notebook cell by cell using the SHIFT/Enter conbination. Alternatively you can select "Cell | Run All" from the notebook server menu and then scroll through the completed notebook to understand the results.
79 |
80 | Take a look at `unstarted.txt` and let me know what you would like to see under any and all topics. To ensure suggestions do not simply get lost I would appreciate reviewers raising issues. A link to the tracker is included in all notebooks.
81 |
--------------------------------------------------------------------------------
/Untitled0.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "metadata": {
3 | "name": ""
4 | },
5 | "nbformat": 3,
6 | "nbformat_minor": 0,
7 | "worksheets": [
8 | {
9 | "cells": [
10 | {
11 | "cell_type": "code",
12 | "collapsed": false,
13 | "input": [
14 | "class NegatableString(str):\n",
15 | " def __rsub__(self, other):\n",
16 | " if other.endswith(self):\n",
17 | " return other[:len(other)-len(self)]\n",
18 | " else:\n",
19 | " raise ValueError(\"Cannot subtract {!r} from {!r}\".format(self, other))"
20 | ],
21 | "language": "python",
22 | "metadata": {},
23 | "outputs": [],
24 | "prompt_number": 1
25 | },
26 | {
27 | "cell_type": "code",
28 | "collapsed": false,
29 | "input": [
30 | "x = NegatableString(\"abc\")\n",
31 | "\"123abc\" - x"
32 | ],
33 | "language": "python",
34 | "metadata": {},
35 | "outputs": [
36 | {
37 | "metadata": {},
38 | "output_type": "pyout",
39 | "prompt_number": 2,
40 | "text": [
41 | "'123'"
42 | ]
43 | }
44 | ],
45 | "prompt_number": 2
46 | },
47 | {
48 | "cell_type": "code",
49 | "collapsed": false,
50 | "input": [
51 | "\"123def\" - x"
52 | ],
53 | "language": "python",
54 | "metadata": {},
55 | "outputs": []
56 | },
57 | {
58 | "cell_type": "code",
59 | "collapsed": false,
60 | "input": [],
61 | "language": "python",
62 | "metadata": {},
63 | "outputs": []
64 | }
65 | ],
66 | "metadata": {}
67 | }
68 | ]
69 | }
--------------------------------------------------------------------------------
/Untitled1.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "metadata": {
3 | "name": ""
4 | },
5 | "nbformat": 3,
6 | "nbformat_minor": 0,
7 | "worksheets": [
8 | {
9 | "cells": [
10 | {
11 | "cell_type": "code",
12 | "collapsed": false,
13 | "input": [
14 | "def f_no_positionals(*, this, that, the_other):\n",
15 | " print(this, \"|\", that, \"|\", the_other)"
16 | ],
17 | "language": "python",
18 | "metadata": {},
19 | "outputs": [],
20 | "prompt_number": 1
21 | },
22 | {
23 | "cell_type": "code",
24 | "collapsed": false,
25 | "input": [
26 | "f_no_positionals(\"first\", \"second\" \"third\")"
27 | ],
28 | "language": "python",
29 | "metadata": {},
30 | "outputs": [
31 | {
32 | "ename": "TypeError",
33 | "evalue": "f_no_positionals() takes 0 positional arguments but 2 were given",
34 | "output_type": "pyerr",
35 | "traceback": [
36 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
37 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mf_no_positionals\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"first\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"second\"\u001b[0m \u001b[0;34m\"third\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
38 | "\u001b[0;31mTypeError\u001b[0m: f_no_positionals() takes 0 positional arguments but 2 were given"
39 | ]
40 | }
41 | ],
42 | "prompt_number": 2
43 | }
44 | ],
45 | "metadata": {}
46 | }
47 | ]
48 | }
--------------------------------------------------------------------------------
/data/images/nb/32-bit-unicode.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/holdenweb/int-py-notes/71c53d109895e346f1f7c3ba225883dbe8d04484/data/images/nb/32-bit-unicode.png
--------------------------------------------------------------------------------
/data/images/nb/functioncall.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/holdenweb/int-py-notes/71c53d109895e346f1f7c3ba225883dbe8d04484/data/images/nb/functioncall.png
--------------------------------------------------------------------------------
/data/images/nb/mutatingdefault.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/holdenweb/int-py-notes/71c53d109895e346f1f7c3ba225883dbe8d04484/data/images/nb/mutatingdefault.png
--------------------------------------------------------------------------------
/data/images/nb/nbstats_out.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/holdenweb/int-py-notes/71c53d109895e346f1f7c3ba225883dbe8d04484/data/images/nb/nbstats_out.png
--------------------------------------------------------------------------------
/data/images/nb/title_graphic.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/holdenweb/int-py-notes/71c53d109895e346f1f7c3ba225883dbe8d04484/data/images/nb/title_graphic.png
--------------------------------------------------------------------------------
/data/templates/base.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "metadata": {
3 | "name": ""
4 | },
5 | "nbformat": 3,
6 | "nbformat_minor": 0,
7 | "worksheets": [
8 | {
9 | "cells": [
10 | {
11 | "cell_type": "heading",
12 | "level": 1,
13 | "metadata": {},
14 | "source": [
15 | "
\n",
16 | "||| WORKING COPY ||| {{ title }}"
17 | ]
18 | },
19 | {
20 | "cell_type": "raw",
21 | "metadata": {},
22 | "source": [
23 | "#!content nbsource/{{ slug }}.ipynb"
24 | ]
25 | },
26 | {
27 | "cell_type": "markdown",
28 | "metadata": {},
29 | "source": [
30 | "This document was created on {{ date }} at {{ time }}."
31 | ]
32 | }
33 | ],
34 | "metadata": {}
35 | }
36 | ]
37 | }
--------------------------------------------------------------------------------
/data/templates/images:
--------------------------------------------------------------------------------
1 | ../images/nb
--------------------------------------------------------------------------------
/data/templates/markdown-only.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "metadata": {
3 | "name": ""
4 | },
5 | "nbformat": 3,
6 | "nbformat_minor": 0,
7 | "worksheets": [
8 | {
9 | "cells": [
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "### Markdown from {{ src_file }} as one cell"
15 | ]
16 | },
17 | {
18 | "cell_type": "markdown",
19 | "metadata": {},
20 | "source": [
21 | "Created on {{ date }} at {{ time }} by __{{program_name }}__"
22 | ]
23 | }
24 | ],
25 | "metadata": {}
26 | }
27 | ]
28 | }
--------------------------------------------------------------------------------
/data/templates/section_base.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "metadata": {
3 | "name": ""
4 | },
5 | "nbformat": 3,
6 | "nbformat_minor": 0,
7 | "worksheets": [
8 | {
9 | "cells": [
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "Source notebook for the top-level {{ title }} module. Source locate in {{ src_file }}. Created automatically by sanity on {{ date }} at {{ time}}."
15 | ]
16 | },
17 | {
18 | "cell_type": "markdown",
19 | "metadata": {},
20 | "source": [
21 | "{% for talk in talks %}\n",
22 | "[{{ talk.name }}](\"{{ talk.slug }}.ipynb\") \n",
23 | "\n",
24 | "{% endfor %}"
25 | ]
26 | }
27 | ],
28 | "metadata": {}
29 | }
30 | ]
31 | }
--------------------------------------------------------------------------------
/data/templates/source_base.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "metadata": {
3 | "name": ""
4 | },
5 | "nbformat": 3,
6 | "nbformat_minor": 0,
7 | "worksheets": [
8 | {
9 | "cells": [
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "#### PRE-RELEASE REVIEW: O'Reilly Intermediate Python Video Series\n",
15 | "#### Topic: {{ title }} \n",
16 | "Source located in `{{ src_file }}`. Automatically rendered from `{{ template }}` on {{ date }} at {{ time}}.\n",
17 | "#### AUTOMATICALLY GENERATED TEST NOTEBOOK - CHANGES WILL BE LOST\n",
18 | "#### To TEST this Notebook
1. select Cell | Run All, or step through with Shift/Enter
2. Notebook turns pink when testing starts
3. It turns white when the it runs to completion.
4. Please check the executed notebook for clarity and correctness.
Please [raise an issue](https://github.com/holdenweb/intermediate-python/issues) about anything you don't understand - by all means attach a saved copy of the notebook to add explanations or questions. Also please let us know about stuff you want to see covered under any heading in the outline.\n",
19 | "#### Your comments on the content are also welcome, particularly when [reported as issues](https://github.com/holdenweb/intermediate-python/issues)"
20 | ]
21 | },
22 | {
23 | "cell_type": "raw",
24 | "metadata": {},
25 | "source": [
26 | "#!content nbsource/{{ slug }}.ipynb"
27 | ]
28 | }
29 | ],
30 | "metadata": {}
31 | }
32 | ]
33 | }
--------------------------------------------------------------------------------
/figures/masters/title_graphic.xcf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/holdenweb/int-py-notes/71c53d109895e346f1f7c3ba225883dbe8d04484/figures/masters/title_graphic.xcf
--------------------------------------------------------------------------------
/nbsource/.gitignore:
--------------------------------------------------------------------------------
1 | *.py
2 |
--------------------------------------------------------------------------------
/nbsource/Adventures in Software Engineering 1.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "## Adventures in Software Engineering #1\n",
8 | "\n",
9 | "This simple exercise shows how to handle a text file whose content is slightly off specification.\n",
10 | "\n",
11 | "The `csv` module is intended to handle files containing comma-separated data. Such data is sourced\n",
12 | "by many different utilities, not all of which correctly observe specifications. I found one such\n",
13 | "file in my own data, a list of videos.\n",
14 | "\n",
15 | "This problem is complicated (but not much) byt he fact that Python 2's `csv` module doesn't handle Unicode at all well."
16 | ]
17 | },
18 | {
19 | "cell_type": "code",
20 | "execution_count": 49,
21 | "metadata": {
22 | "collapsed": false
23 | },
24 | "outputs": [],
25 | "prompt_number": "",
26 | "source": [
27 | "import csv"
28 | ]
29 | },
30 | {
31 | "cell_type": "code",
32 | "execution_count": 50,
33 | "metadata": {
34 | "collapsed": false
35 | },
36 | "outputs": [],
37 | "prompt_number": "",
38 | "source": [
39 | "reader = csv.reader(open(\"data/videos.txt\", \"r\"))\n",
40 | "reader"
41 | ]
42 | },
43 | {
44 | "cell_type": "code",
45 | "execution_count": 51,
46 | "metadata": {
47 | "collapsed": false
48 | },
49 | "outputs": [],
50 | "prompt_number": "",
51 | "source": [
52 | "next(reader)"
53 | ]
54 | },
55 | {
56 | "cell_type": "markdown",
57 | "metadata": {},
58 | "source": [
59 | "As you might spot, there are some very funny things going on in this file.\n",
60 | "Let's take a look at the first few lines of data."
61 | ]
62 | },
63 | {
64 | "cell_type": "code",
65 | "execution_count": 52,
66 | "metadata": {
67 | "collapsed": false
68 | },
69 | "outputs": [],
70 | "prompt_number": "",
71 | "source": [
72 | "open(\"data/videos.txt\", \"r\").readlines()[:5]"
73 | ]
74 | },
75 | {
76 | "cell_type": "markdown",
77 | "metadata": {},
78 | "source": [
79 | "The issue here is the spaces that follow the commas intended to separate the fields.\n",
80 | "This is causing the following double-quote to _not_ be treated as a field delimiter.\n",
81 | "\n",
82 | "The `csv` module allows you to define _dialects_, which affect how the file is interpreted.\n",
83 | "Since this is a very simple issue, however, I chose instead to create a object that could\n",
84 | "be used instead of the file, masking the defects of the original data.\n",
85 | "\n",
86 | "Python's `io` module defines the `StringIO` object, essentially an in-memory replacement for the file.\n",
87 | "Below I create one by concatenating all the lines of the input file after removing the space in\n",
88 | "any occurrence of the sequence `'\", \"'`, rendering it `'\",\"'` instead."
89 | ]
90 | },
91 | {
92 | "cell_type": "code",
93 | "execution_count": 53,
94 | "metadata": {
95 | "collapsed": false
96 | },
97 | "outputs": [],
98 | "prompt_number": "",
99 | "source": [
100 | "from io import BytesIO"
101 | ]
102 | },
103 | {
104 | "cell_type": "code",
105 | "execution_count": 54,
106 | "metadata": {
107 | "collapsed": false
108 | },
109 | "outputs": [],
110 | "prompt_number": "",
111 | "source": [
112 | "file = open(\"data/videos.txt\", \"r\", )\n",
113 | "newfile = BytesIO(u\"\".join(line.decode(\"utf-8\").replace(u'\", \"', u'\",\"') for line in file).encode(\"utf-8\"))"
114 | ]
115 | },
116 | {
117 | "cell_type": "markdown",
118 | "metadata": {},
119 | "source": [
120 | "Rewinding the `StringIO` to the beginning of its content with the `.seek()` method allows\n",
121 | "you to use it as the argument when creating a new `csv.reader ` object.\n",
122 | "Iterating over that object shows the data being interpreted correctly"
123 | ]
124 | },
125 | {
126 | "cell_type": "code",
127 | "execution_count": 57,
128 | "metadata": {
129 | "collapsed": false
130 | },
131 | "outputs": [],
132 | "prompt_number": "",
133 | "source": [
134 | "newfile.seek(0)\n",
135 | "for t in csv.reader(newfile):\n",
136 | " print [tt.decode(\"utf-8\") for tt in t]"
137 | ]
138 | },
139 | {
140 | "cell_type": "code",
141 | "execution_count": null,
142 | "metadata": {
143 | "collapsed": false
144 | },
145 | "outputs": [],
146 | "prompt_number": "",
147 | "source": []
148 | }
149 | ],
150 | "metadata": {
151 | "kernelspec": {
152 | "display_name": "Python 2",
153 | "language": "python",
154 | "name": "python2"
155 | },
156 | "language_info": {
157 | "codemirror_mode": {
158 | "name": "ipython",
159 | "version": 2
160 | },
161 | "file_extension": ".py",
162 | "mimetype": "text/x-python",
163 | "name": "python",
164 | "nbconvert_exporter": "python",
165 | "pygments_lexer": "ipython2",
166 | "version": "2.7.2"
167 | }
168 | },
169 | "nbformat": 4,
170 | "nbformat_minor": 0
171 | }
--------------------------------------------------------------------------------
/nbsource/MyDatabase.sql3:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/holdenweb/int-py-notes/71c53d109895e346f1f7c3ba225883dbe8d04484/nbsource/MyDatabase.sql3
--------------------------------------------------------------------------------
/nbsource/a-python-limerick.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": null,
6 | "metadata": {
7 | "collapsed": false
8 | },
9 | "outputs": [],
10 | "prompt_number": "",
11 | "source": [
12 | "# With acknowledgments to Micheal Spencer\n",
13 | "\n",
14 | "from itertools import repeat\n",
15 | "for feet in [3,3,2,2,3]:\n",
16 | " print \" \".join(\"DA-DA-DUM\"\n",
17 | " for dummy in [None]\n",
18 | "for foot in repeat(\"metric\", feet))"
19 | ]
20 | },
21 | {
22 | "cell_type": "code",
23 | "execution_count": null,
24 | "metadata": {
25 | "collapsed": true
26 | },
27 | "outputs": [],
28 | "prompt_number": "",
29 | "source": []
30 | }
31 | ],
32 | "metadata": {
33 | "kernelspec": {
34 | "display_name": "Python 2",
35 | "language": "python",
36 | "name": "python2"
37 | },
38 | "language_info": {
39 | "codemirror_mode": {
40 | "name": "ipython",
41 | "version": 2
42 | },
43 | "file_extension": ".py",
44 | "mimetype": "text/x-python",
45 | "name": "python",
46 | "nbconvert_exporter": "python",
47 | "pygments_lexer": "ipython2",
48 | "version": "2.7.2"
49 | }
50 | },
51 | "nbformat": 4,
52 | "nbformat_minor": 0
53 | }
--------------------------------------------------------------------------------
/nbsource/accessing-ipython-on-the-cloud.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "#### __PRE-RELEASE REVIEW: O'Reilly Intermediate Python Video Series__\n",
8 | "#### Topic: Accessing IPython on the Cloud \n",
9 | "Source located in `invalid`. Automatically rendered from `UNKNOWN` on 2014-03-21 at 10:21:36.443435.\n",
10 | "#### AUTOMATICALLY GENERATED TEST NOTEBOOK - CHANGES WILL BE LOST\n",
11 | "#### To TEST this Notebook
1. select Cell | Run All, or step through with Shift/Enter
2. Notebook turns pink when testing starts
3. It turns white when the it runs to completion.
4. Please check the executed notebook for clarity and correctness.
Please [raise an issue](https://github.com/holdenweb/intermediate-python/issues) about anything you don't understand - by all means attach a saved copy of the notebook to add explanations or questions. Also please let us know about stuff you want to see covered under any heading in the outline.\n",
12 | "#### Your comments on the content are also welcome, particularly when [reported as issues](https://github.com/holdenweb/intermediate-python/issues) "
13 | ]
14 | },
15 | {
16 | "cell_type": "markdown",
17 | "metadata": {},
18 | "source": [
19 | "#
||| WORKING COPY ||| Tuples"
20 | ]
21 | },
22 | {
23 | "cell_type": "markdown",
24 | "metadata": {},
25 | "source": [
26 | "## Running IPython Notebooks in the Cloud"
27 | ]
28 | },
29 | {
30 | "cell_type": "markdown",
31 | "metadata": {},
32 | "source": [
33 | "First you need to have a Google account (which is also your Notebook Cloud Server account - it's an Appspot app) and an Amazon AWS account. None of this costs anything; this is a really easy way to access Python.\n",
34 | "\n",
35 | "Signing Up for Notebook Cloud Services\n",
36 | "All the computing you need to evaluate these notebooks (and a whole bunch more!) can be accomplished with virtuals that qualify for Amazon's free-tier pricing. Free-tier remains free for the first twelve months, and it's unlikely anything you do will exceed the very generous limits. To sign up for an AWS account:"
37 | ]
38 | },
39 | {
40 | "cell_type": "markdown",
41 | "metadata": {},
42 | "source": [
43 | "\n",
44 | "Visit the Amazon AWS home page http://aws.amazon.com/\n",
45 | " \n",
46 | "Click the \u201cSign Up\u201d link\n",
47 | "\n",
48 | "Enter an email address, select \u201cI am a new user\u201d and click \u201cSign in ..\u201d"
49 | ]
50 | },
51 | {
52 | "cell_type": "markdown",
53 | "metadata": {},
54 | "source": [
55 | "Once you have opened your account (there's the usual \"please confirm\" email with a link) you need to sign up with Notebook Cloud, a Google AppSpot application that uses AWS services on your behalf to create virtual machines running the IPython Notebook server on demand."
56 | ]
57 | },
58 | {
59 | "cell_type": "markdown",
60 | "metadata": {},
61 | "source": [
62 | "Visit the notebook cloud home page http://notebookcloud.appspot.com/\n",
63 | "\n",
64 | "Take a quick look at the simple documentation, and click \"Log in with Google Account.\" Once logged in, click the \"Account Details\" button, enter your AWS credentials (instructions how to get them are shown) and click \"Save.\""
65 | ]
66 | },
67 | {
68 | "cell_type": "markdown",
69 | "metadata": {},
70 | "source": [
71 | "__NOTE__: you can password-protect all your notebooks by entering a password on this form. There is no mechanism to rerieve this password - you have been warned!"
72 | ]
73 | },
74 | {
75 | "cell_type": "markdown",
76 | "metadata": {},
77 | "source": [
78 | "__Using the Notebook Cloud Service__\n",
79 | "\n",
80 | "You are now looking at the IPython Notebook Cloud server control window. For this purpose you only need to start the very smallest instance.\n",
81 | "\n",
82 | "Click the \"Micro\" button to create a new Notebook Server\n",
83 | "\n",
84 | "It will take a minute or two to create a new virtual machine. The Cloud server monitors the status of the virtual machine. Wait until it says \"Serving\" on the status line under the machine description, you are looking for something like:"
85 | ]
86 | },
87 | {
88 | "cell_type": "markdown",
89 | "metadata": {},
90 | "source": [
91 | "__Instance id: i-706d2551 Type: t1.micro Started: 01:04:36 ~ 2014-03-11__\n",
92 | "\n",
93 | "__State__: Serving"
94 | ]
95 | },
96 | {
97 | "cell_type": "markdown",
98 | "metadata": {},
99 | "source": [
100 | "Click on the word \"serving\" and a new window will open up with a notebook server showing an empty list of files. You can then drag Notebook (.ipynb) files into the (initially empty) files list, and an upload button appears. After uploading a notebook you can start it. Each notebook appears in its own browser window or tab.\n",
101 | "\n",
102 | "After updating a notebook you can download a copy with the Notebook Server's \"File | Download As ...\" menu item. This allows you to download either a Python file (in which all the markdown text appears as comments) or a Notebook file that you can upload to another server if you want.\n",
103 | "\n",
104 | "Your notebooks persist on the server between sessions. You can choose, though it's not necessary, to shut them down before closing or shutting down the Notebook Server. When you are done, you might want to shut your Notebook Server down. There's no point leaving the clock ticking when it's not being used, but at the same time it costs hardly anything. I just don't like waste, you could be computing with those cycles. :)\n",
105 | "\n",
106 | "In the Cloud Server window click the \"Stop\" button next to your chosen virtual. It takes a while (maybe up to a minute: what do you care, it's free, right?) to shut down a running virtual. The next time you visit your Notebook Cloud account (or even immediately!) you can restart it by clicking on its \"Start\" button.\n",
107 | "\n",
108 | "The \"Terminate\" button removes the VM instance completely, destroying its file store (so better make sure that you have downloaded copies of all Notebooks you may want to keep before you terminate an instance)."
109 | ]
110 | },
111 | {
112 | "cell_type": "markdown",
113 | "metadata": {},
114 | "source": [
115 | "__Reviewing the Course Content__\n",
116 | "\n",
117 | "Download the project (presumably you will simply clone the repository). Upload them to your Notebook Cloud server and start them.\n",
118 | "\n",
119 | "You can step through the notebook cell by cell using the SHIFT/Enter conbination. Alternatively you can select \"Cell | Run All\" from the notebook server menu and then scroll through the completed notebook to understand the results.\n",
120 | "\n",
121 | "Take a look at unstarted.txt and let me know what you would like to see under any and all topics. To ensure suggestions do not simply get lost I would appreciate reviewers raising issues. A link to the tracker is included in all notebooks."
122 | ]
123 | }
124 | ],
125 | "metadata": {},
126 | "nbformat": 4,
127 | "nbformat_minor": 0
128 | }
--------------------------------------------------------------------------------
/nbsource/closures.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 10,
6 | "metadata": {
7 | "collapsed": false
8 | },
9 | "outputs": [],
10 | "prompt_number": "",
11 | "source": [
12 | "def f(y):\n",
13 | " return y\n",
14 | "\n",
15 | "def c(x):\n",
16 | " def f(y):\n",
17 | " return x\n",
18 | " return f"
19 | ]
20 | },
21 | {
22 | "cell_type": "code",
23 | "execution_count": 11,
24 | "metadata": {
25 | "collapsed": false
26 | },
27 | "outputs": [],
28 | "prompt_number": "",
29 | "source": [
30 | "func = f\n",
31 | "closure = c(42)"
32 | ]
33 | },
34 | {
35 | "cell_type": "code",
36 | "execution_count": 12,
37 | "metadata": {
38 | "collapsed": false
39 | },
40 | "outputs": [],
41 | "prompt_number": "",
42 | "source": [
43 | "from dis import dis\n",
44 | "dis(func)"
45 | ]
46 | },
47 | {
48 | "cell_type": "code",
49 | "execution_count": 13,
50 | "metadata": {
51 | "collapsed": false
52 | },
53 | "outputs": [],
54 | "prompt_number": "",
55 | "source": [
56 | "dis(closure)"
57 | ]
58 | },
59 | {
60 | "cell_type": "code",
61 | "execution_count": 27,
62 | "metadata": {
63 | "collapsed": false
64 | },
65 | "outputs": [],
66 | "prompt_number": "",
67 | "source": [
68 | "x = 'x in module'\n",
69 | "def f2():\n",
70 | " x = 'x in f2'\n",
71 | " def myfunc():\n",
72 | " x = 'x in myfunc'\n",
73 | " class MyClass(object): # Note: not a function!\n",
74 | " x = x\n",
75 | " print(x)\n",
76 | " return MyClass\n",
77 | " print(dis(myfunc))\n",
78 | " myfunc()\n",
79 | "f2()"
80 | ]
81 | },
82 | {
83 | "cell_type": "code",
84 | "execution_count": 36,
85 | "metadata": {
86 | "collapsed": false
87 | },
88 | "outputs": [],
89 | "prompt_number": "",
90 | "source": [
91 | "x = 'x in module'\n",
92 | "def f2():\n",
93 | " x = 'x in f2'\n",
94 | " def myfunc():\n",
95 | " x = 'x in myfunc'\n",
96 | " class MyClass(object):\n",
97 | " y = x\n",
98 | " print(y)\n",
99 | " return MyClass\n",
100 | " print(\"myfunc:\")\n",
101 | " dis(myfunc)\n",
102 | " print(\"myfunc closure:\\n\", myfunc.__closure__)\n",
103 | " myfunc()\n",
104 | "print(f2.__closure__)\n",
105 | "f2()"
106 | ]
107 | },
108 | {
109 | "cell_type": "code",
110 | "execution_count": 35,
111 | "metadata": {
112 | "collapsed": false
113 | },
114 | "outputs": [],
115 | "prompt_number": "",
116 | "source": [
117 | "dis(f2)"
118 | ]
119 | },
120 | {
121 | "cell_type": "code",
122 | "execution_count": 1,
123 | "metadata": {
124 | "collapsed": false
125 | },
126 | "outputs": [],
127 | "prompt_number": "",
128 | "source": [
129 | "class __:\n",
130 | " def __getitem__(__, _):\n",
131 | " return _\n",
132 | "___ = __()[...]\n",
133 | "___"
134 | ]
135 | },
136 | {
137 | "cell_type": "code",
138 | "execution_count": 5,
139 | "metadata": {
140 | "collapsed": false
141 | },
142 | "outputs": [],
143 | "prompt_number": "",
144 | "source": [
145 | "Ellipsis, type(Ellipsis)"
146 | ]
147 | },
148 | {
149 | "cell_type": "code",
150 | "execution_count": 9,
151 | "metadata": {
152 | "collapsed": false
153 | },
154 | "outputs": [],
155 | "prompt_number": "",
156 | "source": [
157 | "e1 = type(Ellipsis)()\n",
158 | "e2 = type(Ellipsis)()\n",
159 | "e1 is e2 is Ellipsis is ..."
160 | ]
161 | },
162 | {
163 | "cell_type": "code",
164 | "execution_count": 4,
165 | "metadata": {
166 | "collapsed": false
167 | },
168 | "outputs": [],
169 | "prompt_number": "",
170 | "source": [
171 | "..."
172 | ]
173 | },
174 | {
175 | "cell_type": "code",
176 | "execution_count": null,
177 | "metadata": {
178 | "collapsed": false
179 | },
180 | "outputs": [],
181 | "prompt_number": "",
182 | "source": []
183 | }
184 | ],
185 | "metadata": {},
186 | "nbformat": 4,
187 | "nbformat_minor": 0
188 | }
--------------------------------------------------------------------------------
/nbsource/context-managers.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "#### PRE-RELEASE REVIEW: O'Reilly Intermediate Python Video Series\n",
8 | "#### Topic: Context Managers \n",
9 | "Source located in `nbsource/context-managers.ipynb`. Automatically rendered from `UNKNOWN` on 2014-03-21 at 10:21:36.572700.\n",
10 | "#### AUTOMATICALLY GENERATED TEST NOTEBOOK - CHANGES WILL BE LOST\n",
11 | "#### To TEST this Notebook
1. select Cell | Run All, or step through with Shift/Enter
2. Notebook turns pink when testing starts
3. It turns white when the it runs to completion.
4. Please check the executed notebook for clarity and correctness.
Please [raise an issue](https://github.com/holdenweb/intermediate-python/issues) about anything you don't understand - by all means attach a saved copy of the notebook to add explanations or questions. Also please let us know about stuff you want to see covered under any heading in the outline.\n",
12 | "#### Your comments on the content are also welcome, particularly when [reported as issues](https://github.com/holdenweb/intermediate-python/issues)"
13 | ]
14 | },
15 | {
16 | "cell_type": "markdown",
17 | "metadata": {},
18 | "source": [
19 | "#
||| WORKING COPY ||| Context Managers"
20 | ]
21 | },
22 | {
23 | "cell_type": "markdown",
24 | "metadata": {},
25 | "source": [
26 | "This document was created on 2014-03-13 at 19:22:25.641179."
27 | ]
28 | },
29 | {
30 | "cell_type": "raw",
31 | "metadata": {},
32 | "source": [
33 | "#!content nbsource/context-managers.ipynb"
34 | ]
35 | },
36 | {
37 | "cell_type": "code",
38 | "execution_count": null,
39 | "metadata": {
40 | "collapsed": false
41 | },
42 | "outputs": [],
43 | "prompt_number": "",
44 | "source": [
45 | "with object():\n",
46 | " print(\"hello\")"
47 | ]
48 | },
49 | {
50 | "cell_type": "code",
51 | "execution_count": null,
52 | "metadata": {
53 | "collapsed": false
54 | },
55 | "outputs": [],
56 | "prompt_number": "",
57 | "source": [
58 | "with open:\n",
59 | " print(\"hello\")"
60 | ]
61 | },
62 | {
63 | "cell_type": "code",
64 | "execution_count": null,
65 | "metadata": {
66 | "collapsed": false
67 | },
68 | "outputs": [],
69 | "prompt_number": "",
70 | "source": [
71 | "hasattr(object(), \"__enter__\"), hasattr(object(), \"__exit__\")"
72 | ]
73 | },
74 | {
75 | "cell_type": "code",
76 | "execution_count": null,
77 | "metadata": {
78 | "collapsed": false
79 | },
80 | "outputs": [],
81 | "prompt_number": "",
82 | "source": [
83 | "def is_context_manager(o):\n",
84 | " return all(hasattr(o, n) for n in (\"__enter__\", \"__exit__\"))"
85 | ]
86 | },
87 | {
88 | "cell_type": "code",
89 | "execution_count": null,
90 | "metadata": {
91 | "collapsed": false
92 | },
93 | "outputs": [],
94 | "prompt_number": "",
95 | "source": [
96 | "is_context_manager(object)"
97 | ]
98 | },
99 | {
100 | "cell_type": "code",
101 | "execution_count": null,
102 | "metadata": {
103 | "collapsed": false
104 | },
105 | "outputs": [],
106 | "prompt_number": "",
107 | "source": [
108 | "f = open(\"context-managers.ipynb\")\n",
109 | "is_context_manager(f), type(f), is_context_manager(type(f))"
110 | ]
111 | },
112 | {
113 | "cell_type": "code",
114 | "execution_count": null,
115 | "metadata": {
116 | "collapsed": false
117 | },
118 | "outputs": [],
119 | "prompt_number": "",
120 | "source": [
121 | "f = open(\"context-managers.ipynb\", \"rb\")\n",
122 | "is_context_manager(f), type(f), is_context_manager(type(f))"
123 | ]
124 | },
125 | {
126 | "cell_type": "code",
127 | "execution_count": null,
128 | "metadata": {
129 | "collapsed": false
130 | },
131 | "outputs": [],
132 | "prompt_number": "",
133 | "source": []
134 | }
135 | ],
136 | "metadata": {},
137 | "nbformat": 4,
138 | "nbformat_minor": 0
139 | }
--------------------------------------------------------------------------------
/nbsource/data/itr-data.csv:
--------------------------------------------------------------------------------
1 | misschaos.chaos-studio.com,China (Shanghai),94,56,0
2 | ns2.rilinfo.net,India (Bangalore),100,0,0
3 | gsrmum.vsnl.net.in,India (Mumbai),72,276,0
4 | core-mgl.cbn.net.id,Indonesia (Mangole),76,232,0
5 | fe0-0.bbr1.bahar.kimianet.ir,Iran (Karaj),100,0,0
6 | gi0-1.thr-001-core-1.ctel.ir,Iran (Tehran),0,0,100
7 | router1.iust.ac.ir,Iran (Tehran),100,0,0
8 | nat.mindu.co.il,Israel (Tel Aviv),100,0,0
9 | cs1mr1.comsourceone.com,Japan (Tokyo),84,152,0
10 | itr-test.isp.qa,Qatar (Doha),65,345,0
11 | gateway.ix.singtel.com,Singapore,100,0,0
12 | tpnoc1-osr-transit.ix.giga.net.tw,Taiwan,78,220,0
13 | gigeuplink.ozservers.net.au,Australia (Brisbane),100,0,0
14 | slinky.arc.net.au,Australia (Melbourne),79,201,0
15 | syd-a-bb1.aarnet.net.au,Australia (Sydney),81,188,0
16 | bdr02.syd01.nsw.vocusconnect.net.au,Australia (Sydney),100,0,0
17 | io.peace.co.nz,New Zealand (Auckland),0,0,100
18 | b2.sxb.tsnz.net,New Zealand (Auckland),84,155,0
19 | ipsec.eskom.co.za,Africa (Gauteng),0,0,100
20 | rt1.mps.bg,Bulgaria (Sofia),0,0,100
21 | feth1-0-0.utland1.bone2.olivant.fo,Faroe Island (Torshavn),75,243,0
22 | n-eb1.n.de.net.dtag.de,Germany,79,209,0
23 | dns1.playnet.it,Italy (Florence),77,228,0
24 | mail.apra.it,Italy (Jesi),100,0,0
25 | mil5-loop0.mil.seabone.net,Italy (Milano),0,0,100
26 | am1-gw1.prioritytelecom.net,Netherlands (Amsterdam),100,0,0
27 | router.nomi.com.pl,Poland,0,0,100
28 | rb1-feth2-0.vlc.s2k-net.net,Spain,0,0,100
29 | mlm1-core.swip.net,Sweden,84,158,0
30 | telenor-gw.bengtdahlgren.se,Sweden (Gothenburg),0,0,100
31 | nrkp-cr1.patrikweb.net,Sweden (Norrkoping),80,197,0
32 | gsr01-tl.blueyonder.co.uk,United Kingdom,81,182,0
33 | gsr01-cr.blueyonder.co.uk,United Kingdom,80,196,0
34 | kt1-3ja.bdr.ex.net,United Kingdom (London),81,181,0
35 | core1-gig2-0.bletchley.ukcore.bt.net,United Kingdom (Milton Keynes),0,0,100
36 | anhm7204.exo.com,California (Anaheim),0,0,100
37 | mc-gateway.lansmart.com,California (Fresno),96,37,0
38 | dnsauth1.sys.gtei.net,California (Los Angeles),98,19,0
39 | rx0ar-technicare.ed.bigpipeinc.com,Canada (Edmonton),91,80,0
40 | gw02.wlfdle.phub.net.cable.rogers.com,Canada (Ontario),100,0,0
41 | anguhub14.net.ubc.ca,Canada (Vancouver),0,0,100
42 | loopback0.gw2.den4.alter.net,Colorado (Denver),91,86,0
43 | router.firstcls.com,Georgia,0,0,100
44 | atl-datacenter-gw2.capitalinternet.com,Georgia (Atlanta),0,0,100
45 | loopback0.gw9.chi2.alter.net,Illinois (Chicago),90,91,0
46 | cisco-gnarly.n-connect.net,Iowa,89,108,0
47 | crystal-cavern.ctcco.com,Kansas (Lenexa),91,88,0
48 | cisco.syssrc.com,Maryland,91,83,0
49 | router-in.nemetschek.net,Maryland (Columbia),91,89,0
50 | pos1-0-0-155m.ar1.bos1.gblx.net,Massachusetts (Boston),91,86,0
51 | lan-d32-0606-0578.uninet-ide.com.mx,Mexico (Chihuahua),100,0,0
52 | rr1.torixt.avantel.net.mx,Mexico (Coahuila),92,79,0
53 | rr2.gdlmha.avantel.net.mx,Mexico (Guadalajara),92,77,0
54 | rr1.reyixt.avantel.net.mx,Mexico (Tamaulipas),90,96,0
55 | revenant.netservicesgroup.com,Michigan (Saginaw),92,77,0
56 | border0-e0.oc48-ypsi.hdl.com,Michigan (Ypsilanti),0,0,100
57 | wormhole.homeisp.com,Missouri (Kansas City),92,71,0
58 | pwps-core01.powerpulse.cc,Nevada (Las Vegas),96,30,0
59 | isp.state.nh.us,New Hampshire,100,0,0
60 | sugaree.arorapc.com,New Jersey,91,84,0
61 | ac-gw.dandy.net,New Jersey (Atlantic City),0,0,100
62 | 180.atm6-0.gw7.nyc9.alter.net,New York (NYC),92,77,0
63 | wookie.core.3z.net,Ohio (Cincinnati),0,0,100
64 | sl-bb21-pen-15-0.sprintlink.net,Pennsylvania (Philadelphia),0,0,100
65 | gw-inet.ktc.com,Texas,100,0,0
66 | core-router.centramedia.net,Texas (Pampa),0,0,100
67 | www.xmission.com,Utah (Salt Lake City),95,45,0
68 | er01.asbn.eli.net,Virginia (Ashburn),0,0,100
69 | core1-sttl.sitespecific.net,Washington (Seattle),0,0,100
70 | gate.netwrx1.com,Wisconsin,100,0,0
71 | core-1601-bmia-elkwpop-1-3.mia.net,Wisconsin (Elkhorn),100,0,0
72 | 5dl-dst-rt2.5ninesdata.com,Wisconsin (Madison),91,89,0
73 | rcorelma1-rcoreats1.impsat.net.ar,Argentina,81,180,0
74 | router-ut1.uniweb.net.co,Colombia (Valle del Cauca),82,173,0
75 | div-core-rtr1.americatelnet.com.pe,Peru (Lima),81,182,0
76 |
--------------------------------------------------------------------------------
/nbsource/elementary-sql.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 12,
6 | "metadata": {
7 | "collapsed": false
8 | },
9 | "outputs": [],
10 | "prompt_number": "",
11 | "source": [
12 | "import sqlite3\n",
13 | "conn = sqlite3.connect(\"MyDatabase.sql3\")\n",
14 | "curs = conn.cursor()"
15 | ]
16 | },
17 | {
18 | "cell_type": "code",
19 | "execution_count": 13,
20 | "metadata": {
21 | "collapsed": false
22 | },
23 | "outputs": [],
24 | "prompt_number": "",
25 | "source": [
26 | "curs.execute(\"SELECT * FROM publisher\")"
27 | ]
28 | },
29 | {
30 | "cell_type": "code",
31 | "execution_count": 18,
32 | "metadata": {
33 | "collapsed": false
34 | },
35 | "outputs": [],
36 | "prompt_number": "",
37 | "source": [
38 | "curs.execute(\"\"\"INSERT INTO Publisher (pubid, pubName, pubURL)\n",
39 | " VALUES (10, 'O''Reilly', 'www.ora.com')\"\"\")"
40 | ]
41 | },
42 | {
43 | "cell_type": "code",
44 | "execution_count": 21,
45 | "metadata": {
46 | "collapsed": false
47 | },
48 | "outputs": [],
49 | "prompt_number": "",
50 | "source": [
51 | "curs.execute(\"\"\"INSERT INTO Publisher (pubid, pubName, pubURL)\n",
52 | " VALUES (12, 'New Riders', 'www.newriders.com')\"\"\")"
53 | ]
54 | },
55 | {
56 | "cell_type": "code",
57 | "execution_count": null,
58 | "metadata": {
59 | "collapsed": false
60 | },
61 | "outputs": [],
62 | "prompt_number": "",
63 | "source": [
64 | "curs.execute(\"\"\"INSERT INTO Book (BkISBN, BkTitle, BkPubNo, BkYear)\n",
65 | " VALUES('1234444455', 'Python Web Programming', 2, 2002)\"\"\")"
66 | ]
67 | },
68 | {
69 | "cell_type": "code",
70 | "execution_count": 22,
71 | "metadata": {
72 | "collapsed": false
73 | },
74 | "outputs": [],
75 | "prompt_number": "",
76 | "source": [
77 | "curs.execute(\"select * from BOOK\")"
78 | ]
79 | },
80 | {
81 | "cell_type": "code",
82 | "execution_count": 23,
83 | "metadata": {
84 | "collapsed": false
85 | },
86 | "outputs": [],
87 | "prompt_number": "",
88 | "source": [
89 | "curs.fetchall()"
90 | ]
91 | },
92 | {
93 | "cell_type": "code",
94 | "execution_count": 24,
95 | "metadata": {
96 | "collapsed": false
97 | },
98 | "outputs": [],
99 | "prompt_number": "",
100 | "source": [
101 | "curs.description"
102 | ]
103 | },
104 | {
105 | "cell_type": "code",
106 | "execution_count": null,
107 | "metadata": {
108 | "collapsed": false
109 | },
110 | "outputs": [],
111 | "prompt_number": "",
112 | "source": [
113 | "curs.execute(\"\"\"INSERT INTO book (bkisbn, bktitle, bkyear, bkpubid)\n",
114 | " VALUES('1278077807', 'Python Web Programming', 2, 2002)"
115 | ]
116 | },
117 | {
118 | "cell_type": "code",
119 | "execution_count": 26,
120 | "metadata": {
121 | "collapsed": false
122 | },
123 | "outputs": [],
124 | "prompt_number": "",
125 | "source": [
126 | "curs.execute(\"SELECT bktitle, bkisbn, pubname from publisher\")\n",
127 | "curs.fetchall()\n",
128 | "curs.description"
129 | ]
130 | },
131 | {
132 | "cell_type": "code",
133 | "execution_count": 28,
134 | "metadata": {
135 | "collapsed": false
136 | },
137 | "outputs": [],
138 | "prompt_number": "",
139 | "source": [
140 | "curs.execute(\"\"\"SELECT BkTitle, BkISBN, PubName\n",
141 | " FROM Book JOIN Publisher ON bkpubid=pubid\"\"\")\n",
142 | "curs.fetchall()"
143 | ]
144 | },
145 | {
146 | "cell_type": "code",
147 | "execution_count": 29,
148 | "metadata": {
149 | "collapsed": false
150 | },
151 | "outputs": [],
152 | "prompt_number": "",
153 | "source": [
154 | "curs.execute(\"\"\"INSERT INTO Publisher (pubid, pubName, pubURL)\n",
155 | " VALUES (12, 'New Riders', 'www.pearson.com')\"\"\")\n",
156 | "curs.fetchall()"
157 | ]
158 | },
159 | {
160 | "cell_type": "code",
161 | "execution_count": 30,
162 | "metadata": {
163 | "collapsed": false
164 | },
165 | "outputs": [],
166 | "prompt_number": "",
167 | "source": [
168 | "curs.execute(\"\"\"INSERT INTO Publisher (pubid, pubName, pubURL)\n",
169 | " VALUES (12, 'New Riders', 'www.pearson.com')\"\"\")\n",
170 | "curs.fetchall()"
171 | ]
172 | },
173 | {
174 | "cell_type": "code",
175 | "execution_count": null,
176 | "metadata": {
177 | "collapsed": false
178 | },
179 | "outputs": [],
180 | "prompt_number": "",
181 | "source": []
182 | }
183 | ],
184 | "metadata": {},
185 | "nbformat": 4,
186 | "nbformat_minor": 0
187 | }
--------------------------------------------------------------------------------
/nbsource/from-import.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": null,
6 | "metadata": {
7 | "collapsed": false
8 | },
9 | "outputs": [],
10 | "prompt_number": "",
11 | "source": []
12 | }
13 | ],
14 | "metadata": {},
15 | "nbformat": 4,
16 | "nbformat_minor": 0
17 | }
--------------------------------------------------------------------------------
/nbsource/functions-as-function-arguments.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "
\n",
8 | "||| WORKING COPY ||| Functions as Function Arguments"
9 | ]
10 | },
11 | {
12 | "cell_type": "markdown",
13 | "metadata": {},
14 | "source": [
15 | "##Function Arguments"
16 | ]
17 | },
18 | {
19 | "cell_type": "markdown",
20 | "metadata": {},
21 | "source": [
22 | "All Python functions can be called with two types of argument. The first type, and the one you are most familiar with, is called positional, because it is associated with a parameter by the position it occupies in the argument list (which generally corresponds to the position of the parameter in the function signature). The second type is called a keyword argument. Keyword arguments are preceded by a name and an equals sign, and they are associated with parameters by name (so they don't have to appear in any particular order)."
23 | ]
24 | },
25 | {
26 | "cell_type": "markdown",
27 | "metadata": {},
28 | "source": [
29 | "If a call has any positional arguments, they must always appear before any keyword arguments. Thus, `\"...\".format(a, b, k1=c, k2=d)` is legal, but `\"...\".format(k1=c, k2=d, a, b)` is not (it will be flagged as a syntax error by the interpreter because keyword arguments may not precede positionals)."
30 | ]
31 | },
32 | {
33 | "cell_type": "markdown",
34 | "metadata": {},
35 | "source": [
36 | "###Field Names"
37 | ]
38 | },
39 | {
40 | "cell_type": "markdown",
41 | "metadata": {},
42 | "source": [
43 | "The first part of the replacement field, immediately following the opening curly bracket, is the field name. This tells the formatting engine which value is to be formatted. The field name begins with either a number, which specifies a positional argument to the `format()` method, or a name, which specifies a named argument. This can be followed by extra information that allows you to index the selected argument (which will presumably be an indexable object such as a list, tuple, or dict) or access one of its attributes."
44 | ]
45 | },
46 | {
47 | "cell_type": "markdown",
48 | "metadata": {},
49 | "source": [
50 | "These features alone can get you pretty far. Let's experiment now and get more comfortable programming by writing a slightly unusual program. Usually we expect to provide variable data to a program and format its results in a standard way. This time we'll provide you with standard data and let you enter format specifications that will select specific elements for display."
51 | ]
52 | },
53 | {
54 | "cell_type": "code",
55 | "execution_count": null,
56 | "metadata": {
57 | "collapsed": false
58 | },
59 | "outputs": [],
60 | "prompt_number": "",
61 | "source": [
62 | "i = 42\n",
63 | "r = 31.97\n",
64 | "c = 2.2 + 3.3j\n",
65 | "s = \"String\"\n",
66 | "lst = [\"zero\", \"one\", \"two\", \"three\", \"four\", \"five\"]\n",
67 | "dct = {\"Jim\": \"Dandy\",\n",
68 | " \"Stella\": \"DuBois\",\n",
69 | " 1: \"integer\"}"
70 | ]
71 | },
72 | {
73 | "cell_type": "code",
74 | "execution_count": null,
75 | "metadata": {
76 | "collapsed": false
77 | },
78 | "outputs": [],
79 | "prompt_number": "",
80 | "source": [
81 | "while True:\n",
82 | " fmt = input(\"Format string: \") \n",
83 | " if not fmt:\n",
84 | " break\n",
85 | " fms = \"{\"+fmt+\"}\"\n",
86 | " print(\"Format:\", fms, \"output:\", fms.format(i, r, c, s, e=lst, f=dct))"
87 | ]
88 | },
89 | {
90 | "cell_type": "code",
91 | "execution_count": null,
92 | "metadata": {
93 | "collapsed": false
94 | },
95 | "outputs": [],
96 | "prompt_number": "",
97 | "source": []
98 | },
99 | {
100 | "cell_type": "code",
101 | "execution_count": null,
102 | "metadata": {
103 | "collapsed": false
104 | },
105 | "outputs": [],
106 | "prompt_number": "",
107 | "source": []
108 | }
109 | ],
110 | "metadata": {},
111 | "nbformat": 4,
112 | "nbformat_minor": 0
113 | }
--------------------------------------------------------------------------------
/nbsource/generator-functions.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "#
||| WORKING COPY ||| Generator Functions"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "This document was created on 2014-03-15 at 00:34:09.565850."
15 | ]
16 | },
17 | {
18 | "cell_type": "code",
19 | "execution_count": null,
20 | "metadata": {
21 | "collapsed": false
22 | },
23 | "outputs": [],
24 | "prompt_number": "",
25 | "source": [
26 | "def f():\n",
27 | " return 42"
28 | ]
29 | },
30 | {
31 | "cell_type": "code",
32 | "execution_count": null,
33 | "metadata": {
34 | "collapsed": false
35 | },
36 | "outputs": [],
37 | "prompt_number": "",
38 | "source": [
39 | "def g():\n",
40 | " yield 42"
41 | ]
42 | },
43 | {
44 | "cell_type": "code",
45 | "execution_count": null,
46 | "metadata": {
47 | "collapsed": false
48 | },
49 | "outputs": [],
50 | "prompt_number": "",
51 | "source": [
52 | "f()"
53 | ]
54 | },
55 | {
56 | "cell_type": "code",
57 | "execution_count": null,
58 | "metadata": {
59 | "collapsed": false
60 | },
61 | "outputs": [],
62 | "prompt_number": "",
63 | "source": [
64 | "g()"
65 | ]
66 | },
67 | {
68 | "cell_type": "code",
69 | "execution_count": null,
70 | "metadata": {
71 | "collapsed": false
72 | },
73 | "outputs": [],
74 | "prompt_number": "",
75 | "source": [
76 | "g1 = g()\n",
77 | "dir(g1)"
78 | ]
79 | },
80 | {
81 | "cell_type": "code",
82 | "execution_count": null,
83 | "metadata": {
84 | "collapsed": false
85 | },
86 | "outputs": [],
87 | "prompt_number": "",
88 | "source": [
89 | "for thing in g1:\n",
90 | " print(thing)"
91 | ]
92 | },
93 | {
94 | "cell_type": "code",
95 | "execution_count": null,
96 | "metadata": {
97 | "collapsed": false
98 | },
99 | "outputs": [],
100 | "prompt_number": "",
101 | "source": [
102 | "for thing in g1:\n",
103 | " print(thing)"
104 | ]
105 | },
106 | {
107 | "cell_type": "code",
108 | "execution_count": null,
109 | "metadata": {
110 | "collapsed": false
111 | },
112 | "outputs": [],
113 | "prompt_number": "",
114 | "source": [
115 | "g2 = g()\n",
116 | "next(g2)"
117 | ]
118 | },
119 | {
120 | "cell_type": "code",
121 | "execution_count": null,
122 | "metadata": {
123 | "collapsed": false
124 | },
125 | "outputs": [],
126 | "prompt_number": "",
127 | "source": [
128 | "next(g2)"
129 | ]
130 | },
131 | {
132 | "cell_type": "code",
133 | "execution_count": null,
134 | "metadata": {
135 | "collapsed": false
136 | },
137 | "outputs": [],
138 | "prompt_number": "",
139 | "source": [
140 | "def n_of_each(iterable, n):\n",
141 | " for thing in iterable:\n",
142 | " for i in range(n):\n",
143 | " yield thing"
144 | ]
145 | },
146 | {
147 | "cell_type": "code",
148 | "execution_count": null,
149 | "metadata": {
150 | "collapsed": false
151 | },
152 | "outputs": [],
153 | "prompt_number": "",
154 | "source": [
155 | "for thing in n_of_each([\"a\", 2, []], 3):\n",
156 | " print(thing)"
157 | ]
158 | },
159 | {
160 | "cell_type": "code",
161 | "execution_count": null,
162 | "metadata": {
163 | "collapsed": false
164 | },
165 | "outputs": [],
166 | "prompt_number": "",
167 | "source": [
168 | "def n_times(iterable, n):\n",
169 | " for i in range(n):\n",
170 | " for thing in iterable:\n",
171 | " yield thing"
172 | ]
173 | },
174 | {
175 | "cell_type": "code",
176 | "execution_count": null,
177 | "metadata": {
178 | "collapsed": false
179 | },
180 | "outputs": [],
181 | "prompt_number": "",
182 | "source": [
183 | "for thing in n_times([\"a\", 2, []], 3):\n",
184 | " print(thing)"
185 | ]
186 | },
187 | {
188 | "cell_type": "code",
189 | "execution_count": null,
190 | "metadata": {
191 | "collapsed": false
192 | },
193 | "outputs": [],
194 | "prompt_number": "",
195 | "source": []
196 | }
197 | ],
198 | "metadata": {},
199 | "nbformat": 4,
200 | "nbformat_minor": 0
201 | }
--------------------------------------------------------------------------------
/nbsource/goodbye.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "#### PRE-RELEASE REVIEW: O'Reilly Intermediate Python Video Series\n",
8 | "#### Topic: Goodbye!\n",
9 | "Source located in `---invalid---`. Automatically rendered from `UNKNOWN` on 2014-03-21 at 10:21:36.443435.\n",
10 | "#### AUTOMATICALLY GENERATED TEST NOTEBOOK - CHANGES WILL BE LOST\n",
11 | "#### To TEST this Notebook
1. select Cell | Run All, or step through with Shift/Enter
2. Notebook turns pink when testing starts
3. It turns white when the it runs to completion.
4. Please check the executed notebook for clarity and correctness.
Please [raise an issue](https://github.com/holdenweb/intermediate-python/issues) about anything you don't understand - by all means attach a saved copy of the notebook to add explanations or questions. Also please let us know about stuff you want to see covered under any heading in the outline.\n",
12 | "#### Your comments on the content are also welcome, particularly when [reported as issues](https://github.com/holdenweb/intermediate-python/issues)"
13 | ]
14 | },
15 | {
16 | "cell_type": "markdown",
17 | "metadata": {},
18 | "source": [
19 | "
\n",
20 | "||| WORKING COPY ||| Tuples"
21 | ]
22 | },
23 | {
24 | "cell_type": "markdown",
25 | "metadata": {},
26 | "source": [
27 | "__How These Lessons Were Created__"
28 | ]
29 | },
30 | {
31 | "cell_type": "markdown",
32 | "metadata": {},
33 | "source": [
34 | "__The Python Software Foundation__\n",
35 | "\n",
36 | "In the words of the PSF:\n",
37 | "The mission of the Python Software Foundation is to promote, protect, and advance the Python programming language, and to support and facilitate the growth of a diverse and international community of Python programmers."
38 | ]
39 | },
40 | {
41 | "cell_type": "markdown",
42 | "metadata": {},
43 | "source": [
44 | "__EuroPython__"
45 | ]
46 | },
47 | {
48 | "cell_type": "markdown",
49 | "metadata": {},
50 | "source": [
51 | "__PyCons__"
52 | ]
53 | },
54 | {
55 | "cell_type": "markdown",
56 | "metadata": {},
57 | "source": [
58 | "__import this__"
59 | ]
60 | },
61 | {
62 | "cell_type": "code",
63 | "execution_count": null,
64 | "metadata": {
65 | "collapsed": false
66 | },
67 | "outputs": [],
68 | "prompt_number": "",
69 | "source": []
70 | }
71 | ],
72 | "metadata": {},
73 | "nbformat": 4,
74 | "nbformat_minor": 0
75 | }
--------------------------------------------------------------------------------
/nbsource/images:
--------------------------------------------------------------------------------
1 | ../data/images/nb
--------------------------------------------------------------------------------
/nbsource/introducing-the-ipython-notebook.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "#### PRE-RELEASE REVIEW: O'Reilly Intermediate Python Video Series\n",
8 | "#### Topic: Introducing the IPython Notebook \n",
9 | "Source located in `nbsource/introducing-the-ipython-notebook.ipynb`. Automatically rendered from `UNKNOWN` on 2014-03-21 at 10:21:36.572700.\n",
10 | "#### AUTOMATICALLY GENERATED TEST NOTEBOOK - CHANGES WILL BE LOST\n",
11 | "#### To TEST this Notebook
1. select Cell | Run All, or step through with Shift/Enter
2. Notebook turns pink when testing starts
3. It turns white when the it runs to completion.
4. Please check the executed notebook for clarity and correctness.
Please [raise an issue](https://github.com/holdenweb/intermediate-python/issues) about anything you don't understand - by all means attach a saved copy of the notebook to add explanations or questions. Also please let us know about stuff you want to see covered under any heading in the outline.\n",
12 | "#### Your comments on the content are also welcome, particularly when [reported as issues](https://github.com/holdenweb/intermediate-python/issues)"
13 | ]
14 | },
15 | {
16 | "cell_type": "markdown",
17 | "metadata": {},
18 | "source": [
19 | "#
||| WORKING COPY ||| Introducing the IPython Notebook"
20 | ]
21 | },
22 | {
23 | "cell_type": "markdown",
24 | "metadata": {},
25 | "source": [
26 | "This document was created on 2014-03-13 at 19:45:16.685080."
27 | ]
28 | },
29 | {
30 | "cell_type": "code",
31 | "execution_count": 2,
32 | "metadata": {
33 | "collapsed": false
34 | },
35 | "outputs": [],
36 | "prompt_number": "",
37 | "source": [
38 | "True"
39 | ]
40 | },
41 | {
42 | "cell_type": "code",
43 | "execution_count": 3,
44 | "metadata": {
45 | "collapsed": false
46 | },
47 | "outputs": [],
48 | "prompt_number": "",
49 | "source": [
50 | "True\n",
51 | "False"
52 | ]
53 | },
54 | {
55 | "cell_type": "code",
56 | "execution_count": 4,
57 | "metadata": {
58 | "collapsed": false
59 | },
60 | "outputs": [],
61 | "prompt_number": "",
62 | "source": [
63 | "True, False # We'll talk about tuples later"
64 | ]
65 | },
66 | {
67 | "cell_type": "markdown",
68 | "metadata": {},
69 | "source": [
70 | "Ctrl/Enter _vs_ Shift/Enter demonstration"
71 | ]
72 | },
73 | {
74 | "cell_type": "markdown",
75 | "metadata": {},
76 | "source": [
77 | "Magics"
78 | ]
79 | },
80 | {
81 | "cell_type": "code",
82 | "execution_count": null,
83 | "metadata": {
84 | "collapsed": false
85 | },
86 | "outputs": [],
87 | "prompt_number": "",
88 | "source": [
89 | "%%file sample.txt\n",
90 | "This is a sample data file\n",
91 | "It has precisely three lines\n",
92 | "Of which this is the last"
93 | ]
94 | },
95 | {
96 | "cell_type": "code",
97 | "execution_count": null,
98 | "metadata": {
99 | "collapsed": false
100 | },
101 | "outputs": [],
102 | "prompt_number": "",
103 | "source": [
104 | "open(\"sample.txt\").readlines()"
105 | ]
106 | },
107 | {
108 | "cell_type": "code",
109 | "execution_count": null,
110 | "metadata": {
111 | "collapsed": false
112 | },
113 | "outputs": [],
114 | "prompt_number": "",
115 | "source": [
116 | "%%file mymodule.py\n",
117 | "\n",
118 | "def f(x):\n",
119 | " print(\"x is:\", x)\n",
120 | " return x+42\n",
121 | "\n",
122 | "print(\"Loaded\")"
123 | ]
124 | },
125 | {
126 | "cell_type": "code",
127 | "execution_count": null,
128 | "metadata": {
129 | "collapsed": false
130 | },
131 | "outputs": [],
132 | "prompt_number": "",
133 | "source": [
134 | "import mymodule"
135 | ]
136 | },
137 | {
138 | "cell_type": "code",
139 | "execution_count": null,
140 | "metadata": {
141 | "collapsed": false
142 | },
143 | "outputs": [],
144 | "prompt_number": "",
145 | "source": [
146 | "mymodule.f(0)"
147 | ]
148 | },
149 | {
150 | "cell_type": "markdown",
151 | "metadata": {},
152 | "source": [
153 | "Repeat the above and demonstrate the need for kernel restart (or other action) after module changes"
154 | ]
155 | }
156 | ],
157 | "metadata": {},
158 | "nbformat": 4,
159 | "nbformat_minor": 0
160 | }
--------------------------------------------------------------------------------
/nbsource/iterables-vs-iterators.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "This document was created on 2014-03-25 at 13:50:06.243908."
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "#
||| WORKING COPY ||| Iterables vs. Iterators"
15 | ]
16 | },
17 | {
18 | "cell_type": "code",
19 | "execution_count": null,
20 | "metadata": {
21 | "collapsed": false
22 | },
23 | "outputs": [],
24 | "prompt_number": "",
25 | "source": [
26 | "test_list = [\"Roberta\", \"Tom\", \"Alice\", \"Dick\"]"
27 | ]
28 | },
29 | {
30 | "cell_type": "code",
31 | "execution_count": null,
32 | "metadata": {
33 | "collapsed": false
34 | },
35 | "outputs": [],
36 | "prompt_number": "",
37 | "source": [
38 | "iterator_1 = iter(test_list) # same as test_list.__iter__()"
39 | ]
40 | },
41 | {
42 | "cell_type": "code",
43 | "execution_count": null,
44 | "metadata": {
45 | "collapsed": false
46 | },
47 | "outputs": [],
48 | "prompt_number": "",
49 | "source": [
50 | "iterator_2 = iter(test_list)"
51 | ]
52 | },
53 | {
54 | "cell_type": "code",
55 | "execution_count": null,
56 | "metadata": {
57 | "collapsed": false
58 | },
59 | "outputs": [],
60 | "prompt_number": "",
61 | "source": [
62 | "id(iterator_1), id(iterator_2)"
63 | ]
64 | },
65 | {
66 | "cell_type": "code",
67 | "execution_count": null,
68 | "metadata": {
69 | "collapsed": false
70 | },
71 | "outputs": [],
72 | "prompt_number": "",
73 | "source": [
74 | "type(iterator_1)"
75 | ]
76 | },
77 | {
78 | "cell_type": "code",
79 | "execution_count": null,
80 | "metadata": {
81 | "collapsed": false
82 | },
83 | "outputs": [],
84 | "prompt_number": "",
85 | "source": [
86 | "for i in test_list:\n",
87 | " for j in test_list:\n",
88 | " print(i, j)"
89 | ]
90 | },
91 | {
92 | "cell_type": "code",
93 | "execution_count": null,
94 | "metadata": {
95 | "collapsed": false
96 | },
97 | "outputs": [],
98 | "prompt_number": "",
99 | "source": [
100 | "for i in iterator_1:\n",
101 | " print(\"outer loop\")\n",
102 | " for j in iterator_2:\n",
103 | " print(\"inner loop\")\n",
104 | " print(i, j)"
105 | ]
106 | },
107 | {
108 | "cell_type": "code",
109 | "execution_count": null,
110 | "metadata": {
111 | "collapsed": false
112 | },
113 | "outputs": [],
114 | "prompt_number": "",
115 | "source": [
116 | "iterator_3 = iter(test_list)\n",
117 | "for i in iterator_3:\n",
118 | " print(\"outer\")\n",
119 | " for j in iterator_3:\n",
120 | " print(\"inner\")\n",
121 | " print(i, j)"
122 | ]
123 | },
124 | {
125 | "cell_type": "code",
126 | "execution_count": null,
127 | "metadata": {
128 | "collapsed": false
129 | },
130 | "outputs": [],
131 | "prompt_number": "",
132 | "source": [
133 | "iterator_4 = iter(test_list)\n",
134 | "id(test_list), id(iter(test_list))"
135 | ]
136 | },
137 | {
138 | "cell_type": "code",
139 | "execution_count": null,
140 | "metadata": {
141 | "collapsed": false
142 | },
143 | "outputs": [],
144 | "prompt_number": "",
145 | "source": [
146 | "id(iterator_4), id(iter(iterator_4)), id(iterator_4.__iter__())"
147 | ]
148 | },
149 | {
150 | "cell_type": "code",
151 | "execution_count": null,
152 | "metadata": {
153 | "collapsed": false
154 | },
155 | "outputs": [],
156 | "prompt_number": "",
157 | "source": [
158 | "next(iterator_4), next(iterator_4)"
159 | ]
160 | },
161 | {
162 | "cell_type": "code",
163 | "execution_count": null,
164 | "metadata": {
165 | "collapsed": false
166 | },
167 | "outputs": [],
168 | "prompt_number": "",
169 | "source": [
170 | "iterator_4.__next__(), iterator_4.__next__()"
171 | ]
172 | },
173 | {
174 | "cell_type": "code",
175 | "execution_count": null,
176 | "metadata": {
177 | "collapsed": false
178 | },
179 | "outputs": [],
180 | "prompt_number": "",
181 | "source": [
182 | "iterator_4.__next__()"
183 | ]
184 | },
185 | {
186 | "cell_type": "code",
187 | "execution_count": null,
188 | "metadata": {
189 | "collapsed": false
190 | },
191 | "outputs": [],
192 | "prompt_number": "",
193 | "source": []
194 | }
195 | ],
196 | "metadata": {},
197 | "nbformat": 4,
198 | "nbformat_minor": 0
199 | }
--------------------------------------------------------------------------------
/nbsource/list-dict-and-set-comprehensions.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "#### PRE-RELEASE REVIEW: O'Reilly Intermediate Python Video Series\n",
8 | "#### Topic: List, Dict, and Set Comprehensions\n",
9 | "Source located in `nbsource/list-dict-and-set-comprehensions.ipynb`. Automatically rendered from `UNKNOWN` on 2014-03-21 at 10:21:36.587296.\n",
10 | "#### AUTOMATICALLY GENERATED TEST NOTEBOOK - CHANGES WILL BE LOST\n",
11 | "#### To TEST this Notebook
1. select Cell | Run All, or step through with Shift/Enter
2. Notebook turns pink when testing starts
3. It turns white when the it runs to completion.
4. Please check the executed notebook for clarity and correctness.
Please [raise an issue](https://github.com/holdenweb/intermediate-python/issues) about anything you don't understand - by all means attach a saved copy of the notebook to add explanations or questions. Also please let us know about stuff you want to see covered under any heading in the outline.\n",
12 | "#### Your comments on the content are also welcome, particularly when [reported as issues](https://github.com/holdenweb/intermediate-python/issues)"
13 | ]
14 | },
15 | {
16 | "cell_type": "markdown",
17 | "metadata": {},
18 | "source": [
19 | "#
||| WORKING COPY ||| List, Dict and Set Comprehensions"
20 | ]
21 | },
22 | {
23 | "cell_type": "markdown",
24 | "metadata": {},
25 | "source": [
26 | "This document was created on 2014-03-15 at 02:58:34.398524."
27 | ]
28 | },
29 | {
30 | "cell_type": "markdown",
31 | "metadata": {},
32 | "source": [
33 | "A very common paradigm for building a list is as follows"
34 | ]
35 | },
36 | {
37 | "cell_type": "code",
38 | "execution_count": 1,
39 | "metadata": {
40 | "collapsed": false
41 | },
42 | "outputs": [],
43 | "prompt_number": "",
44 | "source": [
45 | "squares_list = []\n",
46 | "for i in range(10):\n",
47 | " squares_list.append(i*i)\n",
48 | "\n",
49 | "squares_list"
50 | ]
51 | },
52 | {
53 | "cell_type": "markdown",
54 | "metadata": {},
55 | "source": [
56 | "The Python developers realised that it would be nice to be able to construct such lists \u201call in one go\u201d and invented the _list comprehension_. Using a comprehension we can write an expression that evaluates to the same list as above."
57 | ]
58 | },
59 | {
60 | "cell_type": "code",
61 | "execution_count": 2,
62 | "metadata": {
63 | "collapsed": false
64 | },
65 | "outputs": [],
66 | "prompt_number": "",
67 | "source": [
68 | "squares_list = [x*x for x in range(10)]\n",
69 | "squares_list"
70 | ]
71 | },
72 | {
73 | "cell_type": "code",
74 | "execution_count": 5,
75 | "metadata": {
76 | "collapsed": false
77 | },
78 | "outputs": [],
79 | "prompt_number": "",
80 | "source": [
81 | "x # note that x is purely local to the comprehension"
82 | ]
83 | },
84 | {
85 | "cell_type": "code",
86 | "execution_count": null,
87 | "metadata": {
88 | "collapsed": false
89 | },
90 | "outputs": [],
91 | "prompt_number": "",
92 | "source": []
93 | }
94 | ],
95 | "metadata": {},
96 | "nbformat": 4,
97 | "nbformat_minor": 0
98 | }
--------------------------------------------------------------------------------
/nbsource/mock-objects.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {
6 | "slideshow": {
7 | "slide_type": "slide"
8 | }
9 | },
10 | "source": [
11 | "#
||| WORKING COPY ||| Mock Objects"
12 | ]
13 | },
14 | {
15 | "cell_type": "markdown",
16 | "metadata": {},
17 | "source": [
18 | "This document was created on 2014-03-14 at 23:32:02.943813."
19 | ]
20 | },
21 | {
22 | "cell_type": "code",
23 | "execution_count": "",
24 | "metadata": {
25 | "collapsed": false,
26 | "slideshow": {
27 | "slide_type": "slide"
28 | }
29 | },
30 | "outputs": [],
31 | "prompt_number": "",
32 | "source": [
33 | "from unittest.mock import MagicMock, Mock, call"
34 | ]
35 | },
36 | {
37 | "cell_type": "code",
38 | "execution_count": "",
39 | "metadata": {
40 | "collapsed": false,
41 | "slideshow": {
42 | "slide_type": "fragment"
43 | }
44 | },
45 | "outputs": [],
46 | "prompt_number": "",
47 | "source": [
48 | "m1 = MagicMock()"
49 | ]
50 | },
51 | {
52 | "cell_type": "code",
53 | "execution_count": "",
54 | "metadata": {
55 | "collapsed": false,
56 | "slideshow": {
57 | "slide_type": "subslide"
58 | }
59 | },
60 | "outputs": [],
61 | "prompt_number": "",
62 | "source": [
63 | "dir(m1)"
64 | ]
65 | },
66 | {
67 | "cell_type": "code",
68 | "execution_count": "",
69 | "metadata": {
70 | "collapsed": false
71 | },
72 | "outputs": [],
73 | "prompt_number": "",
74 | "source": [
75 | "m1.method(1, 2, 3)"
76 | ]
77 | },
78 | {
79 | "cell_type": "code",
80 | "execution_count": "",
81 | "metadata": {
82 | "collapsed": false
83 | },
84 | "outputs": [],
85 | "prompt_number": "",
86 | "source": [
87 | "m1.attribute"
88 | ]
89 | },
90 | {
91 | "cell_type": "code",
92 | "execution_count": "",
93 | "metadata": {
94 | "collapsed": false
95 | },
96 | "outputs": [],
97 | "prompt_number": "",
98 | "source": [
99 | "m1.who_knows_what"
100 | ]
101 | },
102 | {
103 | "cell_type": "code",
104 | "execution_count": "",
105 | "metadata": {
106 | "collapsed": false
107 | },
108 | "outputs": [],
109 | "prompt_number": "",
110 | "source": [
111 | "m1.who_knows_what()"
112 | ]
113 | },
114 | {
115 | "cell_type": "code",
116 | "execution_count": "",
117 | "metadata": {
118 | "collapsed": false
119 | },
120 | "outputs": [],
121 | "prompt_number": "",
122 | "source": [
123 | "m1.method_calls"
124 | ]
125 | },
126 | {
127 | "cell_type": "code",
128 | "execution_count": "",
129 | "metadata": {
130 | "collapsed": false
131 | },
132 | "outputs": [],
133 | "prompt_number": "",
134 | "source": [
135 | "issubclass(Mock, type)"
136 | ]
137 | },
138 | {
139 | "cell_type": "code",
140 | "execution_count": "",
141 | "metadata": {
142 | "collapsed": false
143 | },
144 | "outputs": [],
145 | "prompt_number": "",
146 | "source": [
147 | "m1.method.assert_called_once_with(1, 2, 3)"
148 | ]
149 | },
150 | {
151 | "cell_type": "code",
152 | "execution_count": "",
153 | "metadata": {
154 | "collapsed": false
155 | },
156 | "outputs": [],
157 | "prompt_number": "",
158 | "source": [
159 | "m1.who_knows_what.assert_has_calls([call()])"
160 | ]
161 | },
162 | {
163 | "cell_type": "code",
164 | "execution_count": "",
165 | "metadata": {
166 | "collapsed": false
167 | },
168 | "outputs": [],
169 | "prompt_number": "",
170 | "source": [
171 | "m1.who_knows_what.assert_has_calls([call(1, 2, 3)])"
172 | ]
173 | },
174 | {
175 | "cell_type": "code",
176 | "execution_count": "",
177 | "metadata": {
178 | "collapsed": false
179 | },
180 | "outputs": [],
181 | "prompt_number": "",
182 | "source": [
183 | "m2 = MagicMock()"
184 | ]
185 | },
186 | {
187 | "cell_type": "code",
188 | "execution_count": "",
189 | "metadata": {
190 | "collapsed": false
191 | },
192 | "outputs": [],
193 | "prompt_number": "",
194 | "source": [
195 | "next(m2)"
196 | ]
197 | },
198 | {
199 | "cell_type": "code",
200 | "execution_count": "",
201 | "metadata": {
202 | "collapsed": false
203 | },
204 | "outputs": [],
205 | "prompt_number": "",
206 | "source": [
207 | "m2.method_calls"
208 | ]
209 | },
210 | {
211 | "cell_type": "code",
212 | "execution_count": "",
213 | "metadata": {
214 | "collapsed": false
215 | },
216 | "outputs": [],
217 | "prompt_number": "",
218 | "source": [
219 | "m2.mock_calls"
220 | ]
221 | },
222 | {
223 | "cell_type": "code",
224 | "execution_count": "",
225 | "metadata": {
226 | "collapsed": false
227 | },
228 | "outputs": [],
229 | "prompt_number": "",
230 | "source": [
231 | "m2.assert"
232 | ]
233 | },
234 | {
235 | "cell_type": "code",
236 | "execution_count": "",
237 | "metadata": {
238 | "collapsed": false
239 | },
240 | "outputs": [],
241 | "prompt_number": "",
242 | "source": [
243 | "class Mocking(metclass=Mock):\n",
244 | " pass"
245 | ]
246 | },
247 | {
248 | "cell_type": "code",
249 | "execution_count": "",
250 | "metadata": {
251 | "collapsed": false
252 | },
253 | "outputs": [],
254 | "prompt_number": "",
255 | "source": [
256 | "[n for n in dir(MagicMock) if n not in dir(Mock)]"
257 | ]
258 | },
259 | {
260 | "cell_type": "code",
261 | "execution_count": "",
262 | "metadata": {
263 | "collapsed": false
264 | },
265 | "outputs": [],
266 | "prompt_number": "",
267 | "source": [
268 | "[n for n in dir(Mock) if n not in dir(MagicMock)]"
269 | ]
270 | }
271 | ],
272 | "metadata": {},
273 | "nbformat": 4,
274 | "nbformat_minor": 0
275 | }
--------------------------------------------------------------------------------
/nbsource/more-about-imports.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "#
||| WORKING COPY ||| Advanced Imports"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "This document was created on 2014-03-18 at 23:03:23.304544."
15 | ]
16 | },
17 | {
18 | "cell_type": "markdown",
19 | "metadata": {},
20 | "source": [
21 | "The Python import system is a versatile aid to organizing program code in a modular way.\n",
22 | "\n",
23 | "When someone starts programming it is likely that all her programs with be \"monolithic\" - everything will be in one chunk in a single file. This is acceptable for small programs, but not a maintainable strategy as projects grow in size and number.\n",
24 | "\n",
25 | "Suppose she wants to use a function that she wrote for an earlier program. If her solution is to open up the old source file, copy the routine and paste it into her new program, she is storing up trouble (you will often hear this referred to as _technical debt_). If later she makes a nifty improvement to the function she has to edit both source files to let them take advantage of the improvement.\n",
26 | "\n",
27 | "The correct approach (or one correct approach, anyway) is to extract the function into a separate _module_, a `.py` file containing code that can be imported and used by any program with access to it. In this way the task of maintenance is greatly eased: the source only has to be updated once for both programs to benefit. Learning how to split a program up into modules is something that only comes with experience, but it is easy to make a start."
28 | ]
29 | },
30 | {
31 | "cell_type": "markdown",
32 | "metadata": {},
33 | "source": [
34 | "The simplest way to start using imports is to make use of the standard library. As your experience grows you will find that your knowledge of the standard library expands. It's good to read lots of code to gain that kind of experience in what modules the Python library offers and how to use them. Good modules to start with are `sys` and `os`."
35 | ]
36 | },
37 | {
38 | "cell_type": "code",
39 | "execution_count": 7,
40 | "metadata": {
41 | "collapsed": false
42 | },
43 | "outputs": [],
44 | "prompt_number": "",
45 | "source": [
46 | "import sys, os"
47 | ]
48 | },
49 | {
50 | "cell_type": "code",
51 | "execution_count": 8,
52 | "metadata": {
53 | "collapsed": false
54 | },
55 | "outputs": [],
56 | "prompt_number": "",
57 | "source": [
58 | "sys.path"
59 | ]
60 | },
61 | {
62 | "cell_type": "code",
63 | "execution_count": null,
64 | "metadata": {
65 | "collapsed": false
66 | },
67 | "outputs": [],
68 | "prompt_number": "",
69 | "source": [
70 | "for path in sys.path:\n",
71 | " print(\"---------------------------\")\n",
72 | " if os.path.isdir(path):\n",
73 | " !echo $path; cd $path; find . -name \\*.py -or -name \\*.so\n",
74 | " else:\n",
75 | " print(path)"
76 | ]
77 | },
78 | {
79 | "cell_type": "code",
80 | "execution_count": null,
81 | "metadata": {
82 | "collapsed": false
83 | },
84 | "outputs": [],
85 | "prompt_number": "",
86 | "source": []
87 | }
88 | ],
89 | "metadata": {},
90 | "nbformat": 4,
91 | "nbformat_minor": 0
92 | }
--------------------------------------------------------------------------------
/nbsource/names-to-avoid-and-why.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "#### PRE-RELEASE REVIEW: O'Reilly Intermediate Python Video Series\n",
8 | "#### Topic: Names to Avoid and Why\n",
9 | "Source located in `nbsource/names-to-avoid-and-why.ipynb`. Automatically rendered from `UNKNOWN` on 2014-03-21 at 10:21:36.572700.\n",
10 | "#### AUTOMATICALLY GENERATED TEST NOTEBOOK - CHANGES WILL BE LOST\n",
11 | "#### To TEST this Notebook
1. select Cell | Run All, or step through with Shift/Enter
2. Notebook turns pink when testing starts
3. It turns white when the it runs to completion.
4. Please check the executed notebook for clarity and correctness.
Please [raise an issue](https://github.com/holdenweb/intermediate-python/issues) about anything you don't understand - by all means attach a saved copy of the notebook to add explanations or questions. Also please let us know about stuff you want to see covered under any heading in the outline.\n",
12 | "#### Your comments on the content are also welcome, particularly when [reported as issues](https://github.com/holdenweb/intermediate-python/issues)"
13 | ]
14 | },
15 | {
16 | "cell_type": "markdown",
17 | "metadata": {},
18 | "source": [
19 | "This document was created on 2014-03-20 at 16:40:34.599697."
20 | ]
21 | },
22 | {
23 | "cell_type": "markdown",
24 | "metadata": {},
25 | "source": [
26 | "#
||| WORKING COPY ||| Names to Avoid, and Why"
27 | ]
28 | },
29 | {
30 | "cell_type": "raw",
31 | "metadata": {},
32 | "source": [
33 | "#!content nbsource/names-to-avoid-and-why.ipynb"
34 | ]
35 | },
36 | {
37 | "cell_type": "code",
38 | "execution_count": null,
39 | "metadata": {
40 | "collapsed": false
41 | },
42 | "outputs": [],
43 | "prompt_number": "",
44 | "source": [
45 | "[ x for x in dir() if \"built\" in x ]"
46 | ]
47 | },
48 | {
49 | "cell_type": "code",
50 | "execution_count": null,
51 | "metadata": {
52 | "collapsed": false
53 | },
54 | "outputs": [],
55 | "prompt_number": "",
56 | "source": [
57 | "dir(__builtin__)"
58 | ]
59 | },
60 | {
61 | "cell_type": "code",
62 | "execution_count": null,
63 | "metadata": {
64 | "collapsed": false
65 | },
66 | "outputs": [],
67 | "prompt_number": "",
68 | "source": [
69 | "len(dir(__builtin__))"
70 | ]
71 | },
72 | {
73 | "cell_type": "code",
74 | "execution_count": null,
75 | "metadata": {
76 | "collapsed": false
77 | },
78 | "outputs": [],
79 | "prompt_number": "",
80 | "source": [
81 | "sum"
82 | ]
83 | },
84 | {
85 | "cell_type": "code",
86 | "execution_count": null,
87 | "metadata": {
88 | "collapsed": false
89 | },
90 | "outputs": [],
91 | "prompt_number": "",
92 | "source": [
93 | "import keyword"
94 | ]
95 | },
96 | {
97 | "cell_type": "code",
98 | "execution_count": null,
99 | "metadata": {
100 | "collapsed": false
101 | },
102 | "outputs": [],
103 | "prompt_number": "",
104 | "source": [
105 | "dir(keyword)"
106 | ]
107 | },
108 | {
109 | "cell_type": "code",
110 | "execution_count": null,
111 | "metadata": {
112 | "collapsed": false
113 | },
114 | "outputs": [],
115 | "prompt_number": "",
116 | "source": [
117 | "keyword.iskeyword(\"def\")"
118 | ]
119 | },
120 | {
121 | "cell_type": "code",
122 | "execution_count": null,
123 | "metadata": {
124 | "collapsed": false
125 | },
126 | "outputs": [],
127 | "prompt_number": "",
128 | "source": [
129 | "keyword.iskeyword(\"print\")"
130 | ]
131 | },
132 | {
133 | "cell_type": "code",
134 | "execution_count": null,
135 | "metadata": {
136 | "collapsed": false
137 | },
138 | "outputs": [],
139 | "prompt_number": "",
140 | "source": [
141 | "kws = keyword.kwlist\n",
142 | "len(kws)"
143 | ]
144 | },
145 | {
146 | "cell_type": "code",
147 | "execution_count": null,
148 | "metadata": {
149 | "collapsed": false
150 | },
151 | "outputs": [],
152 | "prompt_number": "",
153 | "source": [
154 | "kws"
155 | ]
156 | },
157 | {
158 | "cell_type": "code",
159 | "execution_count": null,
160 | "metadata": {
161 | "collapsed": false
162 | },
163 | "outputs": [],
164 | "prompt_number": "",
165 | "source": [
166 | "class = 14"
167 | ]
168 | },
169 | {
170 | "cell_type": "markdown",
171 | "metadata": {},
172 | "source": [
173 | "Avoid easily-misread words such as \"l\", ..."
174 | ]
175 | },
176 | {
177 | "cell_type": "markdown",
178 | "metadata": {},
179 | "source": [
180 | "Module names should not \"mask out\" standard library modules"
181 | ]
182 | },
183 | {
184 | "cell_type": "code",
185 | "execution_count": null,
186 | "metadata": {
187 | "collapsed": false
188 | },
189 | "outputs": [],
190 | "prompt_number": "",
191 | "source": []
192 | }
193 | ],
194 | "metadata": {},
195 | "nbformat": 4,
196 | "nbformat_minor": 0
197 | }
--------------------------------------------------------------------------------
/nbsource/scipy.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 13,
6 | "metadata": {
7 | "collapsed": false
8 | },
9 | "outputs": [],
10 | "prompt_number": "",
11 | "source": [
12 | "%matplotlib inline"
13 | ]
14 | },
15 | {
16 | "cell_type": "code",
17 | "execution_count": 14,
18 | "metadata": {
19 | "collapsed": false
20 | },
21 | "outputs": [],
22 | "prompt_number": "",
23 | "source": [
24 | "#scipy provides a wrapper around, and additional functionality to numpy\n",
25 | "import numpy as np\n",
26 | "from pylab import figure, show\n",
27 | "from numpy import array\n",
28 | "import matplotlib\n",
29 | "import matplotlib.pyplot as plt\n",
30 | "from scipy.stats import norm"
31 | ]
32 | },
33 | {
34 | "cell_type": "code",
35 | "execution_count": 15,
36 | "metadata": {
37 | "collapsed": false
38 | },
39 | "outputs": [],
40 | "prompt_number": "",
41 | "source": [
42 | "# read data in and extract required values\n",
43 | "# internet traffic data source noted below\n",
44 | "source = 'http://www.internettrafficreport.com/details.htm'\n",
45 | "import csv\n",
46 | "reader = csv.reader(open(\"data/itr-data.csv\", \"r\"))\n",
47 | "num_data = []\n",
48 | "for router, city, indx, resp, loss in reader:\n",
49 | " num_data.append([float(indx), float(resp), float(loss)])\n",
50 | "data = np.array(num_data)"
51 | ]
52 | },
53 | {
54 | "cell_type": "code",
55 | "execution_count": 16,
56 | "metadata": {
57 | "collapsed": false
58 | },
59 | "outputs": [],
60 | "prompt_number": "",
61 | "source": [
62 | "print(data[0])"
63 | ]
64 | },
65 | {
66 | "cell_type": "code",
67 | "execution_count": 17,
68 | "metadata": {
69 | "collapsed": false
70 | },
71 | "outputs": [],
72 | "prompt_number": "",
73 | "source": [
74 | "#how much data do we have?\n",
75 | "data.shape\n"
76 | ]
77 | },
78 | {
79 | "cell_type": "code",
80 | "execution_count": 18,
81 | "metadata": {
82 | "collapsed": false
83 | },
84 | "outputs": [],
85 | "prompt_number": "",
86 | "source": [
87 | "#slice it into topics; each of these is a column of data\n",
88 | "indx = data[:, 0]\n",
89 | "response = data[:, 1]\n",
90 | "loss = data[:, 2]\n"
91 | ]
92 | },
93 | {
94 | "cell_type": "code",
95 | "execution_count": 19,
96 | "metadata": {
97 | "collapsed": false
98 | },
99 | "outputs": [],
100 | "prompt_number": "",
101 | "source": [
102 | "loss"
103 | ]
104 | },
105 | {
106 | "cell_type": "code",
107 | "execution_count": 20,
108 | "metadata": {
109 | "collapsed": false
110 | },
111 | "outputs": [],
112 | "prompt_number": "",
113 | "source": [
114 | "#scipy has statistical methods in scipy.stats\n",
115 | "(mean, std) = norm.fit(np.asarray(response))"
116 | ]
117 | },
118 | {
119 | "cell_type": "code",
120 | "execution_count": 21,
121 | "metadata": {
122 | "collapsed": false
123 | },
124 | "outputs": [],
125 | "prompt_number": "",
126 | "source": [
127 | "# plots histogram, capture its properties\n",
128 | "fig = plt.Figure()\n",
129 | "n, nbins, p = plt.hist(response, bins=10,\n",
130 | " normed=True, facecolor='blue', alpha=0.75)\n",
131 | "# Normalized probability distribution function\n",
132 | "y = matplotlib.mlab.normpdf(nbins, mean, std)\n",
133 | "plt.plot(nbins, y,'r-', linewidth=5)\n",
134 | "plt.xlabel('Response Time (ms)\\n[source: {0}]'.format(source))\n",
135 | "plt.ylabel('Fraction of Cities')\n",
136 | "plt.title(\"Internet Response Time 29 March 2014\\nmean: {0}, std. dev {1}\\n\".\n",
137 | " format(round(mean,1), round(std,1)))\n",
138 | "plt.grid(True)\n",
139 | "\n",
140 | "plt.show()"
141 | ]
142 | },
143 | {
144 | "cell_type": "code",
145 | "execution_count": 22,
146 | "metadata": {
147 | "collapsed": false
148 | },
149 | "outputs": [],
150 | "prompt_number": "",
151 | "source": [
152 | "# plots histogram, capture its properties\n",
153 | "fig = plt.figure(2)\n",
154 | "fig.set_size_inches(10, 4)\n",
155 | "plt.subplot(1, 2, 1)\n",
156 | "n, nbins, p = plt.hist(response, bins=20,\n",
157 | " normed=True, facecolor='blue', alpha=0.75)\n",
158 | "plt.xlabel('Response Time (ms)\\n[source: {0}]'.format(source))\n",
159 | "plt.ylabel('Fraction of Cities')\n",
160 | "plt.title(\"Internet Response Time 29 March 2014\\n\"\n",
161 | " \"mean: {0}, std. dev {1}\\n\".\n",
162 | " format(round(mean, 1), round(std, 1)))\n",
163 | "plt.subplot(1, 2, 2)\n",
164 | "y = matplotlib.mlab.normpdf(nbins, mean, std)\n",
165 | "plt.plot(nbins, y,'r-', linewidth=5)\n",
166 | "plt.ylabel(\"Probability\")\n",
167 | "plt.xlabel(\"Response time (ms)\")\n",
168 | "plt.grid(True)\n",
169 | "fig.show()"
170 | ]
171 | },
172 | {
173 | "cell_type": "code",
174 | "execution_count": 23,
175 | "metadata": {
176 | "collapsed": false
177 | },
178 | "outputs": [],
179 | "prompt_number": "",
180 | "source": [
181 | "from numpy import pi, cos, sin\n",
182 | "from scipy import r_\n",
183 | "from scipy.special import jn, jn_zeros\n",
184 | "def drumhead_height(n, k, distance, angle, t):\n",
185 | " nth_zero = jn_zeros(n, k)\n",
186 | " return cos(t)*cos(n*angle)*jn(n, distance*nth_zero)\n",
187 | "theta = r_[0:2*pi:50j]\n",
188 | "radius = r_[0:1:50j]\n",
189 | "x = array([r*cos(theta) for r in radius])\n",
190 | "y = array([r*sin(theta) for r in radius])\n",
191 | "z = array([drumhead_height(1, 1, r, theta, 0.5) for r in radius])"
192 | ]
193 | },
194 | {
195 | "cell_type": "code",
196 | "execution_count": 24,
197 | "metadata": {
198 | "collapsed": false
199 | },
200 | "outputs": [],
201 | "prompt_number": "",
202 | "source": [
203 | "from mpl_toolkits.mplot3d import Axes3D\n",
204 | "from matplotlib import cm\n",
205 | "fig = plt.figure()\n",
206 | "ax = Axes3D(fig)\n",
207 | "ax.plot_surface(x, y, z, rstride=1, cstride=1, cmap=cm.jet)\n",
208 | "ax.set_xlabel('X')\n",
209 | "ax.set_ylabel('Y')\n",
210 | "ax.set_zlabel('Z')\n",
211 | "fig.show()"
212 | ]
213 | },
214 | {
215 | "cell_type": "code",
216 | "execution_count": null,
217 | "metadata": {
218 | "collapsed": false
219 | },
220 | "outputs": [],
221 | "prompt_number": "",
222 | "source": []
223 | }
224 | ],
225 | "metadata": {},
226 | "nbformat": 4,
227 | "nbformat_minor": 0
228 | }
--------------------------------------------------------------------------------
/nbsource/string-formatting.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "
\n",
8 | "||| WORKING COPY ||| Functions as Function Arguments"
9 | ]
10 | },
11 | {
12 | "cell_type": "markdown",
13 | "metadata": {},
14 | "source": [
15 | "## How Many Ways Do We Need to Format Strings?"
16 | ]
17 | },
18 | {
19 | "cell_type": "markdown",
20 | "metadata": {},
21 | "source": [
22 | "In the beginning there was the __`%`__ operator. Much of its functionality was inspired by the C++ language"
23 | ]
24 | },
25 | {
26 | "cell_type": "markdown",
27 | "metadata": {},
28 | "source": [
29 | "If a call has any positional arguments, they must always appear before any keyword arguments. Thus, `\"...\".format(a, b, k1=c, k2=d)` is legal, but `\"...\".format(k1=c, k2=d, a, b)` is not (it will be flagged as a syntax error by the interpreter because keyword arguments may not precede positionals)."
30 | ]
31 | },
32 | {
33 | "cell_type": "markdown",
34 | "metadata": {},
35 | "source": [
36 | "###Field Names"
37 | ]
38 | },
39 | {
40 | "cell_type": "markdown",
41 | "metadata": {},
42 | "source": [
43 | "The first part of the replacement field, immediately following the opening curly bracket, is the field name. This tells the formatting engine which value is to be formatted. The field name begins with either a number, which specifies a positional argument to the `format()` method, or a name, which specifies a named (_i.e._ keyword) argument. This can be followed by extra information that allows you to index the selected argument or access one of its attributes."
44 | ]
45 | },
46 | {
47 | "cell_type": "markdown",
48 | "metadata": {},
49 | "source": [
50 | "These features alone can get you pretty far. Let's experiment now and get more comfortable programming by writing a slightly unusual program. Usually we expect to provide variable data to a program and format its results in a standard way. This time we'll provide you with standard data and let you enter format specifications that will select specific elements for display."
51 | ]
52 | },
53 | {
54 | "cell_type": "code",
55 | "execution_count": 2,
56 | "metadata": {
57 | "collapsed": false
58 | },
59 | "outputs": [],
60 | "prompt_number": "",
61 | "source": [
62 | "i = 42\n",
63 | "r = 31.97\n",
64 | "c = 2.2 + 3.3j\n",
65 | "s = \"String\"\n",
66 | "lst = [\"zero\", \"one\", \"two\", \"three\", \"four\", \"five\"]\n",
67 | "dct = {\"Jim\": \"Dandy\",\n",
68 | " \"Stella\": \"DuBois\",\n",
69 | " 1: \"integer\"}"
70 | ]
71 | },
72 | {
73 | "cell_type": "code",
74 | "execution_count": "*",
75 | "metadata": {
76 | "collapsed": false
77 | },
78 | "outputs": [],
79 | "prompt_number": "",
80 | "source": [
81 | "while True:\n",
82 | " fmt = input(\"Format string: \") \n",
83 | " if not fmt:\n",
84 | " break\n",
85 | " fms = \"{\"+fmt+\"}\"\n",
86 | " print(\"Format:\", fms, \"output:\", fms.format(i, r, c, s, e=lst, f=dct))"
87 | ]
88 | },
89 | {
90 | "cell_type": "code",
91 | "execution_count": null,
92 | "metadata": {
93 | "collapsed": false
94 | },
95 | "outputs": [],
96 | "prompt_number": "",
97 | "source": []
98 | },
99 | {
100 | "cell_type": "code",
101 | "execution_count": null,
102 | "metadata": {
103 | "collapsed": false
104 | },
105 | "outputs": [],
106 | "prompt_number": "",
107 | "source": []
108 | }
109 | ],
110 | "metadata": {},
111 | "nbformat": 4,
112 | "nbformat_minor": 0
113 | }
--------------------------------------------------------------------------------
/nbsource/unicode.txt:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/holdenweb/int-py-notes/71c53d109895e346f1f7c3ba225883dbe8d04484/nbsource/unicode.txt
--------------------------------------------------------------------------------
/nbsource/unit-testing.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "#
||| WORKING COPY ||| Unit Testing"
8 | ]
9 | },
10 | {
11 | "cell_type": "code",
12 | "execution_count": null,
13 | "metadata": {
14 | "collapsed": false
15 | },
16 | "outputs": [],
17 | "prompt_number": "",
18 | "source": [
19 | "from unittest import TestCase\n",
20 | "[name for name in dir(TestCase) if name.startswith(\"assert\")]"
21 | ]
22 | },
23 | {
24 | "cell_type": "code",
25 | "execution_count": null,
26 | "metadata": {
27 | "collapsed": false
28 | },
29 | "outputs": [],
30 | "prompt_number": "",
31 | "source": [
32 | "%%file widget.py\n",
33 | "#\n",
34 | "# The simplest thing that could possibly ... fail?\n",
35 | "#\n",
36 | "class Widget:\n",
37 | " pass"
38 | ]
39 | },
40 | {
41 | "cell_type": "code",
42 | "execution_count": null,
43 | "metadata": {
44 | "collapsed": false
45 | },
46 | "outputs": [],
47 | "prompt_number": "",
48 | "source": [
49 | "%%file test1.py\n",
50 | "import unittest\n",
51 | "from widget import Widget\n",
52 | "\n",
53 | "class WidgetSizeTestCase(unittest.TestCase):\n",
54 | " def test_creation(self):\n",
55 | " widget = Widget(\"My Widget\")\n",
56 | " self.assertEqual(widget.size(), (50, 50),\n",
57 | " \"Initial size is not (50, 50)\")\n",
58 | "if __name__ == \"__main__\":\n",
59 | " unittest.main()"
60 | ]
61 | },
62 | {
63 | "cell_type": "code",
64 | "execution_count": null,
65 | "metadata": {
66 | "collapsed": false
67 | },
68 | "outputs": [],
69 | "prompt_number": "",
70 | "source": [
71 | "!python test1.py"
72 | ]
73 | },
74 | {
75 | "cell_type": "markdown",
76 | "metadata": {},
77 | "source": [
78 | "The problem is with the instantiation call to the Widget class. The message isn't terribly good, but that isn't the testing framework's fault. Test failures are only reported when an `AssertionError` exception is raised inside its assertion methods. The fact that a test couldn't create a required fixture is sort of incidental. It still needs fixing, though, so this is reported as an error."
79 | ]
80 | },
81 | {
82 | "cell_type": "markdown",
83 | "metadata": {},
84 | "source": [
85 | "The message indicates a problem with a call to `object()` because the `Widget` class doesn't define an `__init__()` method and so `__init__()` is inherited from `object`, the ultimate superclass in the Python universe."
86 | ]
87 | },
88 | {
89 | "cell_type": "markdown",
90 | "metadata": {},
91 | "source": [
92 | "Sometimes responding to such an error in your tests requires a rewrite of a test, but in this case the failure is occuring because the object under test doesn't yet have the required functionality. So let's fix it by defining an `__init__()` method that accepts an argument. This means a small rewrite of `widget.py`."
93 | ]
94 | },
95 | {
96 | "cell_type": "markdown",
97 | "metadata": {},
98 | "source": [
99 | "This demonstrates a fundamental principle of test-driven development. ___Change your code only to fix a failing test___. Strictly speaking we should perhaps have started with only the test, and fixed the first error (which would have been an `ImportError`) by crating the `widget` module, but that would have been taking purism just a little too far."
100 | ]
101 | },
102 | {
103 | "cell_type": "code",
104 | "execution_count": null,
105 | "metadata": {
106 | "collapsed": false
107 | },
108 | "outputs": [],
109 | "prompt_number": "",
110 | "source": [
111 | "%%file widget.py\n",
112 | "#\n",
113 | "# Now with new __init__() method\n",
114 | "#\n",
115 | "class Widget:\n",
116 | " def __init__(self, title):\n",
117 | " self.title = title"
118 | ]
119 | },
120 | {
121 | "cell_type": "code",
122 | "execution_count": null,
123 | "metadata": {
124 | "collapsed": false
125 | },
126 | "outputs": [],
127 | "prompt_number": "",
128 | "source": [
129 | "!python test1.py"
130 | ]
131 | },
132 | {
133 | "cell_type": "code",
134 | "execution_count": null,
135 | "metadata": {
136 | "collapsed": false
137 | },
138 | "outputs": [],
139 | "prompt_number": "",
140 | "source": [
141 | "%%file widget.py\n",
142 | "#\n",
143 | "# With improved __init__() method and a default for initial size\n",
144 | "#\n",
145 | "class Widget:\n",
146 | " def __init__(self, title, size=(50, 50)):\n",
147 | " self.title = title\n",
148 | " self._size = size\n",
149 | " def size(self):\n",
150 | " pass"
151 | ]
152 | },
153 | {
154 | "cell_type": "code",
155 | "execution_count": null,
156 | "metadata": {
157 | "collapsed": false
158 | },
159 | "outputs": [],
160 | "prompt_number": "",
161 | "source": [
162 | "!python test1.py"
163 | ]
164 | },
165 | {
166 | "cell_type": "code",
167 | "execution_count": null,
168 | "metadata": {
169 | "collapsed": false
170 | },
171 | "outputs": [],
172 | "prompt_number": "",
173 | "source": [
174 | "%%file widget.py\n",
175 | "#\n",
176 | "# Now with size() method that does nothing\n",
177 | "#\n",
178 | "class Widget:\n",
179 | " def __init__(self, title, size=(50, 50)):\n",
180 | " self.title = title\n",
181 | " self._size = size\n",
182 | " def size(self):\n",
183 | " return self._size"
184 | ]
185 | },
186 | {
187 | "cell_type": "code",
188 | "execution_count": null,
189 | "metadata": {
190 | "collapsed": false
191 | },
192 | "outputs": [],
193 | "prompt_number": "",
194 | "source": [
195 | "!python test1.py"
196 | ]
197 | }
198 | ],
199 | "metadata": {},
200 | "nbformat": 4,
201 | "nbformat_minor": 0
202 | }
--------------------------------------------------------------------------------
/nbsource/using-json-data-from-the-web.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "##
||| WORKING COPY ||| Using JSON Data from the Web"
8 | ]
9 | },
10 | {
11 | "cell_type": "code",
12 | "execution_count": 7,
13 | "metadata": {
14 | "collapsed": false
15 | },
16 | "outputs": [],
17 | "prompt_number": "",
18 | "source": [
19 | "import requests\n",
20 | "import json"
21 | ]
22 | },
23 | {
24 | "cell_type": "code",
25 | "execution_count": 23,
26 | "metadata": {
27 | "collapsed": false
28 | },
29 | "outputs": [],
30 | "prompt_number": "",
31 | "source": [
32 | "response = requests.get(\"http://www.whitehouse.gov/facts/json/whatsnext/economy\")"
33 | ]
34 | },
35 | {
36 | "cell_type": "code",
37 | "execution_count": 24,
38 | "metadata": {
39 | "collapsed": false
40 | },
41 | "outputs": [],
42 | "prompt_number": "",
43 | "source": [
44 | "response.ok"
45 | ]
46 | },
47 | {
48 | "cell_type": "code",
49 | "execution_count": 36,
50 | "metadata": {
51 | "collapsed": false
52 | },
53 | "outputs": [],
54 | "prompt_number": "",
55 | "source": [
56 | "for h in response.headers:\n",
57 | " print(h, response.headers[h])"
58 | ]
59 | },
60 | {
61 | "cell_type": "code",
62 | "execution_count": 29,
63 | "metadata": {
64 | "collapsed": false
65 | },
66 | "outputs": [],
67 | "prompt_number": "",
68 | "source": [
69 | "access_data = json.loads(response.content.decode(\"utf-8\"))\n",
70 | "type(access_data)"
71 | ]
72 | },
73 | {
74 | "cell_type": "code",
75 | "execution_count": 30,
76 | "metadata": {
77 | "collapsed": false
78 | },
79 | "outputs": [],
80 | "prompt_number": "",
81 | "source": [
82 | "len(access_data)"
83 | ]
84 | },
85 | {
86 | "cell_type": "code",
87 | "execution_count": 39,
88 | "metadata": {
89 | "collapsed": false
90 | },
91 | "outputs": [],
92 | "prompt_number": "",
93 | "source": [
94 | "data = access_data[0]\n",
95 | "data"
96 | ]
97 | },
98 | {
99 | "cell_type": "code",
100 | "execution_count": 44,
101 | "metadata": {
102 | "collapsed": false
103 | },
104 | "outputs": [],
105 | "prompt_number": "",
106 | "source": [
107 | "from IPython.display import HTML\n",
108 | "output = []\n",
109 | "for item in access_data:\n",
110 | " output.append('''{}
'''.format(item['url'], item['body'], item['url_title']))\n",
111 | "HTML(\"\\n\".join(output))"
112 | ]
113 | },
114 | {
115 | "cell_type": "code",
116 | "execution_count": null,
117 | "metadata": {
118 | "collapsed": false
119 | },
120 | "outputs": [],
121 | "prompt_number": "",
122 | "source": []
123 | }
124 | ],
125 | "metadata": {},
126 | "nbformat": 4,
127 | "nbformat_minor": 0
128 | }
--------------------------------------------------------------------------------
/nbsource/what-happens-when-the-interpreter-starts.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "#### PRE-RELEASE REVIEW: O'Reilly Intermediate Python Video Series\n",
8 | "#### Topic: What Happens when the Interpreter Starts\n",
9 | "Source located in `nbsource/what-happens-when-the-interpreter-starts.ipynb`. Automatically rendered from `UNKNOWN` on 2014-03-21 at 10:21:36.572700.\n",
10 | "#### AUTOMATICALLY GENERATED TEST NOTEBOOK - CHANGES WILL BE LOST\n",
11 | "#### To TEST this Notebook
1. select Cell | Run All, or step through with Shift/Enter
2. Notebook turns pink when testing starts
3. It turns white when the it runs to completion.
4. Please check the executed notebook for clarity and correctness.
Please [raise an issue](https://github.com/holdenweb/intermediate-python/issues) about anything you don't understand - by all means attach a saved copy of the notebook to add explanations or questions. Also please let us know about stuff you want to see covered under any heading in the outline.\n",
12 | "#### Your comments on the content are also welcome, particularly when [reported as issues](https://github.com/holdenweb/intermediate-python/issues)"
13 | ]
14 | },
15 | {
16 | "cell_type": "markdown",
17 | "metadata": {},
18 | "source": [
19 | "#
||| WORKING COPY ||| What Happens When the Interpreter Starts"
20 | ]
21 | },
22 | {
23 | "cell_type": "markdown",
24 | "metadata": {},
25 | "source": [
26 | "This document was created on 2014-03-15 at 01:03:55.356290."
27 | ]
28 | },
29 | {
30 | "cell_type": "markdown",
31 | "metadata": {},
32 | "source": [
33 | "When the interpreter is started it \n",
34 | "\n",
35 | "The interpreter finally creates a new namespace with a special name, `\"__main__\"`, to act as the global namespace for the program (or sequence of interactive commands) that is about to be executed.\n",
36 | "\n",
37 | "This new namespace is populated with a few items. In a standard interactive terminal session in a shell window we see six names in a fairly recent Python release."
38 | ]
39 | },
40 | {
41 | "cell_type": "markdown",
42 | "metadata": {},
43 | "source": [
44 | " Python 3.3.2 (default, Nov 19 2013, 03:15:33)\n",
45 | " [GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)] on darwin\n",
46 | " Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n",
47 | " >>> dir()\n",
48 | " ['__builtins__', '__doc__', '__loader__', '__name__', '__package__']\n",
49 | " >>>"
50 | ]
51 | },
52 | {
53 | "cell_type": "code",
54 | "execution_count": 9,
55 | "metadata": {
56 | "collapsed": false
57 | },
58 | "outputs": [],
59 | "prompt_number": "",
60 | "source": [
61 | "dir() # Compare IPython Notebook with the standard interpreter environment"
62 | ]
63 | },
64 | {
65 | "cell_type": "markdown",
66 | "metadata": {},
67 | "source": [
68 | "The notebook server injects quite a lot of other names into your interpreter namespace, and doesn't provide others. You'll not be troubled by most of the differences, but you might like to see what they are."
69 | ]
70 | },
71 | {
72 | "cell_type": "code",
73 | "execution_count": 10,
74 | "metadata": {
75 | "collapsed": false
76 | },
77 | "outputs": [],
78 | "prompt_number": "",
79 | "source": [
80 | "[name for name in dir()\n",
81 | " if name not in {'__builtins__', '__doc__', '__loader__', '__name__', '__package__'}]"
82 | ]
83 | },
84 | {
85 | "cell_type": "code",
86 | "execution_count": 11,
87 | "metadata": {
88 | "collapsed": false
89 | },
90 | "outputs": [],
91 | "prompt_number": "",
92 | "source": [
93 | "dir()"
94 | ]
95 | },
96 | {
97 | "cell_type": "markdown",
98 | "metadata": {},
99 | "source": [
100 | "If you look carefully you will see that the namespace grows as you enter more commands and the notebook server accumulates more history - `_1` will now have been joined by `_2` and `_3`by the next cell. Remember that none of this stuff happens if you are just using a plain interpreter, but most programming environments will inject something into your namespace."
101 | ]
102 | },
103 | {
104 | "cell_type": "code",
105 | "execution_count": 12,
106 | "metadata": {
107 | "collapsed": false
108 | },
109 | "outputs": [],
110 | "prompt_number": "",
111 | "source": [
112 | "_i1, _2"
113 | ]
114 | },
115 | {
116 | "cell_type": "markdown",
117 | "metadata": {},
118 | "source": [
119 | "Since almost all the names begin with an underline, it isn't difficult to screen them out if you want to. Here I'm using a list comprehesnion with a condition."
120 | ]
121 | },
122 | {
123 | "cell_type": "code",
124 | "execution_count": 13,
125 | "metadata": {
126 | "collapsed": false
127 | },
128 | "outputs": [],
129 | "prompt_number": "",
130 | "source": [
131 | "[n for n in dir() if not n.startswith(\"_\")]"
132 | ]
133 | },
134 | {
135 | "cell_type": "markdown",
136 | "metadata": {},
137 | "source": [
138 | "So you see the notebook keeps inputs (and the corresponding outputs) available for later use, which can be very convenient if you want access to what you have been doing."
139 | ]
140 | },
141 | {
142 | "cell_type": "code",
143 | "execution_count": 13,
144 | "metadata": {
145 | "collapsed": false
146 | },
147 | "outputs": [],
148 | "prompt_number": "",
149 | "source": []
150 | }
151 | ],
152 | "metadata": {},
153 | "nbformat": 4,
154 | "nbformat_minor": 0
155 | }
--------------------------------------------------------------------------------
/nbsource2/Adventures in Software Engineering 1.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "### Adventures in Software Engineering #1\n",
8 | "\n",
9 | "This simple exercise shows how to handle a text file whose content is slightly off specification.\n",
10 | "\n",
11 | "The `csv` module is intended to handle files containing comma-separated data. Such data is sourced\n",
12 | "by many different utilities, not all of which correctly observe specifications. I found one such\n",
13 | "file in my own data, a list of videos."
14 | ]
15 | },
16 | {
17 | "cell_type": "code",
18 | "execution_count": null,
19 | "metadata": {
20 | "collapsed": false
21 | },
22 | "outputs": [],
23 | "prompt_number": "",
24 | "source": [
25 | "import csv"
26 | ]
27 | },
28 | {
29 | "cell_type": "code",
30 | "execution_count": null,
31 | "metadata": {
32 | "collapsed": false
33 | },
34 | "outputs": [],
35 | "prompt_number": "",
36 | "source": [
37 | "reader = csv.reader(open(\"data/videos.txt\", \"r\"))\n",
38 | "reader"
39 | ]
40 | },
41 | {
42 | "cell_type": "code",
43 | "execution_count": null,
44 | "metadata": {
45 | "collapsed": false
46 | },
47 | "outputs": [],
48 | "prompt_number": "",
49 | "source": [
50 | "next(reader)"
51 | ]
52 | },
53 | {
54 | "cell_type": "markdown",
55 | "metadata": {},
56 | "source": [
57 | "As you might spot, there are some very funny things going on in this file.\n",
58 | "Let's take a look at the first few lines of data."
59 | ]
60 | },
61 | {
62 | "cell_type": "code",
63 | "execution_count": null,
64 | "metadata": {
65 | "collapsed": false
66 | },
67 | "outputs": [],
68 | "prompt_number": "",
69 | "source": [
70 | "open(\"data/videos.txt\", \"r\").readlines()[:5]"
71 | ]
72 | },
73 | {
74 | "cell_type": "markdown",
75 | "metadata": {},
76 | "source": [
77 | "The issue here is the spaces that follow the commas intended to separate the fields.\n",
78 | "This is causing the following double-quote to _not_ be treated as a field delimiter.\n",
79 | "\n",
80 | "The `csv` module allows you to define _dialects_, which affect how the file is interpreted.\n",
81 | "Since this is a very simple issue, however, I chose instead to create a object that could\n",
82 | "be used instead of the file, masking the defects of the original data.\n",
83 | "\n",
84 | "Python's `io` module defines the `BytesIO` object, essentially an in-memory replacement for the binary file.\n",
85 | "Below I create one by concatenating all the lines of the input file after removing the space in\n",
86 | "any occurrence of the sequence `'\", \"'`, rendering it `'\",\"'` instead.\n",
87 | "Note, however, that this operation should really be a string operation."
88 | ]
89 | },
90 | {
91 | "cell_type": "code",
92 | "execution_count": null,
93 | "metadata": {
94 | "collapsed": false
95 | },
96 | "outputs": [],
97 | "prompt_number": "",
98 | "source": [
99 | "from io import BytesIO"
100 | ]
101 | },
102 | {
103 | "cell_type": "code",
104 | "execution_count": null,
105 | "metadata": {
106 | "collapsed": false
107 | },
108 | "outputs": [],
109 | "prompt_number": "",
110 | "source": [
111 | "file = open(\"data/videos.txt\", \"r\")\n",
112 | "newfile = BytesIO(\"\".join(line.replace('\", \"', '\",\"') for line in file))"
113 | ]
114 | },
115 | {
116 | "cell_type": "markdown",
117 | "metadata": {},
118 | "source": [
119 | "Rewinding the `StringIO` to the beginning of its content with the `.seek()` method allows\n",
120 | "you to use it as the argument when creating a new `csv.reader ` object.\n",
121 | "Iterating over that object shows the data being interpreted correctly"
122 | ]
123 | },
124 | {
125 | "cell_type": "code",
126 | "execution_count": null,
127 | "metadata": {
128 | "collapsed": false
129 | },
130 | "outputs": [],
131 | "prompt_number": "",
132 | "source": [
133 | "newfile.seek(0)\n",
134 | "list(t for t in csv.reader(newfile))"
135 | ]
136 | }
137 | ],
138 | "metadata": {
139 | "kernelspec": {
140 | "display_name": "Python 2",
141 | "language": "python",
142 | "name": "python2"
143 | },
144 | "language_info": {
145 | "codemirror_mode": {
146 | "name": "ipython",
147 | "version": 2
148 | },
149 | "file_extension": ".py",
150 | "mimetype": "text/x-python",
151 | "name": "python",
152 | "nbconvert_exporter": "python",
153 | "pygments_lexer": "ipython2",
154 | "version": "2.7.2"
155 | }
156 | },
157 | "nbformat": 4,
158 | "nbformat_minor": 0
159 | }
--------------------------------------------------------------------------------
/nbsource2/Introduction.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "#ABOUT THE CLASS"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "## Not a lecture class\n",
15 | "\n",
16 | "## All Experience-based\n",
17 | "\n",
18 | "## Discussions are not optional\n",
19 | "* Otherwise we'll be done by lunchtime on Day 1\n",
20 | "\n",
21 | "## We can talk about anything\n",
22 | "* This is about what you need to know\n",
23 | "\n",
24 | "## Post-course notebooks will be delivered\n",
25 | "\n",
26 | "## After Each Presented Topic\n",
27 | "###Possible Discussions\n",
28 | "\n",
29 | "* My suggestions\n",
30 | "\n",
31 | "###And, of course, whatever _you_ want ..."
32 | ]
33 | }
34 | ],
35 | "metadata": {
36 | "kernelspec": {
37 | "display_name": "Python 2",
38 | "language": "python",
39 | "name": "python2"
40 | },
41 | "language_info": {
42 | "codemirror_mode": {
43 | "name": "ipython",
44 | "version": 2
45 | },
46 | "file_extension": ".py",
47 | "mimetype": "text/x-python",
48 | "name": "python",
49 | "nbconvert_exporter": "python",
50 | "pygments_lexer": "ipython2",
51 | "version": "2.7.2"
52 | }
53 | },
54 | "nbformat": 4,
55 | "nbformat_minor": 0
56 | }
--------------------------------------------------------------------------------
/nbsource2/MyDatabase.sql3:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/holdenweb/int-py-notes/71c53d109895e346f1f7c3ba225883dbe8d04484/nbsource2/MyDatabase.sql3
--------------------------------------------------------------------------------
/nbsource2/MyDatabase.sql3-journal:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/holdenweb/int-py-notes/71c53d109895e346f1f7c3ba225883dbe8d04484/nbsource2/MyDatabase.sql3-journal
--------------------------------------------------------------------------------
/nbsource2/a-python-limerick.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 1,
6 | "metadata": {
7 | "collapsed": false
8 | },
9 | "outputs": [],
10 | "prompt_number": "",
11 | "source": [
12 | "# With acknowledgments to Micheal Spencer\n",
13 | "\n",
14 | "from itertools import repeat\n",
15 | "for feet in [3,3,2,2,3]:\n",
16 | " print \" \".join(\"DA-DA-DUM\"\n",
17 | " for dummy in [None]\n",
18 | "for foot in repeat(\"metric\", feet))"
19 | ]
20 | },
21 | {
22 | "cell_type": "code",
23 | "execution_count": null,
24 | "metadata": {
25 | "collapsed": true
26 | },
27 | "outputs": [],
28 | "prompt_number": "",
29 | "source": []
30 | }
31 | ],
32 | "metadata": {
33 | "kernelspec": {
34 | "display_name": "Python 2",
35 | "language": "python",
36 | "name": "python2"
37 | },
38 | "language_info": {
39 | "codemirror_mode": {
40 | "name": "ipython",
41 | "version": 2
42 | },
43 | "file_extension": ".py",
44 | "mimetype": "text/x-python",
45 | "name": "python",
46 | "nbconvert_exporter": "python",
47 | "pygments_lexer": "ipython2",
48 | "version": "2.7.2"
49 | }
50 | },
51 | "nbformat": 4,
52 | "nbformat_minor": 0
53 | }
--------------------------------------------------------------------------------
/nbsource2/accessing-ipython-on-the-cloud.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Running IPython Notebooks in the Cloud"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "First you need to have a Google account (which is also your Notebook Cloud Server account - it's an Appspot app) and an Amazon AWS account. None of this costs anything; this is a really easy way to access Python.\n",
15 | "\n",
16 | "Signing Up for Notebook Cloud Services\n",
17 | "All the computing you need to evaluate these notebooks (and a whole bunch more!) can be accomplished with virtuals that qualify for Amazon's free-tier pricing. Free-tier remains free for the first twelve months, and it's unlikely anything you do will exceed the very generous limits. To sign up for an AWS account:"
18 | ]
19 | },
20 | {
21 | "cell_type": "markdown",
22 | "metadata": {},
23 | "source": [
24 | "\n",
25 | "Visit the Amazon AWS home page http://aws.amazon.com/\n",
26 | " \n",
27 | "Click the \u201cSign Up\u201d link\n",
28 | "\n",
29 | "Enter an email address, select \u201cI am a new user\u201d and click \u201cSign in ..\u201d"
30 | ]
31 | },
32 | {
33 | "cell_type": "markdown",
34 | "metadata": {},
35 | "source": [
36 | "Once you have opened your account (there's the usual \"please confirm\" email with a link) you need to sign up with Notebook Cloud, a Google AppSpot application that uses AWS services on your behalf to create virtual machines running the IPython Notebook server on demand."
37 | ]
38 | },
39 | {
40 | "cell_type": "markdown",
41 | "metadata": {},
42 | "source": [
43 | "Visit the notebook cloud home page http://notebookcloud.appspot.com/\n",
44 | "\n",
45 | "Take a quick look at the simple documentation, and click \"Log in with Google Account.\" Once logged in, click the \"Account Details\" button, enter your AWS credentials (instructions how to get them are shown) and click \"Save.\""
46 | ]
47 | },
48 | {
49 | "cell_type": "markdown",
50 | "metadata": {},
51 | "source": [
52 | "__NOTE__: you can password-protect all your notebooks by entering a password on this form. There is no mechanism to rerieve this password - you have been warned!"
53 | ]
54 | },
55 | {
56 | "cell_type": "markdown",
57 | "metadata": {},
58 | "source": [
59 | "__Using the Notebook Cloud Service__\n",
60 | "\n",
61 | "You are now looking at the IPython Notebook Cloud server control window. For this purpose you only need to start the very smallest instance.\n",
62 | "\n",
63 | "Click the \"Micro\" button to create a new Notebook Server\n",
64 | "\n",
65 | "It will take a minute or two to create a new virtual machine. The Cloud server monitors the status of the virtual machine. Wait until it says \"Serving\" on the status line under the machine description, you are looking for something like:"
66 | ]
67 | },
68 | {
69 | "cell_type": "markdown",
70 | "metadata": {},
71 | "source": [
72 | "__Instance id: i-706d2551 Type: t1.micro Started: 01:04:36 ~ 2014-03-11__\n",
73 | "\n",
74 | "__State__: Serving"
75 | ]
76 | },
77 | {
78 | "cell_type": "markdown",
79 | "metadata": {},
80 | "source": [
81 | "Click on the word \"serving\" and a new window will open up with a notebook server showing an empty list of files. You can then drag Notebook (.ipynb) files into the (initially empty) files list, and an upload button appears. After uploading a notebook you can start it. Each notebook appears in its own browser window or tab.\n",
82 | "\n",
83 | "After updating a notebook you can download a copy with the Notebook Server's \"File | Download As ...\" menu item. This allows you to download either a Python file (in which all the markdown text appears as comments) or a Notebook file that you can upload to another server if you want.\n",
84 | "\n",
85 | "Your notebooks persist on the server between sessions. You can choose, though it's not necessary, to shut them down before closing or shutting down the Notebook Server. When you are done, you might want to shut your Notebook Server down. There's no point leaving the clock ticking when it's not being used, but at the same time it costs hardly anything. I just don't like waste, you could be computing with those cycles. :)\n",
86 | "\n",
87 | "In the Cloud Server window click the \"Stop\" button next to your chosen virtual. It takes a while (maybe up to a minute: what do you care, it's free, right?) to shut down a running virtual. The next time you visit your Notebook Cloud account (or even immediately!) you can restart it by clicking on its \"Start\" button.\n",
88 | "\n",
89 | "The \"Terminate\" button removes the VM instance completely, destroying its file store (so better make sure that you have downloaded copies of all Notebooks you may want to keep before you terminate an instance)."
90 | ]
91 | },
92 | {
93 | "cell_type": "markdown",
94 | "metadata": {},
95 | "source": [
96 | "__Reviewing the Course Content__\n",
97 | "\n",
98 | "Download the project (presumably you will simply clone the repository). Upload them to your Notebook Cloud server and start them.\n",
99 | "\n",
100 | "You can step through the notebook cell by cell using the SHIFT/Enter conbination. Alternatively you can select \"Cell | Run All\" from the notebook server menu and then scroll through the completed notebook to understand the results.\n",
101 | "\n",
102 | "Take a look at unstarted.txt and let me know what you would like to see under any and all topics. To ensure suggestions do not simply get lost I would appreciate reviewers raising issues. A link to the tracker is included in all notebooks."
103 | ]
104 | }
105 | ],
106 | "metadata": {
107 | "kernelspec": {
108 | "display_name": "Python 2",
109 | "language": "python",
110 | "name": "python2"
111 | },
112 | "language_info": {
113 | "codemirror_mode": {
114 | "name": "ipython",
115 | "version": 2
116 | },
117 | "file_extension": ".py",
118 | "mimetype": "text/x-python",
119 | "name": "python",
120 | "nbconvert_exporter": "python",
121 | "pygments_lexer": "ipython2",
122 | "version": "2.7.2"
123 | }
124 | },
125 | "nbformat": 4,
126 | "nbformat_minor": 0
127 | }
--------------------------------------------------------------------------------
/nbsource2/binary.dat:
--------------------------------------------------------------------------------
1 | [0][1][2][3][4][5][6][7][8][9][10][11][12][13][14][15][16][17][18][19][20][21][22][23][24][25][26][27][28][29][30][31][32][33][34][35][36][37][38][39][40][41][42][43][44][45][46][47][48][49][50][51][52][53][54][55][56][57][58][59][60][61][62][63][64][65][66][67][68][69][70][71][72][73][74][75][76][77][78][79][80][81][82][83][84][85][86][87][88][89][90][91][92][93][94][95][96][97][98][99][100][101][102][103][104][105][106][107][108][109][110][111][112][113][114][115][116][117][118][119][120][121][122][123][1 4][125][126][127][128][129][130][131][132][133][134][135][136][137][138][139][140][141][142][143][144][145][146][147][148][149][150][151][152][153][154][155][156][157][158][159][160][161][162][163][164][165][166][167][168][169][170][171][172][173][174][175][176][177][178][179][180][181][182][183][184][185][186][187][188][189][190][191][192][193][194][195][196][197][198][199][200][201][202][203][204][205][206][207][208][209][210][211][212][213][214][215][216][217][218][219][220][221][222][223][224][225][226][227][228][229][230][231][232][233][234][235][236][237][238][239][240][241][242][243][244][245][246][247][248][249][250][251][252][253][254][255][0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
--------------------------------------------------------------------------------
/nbsource2/closures.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "#Closures"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "A closure is a function that encapsulates the values of one or more values passed in from the outside environment.\n",
15 | "\n",
16 | "Here's a regular function `f()` and a function `c()` that returns a closure."
17 | ]
18 | },
19 | {
20 | "cell_type": "code",
21 | "execution_count": null,
22 | "metadata": {
23 | "collapsed": false
24 | },
25 | "outputs": [],
26 | "prompt_number": "",
27 | "source": [
28 | "def f(y):\n",
29 | " return y\n",
30 | "\n",
31 | "def c(x):\n",
32 | " def f(y):\n",
33 | " return x\n",
34 | " return f"
35 | ]
36 | },
37 | {
38 | "cell_type": "markdown",
39 | "metadata": {},
40 | "source": [
41 | "We can assign functions just like any other object in Python.\n",
42 | "So let's capture instances of both, then disassemble them."
43 | ]
44 | },
45 | {
46 | "cell_type": "code",
47 | "execution_count": null,
48 | "metadata": {
49 | "collapsed": false
50 | },
51 | "outputs": [],
52 | "prompt_number": "",
53 | "source": [
54 | "func = f\n",
55 | "closure = c(42)"
56 | ]
57 | },
58 | {
59 | "cell_type": "markdown",
60 | "metadata": {},
61 | "source": [
62 | "Note that the closure uses a different bytecode to load the value of `x` than the function does to return the value of `y`.\n",
63 | "When the closure is compliled it creates what is known as a _cell_ to store references to objects outside the functions scope. This captures the value, and retains it to avoid it becoming garbage if other external references to it are dropped.\n",
64 | "The `LOAD_DEREF` bytecode simply extracts a value from the cell."
65 | ]
66 | },
67 | {
68 | "cell_type": "code",
69 | "execution_count": null,
70 | "metadata": {
71 | "collapsed": false
72 | },
73 | "outputs": [],
74 | "prompt_number": "",
75 | "source": [
76 | "from dis import dis\n",
77 | "dis(func)"
78 | ]
79 | },
80 | {
81 | "cell_type": "code",
82 | "execution_count": null,
83 | "metadata": {
84 | "collapsed": false
85 | },
86 | "outputs": [],
87 | "prompt_number": "",
88 | "source": [
89 | "dis(closure)"
90 | ]
91 | },
92 | {
93 | "cell_type": "markdown",
94 | "metadata": {},
95 | "source": [
96 | "In the next example we look at the interactions with various scopes.\n",
97 | "The `f2()` function defines a function that defines a class and returns it.\n",
98 | "\n",
99 | "When called, `f2()` defines `myfunc()` that, when called, returns a locally-defined class `MyClass`. `f2()` then\n",
100 | "prints the disassembly of `myfunc()` before calling it."
101 | ]
102 | },
103 | {
104 | "cell_type": "code",
105 | "execution_count": null,
106 | "metadata": {
107 | "collapsed": false
108 | },
109 | "outputs": [],
110 | "prompt_number": "",
111 | "source": [
112 | "x = 'x in module'\n",
113 | "def f2():\n",
114 | " x = 'x in f2'\n",
115 | " def myfunc():\n",
116 | " x = 'x in myfunc'\n",
117 | " class MyClass(object): # Note: not a function!\n",
118 | " x = x\n",
119 | " print(x)\n",
120 | " return MyClass\n",
121 | " print \"Disassembly:\", dis(myfunc)\n",
122 | " result = myfunc()\n",
123 | " print \"Result:\", result\n",
124 | "f2()"
125 | ]
126 | },
127 | {
128 | "cell_type": "code",
129 | "execution_count": null,
130 | "metadata": {
131 | "collapsed": false
132 | },
133 | "outputs": [],
134 | "prompt_number": "",
135 | "source": [
136 | "x = 'x in module'\n",
137 | "def f2():\n",
138 | " x = 'x in f2'\n",
139 | " def myfunc():\n",
140 | " x = 'x in myfunc'\n",
141 | " class MyClass(object):\n",
142 | " y = x\n",
143 | " print(y)\n",
144 | " return MyClass\n",
145 | " print \"myfunc code:\"\n",
146 | " print dis(myfunc)\n",
147 | " print \"myfunc closure:\\n\", myfunc.__closure__\n",
148 | " myfunc()\n",
149 | "print \"f2 closure:\", f2.__closure__\n",
150 | "result = f2()\n",
151 | "print \"Result:\", result"
152 | ]
153 | },
154 | {
155 | "cell_type": "code",
156 | "execution_count": null,
157 | "metadata": {
158 | "collapsed": false
159 | },
160 | "outputs": [],
161 | "prompt_number": "",
162 | "source": [
163 | "dis(f2)"
164 | ]
165 | },
166 | {
167 | "cell_type": "markdown",
168 | "metadata": {},
169 | "source": [
170 | "###Possible Discussions\n",
171 | "\n",
172 | "* Are closures generally useful?\n",
173 | "* Why use a closure instead of a class?\n",
174 | "\n",
175 | "###And, of course, whatever _you_ want ..."
176 | ]
177 | }
178 | ],
179 | "metadata": {
180 | "kernelspec": {
181 | "display_name": "Python 2",
182 | "language": "python",
183 | "name": "python2"
184 | },
185 | "language_info": {
186 | "codemirror_mode": {
187 | "name": "ipython",
188 | "version": 2
189 | },
190 | "file_extension": ".py",
191 | "mimetype": "text/x-python",
192 | "name": "python",
193 | "nbconvert_exporter": "python",
194 | "pygments_lexer": "ipython2",
195 | "version": "2.7.2"
196 | }
197 | },
198 | "nbformat": 4,
199 | "nbformat_minor": 0
200 | }
--------------------------------------------------------------------------------
/nbsource2/context-managers.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "#Context Managers"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "A context manager is an object with `__enter__()` and `__exit__()` methods.\n",
15 | "Context managers are used by the `with` statement to establish an execution context\n",
16 | "for a subsidiary block of code.\n",
17 | "\n",
18 | "The `with` statement calls the manager's `__enter__()` method.\n",
19 | "If the optional `as` clause is used then the result of the enter method\n",
20 | "is bound to the variable for use in the code block.\n",
21 | "\n",
22 | "The default object is __not__ a context manager."
23 | ]
24 | },
25 | {
26 | "cell_type": "code",
27 | "execution_count": null,
28 | "metadata": {
29 | "collapsed": false
30 | },
31 | "outputs": [],
32 | "prompt_number": "",
33 | "source": [
34 | "with object():\n",
35 | " print(\"hello\")"
36 | ]
37 | },
38 | {
39 | "cell_type": "code",
40 | "execution_count": null,
41 | "metadata": {
42 | "collapsed": false
43 | },
44 | "outputs": [],
45 | "prompt_number": "",
46 | "source": [
47 | "hasattr(object(), \"__enter__\"), hasattr(object(), \"__exit__\")"
48 | ]
49 | },
50 | {
51 | "cell_type": "markdown",
52 | "metadata": {},
53 | "source": [
54 | "Files, however, _can_ be context managers - though note that\n",
55 | "they don't _have_ to be used that way, so existing paradigms\n",
56 | "continue to work."
57 | ]
58 | },
59 | {
60 | "cell_type": "code",
61 | "execution_count": null,
62 | "metadata": {
63 | "collapsed": false
64 | },
65 | "outputs": [],
66 | "prompt_number": "",
67 | "source": [
68 | "with open(\"context-managers.ipynb\") as f:\n",
69 | " for line in f:\n",
70 | " if \"xyxxy\" in line:\n",
71 | " print(line)\n",
72 | "f.closed"
73 | ]
74 | },
75 | {
76 | "cell_type": "code",
77 | "execution_count": null,
78 | "metadata": {
79 | "collapsed": false
80 | },
81 | "outputs": [],
82 | "prompt_number": "",
83 | "source": [
84 | "def is_context_manager(o):\n",
85 | " return all(hasattr(o, n) for n in (\"__enter__\", \"__exit__\"))"
86 | ]
87 | },
88 | {
89 | "cell_type": "code",
90 | "execution_count": null,
91 | "metadata": {
92 | "collapsed": false
93 | },
94 | "outputs": [],
95 | "prompt_number": "",
96 | "source": [
97 | "is_context_manager(object())"
98 | ]
99 | },
100 | {
101 | "cell_type": "markdown",
102 | "metadata": {},
103 | "source": [
104 | "We can apply the same function to a file object, which it turns\n",
105 | "out is a context manager.\n",
106 | "Since `is_context_manager()` only examines the attributes, it should\n",
107 | "be relatively unsurprising that its class (type) is also seen as\n",
108 | "a context manager."
109 | ]
110 | },
111 | {
112 | "cell_type": "code",
113 | "execution_count": null,
114 | "metadata": {
115 | "collapsed": false
116 | },
117 | "outputs": [],
118 | "prompt_number": "",
119 | "source": [
120 | "is_context_manager(f), type(f), is_context_manager(type(f))"
121 | ]
122 | },
123 | {
124 | "cell_type": "markdown",
125 | "metadata": {},
126 | "source": [
127 | "###A Simple Context Manager"
128 | ]
129 | },
130 | {
131 | "cell_type": "code",
132 | "execution_count": null,
133 | "metadata": {
134 | "collapsed": false
135 | },
136 | "outputs": [],
137 | "prompt_number": "",
138 | "source": [
139 | "class MyCtxm(object):\n",
140 | " def __init__(self):\n",
141 | " self.count = 0\n",
142 | " def __enter__(self):\n",
143 | " return self\n",
144 | " def __exit__(self, ex_type, ex_value, traceback):\n",
145 | " print \"Exit status:\", '\\n', ex_type, '\\n', ex_value, '\\n', traceback\n",
146 | " def bump(self, inc):\n",
147 | " self.count += inc"
148 | ]
149 | },
150 | {
151 | "cell_type": "code",
152 | "execution_count": null,
153 | "metadata": {
154 | "collapsed": false
155 | },
156 | "outputs": [],
157 | "prompt_number": "",
158 | "source": [
159 | "ctxm = MyCtxm()\n",
160 | "with ctxm as cm, open(\"context-managers.ipynb\") as f:\n",
161 | " for line in f:\n",
162 | " cm.bump(1)\n",
163 | "print cm.count"
164 | ]
165 | },
166 | {
167 | "cell_type": "markdown",
168 | "metadata": {},
169 | "source": [
170 | "When the managed code raises an excecption the manager's `__exit__()` \n",
171 | "method is called with the type and argument of the exception as the\n",
172 | "first and second arguments and the traceback as the third argument.\n",
173 | "The method can then decide whether to handle the exception\n",
174 | "condition itself, in which case it should return a True value.\n",
175 | "If it doesn't then the exception is re-raised for handling by\n",
176 | "the surrounding context."
177 | ]
178 | },
179 | {
180 | "cell_type": "code",
181 | "execution_count": null,
182 | "metadata": {
183 | "collapsed": false
184 | },
185 | "outputs": [],
186 | "prompt_number": "",
187 | "source": [
188 | "with ctxm as cm:\n",
189 | " raise ValueError(\"Just for grins\")"
190 | ]
191 | },
192 | {
193 | "cell_type": "markdown",
194 | "metadata": {},
195 | "source": [
196 | "###Possible Discussions\n",
197 | "\n",
198 | "* The [context management protocol](https://docs.python.org/2/library/stdtypes.html#index-37)\n",
199 | "* What else is a context manager?\n",
200 | "* Writing context managers\n",
201 | " * What might they be useful for?\n",
202 | "* Handling in-context uncaught exceptions\n",
203 | "\n",
204 | "###And, of course, whatever _you_ want ..."
205 | ]
206 | }
207 | ],
208 | "metadata": {
209 | "kernelspec": {
210 | "display_name": "Python 2",
211 | "language": "python",
212 | "name": "python2"
213 | },
214 | "language_info": {
215 | "codemirror_mode": {
216 | "name": "ipython",
217 | "version": 2
218 | },
219 | "file_extension": ".py",
220 | "mimetype": "text/x-python",
221 | "name": "python",
222 | "nbconvert_exporter": "python",
223 | "pygments_lexer": "ipython2",
224 | "version": "2.7.2"
225 | }
226 | },
227 | "nbformat": 4,
228 | "nbformat_minor": 0
229 | }
--------------------------------------------------------------------------------
/nbsource2/data/itr-data.csv:
--------------------------------------------------------------------------------
1 | misschaos.chaos-studio.com,China (Shanghai),94,56,0
2 | ns2.rilinfo.net,India (Bangalore),100,0,0
3 | gsrmum.vsnl.net.in,India (Mumbai),72,276,0
4 | core-mgl.cbn.net.id,Indonesia (Mangole),76,232,0
5 | fe0-0.bbr1.bahar.kimianet.ir,Iran (Karaj),100,0,0
6 | gi0-1.thr-001-core-1.ctel.ir,Iran (Tehran),0,0,100
7 | router1.iust.ac.ir,Iran (Tehran),100,0,0
8 | nat.mindu.co.il,Israel (Tel Aviv),100,0,0
9 | cs1mr1.comsourceone.com,Japan (Tokyo),84,152,0
10 | itr-test.isp.qa,Qatar (Doha),65,345,0
11 | gateway.ix.singtel.com,Singapore,100,0,0
12 | tpnoc1-osr-transit.ix.giga.net.tw,Taiwan,78,220,0
13 | gigeuplink.ozservers.net.au,Australia (Brisbane),100,0,0
14 | slinky.arc.net.au,Australia (Melbourne),79,201,0
15 | syd-a-bb1.aarnet.net.au,Australia (Sydney),81,188,0
16 | bdr02.syd01.nsw.vocusconnect.net.au,Australia (Sydney),100,0,0
17 | io.peace.co.nz,New Zealand (Auckland),0,0,100
18 | b2.sxb.tsnz.net,New Zealand (Auckland),84,155,0
19 | ipsec.eskom.co.za,Africa (Gauteng),0,0,100
20 | rt1.mps.bg,Bulgaria (Sofia),0,0,100
21 | feth1-0-0.utland1.bone2.olivant.fo,Faroe Island (Torshavn),75,243,0
22 | n-eb1.n.de.net.dtag.de,Germany,79,209,0
23 | dns1.playnet.it,Italy (Florence),77,228,0
24 | mail.apra.it,Italy (Jesi),100,0,0
25 | mil5-loop0.mil.seabone.net,Italy (Milano),0,0,100
26 | am1-gw1.prioritytelecom.net,Netherlands (Amsterdam),100,0,0
27 | router.nomi.com.pl,Poland,0,0,100
28 | rb1-feth2-0.vlc.s2k-net.net,Spain,0,0,100
29 | mlm1-core.swip.net,Sweden,84,158,0
30 | telenor-gw.bengtdahlgren.se,Sweden (Gothenburg),0,0,100
31 | nrkp-cr1.patrikweb.net,Sweden (Norrkoping),80,197,0
32 | gsr01-tl.blueyonder.co.uk,United Kingdom,81,182,0
33 | gsr01-cr.blueyonder.co.uk,United Kingdom,80,196,0
34 | kt1-3ja.bdr.ex.net,United Kingdom (London),81,181,0
35 | core1-gig2-0.bletchley.ukcore.bt.net,United Kingdom (Milton Keynes),0,0,100
36 | anhm7204.exo.com,California (Anaheim),0,0,100
37 | mc-gateway.lansmart.com,California (Fresno),96,37,0
38 | dnsauth1.sys.gtei.net,California (Los Angeles),98,19,0
39 | rx0ar-technicare.ed.bigpipeinc.com,Canada (Edmonton),91,80,0
40 | gw02.wlfdle.phub.net.cable.rogers.com,Canada (Ontario),100,0,0
41 | anguhub14.net.ubc.ca,Canada (Vancouver),0,0,100
42 | loopback0.gw2.den4.alter.net,Colorado (Denver),91,86,0
43 | router.firstcls.com,Georgia,0,0,100
44 | atl-datacenter-gw2.capitalinternet.com,Georgia (Atlanta),0,0,100
45 | loopback0.gw9.chi2.alter.net,Illinois (Chicago),90,91,0
46 | cisco-gnarly.n-connect.net,Iowa,89,108,0
47 | crystal-cavern.ctcco.com,Kansas (Lenexa),91,88,0
48 | cisco.syssrc.com,Maryland,91,83,0
49 | router-in.nemetschek.net,Maryland (Columbia),91,89,0
50 | pos1-0-0-155m.ar1.bos1.gblx.net,Massachusetts (Boston),91,86,0
51 | lan-d32-0606-0578.uninet-ide.com.mx,Mexico (Chihuahua),100,0,0
52 | rr1.torixt.avantel.net.mx,Mexico (Coahuila),92,79,0
53 | rr2.gdlmha.avantel.net.mx,Mexico (Guadalajara),92,77,0
54 | rr1.reyixt.avantel.net.mx,Mexico (Tamaulipas),90,96,0
55 | revenant.netservicesgroup.com,Michigan (Saginaw),92,77,0
56 | border0-e0.oc48-ypsi.hdl.com,Michigan (Ypsilanti),0,0,100
57 | wormhole.homeisp.com,Missouri (Kansas City),92,71,0
58 | pwps-core01.powerpulse.cc,Nevada (Las Vegas),96,30,0
59 | isp.state.nh.us,New Hampshire,100,0,0
60 | sugaree.arorapc.com,New Jersey,91,84,0
61 | ac-gw.dandy.net,New Jersey (Atlantic City),0,0,100
62 | 180.atm6-0.gw7.nyc9.alter.net,New York (NYC),92,77,0
63 | wookie.core.3z.net,Ohio (Cincinnati),0,0,100
64 | sl-bb21-pen-15-0.sprintlink.net,Pennsylvania (Philadelphia),0,0,100
65 | gw-inet.ktc.com,Texas,100,0,0
66 | core-router.centramedia.net,Texas (Pampa),0,0,100
67 | www.xmission.com,Utah (Salt Lake City),95,45,0
68 | er01.asbn.eli.net,Virginia (Ashburn),0,0,100
69 | core1-sttl.sitespecific.net,Washington (Seattle),0,0,100
70 | gate.netwrx1.com,Wisconsin,100,0,0
71 | core-1601-bmia-elkwpop-1-3.mia.net,Wisconsin (Elkhorn),100,0,0
72 | 5dl-dst-rt2.5ninesdata.com,Wisconsin (Madison),91,89,0
73 | rcorelma1-rcoreats1.impsat.net.ar,Argentina,81,180,0
74 | router-ut1.uniweb.net.co,Colombia (Valle del Cauca),82,173,0
75 | div-core-rtr1.americatelnet.com.pe,Peru (Lima),81,182,0
76 |
--------------------------------------------------------------------------------
/nbsource2/data/videos.txt:
--------------------------------------------------------------------------------
1 | "Building a Diverse Global Community", "Python Brasil, Saõ Paolo, Brazil, 2011", "http://www.youtube.com/watch?v=UZvSXbnq_XI"
2 | "The Open Bastion Portland Launch", "Secret Society, NE Portland, 2011", "http://www.youtube.com/watch?v=cNLSQvID80Y"
3 | "Trailer for Argentina/Brazil Trip", "Portland, 2011", "http://www.youtube.com/watch?v=TgIJ1S3YJ4Y"
4 | "Steve Holden Interviewed at OSCON 2013", "OSCON 2013", "http://www.youtube.com/watch?v=x0MLUNpId7w"
5 | "2011 Frank Willison Memorial Award", "OSCON, Portland, OR, 2011", "http://www.youtube.com/watch?v=I708QrOlM74"
6 | "Building New Communities", "PyCon DE, Leipzig, Germany, 2012", "http://pyvideo.org/video/1479/building-new-communities"
7 | "PSF Chairman's Address", "PyCon, Santa Clara, CA, 2012", "http://pyvideo.org/video/658/python-software-foundation-chairmans-address"
8 | "PSF Chairman's Address", "PyCon, Atlanta, GA, 2011", "http://pyvideo.org/video/453/pycon-2011--python-software-foundation-chairman-a"
9 | "Building the Python Community", "PyCon, Atlanta, GA,, 2010", "http://pyvideo.org/video/290/pycon-2010--keynote--building-the-python-communit"
10 | "Welcome and Chairman's Address", "DjangoCon, Portland, OR, 2010", "http://pyvideo.org/video/46/djangocon-2010--welcome-and-chairman--39-s-addres"
11 | "Why the Django Documentation Sucks", "DjangoCon, Portland, OR, 2011", "http://pyvideo.org/video/87/djangocon-2011--why-the-django-documentation-suck"
12 | "Python 401: Some Advanced Topics (Part 1 of 3)", "PyCon, Chicago, IL, 2009", "http://pyvideo.org/video/158/pycon-2009--python-401--some-advanced-topics--pa0"
13 | "Python 401: Some Advanced Topics (Part 2 of 3)", "PyCon, Chicago, IL, 2009", "http://pyvideo.org/video/159/pycon-2009--python-401--some-advanced-topics--pa1"
14 | "Python 401 Some Advanced Topics (Part 3 of 3)", "PyCOn, Chicago, IL, 2009", "http://pyvideo.org/video/157/pycon-2009--python-401--some-advanced-topics--par"
15 | "The State of the Python Community: Leading the Python tribe", "PyCon, Chicago, IL, 2009", "http://pyvideo.org/video/145/pycon-2009--the-state-of-the-python-community--le"
16 | "O'Reilly School of Technology Webcast with Steve Holden", "Sebastopol, CA, 2010", "http://www.youtube.com/watch?v=K_x5kzZu1GY"
17 | "Steve Holden Welcomes Guests During The Open Bastion Portland Launch", "Portland, OR, 2011", "http://www.youtube.com/watch?v=cNLSQvID80Y"
18 |
--------------------------------------------------------------------------------
/nbsource2/images:
--------------------------------------------------------------------------------
1 | ../data/images/nb
--------------------------------------------------------------------------------
/nbsource2/introducing-the-ipython-notebook.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "#Introducing the IPython Notebook"
8 | ]
9 | },
10 | {
11 | "cell_type": "code",
12 | "execution_count": null,
13 | "metadata": {
14 | "collapsed": false
15 | },
16 | "outputs": [],
17 | "prompt_number": "",
18 | "source": [
19 | "True"
20 | ]
21 | },
22 | {
23 | "cell_type": "code",
24 | "execution_count": null,
25 | "metadata": {
26 | "collapsed": false
27 | },
28 | "outputs": [],
29 | "prompt_number": "",
30 | "source": [
31 | "True\n",
32 | "False"
33 | ]
34 | },
35 | {
36 | "cell_type": "code",
37 | "execution_count": null,
38 | "metadata": {
39 | "collapsed": false
40 | },
41 | "outputs": [],
42 | "prompt_number": "",
43 | "source": [
44 | "True, False # We'll talk about tuples later"
45 | ]
46 | },
47 | {
48 | "cell_type": "markdown",
49 | "metadata": {},
50 | "source": [
51 | "Ctrl/Enter _vs_ Shift/Enter demonstration"
52 | ]
53 | },
54 | {
55 | "cell_type": "markdown",
56 | "metadata": {},
57 | "source": [
58 | "Magics"
59 | ]
60 | },
61 | {
62 | "cell_type": "code",
63 | "execution_count": null,
64 | "metadata": {
65 | "collapsed": false
66 | },
67 | "outputs": [],
68 | "prompt_number": "",
69 | "source": [
70 | "%%file sample.txt\n",
71 | "This is a sample data file\n",
72 | "It has precisely three lines\n",
73 | "Of which this is the last"
74 | ]
75 | },
76 | {
77 | "cell_type": "code",
78 | "execution_count": null,
79 | "metadata": {
80 | "collapsed": false
81 | },
82 | "outputs": [],
83 | "prompt_number": "",
84 | "source": [
85 | "open(\"sample.txt\").readlines()"
86 | ]
87 | },
88 | {
89 | "cell_type": "code",
90 | "execution_count": null,
91 | "metadata": {
92 | "collapsed": false
93 | },
94 | "outputs": [],
95 | "prompt_number": "",
96 | "source": [
97 | "%%file mymodule.py\n",
98 | "\n",
99 | "def f(x):\n",
100 | " print(\"x is:\", x)\n",
101 | " return x+42\n",
102 | "\n",
103 | "print \"Loaded\""
104 | ]
105 | },
106 | {
107 | "cell_type": "code",
108 | "execution_count": null,
109 | "metadata": {
110 | "collapsed": false
111 | },
112 | "outputs": [],
113 | "prompt_number": "",
114 | "source": [
115 | "import mymodule"
116 | ]
117 | },
118 | {
119 | "cell_type": "code",
120 | "execution_count": null,
121 | "metadata": {
122 | "collapsed": false
123 | },
124 | "outputs": [],
125 | "prompt_number": "",
126 | "source": [
127 | "mymodule.f(0)"
128 | ]
129 | },
130 | {
131 | "cell_type": "markdown",
132 | "metadata": {},
133 | "source": [
134 | "Repeat the above and demonstrate the need for kernel restart (or other action) after module changes"
135 | ]
136 | },
137 | {
138 | "cell_type": "markdown",
139 | "metadata": {},
140 | "source": [
141 | "###Possible Discussions\n",
142 | "\n",
143 | "* Keyboard shortcuts in the Jupyter notebook\n",
144 | "* Conversion of notebooks to programs\n",
145 | "\n",
146 | "###And, of course, whatever _you_ want ..."
147 | ]
148 | }
149 | ],
150 | "metadata": {
151 | "kernelspec": {
152 | "display_name": "Python 2",
153 | "language": "python",
154 | "name": "python2"
155 | },
156 | "language_info": {
157 | "codemirror_mode": {
158 | "name": "ipython",
159 | "version": 2
160 | },
161 | "file_extension": ".py",
162 | "mimetype": "text/x-python",
163 | "name": "python",
164 | "nbconvert_exporter": "python",
165 | "pygments_lexer": "ipython2",
166 | "version": "2.7.2"
167 | }
168 | },
169 | "nbformat": 4,
170 | "nbformat_minor": 0
171 | }
--------------------------------------------------------------------------------
/nbsource2/iterables-vs-iterators.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "#Iterables vs. Iterators"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "Many objects in Python can be iterated over - lists, dicts, strings, and so on.\n",
15 | "Objects that can be iterated over in this way are generically known as _iterables_"
16 | ]
17 | },
18 | {
19 | "cell_type": "code",
20 | "execution_count": null,
21 | "metadata": {
22 | "collapsed": false
23 | },
24 | "outputs": [],
25 | "prompt_number": "",
26 | "source": [
27 | "test_list = [\"Roberta\", \"Tom\", \"Alice\", \"Dick\"]"
28 | ]
29 | },
30 | {
31 | "cell_type": "markdown",
32 | "metadata": {},
33 | "source": [
34 | "To iterate over an iterable the interpreter calls the iterable's `__iter__()` method.\n",
35 | "There's a built-in function you can use to do that.\n",
36 | "The `__iter__()` method returns an _iterator_."
37 | ]
38 | },
39 | {
40 | "cell_type": "code",
41 | "execution_count": null,
42 | "metadata": {
43 | "collapsed": false
44 | },
45 | "outputs": [],
46 | "prompt_number": "",
47 | "source": [
48 | "iterator_1 = iter(test_list) # same as test_list.__iter__()"
49 | ]
50 | },
51 | {
52 | "cell_type": "code",
53 | "execution_count": null,
54 | "metadata": {
55 | "collapsed": false
56 | },
57 | "outputs": [],
58 | "prompt_number": "",
59 | "source": [
60 | "iterator_2 = iter(test_list)"
61 | ]
62 | },
63 | {
64 | "cell_type": "code",
65 | "execution_count": null,
66 | "metadata": {
67 | "collapsed": false
68 | },
69 | "outputs": [],
70 | "prompt_number": "",
71 | "source": [
72 | "id(test_list), id(iterator_1), id(iterator_2)"
73 | ]
74 | },
75 | {
76 | "cell_type": "code",
77 | "execution_count": null,
78 | "metadata": {
79 | "collapsed": false
80 | },
81 | "outputs": [],
82 | "prompt_number": "",
83 | "source": [
84 | "type(iterator_1)"
85 | ]
86 | },
87 | {
88 | "cell_type": "code",
89 | "execution_count": null,
90 | "metadata": {
91 | "collapsed": false
92 | },
93 | "outputs": [],
94 | "prompt_number": "",
95 | "source": [
96 | "set(dir(iterator_1)) - set(dir(test_list))"
97 | ]
98 | },
99 | {
100 | "cell_type": "markdown",
101 | "metadata": {},
102 | "source": [
103 | "When you loop over an iterable, the call to the iterable's `__iter__()` method is made\n",
104 | "automatically."
105 | ]
106 | },
107 | {
108 | "cell_type": "code",
109 | "execution_count": null,
110 | "metadata": {
111 | "collapsed": false
112 | },
113 | "outputs": [],
114 | "prompt_number": "",
115 | "source": [
116 | "for i in test_list:\n",
117 | " for j in test_list:\n",
118 | " print(i, j)"
119 | ]
120 | },
121 | {
122 | "cell_type": "markdown",
123 | "metadata": {},
124 | "source": [
125 | "We can adopt the same technique with iterators.\n",
126 | "Note that an iterator's `__iter__()` method always returns `self`, whereas the\n",
127 | "iterable's `__iter__()` returns a new iterator each time."
128 | ]
129 | },
130 | {
131 | "cell_type": "code",
132 | "execution_count": null,
133 | "metadata": {
134 | "collapsed": false
135 | },
136 | "outputs": [],
137 | "prompt_number": "",
138 | "source": [
139 | "for i in iterator_1:\n",
140 | " print(\"outer loop\")\n",
141 | " for j in iterator_2:\n",
142 | " print(\"inner loop\")\n",
143 | " print(i, j)"
144 | ]
145 | },
146 | {
147 | "cell_type": "markdown",
148 | "metadata": {},
149 | "source": [
150 | "The problem here is that on the second and subsequent trips around the outer loop `iterator_2`\n",
151 | "has already been exhausted, so the innner loop is effectively empty.\n",
152 | "We come up against similar issues even sooner if we try to use the same iterator in\n",
153 | "both the inner and the outer loops."
154 | ]
155 | },
156 | {
157 | "cell_type": "code",
158 | "execution_count": null,
159 | "metadata": {
160 | "collapsed": false
161 | },
162 | "outputs": [],
163 | "prompt_number": "",
164 | "source": [
165 | "iterator_3 = iter(test_list)\n",
166 | "for i in iterator_3:\n",
167 | " print(\"outer\")\n",
168 | " for j in iterator_3:\n",
169 | " print(\"inner\")\n",
170 | " print(i, j)"
171 | ]
172 | },
173 | {
174 | "cell_type": "code",
175 | "execution_count": null,
176 | "metadata": {
177 | "collapsed": false
178 | },
179 | "outputs": [],
180 | "prompt_number": "",
181 | "source": [
182 | "iterator_4 = iter(test_list)\n",
183 | "id(test_list), id(iter(test_list))"
184 | ]
185 | },
186 | {
187 | "cell_type": "code",
188 | "execution_count": null,
189 | "metadata": {
190 | "collapsed": false
191 | },
192 | "outputs": [],
193 | "prompt_number": "",
194 | "source": [
195 | "id(iterator_4), id(iter(iterator_4)), id(iterator_4.__iter__())"
196 | ]
197 | },
198 | {
199 | "cell_type": "code",
200 | "execution_count": null,
201 | "metadata": {
202 | "collapsed": false
203 | },
204 | "outputs": [],
205 | "prompt_number": "",
206 | "source": [
207 | "next(iterator_4), next(iterator_4)"
208 | ]
209 | },
210 | {
211 | "cell_type": "code",
212 | "execution_count": null,
213 | "metadata": {
214 | "collapsed": false
215 | },
216 | "outputs": [],
217 | "prompt_number": "",
218 | "source": [
219 | "iterator_4.next(), iterator_4.next()"
220 | ]
221 | },
222 | {
223 | "cell_type": "code",
224 | "execution_count": null,
225 | "metadata": {
226 | "collapsed": false
227 | },
228 | "outputs": [],
229 | "prompt_number": "",
230 | "source": [
231 | "iterator_4.next()"
232 | ]
233 | },
234 | {
235 | "cell_type": "markdown",
236 | "metadata": {
237 | "collapsed": false
238 | },
239 | "source": [
240 | "###Possible Discussions\n",
241 | "\n",
242 | "* What's the essential difference between an iterable and an iterator?\n",
243 | "\n",
244 | "###And, of course, whatever _you_ want ..."
245 | ]
246 | }
247 | ],
248 | "metadata": {
249 | "kernelspec": {
250 | "display_name": "Python 2",
251 | "language": "python",
252 | "name": "python2"
253 | },
254 | "language_info": {
255 | "codemirror_mode": {
256 | "name": "ipython",
257 | "version": 2
258 | },
259 | "file_extension": ".py",
260 | "mimetype": "text/x-python",
261 | "name": "python",
262 | "nbconvert_exporter": "python",
263 | "pygments_lexer": "ipython2",
264 | "version": "2.7.2"
265 | }
266 | },
267 | "nbformat": 4,
268 | "nbformat_minor": 0
269 | }
--------------------------------------------------------------------------------
/nbsource2/list-dict-and-set-comprehensions.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "#List, Dict and Set Comprehensions"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "A very common paradigm for building a list is as follows"
15 | ]
16 | },
17 | {
18 | "cell_type": "code",
19 | "execution_count": null,
20 | "metadata": {
21 | "collapsed": false
22 | },
23 | "outputs": [],
24 | "prompt_number": "",
25 | "source": [
26 | "squares_list = []\n",
27 | "for i in range(10):\n",
28 | " squares_list.append(i*i)\n",
29 | "\n",
30 | "squares_list"
31 | ]
32 | },
33 | {
34 | "cell_type": "markdown",
35 | "metadata": {},
36 | "source": [
37 | "The Python developers realised that it would be nice to be able to construct such lists using just an expression and invented the _list comprehension_. Using a comprehension we can write an expression that evaluates to the same list as above."
38 | ]
39 | },
40 | {
41 | "cell_type": "code",
42 | "execution_count": null,
43 | "metadata": {
44 | "collapsed": false
45 | },
46 | "outputs": [],
47 | "prompt_number": "",
48 | "source": [
49 | "squares_list = [x*x for x in range(10)]\n",
50 | "squares_list"
51 | ]
52 | },
53 | {
54 | "cell_type": "code",
55 | "execution_count": null,
56 | "metadata": {
57 | "collapsed": false
58 | },
59 | "outputs": [],
60 | "prompt_number": "",
61 | "source": [
62 | "x # In Python 3 x is purely local to the comprehension"
63 | ]
64 | },
65 | {
66 | "cell_type": "markdown",
67 | "metadata": {
68 | "collapsed": false
69 | },
70 | "source": [
71 | "You can also add conditions, which can be used to include only \"approved\" values.\n",
72 | "Suppose we wanted a list of the squares of even numbers, we could have written"
73 | ]
74 | },
75 | {
76 | "cell_type": "code",
77 | "execution_count": null,
78 | "metadata": {
79 | "collapsed": false
80 | },
81 | "outputs": [],
82 | "prompt_number": "",
83 | "source": [
84 | "[x*x for x in range(10) if not x % 2]"
85 | ]
86 | },
87 | {
88 | "cell_type": "markdown",
89 | "metadata": {},
90 | "source": [
91 | "More recently the language has been expanded to all the creation of sets and dicts\n",
92 | "using similar syntax."
93 | ]
94 | },
95 | {
96 | "cell_type": "code",
97 | "execution_count": null,
98 | "metadata": {
99 | "collapsed": false
100 | },
101 | "outputs": [],
102 | "prompt_number": "",
103 | "source": [
104 | "{i: \"*\"*i for i in range(20)}"
105 | ]
106 | },
107 | {
108 | "cell_type": "code",
109 | "execution_count": null,
110 | "metadata": {
111 | "collapsed": false
112 | },
113 | "outputs": [],
114 | "prompt_number": "",
115 | "source": [
116 | "{\"&\"*i for i in range(10) if i%2}"
117 | ]
118 | },
119 | {
120 | "cell_type": "markdown",
121 | "metadata": {
122 | "collapsed": true
123 | },
124 | "source": [
125 | "###Possible Discussions\n",
126 | "\n",
127 | "* Is code shorter with comprehensions?\n",
128 | "* Faster?\n",
129 | "* Do comprehensions make code clearer or less readable?\n",
130 | "\n",
131 | "###And, of course, whatever _you_ want ..."
132 | ]
133 | }
134 | ],
135 | "metadata": {
136 | "kernelspec": {
137 | "display_name": "Python 2",
138 | "language": "python",
139 | "name": "python2"
140 | },
141 | "language_info": {
142 | "codemirror_mode": {
143 | "name": "ipython",
144 | "version": 2
145 | },
146 | "file_extension": ".py",
147 | "mimetype": "text/x-python",
148 | "name": "python",
149 | "nbconvert_exporter": "python",
150 | "pygments_lexer": "ipython2",
151 | "version": "2.7.2"
152 | }
153 | },
154 | "nbformat": 4,
155 | "nbformat_minor": 0
156 | }
--------------------------------------------------------------------------------
/nbsource2/mock-objects.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {
6 | "slideshow": {
7 | "slide_type": "slide"
8 | }
9 | },
10 | "source": [
11 | "#Mock Objects"
12 | ]
13 | },
14 | {
15 | "cell_type": "code",
16 | "execution_count": null,
17 | "metadata": {
18 | "collapsed": false,
19 | "slideshow": {
20 | "slide_type": "slide"
21 | }
22 | },
23 | "outputs": [],
24 | "prompt_number": "",
25 | "source": [
26 | "from mock import MagicMock, Mock, call"
27 | ]
28 | },
29 | {
30 | "cell_type": "markdown",
31 | "metadata": {},
32 | "source": [
33 | "In Python 3 the `mock` module became a module in the `unittest` standard library package."
34 | ]
35 | },
36 | {
37 | "cell_type": "code",
38 | "execution_count": null,
39 | "metadata": {
40 | "collapsed": false,
41 | "slideshow": {
42 | "slide_type": "fragment"
43 | }
44 | },
45 | "outputs": [],
46 | "prompt_number": "",
47 | "source": [
48 | "m1 = MagicMock()"
49 | ]
50 | },
51 | {
52 | "cell_type": "code",
53 | "execution_count": null,
54 | "metadata": {
55 | "collapsed": false,
56 | "slideshow": {
57 | "slide_type": "subslide"
58 | }
59 | },
60 | "outputs": [],
61 | "prompt_number": "",
62 | "source": [
63 | "dir(m1)"
64 | ]
65 | },
66 | {
67 | "cell_type": "code",
68 | "execution_count": null,
69 | "metadata": {
70 | "collapsed": false
71 | },
72 | "outputs": [],
73 | "prompt_number": "",
74 | "source": [
75 | "m1.method(1, 2, 3)"
76 | ]
77 | },
78 | {
79 | "cell_type": "code",
80 | "execution_count": null,
81 | "metadata": {
82 | "collapsed": false
83 | },
84 | "outputs": [],
85 | "prompt_number": "",
86 | "source": [
87 | "m1.attribute"
88 | ]
89 | },
90 | {
91 | "cell_type": "code",
92 | "execution_count": null,
93 | "metadata": {
94 | "collapsed": false
95 | },
96 | "outputs": [],
97 | "prompt_number": "",
98 | "source": [
99 | "m1.who_knows_what"
100 | ]
101 | },
102 | {
103 | "cell_type": "code",
104 | "execution_count": null,
105 | "metadata": {
106 | "collapsed": false
107 | },
108 | "outputs": [],
109 | "prompt_number": "",
110 | "source": [
111 | "m1.who_knows_what()"
112 | ]
113 | },
114 | {
115 | "cell_type": "code",
116 | "execution_count": null,
117 | "metadata": {
118 | "collapsed": false
119 | },
120 | "outputs": [],
121 | "prompt_number": "",
122 | "source": [
123 | "m1.method_calls"
124 | ]
125 | },
126 | {
127 | "cell_type": "code",
128 | "execution_count": null,
129 | "metadata": {
130 | "collapsed": false
131 | },
132 | "outputs": [],
133 | "prompt_number": "",
134 | "source": [
135 | "issubclass(Mock, type)"
136 | ]
137 | },
138 | {
139 | "cell_type": "code",
140 | "execution_count": null,
141 | "metadata": {
142 | "collapsed": false
143 | },
144 | "outputs": [],
145 | "prompt_number": "",
146 | "source": [
147 | "m1.method.assert_called_once_with(1, 2, 3)"
148 | ]
149 | },
150 | {
151 | "cell_type": "code",
152 | "execution_count": null,
153 | "metadata": {
154 | "collapsed": false
155 | },
156 | "outputs": [],
157 | "prompt_number": "",
158 | "source": [
159 | "m1.who_knows_what.assert_has_calls([call()])"
160 | ]
161 | },
162 | {
163 | "cell_type": "code",
164 | "execution_count": null,
165 | "metadata": {
166 | "collapsed": false
167 | },
168 | "outputs": [],
169 | "prompt_number": "",
170 | "source": [
171 | "m1.who_knows_what.assert_has_calls([call(1, 2, 3)])"
172 | ]
173 | },
174 | {
175 | "cell_type": "code",
176 | "execution_count": null,
177 | "metadata": {
178 | "collapsed": false
179 | },
180 | "outputs": [],
181 | "prompt_number": "",
182 | "source": [
183 | "m2 = MagicMock()"
184 | ]
185 | },
186 | {
187 | "cell_type": "code",
188 | "execution_count": null,
189 | "metadata": {
190 | "collapsed": false
191 | },
192 | "outputs": [],
193 | "prompt_number": "",
194 | "source": [
195 | "next(m2)"
196 | ]
197 | },
198 | {
199 | "cell_type": "code",
200 | "execution_count": null,
201 | "metadata": {
202 | "collapsed": false
203 | },
204 | "outputs": [],
205 | "prompt_number": "",
206 | "source": [
207 | "m2.method_calls"
208 | ]
209 | },
210 | {
211 | "cell_type": "code",
212 | "execution_count": null,
213 | "metadata": {
214 | "collapsed": false
215 | },
216 | "outputs": [],
217 | "prompt_number": "",
218 | "source": [
219 | "m2.mock_calls"
220 | ]
221 | },
222 | {
223 | "cell_type": "code",
224 | "execution_count": null,
225 | "metadata": {
226 | "collapsed": false
227 | },
228 | "outputs": [],
229 | "prompt_number": "",
230 | "source": [
231 | "m2.assert_has_calls()"
232 | ]
233 | },
234 | {
235 | "cell_type": "code",
236 | "execution_count": null,
237 | "metadata": {
238 | "collapsed": false
239 | },
240 | "outputs": [],
241 | "prompt_number": "",
242 | "source": [
243 | "class Mocking():\n",
244 | " __metclass__ = Mock\n",
245 | " pass"
246 | ]
247 | },
248 | {
249 | "cell_type": "code",
250 | "execution_count": null,
251 | "metadata": {
252 | "collapsed": false
253 | },
254 | "outputs": [],
255 | "prompt_number": "",
256 | "source": [
257 | "[n for n in dir(MagicMock) if n not in dir(Mock)]"
258 | ]
259 | },
260 | {
261 | "cell_type": "code",
262 | "execution_count": null,
263 | "metadata": {
264 | "collapsed": false
265 | },
266 | "outputs": [],
267 | "prompt_number": "",
268 | "source": [
269 | "[n for n in dir(Mock) if n not in dir(MagicMock)]"
270 | ]
271 | },
272 | {
273 | "cell_type": "markdown",
274 | "metadata": {},
275 | "source": [
276 | "###Possible Discussions\n",
277 | "\n",
278 | "* When can mocking be dangerous?\n",
279 | "\n",
280 | "###And, of course, whatever _you_ want ..."
281 | ]
282 | }
283 | ],
284 | "metadata": {
285 | "kernelspec": {
286 | "display_name": "Python 2",
287 | "language": "python",
288 | "name": "python2"
289 | },
290 | "language_info": {
291 | "codemirror_mode": {
292 | "name": "ipython",
293 | "version": 2
294 | },
295 | "file_extension": ".py",
296 | "mimetype": "text/x-python",
297 | "name": "python",
298 | "nbconvert_exporter": "python",
299 | "pygments_lexer": "ipython2",
300 | "version": "2.7.2"
301 | }
302 | },
303 | "nbformat": 4,
304 | "nbformat_minor": 0
305 | }
--------------------------------------------------------------------------------
/nbsource2/mod_simple.py:
--------------------------------------------------------------------------------
1 | """This is a simple module to demonstrate the import mechanism.
2 | It contains a function definition and a print statement that will
3 | cause output to appear when the module's code is executed.
4 |
5 | This version has been modified to use stars not minus signs."""
6 |
7 | def f_simple(s):
8 | line = "*"*len(s)
9 | return "\n".join([line, s, line])
10 |
11 | print(f_simple("This is a test string"))
--------------------------------------------------------------------------------
/nbsource2/mymodule.py:
--------------------------------------------------------------------------------
1 |
2 | def f(x):
3 | print("x is:", x)
4 | return x+42
5 |
6 | print("Loaded")
--------------------------------------------------------------------------------
/nbsource2/names-to-avoid-and-why.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "#Names to Avoid, and Why"
8 | ]
9 | },
10 | {
11 | "cell_type": "code",
12 | "execution_count": null,
13 | "metadata": {
14 | "collapsed": false
15 | },
16 | "outputs": [],
17 | "prompt_number": "",
18 | "source": [
19 | "[ x for x in dir() if \"built\" in x ]"
20 | ]
21 | },
22 | {
23 | "cell_type": "code",
24 | "execution_count": null,
25 | "metadata": {
26 | "collapsed": false
27 | },
28 | "outputs": [],
29 | "prompt_number": "",
30 | "source": [
31 | "dir(__builtin__)"
32 | ]
33 | },
34 | {
35 | "cell_type": "code",
36 | "execution_count": null,
37 | "metadata": {
38 | "collapsed": false
39 | },
40 | "outputs": [],
41 | "prompt_number": "",
42 | "source": [
43 | "len(dir(__builtin__))"
44 | ]
45 | },
46 | {
47 | "cell_type": "code",
48 | "execution_count": null,
49 | "metadata": {
50 | "collapsed": false
51 | },
52 | "outputs": [],
53 | "prompt_number": "",
54 | "source": [
55 | "sum"
56 | ]
57 | },
58 | {
59 | "cell_type": "code",
60 | "execution_count": null,
61 | "metadata": {
62 | "collapsed": false
63 | },
64 | "outputs": [],
65 | "prompt_number": "",
66 | "source": [
67 | "import keyword"
68 | ]
69 | },
70 | {
71 | "cell_type": "code",
72 | "execution_count": null,
73 | "metadata": {
74 | "collapsed": false
75 | },
76 | "outputs": [],
77 | "prompt_number": "",
78 | "source": [
79 | "dir(keyword)"
80 | ]
81 | },
82 | {
83 | "cell_type": "code",
84 | "execution_count": null,
85 | "metadata": {
86 | "collapsed": false
87 | },
88 | "outputs": [],
89 | "prompt_number": "",
90 | "source": [
91 | "keyword.iskeyword(\"def\")"
92 | ]
93 | },
94 | {
95 | "cell_type": "code",
96 | "execution_count": null,
97 | "metadata": {
98 | "collapsed": false
99 | },
100 | "outputs": [],
101 | "prompt_number": "",
102 | "source": [
103 | "keyword.iskeyword(\"print\")"
104 | ]
105 | },
106 | {
107 | "cell_type": "code",
108 | "execution_count": null,
109 | "metadata": {
110 | "collapsed": false
111 | },
112 | "outputs": [],
113 | "prompt_number": "",
114 | "source": [
115 | "kws = keyword.kwlist\n",
116 | "len(kws)"
117 | ]
118 | },
119 | {
120 | "cell_type": "code",
121 | "execution_count": null,
122 | "metadata": {
123 | "collapsed": false
124 | },
125 | "outputs": [],
126 | "prompt_number": "",
127 | "source": [
128 | "kws"
129 | ]
130 | },
131 | {
132 | "cell_type": "markdown",
133 | "metadata": {},
134 | "source": [
135 | "Avoid easily-misread words such as \"l\", ..."
136 | ]
137 | },
138 | {
139 | "cell_type": "markdown",
140 | "metadata": {},
141 | "source": [
142 | "Module names should not \"mask out\" standard library modules"
143 | ]
144 | },
145 | {
146 | "cell_type": "markdown",
147 | "metadata": {},
148 | "source": [
149 | "###Possible Discussions\n",
150 | "\n",
151 | "* How important is the naming of objects?\n",
152 | "\n",
153 | "###And, of course, whatever _you_ want ..."
154 | ]
155 | }
156 | ],
157 | "metadata": {
158 | "kernelspec": {
159 | "display_name": "Python 2",
160 | "language": "python",
161 | "name": "python2"
162 | },
163 | "language_info": {
164 | "codemirror_mode": {
165 | "name": "ipython",
166 | "version": 2
167 | },
168 | "file_extension": ".py",
169 | "mimetype": "text/x-python",
170 | "name": "python",
171 | "nbconvert_exporter": "python",
172 | "pygments_lexer": "ipython2",
173 | "version": "2.7.2"
174 | }
175 | },
176 | "nbformat": 4,
177 | "nbformat_minor": 0
178 | }
--------------------------------------------------------------------------------
/nbsource2/sample.txt:
--------------------------------------------------------------------------------
1 | This is a sample data file
2 | It has precisely three lines
3 | Of which this is the last
--------------------------------------------------------------------------------
/nbsource2/scoping.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": null,
6 | "metadata": {
7 | "collapsed": true
8 | },
9 | "outputs": [],
10 | "prompt_number": "",
11 | "source": [
12 | "crud = \"module crud\""
13 | ]
14 | },
15 | {
16 | "cell_type": "code",
17 | "execution_count": null,
18 | "metadata": {
19 | "collapsed": false
20 | },
21 | "outputs": [],
22 | "prompt_number": "",
23 | "source": [
24 | "def f():\n",
25 | " print crud\n",
26 | " crud = \"function crud\"\n",
27 | " print crud\n",
28 | "f()"
29 | ]
30 | },
31 | {
32 | "cell_type": "code",
33 | "execution_count": null,
34 | "metadata": {
35 | "collapsed": false
36 | },
37 | "outputs": [],
38 | "prompt_number": "",
39 | "source": [
40 | "def f2():\n",
41 | " crud = \"f2 crud\"\n",
42 | " def f3():\n",
43 | " print crud\n",
44 | " f3()\n",
45 | "f2()"
46 | ]
47 | },
48 | {
49 | "cell_type": "code",
50 | "execution_count": null,
51 | "metadata": {
52 | "collapsed": false
53 | },
54 | "outputs": [],
55 | "prompt_number": "",
56 | "source": [
57 | "f2()"
58 | ]
59 | },
60 | {
61 | "cell_type": "code",
62 | "execution_count": null,
63 | "metadata": {
64 | "collapsed": false
65 | },
66 | "outputs": [],
67 | "prompt_number": "",
68 | "source": [
69 | "def f2():\n",
70 | " crud = \"f2 crud\"\n",
71 | " def f3():\n",
72 | " global crud\n",
73 | " print crud\n",
74 | " f3()\n",
75 | " print crud\n",
76 | "f2()"
77 | ]
78 | },
79 | {
80 | "cell_type": "code",
81 | "execution_count": null,
82 | "metadata": {
83 | "collapsed": false
84 | },
85 | "outputs": [],
86 | "prompt_number": "",
87 | "source": [
88 | "def f2():\n",
89 | " crud = \"f2 crud\"\n",
90 | " def f3():\n",
91 | " crud = \"f3 crud\"\n",
92 | " print crud\n",
93 | " f3()\n",
94 | " print crud\n",
95 | "f2()"
96 | ]
97 | },
98 | {
99 | "cell_type": "code",
100 | "execution_count": null,
101 | "metadata": {
102 | "collapsed": false
103 | },
104 | "outputs": [],
105 | "prompt_number": "",
106 | "source": [
107 | "crud = \"module crud\"\n",
108 | "class c:\n",
109 | " print crud\n",
110 | " crud = \"class crud\"\n",
111 | " print crud\n",
112 | "\n",
113 | "print c.crud\n"
114 | ]
115 | },
116 | {
117 | "cell_type": "markdown",
118 | "metadata": {},
119 | "source": [
120 | "###Possible Discussions\n",
121 | "\n",
122 | "* How useful is lexical scoping?\n",
123 | "* Are global variables really a bad thing?\n",
124 | "\n",
125 | "###And, of course, whatever _you_ want ..."
126 | ]
127 | }
128 | ],
129 | "metadata": {
130 | "kernelspec": {
131 | "display_name": "Python 2",
132 | "language": "python",
133 | "name": "python2"
134 | },
135 | "language_info": {
136 | "codemirror_mode": {
137 | "name": "ipython",
138 | "version": 2
139 | },
140 | "file_extension": ".py",
141 | "mimetype": "text/x-python",
142 | "name": "python",
143 | "nbconvert_exporter": "python",
144 | "pygments_lexer": "ipython2",
145 | "version": "2.7.2"
146 | }
147 | },
148 | "nbformat": 4,
149 | "nbformat_minor": 0
150 | }
--------------------------------------------------------------------------------
/nbsource2/secret_sauce.py:
--------------------------------------------------------------------------------
1 | consumer_key = API_key = b"SSTAZboiGXDN8wnDfNk16Q"
2 | consumer_secret = API_secret = b"gc1iYPMurFj8pdNnJKdxcZFifUlsrH8CX1uoXoODbw"
--------------------------------------------------------------------------------
/nbsource2/star-spangled.txt:
--------------------------------------------------------------------------------
1 | These
2 | are
3 | not
4 | the
5 | words
6 | of
7 | the
8 | star
9 | spangled
10 | banner
--------------------------------------------------------------------------------
/nbsource2/string-formatting.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# String Formatting"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "## How Many Ways Do We Need to Format Strings?"
15 | ]
16 | },
17 | {
18 | "cell_type": "markdown",
19 | "metadata": {},
20 | "source": [
21 | "In the beginning there was the __`%`__ operator. Much of its functionality was inspired by the C++ language"
22 | ]
23 | },
24 | {
25 | "cell_type": "markdown",
26 | "metadata": {},
27 | "source": [
28 | "If a call has any positional arguments, they must always appear before any keyword arguments. Thus, `\"...\".format(a, b, k1=c, k2=d)` is legal, but `\"...\".format(k1=c, k2=d, a, b)` is not (it will be flagged as a syntax error by the interpreter because keyword arguments may not precede positionals)."
29 | ]
30 | },
31 | {
32 | "cell_type": "markdown",
33 | "metadata": {},
34 | "source": [
35 | "###Field Names"
36 | ]
37 | },
38 | {
39 | "cell_type": "markdown",
40 | "metadata": {},
41 | "source": [
42 | "The first part of the replacement field, immediately following the opening curly bracket, is the field name. This tells the formatting engine which value is to be formatted. The field name begins with either a number, which specifies a positional argument to the `format()` method, or a name, which specifies a named (_i.e._ keyword) argument. This can be followed by extra information that allows you to index the selected argument or access one of its attributes."
43 | ]
44 | },
45 | {
46 | "cell_type": "markdown",
47 | "metadata": {},
48 | "source": [
49 | "These features alone can get you pretty far. Let's experiment now and get more comfortable programming by writing a slightly unusual program. Usually we expect to provide variable data to a program and format its results in a standard way. This time we'll provide you with standard data and let you enter format specifications that will select specific elements for display."
50 | ]
51 | },
52 | {
53 | "cell_type": "code",
54 | "execution_count": null,
55 | "metadata": {
56 | "collapsed": false
57 | },
58 | "outputs": [],
59 | "prompt_number": "",
60 | "source": [
61 | "i = 42\n",
62 | "r = 31.97\n",
63 | "c = 2.2 + 3.3j\n",
64 | "s = \"String\"\n",
65 | "lst = [\"zero\", \"one\", \"two\", \"three\", \"four\", \"five\"]\n",
66 | "dct = {\"Jim\": \"Dandy\",\n",
67 | " \"Stella\": \"DuBois\",\n",
68 | " 1: \"integer\"}"
69 | ]
70 | },
71 | {
72 | "cell_type": "code",
73 | "execution_count": null,
74 | "metadata": {
75 | "collapsed": false
76 | },
77 | "outputs": [],
78 | "prompt_number": "",
79 | "source": [
80 | "while True:\n",
81 | " fmt = raw_input(\"Format string: \") \n",
82 | " if not fmt:\n",
83 | " break\n",
84 | " #fms = \"{\"+fmt+\"}\"\n",
85 | " print \"Format:\", fmt, \"output:\", fmt.format(i, r, c, s, e=lst, f=dct)"
86 | ]
87 | },
88 | {
89 | "cell_type": "markdown",
90 | "metadata": {
91 | "collapsed": false
92 | },
93 | "source": [
94 | "###Possible Discussions\n",
95 | "\n",
96 | "* What reasons do I have for dropping the `%` operator?\n",
97 | "\n",
98 | "###And, of course, whatever _you_ want ..."
99 | ]
100 | }
101 | ],
102 | "metadata": {
103 | "kernelspec": {
104 | "display_name": "Python 2",
105 | "language": "python",
106 | "name": "python2"
107 | },
108 | "language_info": {
109 | "codemirror_mode": {
110 | "name": "ipython",
111 | "version": 2
112 | },
113 | "file_extension": ".py",
114 | "mimetype": "text/x-python",
115 | "name": "python",
116 | "nbconvert_exporter": "python",
117 | "pygments_lexer": "ipython2",
118 | "version": "2.7.2"
119 | }
120 | },
121 | "nbformat": 4,
122 | "nbformat_minor": 0
123 | }
--------------------------------------------------------------------------------
/nbsource2/test1.py:
--------------------------------------------------------------------------------
1 | import unittest
2 | from widget import Widget
3 |
4 | class WidgetSizeTestCase(unittest.TestCase):
5 | def test_creation(self):
6 | widget = Widget("My Widget")
7 | self.assertEqual(widget.size(), (50, 50),
8 | "Initial size is not (50, 50)")
9 | if __name__ == "__main__":
10 | unittest.main()
--------------------------------------------------------------------------------
/nbsource2/the-function-api-signatures-and-specifications.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "#The Function API - Signatures and Specifications"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "I try to distinguish between _parameters_ (the names used as placeholders in the definition) and _arguments_ (the expressions passed to a function when it is called). I am not always successful, and many programmers appear to use the terms interchangeably. I don't think this is helpful for learners."
15 | ]
16 | },
17 | {
18 | "cell_type": "code",
19 | "execution_count": null,
20 | "metadata": {
21 | "collapsed": false
22 | },
23 | "outputs": [],
24 | "prompt_number": "",
25 | "source": [
26 | "def f(ff, arg):\n",
27 | " return ff(arg)"
28 | ]
29 | },
30 | {
31 | "cell_type": "code",
32 | "execution_count": null,
33 | "metadata": {
34 | "collapsed": false
35 | },
36 | "outputs": [],
37 | "prompt_number": "",
38 | "source": [
39 | "f(len, \"abcdef\")"
40 | ]
41 | },
42 | {
43 | "cell_type": "code",
44 | "execution_count": null,
45 | "metadata": {
46 | "collapsed": false
47 | },
48 | "outputs": [],
49 | "prompt_number": "",
50 | "source": [
51 | "def g(f):\n",
52 | " return f(g)"
53 | ]
54 | },
55 | {
56 | "cell_type": "code",
57 | "execution_count": null,
58 | "metadata": {
59 | "collapsed": false
60 | },
61 | "outputs": [],
62 | "prompt_number": "",
63 | "source": [
64 | "g(id)"
65 | ]
66 | },
67 | {
68 | "cell_type": "code",
69 | "execution_count": null,
70 | "metadata": {
71 | "collapsed": false
72 | },
73 | "outputs": [],
74 | "prompt_number": "",
75 | "source": [
76 | "g(dir)"
77 | ]
78 | },
79 | {
80 | "cell_type": "code",
81 | "execution_count": null,
82 | "metadata": {
83 | "collapsed": false
84 | },
85 | "outputs": [],
86 | "prompt_number": "",
87 | "source": [
88 | "def f_multi(arg_1, *other_args):\n",
89 | " print \"Arg 1:\", arg_1\n",
90 | " print \"There are\", len(other_args), \"other args\"\n",
91 | " for arg in other_args:\n",
92 | " print \"...\", arg"
93 | ]
94 | },
95 | {
96 | "cell_type": "code",
97 | "execution_count": null,
98 | "metadata": {
99 | "collapsed": false
100 | },
101 | "outputs": [],
102 | "prompt_number": "",
103 | "source": [
104 | "f_multi(\"first\", \"second\", \"third\")"
105 | ]
106 | },
107 | {
108 | "cell_type": "code",
109 | "execution_count": null,
110 | "metadata": {
111 | "collapsed": false
112 | },
113 | "outputs": [],
114 | "prompt_number": "",
115 | "source": [
116 | "f_multi(\"first\", \"second\", \"third\", \"fourth\", \"fifth\")"
117 | ]
118 | },
119 | {
120 | "cell_type": "code",
121 | "execution_count": null,
122 | "metadata": {
123 | "collapsed": false
124 | },
125 | "outputs": [],
126 | "prompt_number": "",
127 | "source": [
128 | "def f_kwo(arg_n=\"!!!\", *args):\n",
129 | " print arg_n, args"
130 | ]
131 | },
132 | {
133 | "cell_type": "code",
134 | "execution_count": null,
135 | "metadata": {
136 | "collapsed": false
137 | },
138 | "outputs": [],
139 | "prompt_number": "",
140 | "source": [
141 | "f_kwo(\"one\", \"two\", \"three\")"
142 | ]
143 | },
144 | {
145 | "cell_type": "code",
146 | "execution_count": null,
147 | "metadata": {
148 | "collapsed": false
149 | },
150 | "outputs": [],
151 | "prompt_number": "",
152 | "source": [
153 | "f_kwo(\"one\", \"two\", arg_n=\"three\")"
154 | ]
155 | },
156 | {
157 | "cell_type": "markdown",
158 | "metadata": {},
159 | "source": [
160 | "###Possible Discussions\n",
161 | "\n",
162 | "* How useful are `*args` and `**kwargs`?\n",
163 | "\n",
164 | "###And, of course, whatever _you_ want ..."
165 | ]
166 | }
167 | ],
168 | "metadata": {
169 | "kernelspec": {
170 | "display_name": "Python 2",
171 | "language": "python",
172 | "name": "python2"
173 | },
174 | "language_info": {
175 | "codemirror_mode": {
176 | "name": "ipython",
177 | "version": 2
178 | },
179 | "file_extension": ".py",
180 | "mimetype": "text/x-python",
181 | "name": "python",
182 | "nbconvert_exporter": "python",
183 | "pygments_lexer": "ipython2",
184 | "version": "2.7.2"
185 | }
186 | },
187 | "nbformat": 4,
188 | "nbformat_minor": 0
189 | }
--------------------------------------------------------------------------------
/nbsource2/unicode.txt:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/holdenweb/int-py-notes/71c53d109895e346f1f7c3ba225883dbe8d04484/nbsource2/unicode.txt
--------------------------------------------------------------------------------
/nbsource2/unit-testing.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "#Unit Testing"
8 | ]
9 | },
10 | {
11 | "cell_type": "code",
12 | "execution_count": null,
13 | "metadata": {
14 | "collapsed": false
15 | },
16 | "outputs": [],
17 | "prompt_number": "",
18 | "source": [
19 | "from unittest import TestCase\n",
20 | "[name for name in dir(TestCase) if name.startswith(\"assert\")]"
21 | ]
22 | },
23 | {
24 | "cell_type": "code",
25 | "execution_count": null,
26 | "metadata": {
27 | "collapsed": false
28 | },
29 | "outputs": [],
30 | "prompt_number": "",
31 | "source": [
32 | "%%file widget.py\n",
33 | "#\n",
34 | "# The simplest thing that could possibly ... fail?\n",
35 | "#\n",
36 | "class Widget:\n",
37 | " pass"
38 | ]
39 | },
40 | {
41 | "cell_type": "code",
42 | "execution_count": null,
43 | "metadata": {
44 | "collapsed": false
45 | },
46 | "outputs": [],
47 | "prompt_number": "",
48 | "source": [
49 | "%%file test1.py\n",
50 | "import unittest\n",
51 | "from widget import Widget\n",
52 | "\n",
53 | "class WidgetSizeTestCase(unittest.TestCase):\n",
54 | " def test_creation(self):\n",
55 | " widget = Widget(\"My Widget\")\n",
56 | " self.assertEqual(widget.size(), (50, 50),\n",
57 | " \"Initial size is not (50, 50)\")\n",
58 | "if __name__ == \"__main__\":\n",
59 | " unittest.main()"
60 | ]
61 | },
62 | {
63 | "cell_type": "code",
64 | "execution_count": null,
65 | "metadata": {
66 | "collapsed": false
67 | },
68 | "outputs": [],
69 | "prompt_number": "",
70 | "source": [
71 | "!python test1.py"
72 | ]
73 | },
74 | {
75 | "cell_type": "markdown",
76 | "metadata": {},
77 | "source": [
78 | "The problem is with the instantiation call to the Widget class. The message isn't terribly good, but that isn't the testing framework's fault. Test failures are only reported when an `AssertionError` exception is raised inside its assertion methods. The fact that a test couldn't create a required fixture is sort of incidental. It still needs fixing, though, so this is reported as an error."
79 | ]
80 | },
81 | {
82 | "cell_type": "markdown",
83 | "metadata": {},
84 | "source": [
85 | "The message indicates a problem with a call to `object()` because the `Widget` class doesn't define an `__init__()` method and so `__init__()` is inherited from `object`, the ultimate superclass in the Python universe."
86 | ]
87 | },
88 | {
89 | "cell_type": "markdown",
90 | "metadata": {},
91 | "source": [
92 | "Sometimes responding to such an error in your tests requires a rewrite of a test, but in this case the failure is occuring because the object under test doesn't yet have the required functionality. So let's fix it by defining an `__init__()` method that accepts an argument. This means a small rewrite of `widget.py`."
93 | ]
94 | },
95 | {
96 | "cell_type": "markdown",
97 | "metadata": {},
98 | "source": [
99 | "This demonstrates a fundamental principle of test-driven development. ___Change your code only to fix a failing test___. Strictly speaking we should perhaps have started with only the test, and fixed the first error (which would have been an `ImportError`) by crating the `widget` module, but that would have been taking purism just a little too far."
100 | ]
101 | },
102 | {
103 | "cell_type": "code",
104 | "execution_count": null,
105 | "metadata": {
106 | "collapsed": false
107 | },
108 | "outputs": [],
109 | "prompt_number": "",
110 | "source": [
111 | "%%file widget.py\n",
112 | "#\n",
113 | "# Now with new __init__() method\n",
114 | "#\n",
115 | "class Widget:\n",
116 | " def __init__(self, title):\n",
117 | " self.title = title"
118 | ]
119 | },
120 | {
121 | "cell_type": "code",
122 | "execution_count": null,
123 | "metadata": {
124 | "collapsed": false
125 | },
126 | "outputs": [],
127 | "prompt_number": "",
128 | "source": [
129 | "!python test1.py"
130 | ]
131 | },
132 | {
133 | "cell_type": "code",
134 | "execution_count": null,
135 | "metadata": {
136 | "collapsed": false
137 | },
138 | "outputs": [],
139 | "prompt_number": "",
140 | "source": [
141 | "%%file widget.py\n",
142 | "#\n",
143 | "# With improved __init__() method and a default for initial size\n",
144 | "#\n",
145 | "class Widget:\n",
146 | " def __init__(self, title, size=(50, 50)):\n",
147 | " self.title = title\n",
148 | " self._size = size\n",
149 | " def size(self):\n",
150 | " pass"
151 | ]
152 | },
153 | {
154 | "cell_type": "code",
155 | "execution_count": null,
156 | "metadata": {
157 | "collapsed": false
158 | },
159 | "outputs": [],
160 | "prompt_number": "",
161 | "source": [
162 | "!python test1.py"
163 | ]
164 | },
165 | {
166 | "cell_type": "code",
167 | "execution_count": null,
168 | "metadata": {
169 | "collapsed": false
170 | },
171 | "outputs": [],
172 | "prompt_number": "",
173 | "source": [
174 | "%%file widget.py\n",
175 | "#\n",
176 | "# Now with size() method that does nothing\n",
177 | "#\n",
178 | "class Widget:\n",
179 | " def __init__(self, title, size=(50, 50)):\n",
180 | " self.title = title\n",
181 | " self._size = size\n",
182 | " def size(self):\n",
183 | " return self._size"
184 | ]
185 | },
186 | {
187 | "cell_type": "code",
188 | "execution_count": null,
189 | "metadata": {
190 | "collapsed": false
191 | },
192 | "outputs": [],
193 | "prompt_number": "",
194 | "source": [
195 | "!python test1.py"
196 | ]
197 | },
198 | {
199 | "cell_type": "markdown",
200 | "metadata": {},
201 | "source": [
202 | "###Possible Discussions\n",
203 | "\n",
204 | "* How extensively is your code tested?\n",
205 | "* Are you doing coverage tests?\n",
206 | "\n",
207 | "###And, of course, whatever _you_ want ..."
208 | ]
209 | }
210 | ],
211 | "metadata": {
212 | "kernelspec": {
213 | "display_name": "Python 2",
214 | "language": "python",
215 | "name": "python2"
216 | },
217 | "language_info": {
218 | "codemirror_mode": {
219 | "name": "ipython",
220 | "version": 2
221 | },
222 | "file_extension": ".py",
223 | "mimetype": "text/x-python",
224 | "name": "python",
225 | "nbconvert_exporter": "python",
226 | "pygments_lexer": "ipython2",
227 | "version": "2.7.2"
228 | }
229 | },
230 | "nbformat": 4,
231 | "nbformat_minor": 0
232 | }
--------------------------------------------------------------------------------
/nbsource2/using-json-data-from-the-web.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "#Using JSON Data from the Web"
8 | ]
9 | },
10 | {
11 | "cell_type": "code",
12 | "execution_count": null,
13 | "metadata": {
14 | "collapsed": false
15 | },
16 | "outputs": [],
17 | "prompt_number": "",
18 | "source": [
19 | "import requests\n",
20 | "import json"
21 | ]
22 | },
23 | {
24 | "cell_type": "code",
25 | "execution_count": null,
26 | "metadata": {
27 | "collapsed": false
28 | },
29 | "outputs": [],
30 | "prompt_number": "",
31 | "source": [
32 | "response = requests.get(\"http://www.whitehouse.gov/facts/json/whatsnext/economy\")"
33 | ]
34 | },
35 | {
36 | "cell_type": "code",
37 | "execution_count": null,
38 | "metadata": {
39 | "collapsed": false
40 | },
41 | "outputs": [],
42 | "prompt_number": "",
43 | "source": [
44 | "response.ok"
45 | ]
46 | },
47 | {
48 | "cell_type": "code",
49 | "execution_count": null,
50 | "metadata": {
51 | "collapsed": false
52 | },
53 | "outputs": [],
54 | "prompt_number": "",
55 | "source": [
56 | "for h in response.headers:\n",
57 | " print h+\":\", response.headers[h]"
58 | ]
59 | },
60 | {
61 | "cell_type": "code",
62 | "execution_count": null,
63 | "metadata": {
64 | "collapsed": false
65 | },
66 | "outputs": [],
67 | "prompt_number": "",
68 | "source": [
69 | "access_data = json.loads(response.content.decode(\"utf-8\"))\n",
70 | "type(access_data)"
71 | ]
72 | },
73 | {
74 | "cell_type": "code",
75 | "execution_count": null,
76 | "metadata": {
77 | "collapsed": false
78 | },
79 | "outputs": [],
80 | "prompt_number": "",
81 | "source": [
82 | "len(access_data)"
83 | ]
84 | },
85 | {
86 | "cell_type": "code",
87 | "execution_count": null,
88 | "metadata": {
89 | "collapsed": false
90 | },
91 | "outputs": [],
92 | "prompt_number": "",
93 | "source": [
94 | "data = access_data[0]\n",
95 | "data"
96 | ]
97 | },
98 | {
99 | "cell_type": "code",
100 | "execution_count": null,
101 | "metadata": {
102 | "collapsed": false
103 | },
104 | "outputs": [],
105 | "prompt_number": "",
106 | "source": [
107 | "from IPython.display import HTML\n",
108 | "output = []\n",
109 | "for item in access_data:\n",
110 | " output.append(u'''{}
'''\n",
111 | " .format(item['url'], item['body'], item['url_title']))\n",
112 | "HTML(\"\\n\".join(output))"
113 | ]
114 | },
115 | {
116 | "cell_type": "markdown",
117 | "metadata": {
118 | "collapsed": true
119 | },
120 | "source": [
121 | "###Possible Discussions\n",
122 | "\n",
123 | "* How does JSON compare with XML?\n",
124 | "* Should we be providing JSON-based services?\n",
125 | "\n",
126 | "###And, of course, whatever _you_ want ..."
127 | ]
128 | }
129 | ],
130 | "metadata": {
131 | "kernelspec": {
132 | "display_name": "Python 2",
133 | "language": "python",
134 | "name": "python2"
135 | },
136 | "language_info": {
137 | "codemirror_mode": {
138 | "name": "ipython",
139 | "version": 2
140 | },
141 | "file_extension": ".py",
142 | "mimetype": "text/x-python",
143 | "name": "python",
144 | "nbconvert_exporter": "python",
145 | "pygments_lexer": "ipython2",
146 | "version": "2.7.2"
147 | }
148 | },
149 | "nbformat": 4,
150 | "nbformat_minor": 0
151 | }
--------------------------------------------------------------------------------
/nbsource2/what-happens-when-the-interpreter-starts.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "#### PRE-RELEASE REVIEW: O'Reilly Intermediate Python Video Series\n",
8 | "#### Topic: What Happens when the Interpreter Starts\n",
9 | "Source located in `nbsource/what-happens-when-the-interpreter-starts.ipynb`. Automatically rendered from `UNKNOWN` on 2014-03-21 at 10:21:36.572700.\n",
10 | "#### AUTOMATICALLY GENERATED TEST NOTEBOOK - CHANGES WILL BE LOST\n",
11 | "#### To TEST this Notebook
1. select Cell | Run All, or step through with Shift/Enter
2. Notebook turns pink when testing starts
3. It turns white when the it runs to completion.
4. Please check the executed notebook for clarity and correctness.
Please [raise an issue](https://github.com/holdenweb/intermediate-python/issues) about anything you don't understand - by all means attach a saved copy of the notebook to add explanations or questions. Also please let us know about stuff you want to see covered under any heading in the outline.\n",
12 | "#### Your comments on the content are also welcome, particularly when [reported as issues](https://github.com/holdenweb/intermediate-python/issues)"
13 | ]
14 | },
15 | {
16 | "cell_type": "markdown",
17 | "metadata": {},
18 | "source": [
19 | "#
||| WORKING COPY ||| What Happens When the Interpreter Starts"
20 | ]
21 | },
22 | {
23 | "cell_type": "markdown",
24 | "metadata": {},
25 | "source": [
26 | "This document was created on 2014-03-15 at 01:03:55.356290."
27 | ]
28 | },
29 | {
30 | "cell_type": "markdown",
31 | "metadata": {},
32 | "source": [
33 | "When the interpreter is started it \n",
34 | "\n",
35 | "The interpreter finally creates a new namespace with a special name, `\"__main__\"`, to act as the global namespace for the program (or sequence of interactive commands) that is about to be executed.\n",
36 | "\n",
37 | "This new namespace is populated with a few items. In a standard interactive terminal session in a shell window we see six names in a fairly recent Python release."
38 | ]
39 | },
40 | {
41 | "cell_type": "markdown",
42 | "metadata": {},
43 | "source": [
44 | " Python 3.3.2 (default, Nov 19 2013, 03:15:33)\n",
45 | " [GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)] on darwin\n",
46 | " Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n",
47 | " >>> dir()\n",
48 | " ['__builtins__', '__doc__', '__loader__', '__name__', '__package__']\n",
49 | " >>>"
50 | ]
51 | },
52 | {
53 | "cell_type": "code",
54 | "execution_count": 9,
55 | "metadata": {
56 | "collapsed": false
57 | },
58 | "outputs": [],
59 | "prompt_number": "",
60 | "source": [
61 | "dir() # Compare IPython Notebook with the standard interpreter environment"
62 | ]
63 | },
64 | {
65 | "cell_type": "markdown",
66 | "metadata": {},
67 | "source": [
68 | "The notebook server injects quite a lot of other names into your interpreter namespace, and doesn't provide others. You'll not be troubled by most of the differences, but you might like to see what they are."
69 | ]
70 | },
71 | {
72 | "cell_type": "code",
73 | "execution_count": 10,
74 | "metadata": {
75 | "collapsed": false
76 | },
77 | "outputs": [],
78 | "prompt_number": "",
79 | "source": [
80 | "[name for name in dir()\n",
81 | " if name not in {'__builtins__', '__doc__', '__loader__', '__name__', '__package__'}]"
82 | ]
83 | },
84 | {
85 | "cell_type": "code",
86 | "execution_count": 11,
87 | "metadata": {
88 | "collapsed": false
89 | },
90 | "outputs": [],
91 | "prompt_number": "",
92 | "source": [
93 | "dir()"
94 | ]
95 | },
96 | {
97 | "cell_type": "markdown",
98 | "metadata": {},
99 | "source": [
100 | "If you look carefully you will see that the namespace grows as you enter more commands and the notebook server accumulates more history - `_1` will now have been joined by `_2` and `_3`by the next cell. Remember that none of this stuff happens if you are just using a plain interpreter, but most programming environments will inject something into your namespace."
101 | ]
102 | },
103 | {
104 | "cell_type": "code",
105 | "execution_count": 12,
106 | "metadata": {
107 | "collapsed": false
108 | },
109 | "outputs": [],
110 | "prompt_number": "",
111 | "source": [
112 | "_i1, _2"
113 | ]
114 | },
115 | {
116 | "cell_type": "markdown",
117 | "metadata": {},
118 | "source": [
119 | "Since almost all the names begin with an underline, it isn't difficult to screen them out if you want to. Here I'm using a list comprehesnion with a condition."
120 | ]
121 | },
122 | {
123 | "cell_type": "code",
124 | "execution_count": 13,
125 | "metadata": {
126 | "collapsed": false
127 | },
128 | "outputs": [],
129 | "prompt_number": "",
130 | "source": [
131 | "[n for n in dir() if not n.startswith(\"_\")]"
132 | ]
133 | },
134 | {
135 | "cell_type": "markdown",
136 | "metadata": {},
137 | "source": [
138 | "So you see the notebook keeps inputs (and the corresponding outputs) available for later use, which can be very convenient if you want access to what you have been doing."
139 | ]
140 | },
141 | {
142 | "cell_type": "code",
143 | "execution_count": 13,
144 | "metadata": {
145 | "collapsed": false
146 | },
147 | "outputs": [],
148 | "prompt_number": "",
149 | "source": []
150 | }
151 | ],
152 | "metadata": {},
153 | "nbformat": 4,
154 | "nbformat_minor": 0
155 | }
--------------------------------------------------------------------------------
/nbsource2/widget.py:
--------------------------------------------------------------------------------
1 | #
2 | # Now with size() method that does nothing
3 | #
4 | class Widget:
5 | def __init__(self, title, size=(50, 50)):
6 | self.title = title
7 | self._size = size
8 | def size(self):
9 | return self._size
--------------------------------------------------------------------------------
/outline.txt:
--------------------------------------------------------------------------------
1 | #
2 | # This is the configuration file for automating the production of
3 | # an Intermediate Python video series of approximately 6 hours duration
4 | #
5 | # Note that starred items are intended to be of shorter duration
6 | #
7 | # The first section introduces the learningenvironment and whets
8 | # the student's appetite for what is to come.
9 | #
10 | Welcome to Class
11 | Bits of Python History *
12 | Introducing the IPython Notebook
13 | Accessing IPython Notebook on the Cloud
14 | Downloading your IPython notebooks
15 | IDEs and Productivity Tools *
16 | #
17 | # This is followed by a discussion of how Python creates and
18 | # uses namespaces, and the relationships between namespaces.
19 | #
20 | The Python Environment
21 | Namespaces in Python
22 | Config Files
23 | Visual Python *
24 | xkcd import antigravity *
25 | #
26 | # We now cover some important data types that couldn't be
27 | # covered in the introductory class.
28 | #
29 | More Python Data Types
30 | Tuples
31 | Dicts and Sets
32 | Slicing
33 | Advanced Slicing
34 | Regular Expressions
35 | #
36 | # This section advances the student's understanding of
37 | # Python's objects and how they interact with the interpreter
38 | #
39 | Interacting with the Interpreter
40 | It's not the Snake *
41 | Responding to Python Operators
42 | Callable Instances
43 | The Python Iteration Protocol
44 | Iterables vs. Iterators
45 | Generator Functions
46 | Writing Iterable Objects
47 | Context Managers
48 | #
49 | # Here we introduce some more advanced concepts that will
50 | # help students to write more concise, efficient code.
51 | #
52 | Comprehensions and Generator Expressions
53 | List Comprehensions
54 | Set Comprehensions
55 | Dict Comprehensions
56 | Generator Expressions
57 | #
58 | # To be able to build larger programs it's essential to
59 | # know how to split them up into pieces.
60 | #
61 | Importing Modules
62 | What Happens When the Interpreter Starts
63 | How import Works
64 | import ... as ...
65 | from ... import
66 | Structuring Packages
67 | Relative Imports
68 | Reloading Modules
69 | #
70 | # This section enhances the student's understanding of
71 | # how to write functions.
72 | #
73 | More About Functions
74 | The Function API - Signatures and Specifications
75 | Functions and classes as Function Arguments
76 | Generality of Function Return Values
77 | Introducing Decorators
78 | #
79 | # Everyone needs persistent data, and this section
80 | # looks at some of the techniques you can use.
81 | #
82 | Storing, Retrieving and Handling Data
83 | Reading and Writing Files
84 | Handling Filenames
85 | Elementary SQL
86 | The Python DBAPI
87 | ORM Principles and Sample Code
88 | Struct Module - Handling Binary Data
89 | #
90 | # This section explains how to get on the 'net with Python
91 | #
92 | Simple Networking
93 | The Requests Library
94 | Using JSON Data
95 | Web Frameworks *
96 | #
97 | # Here we explain it in detail and establish basic principle.
98 | #
99 | What is Unicode?
100 | Bytearrays and Bytestrings
101 | Decoding to Unicode
102 | Encoding from Unicode
103 | #
104 | # This section helps scientists, technicians and accountants
105 | #
106 | Python for Scientific and Financiap Applications
107 | Numpy
108 | SciPy
109 | Pandas
110 | matplotlib
111 | Decimal
112 | Data Analysis Modules *
113 | Natural Language Analysis *
114 | #
115 | # We give students some insight into building
116 | # reliable software that others can readily understand
117 | #
118 | Testing Your Code
119 | Unit Testing
120 | Mock Objects
121 | PEP 8 Python Style
122 | Names to Avoid, and Why
123 | A Look at Internal CPython Interpreter Code *
124 | #
125 | # Even the most wonderful things must end.
126 | # This last section wraps the whole course up.
127 | #
128 | Goodbye!
129 | How These Lessons Were Created *
130 | The Python Software Foundation *
131 | EuroPython *
132 | PyCons *
133 | import this *
134 |
--------------------------------------------------------------------------------
/requirements.txt:
--------------------------------------------------------------------------------
1 | Cython==0.20.1
2 | Flask==1.0
3 | Jinja2==2.11.3
4 | Kivy==1.8.0
5 | Kivy-Garden==0.1.1
6 | MarkupSafe==0.18
7 | Pygments==1.6
8 | SQLAlchemy==1.3.0
9 | SexMachine==0.1.1
10 | Sphinx==1.2b3
11 | Werkzeug==0.15.3
12 | astroid==1.0.1
13 | brewer2mpl==1.4
14 | distribute==0.6.34
15 | docutils==0.11
16 | dropbox==1.6
17 | ipython==2.1.0
18 | ipython-sql==0.3.1
19 | itsdangerous==0.23
20 | logilab-common==0.60.0
21 | matplotlib==1.3.1
22 | mock==1.0.1
23 | nbdiff==1.0.3
24 | nose==1.3.0
25 | numpy==1.8.0
26 | pandas==0.14.0
27 | prettytable==0.7.2
28 | psycopg2==2.5.2
29 | py==1.4.19
30 | pylint==1.0.0
31 | pyparsing==2.0.1
32 | pyperclip==1.3
33 | pytest==2.5.1
34 | python-Levenshtein==0.11.2
35 | python-dateutil==2.2
36 | pytz==2014.4
37 | pyzmq==14.3.1
38 | requests==2.20.0
39 | rpy2==2.4.3
40 | runipy==0.1.1
41 | six==1.5.2
42 | sqlparse==0.1.11
43 | tornado==3.1.1
44 |
--------------------------------------------------------------------------------
/whitehouse_json.py:
--------------------------------------------------------------------------------
1 | # -*- coding: utf-8 -*-
2 | import json
3 | import requests
4 | response = requests.get("http://www.whitehouse.gov/facts/json/whatsnext/economy")
5 | if response.ok:
6 | access_data = json.loads(response.content.decode("utf-8"))
7 | data = access_data[0]
8 | output = []
9 | for item in access_data:
10 | output.append('''{}
'''.format(item['url'], item['body'], item['url_title']))
11 | output_string = """\
12 |
13 | Whitehouse Data
14 |
15 | Economic Futures from the Whitehouse"
16 | {}
17 |
18 |
19 | """.format("\n".join(output))
20 | print(output_string)
21 | else:
22 | import sys
23 | sys.exit("Server status code {}".format(response.status_code))
--------------------------------------------------------------------------------