From 404c015a26a80a44e6a42ceca2bf753c9ab30753 Mon Sep 17 00:00:00 2001 From: "chris.barker" Date: Wed, 16 Sep 2015 07:48:19 -0700 Subject: [PATCH 001/159] added a reference to one of Hettingger's talks. --- slides_sources/source/session05.rst | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/slides_sources/source/session05.rst b/slides_sources/source/session05.rst index b71293db..f8e24609 100644 --- a/slides_sources/source/session05.rst +++ b/slides_sources/source/session05.rst @@ -185,8 +185,8 @@ Function arguments in variables function arguments are really just -* a tuple (positional arguments) -* a dict (keyword arguments) +* a tuple (positional arguments) +* a dict (keyword arguments) .. code-block:: python @@ -233,13 +233,13 @@ The ``format`` method takes keyword arguments: Build a dict of the keys and values: -.. code-block:: ipython +.. code-block:: ipython In [25]: d = {u"last":u"Barker", u"first":u"Chris"} And pass to ``format()``with ``**`` -.. code-block:: ipython +.. code-block:: ipython In [26]: u"My name is {first} {last}".format(**d) Out[26]: u'My name is Chris Barker' @@ -366,7 +366,7 @@ But there is another option: I happened on this thread on stack overflow: -http://stackoverflow.com/questions/3975376/understanding-dict-copy-shallow-or-deep +http://stackoverflow.com/questions/3975376/understanding-dict-copy-shallow-or-deep The OP is pretty confused -- can you sort it out? @@ -401,7 +401,7 @@ Huh?! .. nextslide:: -Remember that that default argument is defined when the function is created: there will be only one list, and every time the function is called, that same list is used. +Remember that that default argument is defined when the function is created: there will be only one list, and every time the function is called, that same list is used. The solution: @@ -486,7 +486,7 @@ This can be expressed with a single line using a "list comprehension" What about nested for loops? -.. code-block:: python +.. code-block:: python new_list = [] for var in a_list: @@ -516,7 +516,7 @@ But usually you at least have a conditional in the loop: You can add a conditional to the comprehension: -.. code-block:: python +.. code-block:: python new_list = [expr for var in a_list if something_is_true] @@ -528,7 +528,7 @@ You can add a conditional to the comprehension: Examples: -.. code-block:: ipython +.. code-block:: ipython In [341]: [x**2 for x in range(3)] Out[341]: [0, 1, 4] @@ -1115,3 +1115,10 @@ divisible 2, 3 and 4. c. Extra credit: do it all as a one-liner by nesting a set comprehension inside a list comprehension. (OK, that may be getting carried away!) +================================ +Material to review for Homework: +================================= + +Raymond Hetinger + + From 4694b9291ecdcf51499196f54baa7d7c3d444ab0 Mon Sep 17 00:00:00 2001 From: "chris.barker" Date: Wed, 16 Sep 2015 08:58:38 -0700 Subject: [PATCH 002/159] another link... --- slides_sources/source/session05.rst | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/slides_sources/source/session05.rst b/slides_sources/source/session05.rst index f8e24609..b64dee3f 100644 --- a/slides_sources/source/session05.rst +++ b/slides_sources/source/session05.rst @@ -1119,6 +1119,12 @@ divisible 2, 3 and 4. Material to review for Homework: ================================= -Raymond Hetinger +Raymond Hettinger: + +https://youtu.be/HTLu2DFOdTg + +https://speakerdeck.com/pyconslides/pythons-class-development-toolkit-by-raymond-hettinger + + From 94adcdfd9a75803413d1d81dd8dba1af873d6bc7 Mon Sep 17 00:00:00 2001 From: Chris Barker Date: Mon, 21 Sep 2015 22:43:19 -0700 Subject: [PATCH 003/159] updated install instructions --- Syllabus.rst | 10 ++++ slides_sources/source/session05.rst | 2 +- .../source/supplements/python_for_linux.rst | 58 ++++++++----------- .../source/supplements/python_for_mac.rst | 44 ++++++++------ .../source/supplements/python_for_windows.rst | 25 ++++---- 5 files changed, 70 insertions(+), 69 deletions(-) diff --git a/Syllabus.rst b/Syllabus.rst index 8a83112b..1d171f87 100644 --- a/Syllabus.rst +++ b/Syllabus.rst @@ -192,6 +192,16 @@ References for getting started Blogger about Python. Lots of practical examples. Python3, with some references to differences to Python 2. Also avaiable as a Kindle book: http://www.amazon.com/Python-101-Michael-Driscoll-ebook/dp/B00KQTFHNK +* **Problem Solving with Algorithms and Data Stuctures** + +http://interactivepython.org/runestone/static/pythonds/index.html + +* **Python Course** + +http://www.python-course.eu/python3_course.php + + + References for getting better, once you know the basics -------------------------------------------------------- diff --git a/slides_sources/source/session05.rst b/slides_sources/source/session05.rst index b64dee3f..af4306d4 100644 --- a/slides_sources/source/session05.rst +++ b/slides_sources/source/session05.rst @@ -1117,7 +1117,7 @@ divisible 2, 3 and 4. ================================ Material to review for Homework: -================================= +================================ Raymond Hettinger: diff --git a/slides_sources/source/supplements/python_for_linux.rst b/slides_sources/source/supplements/python_for_linux.rst index 183be0f5..3843e888 100644 --- a/slides_sources/source/supplements/python_for_linux.rst +++ b/slides_sources/source/supplements/python_for_linux.rst @@ -11,19 +11,19 @@ Getting The Tools Python ------- -You probably already have python. Try: +You probably already have python. But most distributions as of this date use python2 as the default. So you need to type ``python3`` to get version 3. Try: .. code-block:: bash - $ python - Python 2.7.8 (v2.7.8:ee879c0ffa11, Jun 29 2014, 21:07:35) - [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on linux + $ python3 + Python 3.5.0 (v3.5.0:374f501f4567, Sep 12 2015, 11:00:19) + Type "help", "copyright", "credits" or "license" for more information. -You can see what version you've got. If you don't have 2.7.*, then you'll need to go try to find a newer version -- your distribution may have a package named something like: +You can see what version you've got. If you don't have 3.4 or above, then you'll need to go try to find a newer version -- your distribution may have a package named something like: .. code-block:: bash - $ apt-get install python2.7 + $ apt-get install python3.4 Or ``yum install`` or ??? @@ -31,9 +31,7 @@ Or ``yum install`` or ??? Terminal --------- -Every Linux box has a terminal emulator -- find and use it. - - +Every Linux box has a terminal emulator -- find and use it. git ---- @@ -44,49 +42,41 @@ git is likely to be there on your system already, but if not: $apt-get install git +or the equivalent for your system. + pip --- -``pip`` is the Python package installer. - -Many python packages are also available directly from your distro -- but you'll get the latest and greatest if you use ``pip`` to install it instead. - -To get pip, the first option is to use your system package manager, something like: - -.. code-block:: bash - - $apt-get install python-pip - -If that doesn't work, you can get it from: +``pip`` is the Python package installer. It is updated faster than python itself, so once you have python, you want to get the latest version of pip working:: -https://pip.pypa.io/en/latest/installing.html + $ python3 -m ensurepip --upgrade -download ``get-pip.py`` from that site, and run it with python:: +[first make sure that ``python`` gives you the one you want. You may need to call ``python3`` instead] - $ python get-pip.py - -It should download and install ``pip`` (and ``setuptools``) +It should download and install the latest ``pip``. You can now use pip to install other packages. + iPython -------- One we are going to use in class is ``iPython``:: - $ pip install ipython + $ python3 -m pip install ipython You should now be able to run ``iPython``:: - $ ipython - Python 2.7.8 (v2.7.8:ee879c0ffa11, Jun 29 2014, 21:07:35) - Type "copyright", "credits" or "license" for more information. + $ ipython + Python 3.5.0 (v3.5.0:374f501f4567, Sep 12 2015, 11:00:19) + Type "copyright", "credits" or "license" for more information. + + IPython 4.0.0 -- An enhanced Interactive Python. + ? -> Introduction and overview of IPython's features. + %quickref -> Quick reference. + help -> Python's own help system. + object? -> Details about 'object', use 'object??' for extra details. - IPython 2.0.0 -- An enhanced Interactive Python. - ? -> Introduction and overview of IPython's features. - %quickref -> Quick reference. - help -> Python's own help system. - object? -> Details about 'object', use 'object??' for extra details. diff --git a/slides_sources/source/supplements/python_for_mac.rst b/slides_sources/source/supplements/python_for_mac.rst index 205ab54b..65836d2d 100644 --- a/slides_sources/source/supplements/python_for_mac.rst +++ b/slides_sources/source/supplements/python_for_mac.rst @@ -8,7 +8,7 @@ Getting The Tools .. rst-class:: left -OS-X comes with Python out of the box, but not the full setup you'll need for development, and this class. +OS-X comes with Python out of the box, but not the full setup you'll need for development, and this class. It also doesn't ahve the latest version(s). So we recommend installing a new version. .. rst-class:: left @@ -26,12 +26,17 @@ latest version, and you really don't want to mess with the system installation. So I recommend installing an independent installation from ``python.org``: -Download and install Python 2.7.8 from Python.org: +Download and install Python 3.5.0 64/32 bit installer from Python.org: https://www.python.org/ftp/python/2.7.8/python-2.7.8-macosx10.6.dmg Simple as that. +Oddly, this does NOT install a ``python`` command, but rather a ``python3`` command. If you want to be able to simply type ``python`` and get python3, then you can add a symlink to the install:: + + $ cd /Library/Frameworks/Python.framework/Versions/3.5/bin + $ ln -s python3.5 python + Terminal --------- @@ -57,6 +62,12 @@ http://sourceforge.net/projects/git-osx-installer/ Is a big download and install, but has everything you need out of the box. +NOTE: if you get a warnign about it beign unsigned, you'll need to go to yoru system preferences: + + "Security and Privacy" + + Then check the box saying "Open Anyway". Or maybe check the box saying you can install untrused pacakges -- depends on the OS-X version + This one: http://git-scm.com/download/mac @@ -79,15 +90,13 @@ After either of these is installed, the ``git`` command should work: pip --- -``pip`` is the Python package installer. Unfortunately, it doesn't come out of the box with Python2.7, so you need to install it: - -https://pip.pypa.io/en/latest/installing.html +``pip`` is the Python package installer. It is updated faster than python itself, so once you have python, you want to get the latest version of pip working:: -download ``get-pip.py`` from that site, and run it with python:: + $ python -m ensurepip --upgrade - $ python get-pip.py +[first make sure that ``python`` gives you the one you want. You may need to call ``python3`` instead] -It should download and install ``pip`` (and ``setuptools``) +It should download and install the latest ``pip``. You can now use pip to install other packages. @@ -96,21 +105,18 @@ iPython One we are going to use in class is ``iPython``:: - $ pip install ipython + $ python3 -m pip install ipython You should now be able to run ``iPython``:: - $ ipython - Python 2.7.8 (v2.7.8:ee879c0ffa11, Jun 29 2014, 21:07:35) - Type "copyright", "credits" or "license" for more information. - - IPython 2.0.0 -- An enhanced Interactive Python. - ? -> Introduction and overview of IPython's features. - %quickref -> Quick reference. - help -> Python's own help system. - object? -> Details about 'object', use 'object??' for extra details. - + Python 3.5.0 (v3.5.0:374f501f4567, Sep 12 2015, 11:00:19) + Type "copyright", "credits" or "license" for more information. + IPython 4.0.0 -- An enhanced Interactive Python. + ? -> Introduction and overview of IPython's features. + %quickref -> Quick reference. + help -> Python's own help system. + object? -> Details about 'object', use 'object??' for extra details. diff --git a/slides_sources/source/supplements/python_for_windows.rst b/slides_sources/source/supplements/python_for_windows.rst index fd54c209..7b823881 100644 --- a/slides_sources/source/supplements/python_for_windows.rst +++ b/slides_sources/source/supplements/python_for_windows.rst @@ -21,7 +21,7 @@ But for core use, the installer from python.org is the way to go: https://www.python.org/downloads/ -You want the installer for Python 2.7.8 -- probably 64 bit, though if you have a 32 bit sytem, you can get that. There is essentially no difference for the purposes of this course. +You want the installer for Python 3.4.3 -- probably 64 bit, though if you have a 32 bit sytem, you can get that. There is essentially no difference for the purposes of this course. Double click and install. @@ -59,21 +59,19 @@ which integrates git with the filemanager. But for the purposes of learning, it http://git-scm.com/download/win -I think that gives you a "Git bash shell" -- a command window that gives you a \*nix - like command line shell. +That gives you a "Git bash shell" -- a command window that gives you a \*nix - like command line shell. + +This is actually your best bet for runing Python also -- If you use the Git Bash, shell, you can use the same commands as Linux ans OS-X users. pip --- -``pip`` is the Python package installer. Unfortunately, it doesn't come out of the box with Python2.7, so you need to install it: - -https://pip.pypa.io/en/latest/installing.html +``pip`` is the Python package installer. It is updated faster than python itself, so once you have python you want to get the latest version of pip working:: -download ``get-pip.py`` from that site, and run it with python:: + $ python -m ensurepip --upgrade - $ python get-pip.py - -It should download and install ``pip`` (and ``setuptools``) +It should download and install the latest ``pip``. You can now use pip to install other packages. @@ -84,10 +82,10 @@ One we are going to use in class is ``iPython``:: $ pip install ipython -You should now be able to run ``iPython``:: +You should now be able to run ``iPython`` from the git bash shell:: $ ipython - Python 2.7.8 (v2.7.8:ee879c0ffa11, Jun 29 2014, 21:07:35) + Python 2.7.8 (v2.7.8:ee879c0ffa11, Jun 29 2014, 21:07:35) Type "copyright", "credits" or "license" for more information. IPython 2.0.0 -- An enhanced Interactive Python. @@ -96,9 +94,6 @@ You should now be able to run ``iPython``:: help -> Python's own help system. object? -> Details about 'object', use 'object??' for extra details. - - - - +We will use this as our default python interpreter. From f1848e98942f2ab29acfe567eaf879eaa2838e8d Mon Sep 17 00:00:00 2001 From: Maria Date: Fri, 2 Oct 2015 20:29:29 -0700 Subject: [PATCH 004/159] fixed python installation for windows --- .../source/supplements/python_for_windows.rst | 25 ++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/slides_sources/source/supplements/python_for_windows.rst b/slides_sources/source/supplements/python_for_windows.rst index 7b823881..da490a46 100644 --- a/slides_sources/source/supplements/python_for_windows.rst +++ b/slides_sources/source/supplements/python_for_windows.rst @@ -29,19 +29,23 @@ Double click and install. Terminal --------- -You can use the "DOS Box" as a terminal, though the newer "powershell" is a better option. +If you are confident in your use of "DOS Box" or "powershell", feel free to use one of those. However, your life may be easier if you install "Git Bash", as then you can follow instructions exactly, and do not have to translate. Also, your instructors are more experienced with Bash. -But to use the Python in the terminal efectively, you need to put a couple paths on your "PATH" environment variable: +When you install Git Bash, you are installing git (and a git gui) as well, thus killing two birds with one stone, metaphorically speaking. + +https://git-for-windows.github.io/ + +This is actually your best bet for runing Python also -- If you use the Git Bash shell, you can use the same commands as Linux ans OS-X users. Regardless of which shell you choose, you will need to add Python to your environemnt. To add Python to your environment variable follow the instructions here: http://www.computerhope.com/issues/ch000549.htm -You want to add: +You will want to add: -``C:\Python2.7`` +``C:\Python34`` and -``C:\Python2.7\Scripts`` +``C:\Python34\Scripts`` to ``PATH`` @@ -49,20 +53,13 @@ to ``PATH`` git ---- -Get a git client -- the gitHub GUI client may be nice -- I honestly don't know. +If you installed Git Bash, you will already have git, both usable in the terminal and as a gui, and can safely skip this section. If not, you still need a git client. You can use the above link and install git (it will install the bash shell as well, of course, but you can use your shell of choice instead). There is also TortoiseGit: https://code.google.com/p/tortoisegit/ -which integrates git with the filemanager. But for the purposes of learning, it may be better to use a command line client: - -http://git-scm.com/download/win - -That gives you a "Git bash shell" -- a command window that gives you a \*nix - like command line shell. - -This is actually your best bet for runing Python also -- If you use the Git Bash, shell, you can use the same commands as Linux ans OS-X users. - +which integrates git with the filemanager. Feel free to use this if you already have an understaning of how git works, but for the purposes of learning, it may be better to use a command line client (git Bash above) pip --- From 807fc1b65579cf2da868737272a04f92631ecb2d Mon Sep 17 00:00:00 2001 From: Maria Date: Fri, 2 Oct 2015 20:35:54 -0700 Subject: [PATCH 005/159] fixed typos missed in previous fix of python installation for windows --- slides_sources/source/supplements/python_for_windows.rst | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/slides_sources/source/supplements/python_for_windows.rst b/slides_sources/source/supplements/python_for_windows.rst index da490a46..940b5e98 100644 --- a/slides_sources/source/supplements/python_for_windows.rst +++ b/slides_sources/source/supplements/python_for_windows.rst @@ -2,8 +2,6 @@ Setting up Windows for Python and this class *********************************************************** -NOTE: this is from memory: no system to test on right now. - ================== Getting The Tools ================== @@ -29,13 +27,13 @@ Double click and install. Terminal --------- -If you are confident in your use of "DOS Box" or "powershell", feel free to use one of those. However, your life may be easier if you install "Git Bash", as then you can follow instructions exactly, and do not have to translate. Also, your instructors are more experienced with Bash. +If you are confident in your use of "DOS Box" or "powershell", feel free to use one of those. However, your life may be easier if you install "Git Bash", as then you can follow terminal instructions exactly, and do not have to translate. Also, your instructors are more experienced with Bash. When you install Git Bash, you are installing git (and a git gui) as well, thus killing two birds with one stone, metaphorically speaking. https://git-for-windows.github.io/ -This is actually your best bet for runing Python also -- If you use the Git Bash shell, you can use the same commands as Linux ans OS-X users. Regardless of which shell you choose, you will need to add Python to your environemnt. To add Python to your environment variable follow the instructions here: +This is actually your best bet for runing Python also -- If you use the Git Bash shell, you can use the same commands as Linux and OS-X users. Regardless of which shell you choose, you will need to add Python to your environemnt. To add Python to your environment variable, follow the instructions here: http://www.computerhope.com/issues/ch000549.htm @@ -59,7 +57,7 @@ There is also TortoiseGit: https://code.google.com/p/tortoisegit/ -which integrates git with the filemanager. Feel free to use this if you already have an understaning of how git works, but for the purposes of learning, it may be better to use a command line client (git Bash above) +which integrates git with the filemanager. Feel free to use this if you already have an understanding of how git works, but for the purposes of learning, it may be better to use a command line client (git Bash above). pip --- From c5258f3534d721287a6158c4a77d1c110e2e7fea Mon Sep 17 00:00:00 2001 From: Maria Date: Fri, 2 Oct 2015 20:41:57 -0700 Subject: [PATCH 006/159] made yet more improvements in the python installation for windows --- slides_sources/source/supplements/python_for_windows.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/slides_sources/source/supplements/python_for_windows.rst b/slides_sources/source/supplements/python_for_windows.rst index 940b5e98..c5cb4ad6 100644 --- a/slides_sources/source/supplements/python_for_windows.rst +++ b/slides_sources/source/supplements/python_for_windows.rst @@ -33,7 +33,7 @@ When you install Git Bash, you are installing git (and a git gui) as well, thus https://git-for-windows.github.io/ -This is actually your best bet for runing Python also -- If you use the Git Bash shell, you can use the same commands as Linux and OS-X users. Regardless of which shell you choose, you will need to add Python to your environemnt. To add Python to your environment variable, follow the instructions here: +This is actually your best bet for running Python also -- If you use the Git Bash shell, you can use the same commands as Linux and OS-X users. Regardless of which shell you choose, you will need to add Python to your environemnt. It is possible that this was done during the installation of python. If you type 'which python' into your terminal, and get back the answer '/c/python34/python', then you are good to go, otherwise, follow the instructions here: http://www.computerhope.com/issues/ch000549.htm From 778ced0c36b5c4d6e61027f6f76b804f08395c55 Mon Sep 17 00:00:00 2001 From: Chris Barker Date: Sat, 3 Oct 2015 23:12:06 -0700 Subject: [PATCH 007/159] updating session 1 -- more to do. --- slides_sources/source/session01.rst | 318 +++++++++--------- .../source/supplements/python_for_mac.rst | 19 +- 2 files changed, 167 insertions(+), 170 deletions(-) diff --git a/slides_sources/source/session01.rst b/slides_sources/source/session01.rst index fe532e08..b2992177 100644 --- a/slides_sources/source/session01.rst +++ b/slides_sources/source/session01.rst @@ -15,6 +15,17 @@ Session One: Introductions .. _xkcd.com/353: http://xkcd.com/353 +Goals for Session One: +====================== + +* Meet each other, set expectations for the class. + +* Schedule lightning talks. + +* Get you all up and running with Python + +* Start having fun with Python with a quick tutorial + Introductions ============= @@ -301,15 +312,13 @@ This class uses Python 3.4 -- not Python 2.* * Adoption of Python 3 is growing fast - * A few key packages still not supported (https://python3wos.appspot.com/) - * Most code in the wild is still 2.x - -* You *can* learn to write Python that is forward compatible from 2.x to 3.x - -* We will cover that more later in the program. + * Almost all key packages still supported (https://python3wos.appspot.com/) + * But most code in the wild is still 2.x * If you find yourself needing to work with Python 2 and 3, there are ways to write compatible code: https://wiki.python.org/moin/PortingPythonToPy3k +* We will cover that more later in the program. Also: a short intro to the differences you really need to know about up font later this session. + Introduction to Your Environment ================================ @@ -330,16 +339,22 @@ Your Command Line (cli) Having some facility on the command line is important -We won't cover this in class, so if you are not comfortable, please bone up at -home. +We won't cover this in class, so if you are not comfortable, +please bone up at home. I suggest running through the **cli** tutorial at "learn code the hard way": -`http://cli.learncodethehardway.org/book`_ +http://cli.learncodethehardway.org/book/ + +**Windows:** -.. _http://cli.learncodethehardway.org/book: http://cli.learncodethehardway.org/book +Most of the demos in class, etc, will be done using the "bash" command line shell on OS-X. This is identical to the bash shell on Linux. +Windows provides the "DOS" command line, which is OK, but pretty old an limited, or "Power Shell" -- a more modern, powerful, flexible command shell. +If you are comfortable with either of these -- go for it. + +If not, you can use the "git Bash" shell -- which is much like the bash shell on OS-X and *nix. Your Interpreter ---------------- @@ -348,13 +363,12 @@ Python comes with a built-in interpreter. You see it when you type ``python`` at the command line: -.. code-block:: pycon +.. code-block:: python - $ python - Python 2.7.5 (default, Aug 25 2013, 00:04:04) - [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin - Type "help", "copyright", "credits" or "license" for more information. - >>> + $ python + Python 3.5.0 (v3.5.0:374f501f4567, Sep 12 2015, 11:00:19) + [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin + Type "help", "copyright", "credits" or "license" for more information. That last thing you see, ``>>>`` is the "Python prompt". @@ -365,7 +379,7 @@ This is where you type code. Try it out: -.. code-block:: pycon +.. code-block:: python >>> print "hello world!" hello world! @@ -520,21 +534,21 @@ We will take the time here in class to get this going. This helps to ensure that you will be able to work. -Step 1: Python 2.7 +Step 1: Python 3.4 ------------------ .. rst-class:: large -You have this already, RIGHT? +Do you already have this?? .. code-block:: bash - $ python - Python 2.7.5 (default, Aug 25 2013, 00:04:04) - [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin - Type "help", "copyright", "credits" or "license" for more information. - >>> ^D - $ + + $ python + Python 3.5.0 (v3.5.0:374f501f4567, Sep 12 2015, 11:00:19) + [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin + Type "help", "copyright", "credits" or "license" for more information. + >>> ^D If not: @@ -553,28 +567,13 @@ Sometimes you need a bit more. Pip allows you to install Python packages to expand your system. -You install it by downloading and then executing an installer script: - -.. code-block:: bash - - $ curl -O https://bootstrap.pypa.io/get-pip.py - % Total % Received % Xferd Average Speed Time Time Time Current - Dload Upload Total Spent Left Speed - 100 1309k 100 1309k 0 0 449k 0 0:00:02 0:00:02 --:--:-- 449k - - $ python get-pip.py - -(or go to: http://pip.readthedocs.org/en/latest/installing.html) - -(Windows users will need to do that....) - -.. nextslide:: Using Pip +The previous instructions include pip as well - make sure it's working. Once you've installed pip, you use it to install Python packages by name: .. code-block:: bash - $ pip install foobar + $ python -m pip install foobar ... To find packages (and their proper names), you can search the python @@ -589,55 +588,11 @@ Step 3: Install iPython As this is an intro class, we are going to use almost entirely features of standard library. But there are a couple things you may want: -**iPython** +**iPython** is an "enhanced python shell" -- it make s it easier to work with python interatively. .. code-block:: bash - $pip install ipython - -If you are using SublimeText, you may want: - -.. code-block:: bash - - $ pip install PdbSublimeTextSupport - - -Step 4: Clone Class Repository ------------------------------- - -`gitHub `_ is an industry-standard system for -collaboration on software projects -- particularly open source ones. - -We will use it this class to manage submitting and reviewing your work, etc. - -**Wait!** Don't have a gitHub account? Set one up now. - -Next, you'll make a copy of the class repository using ``git``. - -The canonical copy is in the UWPCE organization on GitHub: - -https://github.com/UWPCE-PythonCert/IntroToPython - -Open that URL, and click on the *Fork* button at the top right corner. - -This will make a copy of this repository in *your* github account. - - -.. nextslide:: Clone Your Fork - -From here, you'll want to make a clone of your copy on your local machine. - -At your command line, run the following commands: - -.. code-block:: bash - - $ cd your_working_directory_for_the_class - $ git clone https://github.com//IntroToPython.git - -(you can copy and paste that link from the gitHub page) - -**Remember**, should be replaced by your github account name. - + $ python -m pip install ipython Introduction to iPython ======================= @@ -674,18 +629,15 @@ Start it up .. code-block:: bash - $ipython - - $ ipython - Python 2.7.6 (v2.7.6:3a1db0d2747e, Nov 10 2013, 00:42:54) - Type "copyright", "credits" or "license" for more information. - - IPython 2.0.0 -- An enhanced Interactive Python. - ? -> Introduction and overview of IPython's features. - %quickref -> Quick reference. - help -> Python's own help system. - object? -> Details about 'object', use 'object??' for extra details. + $ ipython + Python 3.5.0 (v3.5.0:374f501f4567, Sep 12 2015, 11:00:19) + Type "copyright", "credits" or "license" for more information. + IPython 4.0.0 -- An enhanced Interactive Python. + ? -> Introduction and overview of IPython's features. + %quickref -> Quick reference. + help -> Python's own help system. + object? -> Details about 'object', use 'object??' for extra details. .. ifslides:: @@ -762,7 +714,6 @@ To run it, you have a couple options: [demo] - Basic Python Syntax =================== @@ -798,6 +749,7 @@ All of programming is really about manipulating values. [demo] + Literals for the Basic Value types: ------------------------------------ @@ -845,7 +797,7 @@ Statements: In [6]: # statements do not return a value, may contain an expression - In [7]: print "this" + In [7]: print("this") this In [8]: line_count = 42 @@ -853,41 +805,25 @@ Statements: In [9]: -.. nextslide:: The Print Statement +.. nextslide:: The Print Function It's kind of obvious, but handy when playing with code: .. code-block:: ipython - In [1]: print "something" + In [1]: print ("something") something You can print multiple things: .. code-block:: ipython - In [2]: print "the value is", 5 + In [2]: print("the value is", 5) the value is 5 .. nextslide:: -Python automatically adds a newline, which you can suppress with a comma: - - -.. code-block:: ipython - - In [12]: for i in range(5): - ....: print "the value is", - ....: print i - ....: - the value is 0 - the value is 1 - the value is 2 - the value is 3 - -.. nextslide:: - Any python object can be printed (though it might not be pretty...) .. code-block:: ipython @@ -896,7 +832,7 @@ Any python object can be printed (though it might not be pretty...) ...: pass ...: - In [2]: print bar + In [2]: print(bar) @@ -913,7 +849,7 @@ Blocks of code are delimited by a colon and indentation: .. code-block:: python for i in range(100): - print i**2 + print(i**2) .. code-block:: python @@ -944,12 +880,12 @@ These two blocks look the same: .. code-block:: python for i in range(100): - print i**2 + print(i**2) .. code-block:: python for i in range(100): - print i**2 + print(i**2) .. nextslide:: @@ -981,7 +917,7 @@ Even when you hit the key Expressions ----------------- +------------ An *expression* is made up of values and operators. @@ -1028,7 +964,7 @@ Symbols are how we give names to values (objects). * Symbols don't have a type; values do - * This is why python is 'Dynamic' + * This is why python is "Dynamic" Symbols and Type @@ -1098,7 +1034,7 @@ Variables? * In most languages, what I'm calling symbols, or names, are called "variables". -* In fact, Ill probably call them variables in this class. +* In fact, I'll probably call them variables in this class. * That's because they are used, for the most part, for the same purposes. @@ -1184,7 +1120,6 @@ Using this feature, we can swap values between two names in one statement: Multiple assignment and symbol swapping can be very useful in certain contexts - Deleting -------- @@ -1435,6 +1370,7 @@ You *cannot* use these words as symbols. .. nextslide:: + If you try to use any of the keywords as symbols, you will cause a ``SyntaxError``: @@ -1449,7 +1385,7 @@ If you try to use any of the keywords as symbols, you will cause a .. code-block:: ipython In [14]: def a_function(else='something'): - ....: print else + ....: print(else) ....: File "", line 1 def a_function(else='something'): @@ -1590,7 +1526,7 @@ function defs must be executed before the functions can be called: .. code-block:: ipython In [18]: def simple(): - ....: print "I am a simple function" + ....: print("I am a simple function") ....: In [19]: simple() @@ -1621,7 +1557,7 @@ back is .. code-block:: ipython In [5]: def exceptional(): - ...: print "I am exceptional!" + ...: print("I am exceptional!") ...: print 1/0 ...: In [6]: def passive(): @@ -1655,8 +1591,8 @@ Functions: Tracebacks in exceptional() 1 def exceptional(): - 2 print "I am exceptional!" - ----> 3 print 1/0 + 2 print("I am exceptional!") + ----> 3 print(1/0) 4 ZeroDivisionError: integer division or modulo by zero @@ -1686,7 +1622,7 @@ if you don't explicilty put ``return`` there, Python will: ...: In [10]: fun() In [11]: result = fun() - In [12]: print result + In [12]: print(result) None note that the interpreter eats ``None`` @@ -1707,7 +1643,7 @@ This is useful when debugging! In [14]: def no_error(): ....: return 'done' ....: # no more will happen - ....: print 1/0 + ....: print(1/0) ....: In [15]: no_error() Out[15]: 'done' @@ -1751,7 +1687,7 @@ In a ``def`` statement, the values written *inside* the parens are In [22]: def fun(x, y, z): ....: q = x + y + z - ....: print x, y, z, q + ....: print(x, y, z, q) ....: x, y, z are *local* symbols -- so is q @@ -1781,11 +1717,11 @@ In order to do anything interesting at all (including this week's homework), you In [12]: def test(a): ....: if a == 5: - ....: print "that's the value I'm looking for!" + ....: print("that's the value I'm looking for!") ....: elif a == 7: - ....: print "that's an OK number" + ....: print("that's an OK number") ....: else: - ....: print "that number won't do!" + ....: print("that number won't do!") In [13]: test(5) that's the value I'm looking for! @@ -1818,10 +1754,79 @@ Schedule the lightning talks: [demo] +Python 2-3 Differences +====================== + +Much of the example code you'll find online is Python2, rather than Python3 + +For the most part, they are the same -- so you can sue those examples to learn from. + +There are a lot of subtle differences that you don't need to concern yourself with just yet. + +But a couple that you'll need to know right off the bat: + +print +===== + +In python2, ``print`` is a "statement", rather than a function. That means it didn't require parenthes around what you want printed:: + + print something, something_else + +This made it a bit less flexible and powerful. + +But -- if you try to use it that way in Python3, you'll get an error:: + + In [15]: print "this" + File "", line 1 + print "this" + ^ + SyntaxError: Missing parentheses in call to 'print' + +So -- if you get this error, simply add the parentheses:: + + In [16]: print ("this") + this + +.. nextslide:: division + +In python 3, the divsion operator is "smart" when you divide integers:: + + In [17]: 1 / 2 + Out[17]: 0.5 + +However in python2, if you use integers, is will give you an integer result:: + + In [1]: 1/2 + Out[1]: 0 + +In both versions, you can get "integer division" if you want it with a double slash:: + + In [1]: 1/2 + Out[1]: 0 + +And in python2, you can get the behavior of py3 with "true division":: + + In [2]: from __future__ import division + + In [3]: 1/2 + Out[3]: 0.5 + +For the most part, you just need to be a bit careful with the rare cases where py2 code counts on integer division. + +Other py2/py3 differences +------------------------- + +Most of the other differences are essentially of implementation details, like getting iterators instead of sequences -- we'll talk about that more when it comes up in class. + +There are also a few syntax differences with more advances topics: Exceptions, super(), etc. + +We'll talk about all that when we cover those topics. + + Homework ======== -??? Tasks by Next Week +Tasks and reading by Next Week Task 1 @@ -1924,19 +1929,21 @@ Write a function that draws a grid like the following:: .. nextslide:: -Hint: to print more than one value on a line, you can print a comma-separated sequence: -``print '+', '-'`` +Hint: to print more than one value on a line, you can pass multiple names into the print function: +``print('+', '-')`` -If the sequence ends with a comma, Python leaves the line unfinished, so the value printed next appears on the same line. +If you don't want a newline after somethign is printed, you tell python what you want the print to end with like so: :: - print '+', - print '-' + print('+', end=' '), + print('-') The output of these statements is ``'+ -'``. -A print statement all by itself ends the current line and goes to the next line. +A print function with no arguments ends the current line and goes to the next line:: + + print() .. nextslide:: @@ -1988,31 +1995,16 @@ http://pyvideo.org/video/1850/a-hands-on-introduction-to-python-for-beginning-p Next Class =========== -.. rst-class:: left - -Next class I will be out of town. - -.. rst-class:: left - -You will be in the capable hands of Cris Ewing - -.. rst-class:: left - -Cris is the instructor for the next class in this sequence - -.. rst-class:: left - -And a great teacher. Office Hours ------------ I'll do office hours on either Saturday or Sunday from 12:00 -- 3:00 -Probably in Wallingford, or maybe South Lake Union +Probably in Wallingford, or Fremont Do you have a preference? -Nathan's office hours?? +Maria's office hours?? diff --git a/slides_sources/source/supplements/python_for_mac.rst b/slides_sources/source/supplements/python_for_mac.rst index 65836d2d..5fbd9402 100644 --- a/slides_sources/source/supplements/python_for_mac.rst +++ b/slides_sources/source/supplements/python_for_mac.rst @@ -1,6 +1,6 @@ -*********************************************************** -Setting up your Mac for Python and this class -*********************************************************** +****************************** +Setting up your Mac for Python +****************************** ================== Getting The Tools @@ -8,7 +8,9 @@ Getting The Tools .. rst-class:: left -OS-X comes with Python out of the box, but not the full setup you'll need for development, and this class. It also doesn't ahve the latest version(s). So we recommend installing a new version. +OS-X comes with Python out of the box, but not the full setup you'll need for development, and this class. It also doesn't have the latest version(s), and no versino of Python 3. + +So we recommend installing a new version. .. rst-class:: left @@ -19,16 +21,18 @@ OS-X comes with Python out of the box, but not the full setup you'll need for de If you use ``macports`` or ``homebrew`` to manage \*nix software on your machine, feel free to use those for ``python``, ``git``, etc, as well. If not, then read on. Python -------- +------ While OS-X does provide python out of the box -- it tends not to have the latest version, and you really don't want to mess with the system installation. So I recommend installing an independent installation from ``python.org``: -Download and install Python 3.5.0 64/32 bit installer from Python.org: +Download and install Python 3.4.3 64/32 bit installer from Python.org: + +https://www.python.org/ftp/python/3.4.3/python-3.4.3-macosx10.6.pkg -https://www.python.org/ftp/python/2.7.8/python-2.7.8-macosx10.6.dmg +(you can probably use 3.5.0, too, but it's brand new, and there are a number of third party pacakges that are not yet available for it) Simple as that. @@ -37,6 +41,7 @@ Oddly, this does NOT install a ``python`` command, but rather a ``python3`` comm $ cd /Library/Frameworks/Python.framework/Versions/3.5/bin $ ln -s python3.5 python +(or an alias in your shell -- an *nix geeks here?) Terminal --------- From b3c5d8e1c75393b67665d237b4e5d55b8cdba3e7 Mon Sep 17 00:00:00 2001 From: Chris Barker Date: Sun, 4 Oct 2015 11:41:02 -0700 Subject: [PATCH 008/159] updated references to "homework" to be to "excercises" --- .../source/homework/circle_class.rst | 242 - .../source/homework/html_builder.rst | 308 - slides_sources/source/homework/index.rst | 10 - .../source/homework/kata_fourteen.rst | 139 - .../source/homework/sample_html.html | 27 - slides_sources/source/homework/sherlock.txt | 13052 ---------------- .../source/homework/sherlock_small.txt | 17 - slides_sources/source/index.rst | 7 +- slides_sources/source/session01.rst | 37 +- slides_sources/source/session02.rst | 12 +- slides_sources/source/session03.rst | 15 +- slides_sources/source/session04.rst | 21 +- slides_sources/source/session05.rst | 8 +- slides_sources/source/session06.rst | 6 +- slides_sources/source/session07.rst | 6 +- 15 files changed, 55 insertions(+), 13852 deletions(-) delete mode 100644 slides_sources/source/homework/circle_class.rst delete mode 100644 slides_sources/source/homework/html_builder.rst delete mode 100644 slides_sources/source/homework/index.rst delete mode 100644 slides_sources/source/homework/kata_fourteen.rst delete mode 100644 slides_sources/source/homework/sample_html.html delete mode 100644 slides_sources/source/homework/sherlock.txt delete mode 100644 slides_sources/source/homework/sherlock_small.txt diff --git a/slides_sources/source/homework/circle_class.rst b/slides_sources/source/homework/circle_class.rst deleted file mode 100644 index 65293e70..00000000 --- a/slides_sources/source/homework/circle_class.rst +++ /dev/null @@ -1,242 +0,0 @@ -.. _homework_circle_class: - -================================== -Circle Class Homework Assignment -================================== - -Circle Class -============ - -Goal: ------- - -The goal is to create a class that represents a simple circle. - -A Circle can be defined by either specifying the radius or the diameter, -and the user can query the circle for either its radius or diameter. - -Other abilities of a Circle instance: - - * Compute the circle's area - * Print the circle and get something nice - * Be able to add two circles together - * Be able to compare two circles to see which is bigger - * Be able to compare to see if there are equal - * (follows from above) be able to put them in a list and sort them - -.. nextslide:: - -This exercise should use "new style classes" i.e. inherit from ``object`` - -You will also use: - - - properties - - a classmethod - - a define a bunch of "special methods" - - -General Instructions: ---------------------- - -1. For each step, write a couple of unit tests that test the new features. - -2. Run these tests (and they will fail the first time) - -3. Add the code required for your tests to pass. - - -Step 1: -------- - -create class called ``Circle`` -- it's signature should look like:: - - c = Circle(the_radius) - -The radius is a required parameter (can't have a circle without one!) - -the resulting circle should have a attribute for the radius:: - - c.radius - -So you can do: - -.. code-block:: python - - >> c = Circle(4) - >> print c.radius - 4 - -Remember: tests first! - -Step 2: -------- - -Add a "diameter" property, so the user can get the diameter of the circle: - -.. code-block:: python - - >> c = Circle(4) - >> print c.diameter - 8 - -Step 3: -------- - -Set up the diameter property so that the user can set the diameter of the circle: - -.. code-block:: python - - >> c = Circle(4) - >> c.diameter = 2 - >> print c.diameter - 2 - >> print c.radius - 1 - -**NOTE** that the radius has changed! - -Step 4: --------- - -Add an ``area`` property so the user can get the area of the circle: - -.. code-block:: python - - >> c = Circle(2) - >> print c.area - 12.566370 - -(``pi`` can be found in the math module) - -The user should not be able to set the area: - -.. code-block:: python - - >> c = Circle(2) - >> c.area = 42 - AttributeError - -Step 5: -------- - -Add an "alternate constructor" that lets the user create a Circle directly -with the diameter: - -.. code-block:: python - - >> c = Circle.from_diameter(8) - >> print c.diameter - 8 - >> print c.radius - 4 - -Step 6: -------- - -Add __str__ and __repr__ methods to your Circle class. - -Now you can print it: - -.. code-block:: ipython - - In [2]: c = Circle(4) - - In [3]: print c - Circle with radius: 4.000000 - - In [4]: repr(c) - Out[4]: 'Circle(4)' - - In [5]: d = eval(repr(c)) - - In [6]: d - Out[6]: Circle(4) - -Step 7: --------- - -Add some of the numeric protocol to your Circle: - -You should be able to add two circles: - -.. code-block:: ipython - - In [7]: c1 = Circle(2) - - In [8]: c2 = Circle(4) - - In [9]: c1 + c2 - Out[9]: Circle(6) - -and multiply one times a number: - -.. code-block:: ipython - - In [16]: c2 * 3 - Out[16]: Circle(12) - -(what happens with ``3 * c2`` ? -- can you fix that?) - -.. nextslide:: - -Step 8: --------- -add the ability to compare two circles: - -.. code-block:: ipython - - In [10]: c1 > c2 - Out[10]: False - - In [11]: c1 < c2 - Out[11]: True - - In [12]: c1 == c2 - Out[12]: False - - In [13]: c3 = Circle(4) - - In [14]: c2 == c3 - Out[14]: True - -.. nextslide:: - -Once the comparing is done, you should be able to sort a list of circles: - -.. code-block:: ipython - - In [18]: print circles - [Circle(6), Circle(7), Circle(8), Circle(4), Circle(0), Circle(2), Circle(3), Circle(5), Circle(9), Circle(1)] - - In [19]: circl - circle circle.py circle.pyc circles - - In [19]: circles.sort() - - In [20]: print circles - [Circle(0), Circle(1), Circle(2), Circle(3), Circle(4), Circle(5), Circle(6), Circle(7), Circle(8), Circle(9)] - -**NOTE:** make sure to write unit tests for all of this! Ideally before writing the code. - -Step 8: Optional Features: --------------------------- - -* See if you can make "reflected" numerics do the right thing: - -.. code-block:: python - - a_circle * 3 == 3 * a_circle - -* What else makes sense: division? others? - -* Add the "augmented assignment" operators, where they make sense: - -.. code-block:: python - - a_circle += another_circle - - a_circle *= 2 - -* look through all the "magic methods" and see what makes sense for circles - - diff --git a/slides_sources/source/homework/html_builder.rst b/slides_sources/source/homework/html_builder.rst deleted file mode 100644 index d1345998..00000000 --- a/slides_sources/source/homework/html_builder.rst +++ /dev/null @@ -1,308 +0,0 @@ -.. _homework_html_renderer: - -================================== -HTML Renderer Homework Assignment -================================== - -HTML Render -============ - -Goal: ------- - -The goal is to create a set of classes to render html pages -- in a "pretty printed" way. i.e nicely indented and human readable. We'll try to get to all the features required to render: - -:download:`sample_html.html <./sample_html.html>` - -The exercise is broken down into a number of steps -- each requiring a few more OO concepts in Python. - -General Instructions: ---------------------- - -For each step, add the required functionality. There is example code to run your code for each step in: ``code\session06\run_html_render.py`` - -name your file: ``html_render.py`` -- so it can be imported by ``run_html_render.py`` - -You should be able to run that code at each step, uncommenting each new step in ``run_html_render.py`` as you go. - -It builds up a html tree, and then calls the ``render()`` method of your element to render the page. - -It uses a ``cStringIO`` object (like a file, but in memory) to render to memory, then dumps it to the console, and writes a file. Take a look at the code at the end to make sure you understand it. - -The html generated at each step is in the files: ``test_html_ouput?.html`` - -At each step, your results should look similar that those (maybe not identical...) - - -Step 1: -------- - -Create an ``Element`` class for rendering an html element (xml element). - -It should have class attributes for the tag name ("html" first) and the indentation (spaces to indent for pretty printing) - -The constructor signature should look like - -.. code-block:: python - - Element(content=None) - -where ``content`` is a string - -It should have an ``append`` method that can add another string to the content. - -It should have a ``render(file_out, ind = "")`` method that renders the tag and the strings in the content. - -``file_out`` could be any file-like object ( i.e. have a ``write()`` method ). - -.. nextslide:: - -``ind`` is a string with the indentation level in it: the amount that the tag should be indented for pretty printing. - - - This is a little tricky: ``ind`` will be the amount that this element should be indented already. It will be from zero (an empty string) to a lot of spaces, depending on how deep it is in the tree. - -The amount of indentation should be set by the class attribute: ``indent`` - -NOTE: don't worry too much about indentation at this stage -- the primary goal is to get proper, compliant html. i.e. the opening and closing tags rendered correctly. Worry about cleaning up the indentation once you've got that working. - -You should now be able to render an html tag with text in it as contents. - -See: step 1. in ``run_html_render.py`` - -Step 2: --------- - -Create a couple subclasses of ``Element``, for a ``html``, ````, and ``

`` tag. All you should have to do is override the ``tag`` class attribute (you may need to add a ``tag`` class attribute to the Element class first...). - -Now you can render a few different types of element. - -Extend the ``Element.render()`` method so that it can render other elements inside the tag in addition to strings. Simple recursion should do it. i.e. it can call the ``render()`` method of the elements it contains. You'll need to be smart about setting the ``ind`` optional parameter -- so that the nested elements get indented correctly. - -Figure out a way to deal with the fact that the contained elements could be either simple strings or ``Element`` s with render methods (there are a few ways to handle that...). - -You should now be able to render a basic web page with an html tag around -the whole thing, a ```` tag inside, and multiple ``

`` tags inside that, with text inside that. And all indented nicely. - -See ``test_html_output2.html`` - -NOTE: when you run step 2 in ``run_html_render.py``, you will want o comment out step 1 -- that way you'll only get one set of output. - -Step 3: --------- - -Create a ```` element -- simple subclass. - -Create a ``OneLineTag`` subclass of ``Element``: - -* It should override the render method, to render everything on one line -- for the simple tags, like:: - - PythonClass - Session 6 example - -Create a ``Title`` subclass of ``OneLineTag`` class for the title. - -You should now be able to render an html doc with a head element, with a -title element in that, and a body element with some ``

`` elements and some text. - -See ``test_html_output3.html`` - -Step 4: --------- - -Extend the ``Element`` class to accept a set of attributes as keywords to the -constructor, ie. (``run_html_render.py``) - -.. code-block:: python - - Element("some text content", id="TheList", style="line-height:200%") - -( remember ``**kwargs``? ) - -The render method will need to be extended to render the attributes properly. - -You can now render some ``

`` tags (and others) with attributes - -See ``test_html_output4.html`` - -Step 5: --------- - -Create a ``SelfClosingTag`` subclass of Element, to render tags like:: - -


and
(horizontal rule and line break). - -You will need to override the render method to render just the one tag and -attributes, if any. - -Create a couple subclasses of ``SelfClosingTag`` for and
and
- -See ``test_html_output5.html`` - -Step 6: -------- - -Create a ``A`` class for an anchor (link) element. Its constructor should look like:: - - A(self, link, content) - -where link is the link, and content is what you see. It can be called like so:: - - A(u"http://google.com", u"link to google") - -You should be able to subclass from ``Element``, and only override the ``__init__`` --- Calling the ``Element`` ``__init__`` from the ``A __init__`` - -You can now add a link to your web page. - -See ``test_html_output6.html`` - -Step 7: --------- - -Create ``Ul`` class for an unordered list (really simple subclass of ``Element``) - -Create ``Li`` class for an element in a list (also really simple) - -Add a list to your web page. - -Create a ``Header`` class -- this one should take an integer argument for the -header level. i.e

,

,

, called like - -.. code-block:: python - - H(2, "The text of the header") - -for an

header - -It can subclass from ``OneLineTag`` -- overriding the ``__init__``, then calling the superclass ``__init__`` - -See ``test_html_output7.html`` - -Step 8: --------- - -Update the ``Html`` element class to render the "" tag at the head of the page, before the html element. - -You can do this by subclassing ``Element``, overriding ``render()``, but then calling the ``Element`` render from the new render. - -Create a subclass of ``SelfClosingTag`` for ```` (like for ``
`` and ``
`` and add the meta element to the beginning of the head element to give your document an encoding. - -The doctype and encoding are HTML 5 and you can check this at: http://validator.w3.org. - -You now have a pretty full-featured html renderer -- play with it, add some -new tags, etc.... - -See ``test_html_output8.html`` - - -HTML Primer -============ - -.. rst-class:: medium - - The very least you need to know about html to do this assigment. - -If you are familar with html, then this will all make sense to you. If you have -never seen html before, this might be a bit intimidating, but you really don't -need to know much to do this assignment. - -First of all, sample output from each step is provided. So all you really need -to do is look at that, and make your code do the same thing. But it does help to -know a little bit about what you are doing. - -HTML ----- - -HTML is "Hyper Text Markup Language". Hypertext, because it can contain links -to other pages, and markup language means that text is "marked up" with -instructions about how to format the text, etc. - -Here is a good basic intro: - -http://www.w3schools.com/html/html_basic.asp - -And there are countless others online. - -But here is a tiny intro of just what you need to know for this project. - -Elements --------- - -Modern HTML is a particular dialect of XML (eXrensible Markup Language), -which is itself a special case of SGML (Standard Generalized Markup Language) - -It inherits from SGML a basic structure: each piece of the document is an element. each element is described by a "tag". each tag has a different meaning, but they all have the same structure:: - - some content - -that is, the tag name is surrounded by < and >, which marks the beginning of -the element, and the end of the element is indicated by the same tag with a slash. - -The real power is that these elements can be nested arbitrarily deep. In order to keep that all readable, we often want to indent the content inside the tags, so it's clear what belongs with what. That is one of the tricky bits of this assignment. - -Basic tags ----------- - -.. code-block:: html - - is the core tag indicating the entire document - -

is a single paragraph of text

- - is the tag that indicated the text of the document - - defines the header of the document -- a place for metadata - -Attributes: ------------- - -In addition to the tag name and the content, extra attributes can be attached to a tag. These are added to the "opening tag", with name="something", another_name="somethign else" format: - -.. code-block:: html - -

- -There can be all sorts of stuff stored in attributes -- some required for specific tags, some extra, like font sizes and colors. Note that since tags can essentially have any attributes, your code will need to support that -- doesn't it kind of look like a dict? And keyword arguments? - -Special Elements ----------------- - -The general structure is everything is between and opening and closing tag. But some elements don't really have content -- just attributes. So the slash goes at the end of the tag, after the attributes. We can call these self-closing tags: - -.. code-block:: html - - - -To make a link, you use an "anchor" tag: ````. It required attributes to indicate what the link is: - -.. code-block:: html - - link - -the ``href`` attribute is the link (hyper reference). - -To make a bulleted list, you use a