summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPrabhu Ramachandran2019-07-16 14:25:56 +0530
committerPrabhu Ramachandran2019-07-16 14:25:56 +0530
commitcf7d3d425fda005768da2819dd714979577bc25d (patch)
tree8dd1c306fb5c220fca448b1d44b0c5396f5c9777
parentda8fd7e44675d0287c4095ea63e6bf56913dbfb3 (diff)
downloadpython-workshops-cf7d3d425fda005768da2819dd714979577bc25d.tar.gz
python-workshops-cf7d3d425fda005768da2819dd714979577bc25d.tar.bz2
python-workshops-cf7d3d425fda005768da2819dd714979577bc25d.zip
Adding some basic content for the CBSE teachers.
-rw-r--r--cbse/README.md12
-rw-r--r--cbse/data/data.txt100
-rw-r--r--cbse/images/four_plot.pngbin0 -> 54760 bytes
-rw-r--r--cbse/images/penguins.pngbin0 -> 175335 bytes
-rw-r--r--cbse/rise.css21
-rw-r--r--cbse/stats_plots.ipyml443
-rw-r--r--cbse/stdlib.ipyml347
7 files changed, 923 insertions, 0 deletions
diff --git a/cbse/README.md b/cbse/README.md
new file mode 100644
index 0000000..a09490f
--- /dev/null
+++ b/cbse/README.md
@@ -0,0 +1,12 @@
+## CBSE class material
+
+Python is in the school syllabus in India, YAY!
+
+This directory has some random material based on content required for the CBSE
+CS and IP courses for class eleven and twelve.
+
+These items are essentially things that are mentioned in the syllabus but not
+quite covered explicitly in the basic and advanced material as of now.
+
+The idea is to build the content out so that it can be used to train the CBSE
+teachers.
diff --git a/cbse/data/data.txt b/cbse/data/data.txt
new file mode 100644
index 0000000..51bddee
--- /dev/null
+++ b/cbse/data/data.txt
@@ -0,0 +1,100 @@
+6.930830303506971291e+01 1.950570203348928500e+00
+5.076032587226054460e+01 1.968615715715650527e+00
+6.285323760945500027e+01 1.343008070563283418e+00
+7.068897251770047774e+01 1.602208201785916941e+00
+7.855575649137608707e+01 1.515231745959232823e+00
+7.338804260048922856e+01 1.758085121159586706e+00
+6.678171330352577684e+01 1.818107581483499935e+00
+6.266267150399274044e+01 1.397147379699145819e+00
+7.291387445276571100e+01 1.853041933663587715e+00
+5.758895821660193093e+01 1.541074791458244775e+00
+8.741086188682072589e+01 1.900006149089543461e+00
+5.974762642431191750e+01 1.766171119670132406e+00
+5.895949664240657029e+01 2.066534724523970024e+00
+6.831772071612060415e+01 1.676576044877295502e+00
+7.016925230944174530e+01 1.878674928885412099e+00
+6.785858880827163375e+01 1.767609856765382403e+00
+7.346093988591063351e+01 1.787715112577836107e+00
+6.536712696494544161e+01 1.514239640318239433e+00
+4.845403792332015769e+01 2.067523467234714918e+00
+5.095139199919471196e+01 1.801231087590574598e+00
+7.678759427884165234e+01 1.304515789665460446e+00
+6.550194957818187902e+01 1.625383646243619751e+00
+7.294183813089821911e+01 1.829012036743802305e+00
+7.417440889217505173e+01 1.502673709043662154e+00
+5.153659799589171087e+01 1.141933865376840274e+00
+7.202431747175650401e+01 2.015480250062639644e+00
+7.722488692506351526e+01 1.851401880119911691e+00
+6.921991992076141287e+01 1.735078977541309575e+00
+7.433152623275108795e+01 1.831870970931273712e+00
+5.679952287071731121e+01 1.453929162226594851e+00
+4.949969219798052933e+01 1.881247437575087389e+00
+6.885547540885383455e+01 1.573017299906890765e+00
+6.560384734336237500e+01 1.596312342192494294e+00
+6.118535364761604001e+01 1.618355254549173505e+00
+3.998768593037834762e+01 1.780801619519296830e+00
+5.899495542280129712e+01 1.958639246875641948e+00
+9.027864304257214201e+01 1.705261409944323736e+00
+7.374963700418278734e+01 2.064829262096163465e+00
+7.329656023518256802e+01 2.145789349795321321e+00
+4.480373990352278213e+01 1.398988639470314377e+00
+5.910810464939252284e+01 1.645364113266524209e+00
+5.939882929963420111e+01 1.661376053611094195e+00
+5.138189669187610775e+01 1.862474848124291427e+00
+7.000818885060245123e+01 1.746747115588108246e+00
+5.128230006803249807e+01 2.246174564686817199e+00
+6.211228034160293987e+01 2.169577416066632569e+00
+7.874946499062478722e+01 1.617718696650444654e+00
+6.766867866433598522e+01 1.584105663956464349e+00
+7.038916643810861729e+01 1.806817406734167797e+00
+5.699525996524211990e+01 1.646484170154205895e+00
+6.925498307085244676e+01 2.107439054205144213e+00
+6.750742970161464029e+01 1.874355656324631081e+00
+5.563132610804547795e+01 1.380120220966304156e+00
+6.269437740837161499e+01 1.921957966168384768e+00
+7.790558098574634016e+01 1.634775372218683742e+00
+6.211528049020471087e+01 1.656780145381931080e+00
+6.057043128908530605e+01 1.652972071282185373e+00
+5.329997014036838721e+01 1.420679490635214037e+00
+6.529297048078322518e+01 1.618464444787295342e+00
+3.921081308711838176e+01 1.541839594629314192e+00
+7.285547213933116950e+01 1.630859614770092669e+00
+5.683507559457325442e+01 1.862540613426657110e+00
+5.597969415397507476e+01 1.573569676967984776e+00
+6.806595565078583832e+01 1.750620647982633615e+00
+7.107706593555913344e+01 1.597893888509045901e+00
+7.504897940666698730e+01 1.664466695270478080e+00
+5.698579572299380658e+01 1.659222122103459984e+00
+6.543288085386846831e+01 1.713484701936906163e+00
+4.808865783696988672e+01 2.109078716378748553e+00
+7.014690314095311408e+01 1.560346619725913264e+00
+8.575885336562197381e+01 1.908869356619086632e+00
+7.719610459731612195e+01 1.711365885648766128e+00
+6.997728697499780992e+01 1.381206111441030338e+00
+6.306612372698030811e+01 1.586602768408121644e+00
+6.885643100290478458e+01 1.636465278582771798e+00
+6.616529774636667582e+01 1.456442393840382987e+00
+6.446565885803174467e+01 1.458092352780755219e+00
+5.080314466991057998e+01 2.121796211750642591e+00
+6.349031616958593105e+01 1.723911969656314325e+00
+6.320189575072709687e+01 1.702838265973882326e+00
+4.875875986665490558e+01 1.671161025329898520e+00
+5.481407554505388191e+01 1.713161230306921023e+00
+6.716838657586048100e+01 1.341905545247733089e+00
+7.312602310814941120e+01 1.784572705798955994e+00
+6.628248823538058332e+01 1.457321839881880354e+00
+5.155608202538209639e+01 1.607908725441836495e+00
+6.065055871054154579e+01 1.828651795153167781e+00
+6.158308850555339831e+01 2.036482299332666113e+00
+5.195748136642885839e+01 1.767037990692025495e+00
+5.756582511602591268e+01 1.711833796350672232e+00
+7.111171307393533425e+01 1.746281803207642369e+00
+7.143903327480690280e+01 1.616871142267646055e+00
+5.956985884442587320e+01 1.547057255643262019e+00
+7.293738156698248076e+01 2.039943284647624644e+00
+6.652942040385430289e+01 1.848867302282730218e+00
+7.934667547336948701e+01 1.665646482940486228e+00
+5.513161764250422436e+01 1.871649144175183022e+00
+7.681689851743325903e+01 1.670519391257080599e+00
+3.862946704063036663e+01 1.641104966753390126e+00
+6.557613932104308674e+01 1.471827408446917396e+00
diff --git a/cbse/images/four_plot.png b/cbse/images/four_plot.png
new file mode 100644
index 0000000..00a3a7a
--- /dev/null
+++ b/cbse/images/four_plot.png
Binary files differ
diff --git a/cbse/images/penguins.png b/cbse/images/penguins.png
new file mode 100644
index 0000000..d288f93
--- /dev/null
+++ b/cbse/images/penguins.png
Binary files differ
diff --git a/cbse/rise.css b/cbse/rise.css
new file mode 100644
index 0000000..11d733d
--- /dev/null
+++ b/cbse/rise.css
@@ -0,0 +1,21 @@
+/* Customizations for RISE slides
+ */
+
+/* Increase font size for the code cells.
+*/
+
+div.cell.code_cell {
+ font-size: 150%;
+}
+
+/*
+div.CodeMirror-lines {
+ font-size: 125%;
+}*/
+
+/* Tables were rendered out as tiny values
+ since the font-size was set to 12px somehow.
+*/
+.rendered_html table {
+ font-size: 100%
+}
diff --git a/cbse/stats_plots.ipyml b/cbse/stats_plots.ipyml
new file mode 100644
index 0000000..90c67e9
--- /dev/null
+++ b/cbse/stats_plots.ipyml
@@ -0,0 +1,443 @@
+cells:
+
+- markdown: |
+ # More plotting and elementary stats
+
+ ## A quick exercise
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- code: |
+ # Start with this.
+ %pylab inline
+
+ id: 0
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- markdown: |
+ ## Simple plotting
+
+ - Plot $x, -x, \sin(x), x \sin(x)$ in range $-5\pi$ to $5\pi$
+ - Add a legend
+ - Annotate the origin
+ - Set axes limits to the range of x
+
+ <img src="images/four_plot.png" alt="Sample plot to reproduce" width="50%"></img>
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- code: |
+ # Solution
+
+ id: 1
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- markdown: |
+ ## Mean, standard deviation, percentiles, ...
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- code: |
+ x = np.array([3, 2, 1, 4, 4, 5, 15, 24, 22, 25, 18, 32, 33])
+
+- code: |
+ np.mean(x)
+
+- code: |
+ np.median(x)
+
+- markdown: |
+ ## Variance, standard deviation and degrees of freedom
+
+ $$S^2 = \frac{\sum_{i=1}^n (X_i - \bar{X})^2}{n-1}$$
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- code: |
+ np.var(x, ddof=1)
+
+- markdown: |
+ `ddof=1` corresponds to $n-1$ in the denominator.
+
+- code: |
+ np.std(x, ddof=1)
+
+- markdown: |
+ ## Percentiles
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- code: |
+ np.percentile(x, 34)
+
+ id: 3
+
+- code: |
+ np.percentile(x, [25, 50, 75, 34])
+
+ id: 4
+
+- code: |
+ sorted(x)
+
+ id: 5
+ metadata:
+ slideshow:
+ slide_type: slide
+
+
+- markdown: |
+ ## More statistics from `scipy.stats`
+
+ Use the `scipy.stats` module for more stats related functions
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- code: |
+ import scipy.stats
+
+- code: |
+ scipy.stats?
+
+- code: |
+ scipy.stats.mode(x)
+
+- code: |
+ scipy.stats.gmean(x)
+
+- code: |
+ scipy.stats.hmean(x)
+
+
+- markdown: |
+
+ ## Load the given `data.txt` file
+
+ - Load up the data file into numpy arrays.
+ - Plot the two columns one vs the other with points.
+ - Find the mean and standard deviation of the columns
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- code: |
+ x, y = loadtxt('data/data.txt', unpack=True)
+
+- code: |
+ # Find mean and standard deviation
+
+- code: |
+ # Your plotting code here...
+
+
+- markdown: |
+ ## More plotting functions
+
+ We explore more plotting functions below
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+
+- code: |
+ scatter(x, y, s=x, c=y);
+
+ id: 7
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- code: |
+ # Histogram
+ hist(y);
+
+ id: 8
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- code: |
+ hist(y, cumulative=True);
+
+ id: 9
+
+- code: |
+ # Boxplot
+ boxplot(y, showmeans=True);
+ grid()
+ axis('tight');
+
+ id: 10
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- markdown: |
+
+ ## Simple image processing
+
+ - Load the image.
+ - Show the image.
+ - Drop every alternate pixel to reduce the size of the image
+ - Crop the picture to only show the baby penguin.
+
+ <img src="images/penguins.png" alt="Sample plot" width="50%"></img>
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- code: |
+ imshow(img[:,:,3], cmap='gray');
+ colorbar();
+
+ id: 11
+
+- code: |
+ img = imread('images/penguins.png')
+ figure(figsize=(10, 5))
+ subplot(2, 2, 1)
+ imshow(img)
+ subplot(2, 2, 2)
+ imshow(img[::2,::2])
+ subplot(2, 2, 3)
+ imshow(img[225:,100:250])
+ subplot(2, 2, 4)
+ imshow(img[:,:,1])
+
+ id: 12
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- markdown: |
+
+ Use a for loop to plot the 3 channels of the image in a sub-plot,
+ i.e. r, g, b color channels.
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- code: |
+ # Solution.
+
+
+- markdown: |
+ ## Histogram of image pixel data
+
+ - Changing the dimensions of a numpy array.
+ - Making a 2D array into a 1D array.
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+
+- code: |
+ a = np.arange(9)
+ a.shape = (3, 3)
+ a.ravel()
+
+ id: 13
+
+- code: |
+ np.ravel(img[:, :, 0]).shape
+
+ id: 14
+
+- code: |
+ # Doing more
+ #print(numpy.ravel(img[:,:,0]).shape)
+ hist(numpy.ravel(img[:,:,3]));
+
+ id: 15
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- code: |
+ # Putting it together
+ print(numpy.ravel(img[:,:,0]).shape)
+ for color in [0, 1, 2, 3]:
+ subplot(2, 2, color + 1)
+ hist(numpy.ravel(img[:,:,color]), bins=40, density=True, edgecolor='b');
+
+ id: 17
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- markdown: |
+
+ ## Pie charts
+
+ | **Cancer** | Lung | Breast | Colon | Prostate | Melanoma | Bladder |
+ |-------------|------|--------|-------|----------|----------|---------|
+ | **Numbers** | 42 | 50 | 32 | 55 | 9 | 12 |
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- code: |
+ # Solution
+ cancer = ['Lung', 'Breast', 'Colon', 'Prostate', 'Melanoma', 'Bladder']
+ numbers = [42, 50, 32, 55, 9, 12]
+ pie(numbers, labels=cancer);
+
+ id: 18
+ metadata:
+ slideshow:
+ slide_type: slide
+
+
+- markdown: |
+ ## More statistics
+
+ - Covariance, correlation
+ - Linear regression
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+
+- markdown: |
+ ## Covariance/correlation
+
+ $$Cov(X, Y) = E[(X-\mu_x)(Y - \mu_y)] = E[XY] - E[X]E[Y]$$
+
+ $$Corr(X, Y) = Cov(X, Y)/\sqrt{Var(X) Var(Y)}$$
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- code: |
+ # Loading some data
+ x, y = loadtxt('data/data.txt', unpack=True)
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- code: |
+ np.cov(x, y)
+
+
+- code: |
+ np.corrcoef(x, y)
+
+
+- markdown: |
+ ## Trying with other data
+
+ - Find the correlation coefficient and covariance
+ - Plot the data.
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- code: |
+ x1 = np.array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
+ y1 = np.array([30, 33, 53, 73, 78, 85, 91, 92, 100, 120])
+
+- code: |
+ # Your solution.
+
+
+- markdown: |
+ ## Simple linear regression
+
+ - Already seen how to do least-square fits with numpy
+ - Simpler way with `scipy.stats.linregress`
+ - Can also use `np.polyfit`
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+
+- code: |
+ scipy.stats.linregress(x1, y1)
+
+- code: |
+ # Result is a named tuple (collections.namedtuple)!
+
+- code: |
+ res = scipy.stats.linregress(x1, y1)
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- code: |
+ res
+
+- code: |
+ res[0]
+
+- code: |
+ res.slope
+
+- code: |
+ # Using np.polyfit
+
+ np.polyfit(x1, y1, deg=1)
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- code: |
+ np.polyfit?
+
+- markdown: |
+ * Use `deg=1` for linear regression.
+ * Use `deg=2` for quadratic functions.
+ * ...
+
+
+# The lines below here may be deleted if you do not need them.
+# ---------------------------------------------------------------------------
+metadata:
+ celltoolbar: Slideshow
+ kernelspec:
+ display_name: Python 3
+ language: python
+ name: python3
+ language_info:
+ codemirror_mode:
+ name: ipython
+ version: 3
+ file_extension: .py
+ mimetype: text/x-python
+ name: python
+ nbconvert_exporter: python
+ pygments_lexer: ipython3
+ version: 3.6.0
+ rise:
+ scroll: true
+ transition: none
+nbformat: 4
+nbformat_minor: 2
diff --git a/cbse/stdlib.ipyml b/cbse/stdlib.ipyml
new file mode 100644
index 0000000..0b41aa0
--- /dev/null
+++ b/cbse/stdlib.ipyml
@@ -0,0 +1,347 @@
+cells:
+
+- markdown: |
+ # Some useful standard library modules
+
+ Focus is on the syllabus:
+
+ - `math`
+ - `statistics`
+ - `urllib`
+ - More info from: https://docs.python.org/3/library/
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- markdown: |
+ ## Basic mathematical functions: `math`
+
+ - Useful when `numpy` is not available
+ - Much simpler
+ - Does not handle lists or arrays
+ - Only scalars
+ - https://docs.python.org/3/library/math.html
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- code: |
+ import math
+
+- code: |
+ math?
+
+- code: |
+ math.sin(math.pi/4)
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- code: |
+ math.acos(0.5)*180/math.pi
+
+- code: |
+ math.degrees(math.acos(0.5))
+
+- code: |
+ dir(math)
+
+
+- code: |
+ math.fsum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1])
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- code: |
+ math.frexp(2)
+
+- code: |
+ math.frexp(1.01235e5)
+
+- markdown: |
+ ## Differences from numpy
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+
+- code: |
+ import numpy as np
+ np.sin([1, 2, 3])
+
+- code: |
+ math.sin([1, 2, 3])
+
+- markdown: |
+
+ See: https://docs.python.org/3/library/math.html
+
+
+- markdown: |
+ ## `statistics` module
+
+ - New since Python 3.4
+ - Again useful primarily when `numpy` is not available
+ - https://docs.python.org/3/library/statistics.html
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- code: |
+ import statistics
+
+- code: |
+ statistics?
+
+- code: |
+ statistics.mean([-1.0, 2.5, 3.25, 5.75])
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- code: |
+ statistics.median([1, 3, 5, 7])
+
+- code: |
+ statistics.median_low([1, 3, 5, 7])
+
+- code: |
+ statistics.median_high([1, 3, 5, 7])
+
+- code: |
+ statistics.median_grouped([1, 3, 5, 7])
+
+- code: |
+ statistics.mode([2, 3, 3, 5])
+
+- code: |
+ statistics.mode([2, 3, 3, 5, 5])
+
+- code: |
+ statistics.harmonic_mean([2.5, 3, 10])
+
+- code: |
+ statistics.stdev([1.5, 2.5, 2.5, 2.75, 3.25, 4.75])
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- code: |
+ statistics.pstdev([1.5, 2.5, 2.5, 2.75, 3.25, 4.75])
+
+- code: |
+ import numpy as np
+ np.std([1.5, 2.5, 2.5, 2.75, 3.25, 4.75])
+
+- code: |
+ np.std([1.5, 2.5, 2.5, 2.75, 3.25, 4.75], ddof=1)
+
+
+- markdown: |
+ ## `urllib` module
+
+ - Changed significantly in Python 3
+ - Very convenient
+ - https://docs.python.org/3/library/urllib.html
+
+ - Three parts:
+ - `urllib.request`
+ - `urllib.parse`
+ - `urllib.error`
+ - `urllib.robotparser`
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- markdown: |
+ ## Reading a web page
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- code: |
+ import urllib.request
+
+- code: |
+ f = urllib.request.urlopen('http://www.w3.org/')
+ f
+
+- code: |
+ # f can be used like a file object
+ print(f.read(300))
+
+- code: |
+ print(f.read(100))
+
+- code: |
+ f.close()
+ # close the request object
+
+- markdown: |
+ - `f.read()` will read everything.
+ - Note that the returned object is a "bytes" object
+
+
+- code: |
+ # Bytes and unicode
+ data = f.read(300)
+ type(data)
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- markdown: |
+ - Must use a suitable encoding to decode these bytes
+ - `'utf-8'` is good usually.
+
+- code: |
+ data.decode('utf-8')
+
+
+- code: |
+ # Using a with statement
+
+ with urllib.request.urlopen('http://www.w3.org/') as f:
+ print(f.read(300))
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- markdown: |
+ Avoids the need to explicitly close the request in the end.
+
+
+- markdown: |
+ ## More advanced tasks
+
+ - Lot more information in documentation
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- code: |
+ # Sending data to a CGI script via POST.
+ req = urllib.request.Request(url='https://localhost/cgi-bin/test.cgi',
+ data=b'This data is passed to stdin of the CGI')
+
+- code: |
+ with urllib.request.urlopen(req) as f:
+ print(f.read().decode('utf-8'))
+
+
+- code: |
+ # A PUT request
+ DATA = b'some data'
+ req = urllib.request.Request(url='http://localhost:8080', data=DATA,method='PUT')
+ with urllib.request.urlopen(req) as f:
+ pass
+
+ print(f.status)
+ print(f.reason)
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+
+- markdown: |
+ ## Performing a GET with parameters
+
+ - Lets say you want to do a wikipedia search!
+ - Construct the parameters with `urllib.parse.urlencode`
+ - The wikipedia API requires this URL in a GET request
+
+ - https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=python
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- code: |
+ import urllib.parse
+
+- code: |
+ params = urllib.parse.urlencode(
+ {'action': 'query',
+ 'list': 'search', 'srsearch': 'python'}
+ )
+ params
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- code: |
+ url = 'https://en.wikipedia.org/w/api.php?%s' % params
+ url
+
+- code: |
+ with urllib.request.urlopen(url) as f:
+ print(f.read().decode('utf-8'))
+
+
+- markdown: |
+ ## More on parsing
+
+ - Use `urllib.parse` to parse/construct URLs.
+
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- code: |
+ import urllib.parse
+
+- code: |
+ urllib.parse.urlparse('http://google.com')
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- code: |
+ urllib.parse.urlparse(ftp://ubuntu.com')
+
+- code: |
+ urllib.parse.urlparse('https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=python')
+
+- code: |
+ urllib.parse.urlsplit('https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=python')
+
+
+- markdown: |
+ ## Robots.txt
+
+ - Learn more: https://www.robotstxt.org
+ - Helps control robots which parse a website.
+ - `urllib.robotparser` helps parse and detect setup
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- markdown: |
+ ## More information
+
+ - See documentation for more information.
+ - https://docs.python.org/3/library/urllib.html
+
+
+ metadata:
+ slideshow:
+ slide_type: slide