Skip to content

Commit f2562dc

Browse files
committed
chapter 1 rewrite
1 parent cea4bb8 commit f2562dc

File tree

3 files changed

+280
-82
lines changed

3 files changed

+280
-82
lines changed

doc/source/1_introduction.rst

Lines changed: 240 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -66,84 +66,235 @@ achieve more and do less work.
6666
Obtaining the right software tools
6767
----------------------------------
6868

69-
In order to do this module, you'll need some core software tools. As the module
70-
proceeds we'll also install several more Python packages, but you don't need to
71-
install those right now. The core tools you will need are:
69+
In order to do the exercises in this book, you'll need some core software
70+
tools. At various points you'll also need install several more Python packages,
71+
but you don't need to install those right now. The core tools you will need
72+
are:
7273

7374
1. Python version 3.6 or later.
7475
2. Git (the revision control system we're going to use).
75-
3. A Python-aware text editor. Visual Studio Code is recommended, and all
76-
the instructions in this course will assume that this is what you are using.
76+
3. A Python-aware text editor or :term:`integrated development
77+
environment`. Visual Studio Code is recommended, and all the
78+
instructions will assume that this is what you are using.
79+
80+
.. only:: book
81+
82+
Installation instructions are the piece of information most liable to go
83+
rapidly out of date. Further, at the point at which you install software,
84+
you are essentially by definition seated at a computer with internet
85+
access. Rather than provide the install information here, this information
86+
is instead provided on the book website:
87+
88+
`https://object-oriented-programming.github.io/installation.html
89+
<https://object-oriented-programming.github.io/installation.html>`__
90+
91+
.. only:: latex and not book
92+
93+
Installation instructions are the piece of information most liable to go
94+
rapidly out of date. Further, at the point at which you install software,
95+
you are essentially by definition seated at a computer with internet
96+
access. Rather than provide the install information here, this information
97+
is instead provided on `the book website:
98+
<https://object-oriented-programming.github.io/installation.html>`__.
99+
100+
.. only:: html
101+
102+
Installation instructions for these pieces of code are provided `on this
103+
page <https://object-oriented-programming.github.io/installation.html>`__`.
104+
105+
Python versions
106+
...............
107+
108+
Python is a living language which regularly releases new versions. One very big
109+
transition, which took over a decade, was the switch from version 2 to version
110+
3 of the language. For many years both versions were in widespread use and
111+
texts needed to cover their differences. However, Python 2 officially reached
112+
end of life on 1 January 2020 and not even critical security updates are now
113+
made to that version. You should, therefore, only ever use Python 3, and all
114+
references in this book are to that version.
115+
116+
Within Python 3, there is a minor version release approximately every year.
117+
Once released, this receives security updates for 5 years. At the time of
118+
writing, Python 3.10 is the newest release version, and Python 3.6 is the
119+
oldest version that still recieves security fixes. The user-facing differences
120+
between minor Python versions are usually fairly minimal, so for the purposes
121+
of this book it doesn't matter which of the currently supported versions of
122+
Python you use.
77123

78-
The Faculty of Natural Sciences at Imperial has
79-
:doc:`centralised instructions for installing all of these tools <fons:index>`, and we'll follow those.
124+
.. warning::
125+
126+
The example code in the exercises uses :ref:`f-strings <tut-f-strings>`
127+
which were introduced in Python 3.6, so the code will not work in earlier
128+
versions of Python.
129+
130+
Setting up a Python virtual environment
131+
---------------------------------------
132+
133+
.. details:: Video: setting up your virtual environment.
134+
135+
.. vimeo:: 486546635
136+
137+
.. only:: html
138+
139+
Imperial students can also `watch this video on Panopto <https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=011d73de-d93c-4dc8-8996-ac8501521b33>`__
140+
141+
In the course of the exercises, You're going to create, edit, and install a
142+
whole bunch of Python packages. It's highly desirable have a predictable
143+
programming environment in which the experiments you're doing don't interfere
144+
with anything else for which you might be using Python, and conversely which
145+
remains unaffected by any packages you may have installed elsewhere. This
146+
separation can be achieved by doing everything in a Python :term:`virtual
147+
environment`, or :term:`venv`.
148+
149+
A virtual environment is a folder containing a local installation of Python
150+
which links back to the Python you installed on your computer. This means that
151+
virtual environments behave like separate Python installations for most
152+
purposes, but are fast to install and take very little space because they share
153+
most of their files with the already installed Python.
154+
155+
Creating a working folder
156+
.........................
157+
158+
Start by creating a completely fresh folder for your work on this book. You can
159+
call this anything you like. On my computer this is called
160+
:file:`principles_of_programming`. You can create this folder using the
161+
Windows File Explorer, Mac Finder, or by typing the following in a terminal:
162+
163+
.. code-block::
164+
165+
$ mkdir principles_of_programming
166+
167+
Obviously you replace :file:`principles_of_programming` with whatever you
168+
decide to call the folder. The dollar sign is the command prompt. Its different
169+
on some systems, for example, it's often a greater than sign (`>`) or a percent
170+
symbol (`%`). The text to the left of the command prompt might also be
171+
different depending on which terminal program you are using on which operating
172+
system, but we are only concerned with the commands to the right of the prompt.
173+
174+
.. hint::
175+
176+
Modern operating systems are quite capable of dealing with folder names and
177+
file names containing spaces. However, there are many pieces of software
178+
(including some Python packages) that don't correctly deal with spaces in
179+
folder and file names. It's therefore a safer option to avoid spaces and
180+
instead to separate words with underscores (:file:`_`).
181+
182+
Creating the venv
183+
.................
184+
185+
Now that we have our working folder, we will switch to doing everything in our
186+
:term:`IDE`, so launch Visual Studio Code. Click on `Open...` in
187+
the main window or in the `File` menu and select the folder you just created.
188+
This will open a Visual Studio Code workspace in that folder. You will probably
189+
be able to see a terminal window at the bottom of the screen. If it's not
190+
there then open the `View` menu and select `Terminal` to make it appear.
191+
192+
The most straightforward way to create a venv is on the terminal
193+
command line, not from within Python itself. This is accomplished
194+
using Python's :mod:`venv` package. For example, to create a venv
195+
called `my_venv` on Windows, you would type:
196+
197+
.. code-block:: console
198+
199+
> py -m venv my_venv
80200
81-
Python
82-
......
201+
while on Mac or Linux you would type:
83202

84-
Follow the :doc:`FoNS Python instructions <fons:python>`. We will exclusively
85-
use :ref:`virtual environments <fons:python_virtual_environments>` so it doesn't matter at
86-
all whether you use Python from Anaconda or from another source. Mac users
87-
should note, though that the built-in Python will not do, so you should use
88-
either Homebrew or Anaconda.
203+
.. code-block:: console
204+
205+
$ python3 -m venv my_venv
206+
207+
Don't forget that the `>` or `$` stands for the command prompt: you don't
208+
type it. This command will create the folder `my_venv` and various
209+
subfolders containing things like the Python program itself and space
210+
for any packages which you install in the venv. If there was already a
211+
file or folder called `my_venv` in the current folder then you'll get
212+
an error, so make sure you choose a new name.
89213

90214
.. note::
91215

92-
The example code in the exercises uses :ref:`f-strings <tut-f-strings>`
93-
which were introduced in Python 3.6, so the code will not work in earlier
94-
versions of Python.
216+
Running `py` on Windows or `python3` on Mac or Linux is a mechanism to
217+
attempt to ensure that the right version of Python runs. If you have
218+
multiple Python installations on your computer then you might end up
219+
running the wrong one. If this happens then you will need to type the full
220+
path to the Python you want to use (starting with `/` on Mac or Linux or
221+
`\` on Windows). Once the venv is installed and activated, it will be
222+
sufficient to type `python` as the venv will ensure that this is the
223+
correct version.
95224

96-
Git
97-
...
225+
A venv doesn't usually contain any particularly valuable data, so you
226+
should regard them as essentially disposable. In particular, if
227+
something goes wrong when creating a venv, just delete it and start
228+
again. In the bash or zsh shells you would type:
229+
230+
.. code-block:: console
98231
99-
Git is a revision control system. Revision control systems enable you to keep
100-
track of the different versions of a piece of code as you work on them, and to
101-
have these versions on different computers as well as backed up in the cloud. We
102-
will use Git and GitHub classroom as a mechanism for distributing, working with
103-
and submitting code exercises.
232+
$ rm -rf my_venv
104233
105234
.. warning::
106235

107-
When you come to the assessable programming tests that make up 100% of the
108-
assessment for this module, the code will be distributed and submitted using
109-
Git. It is therefore essential that you incorporate Git into your day to day
110-
workflow so that when you come to the test, it's second nature. You will not
111-
receive marks for test answers that are not committed and pushed.
236+
`rm -rf` will delete its argument and all its subdirectories
237+
without further prompts or warnings. There is no undo operation.
238+
Be very careful about what you delete.
112239

113-
Visual Studio Code
114-
..................
240+
.. _activate_venv:
115241

116-
Visual Studio Code is a Python-aware Integrated Development Environment (IDE).
117-
This means that it incorporates editing files with other programming features
118-
such as :ref:`debugging`, Git support, and built-in :ref:`terminal
119-
<terminal-vscode>`. Visual Studio Code also provides an incredibly useful remote
120-
collaborative coding feature called Live Share. This will be very useful for
121-
getting remote help from an instructor.
242+
Using a venv
243+
............
122244

123-
Setting up a Python environment for this course
124-
-----------------------------------------------
245+
If you run Python from the terminal, then the simplest way to use the
246+
venv is to source its activate script. If using bash or zsh on Mac or
247+
Linux you would type:
125248

126-
.. details:: Video: setting up your virtual environment.
249+
.. code-block:: console
127250
128-
.. vimeo:: 486546635
251+
$ source my_venv/bin/activate
129252
130-
.. only:: html
253+
while using bash on Windows you would type:
131254

132-
Imperial students can also `watch this video on Panopto <https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=011d73de-d93c-4dc8-8996-ac8501521b33>`__
255+
.. code-block:: console
256+
257+
$ source my_venv/Scripts/activate
133258
259+
If using PowerShell on Windows then you type:
134260

135-
During this course, we're going to create, edit, and install a whole bunch of
136-
Python packages. In order to have a predictable programming environment in which
137-
the experiments we're doing don't interfere with anything outside the course for
138-
which we might be using Python, and conversely to ensure that nothing we've
139-
installed elsewhere interferes with how we're doing the course, we'll do
140-
everything in a Python :term:`virtual environment`, or :term:`venv`. You should read up on Python
141-
virtual environments on the :ref:`Faculty of Natural Sciences Python installation
142-
page <fons:python_virtual_environments>`.
261+
.. code-block:: powershell
262+
263+
> .\my_venv\Scripts\activate.ps1
264+
265+
Obviously, you would use the folder name of your venv instead of
266+
`my_venv`. In either case, your command prompt will change to indicate
267+
that you are now using the venv. It might look something like:
268+
269+
.. code-block:: console
270+
271+
(my_venv) $
272+
273+
Any subsequent invocations of Python commands such as `python3` will
274+
now use the version from the venv, with access to whatever packages
275+
you have installed in that venv. If you are using a terminal shell
276+
other than bash or zsh, then see the :mod:`venv` package documentation
277+
for the correct activation command.
143278

144279
.. hint::
145280

146-
Don't forget that you need to activate the venv in every new :ref:`terminal <fons:terminal>` session.
281+
The default permissions settings on your Windows computer may not permit you
282+
to run the activation script. This can be fixed by running:
283+
284+
.. code-block:: console
285+
286+
> Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
287+
288+
For further information, see :doc:`the official Python venv documentation
289+
<library/venv>`.
290+
291+
.. hint::
292+
293+
Venv activation is just for one terminal session. You need to activate the
294+
venv every time you open a new terminal, though if you are lucky then
295+
Visual Studio Code will notice the venv and activate it for you. If you
296+
find that Python can't find your packages or tests, then the first thing to
297+
check is whether you remembered to activate the venv.
147298

148299
Installing Python packages
149300
--------------------------
@@ -189,35 +340,52 @@ Pip can also be used to upgrade a package to the latest version:
189340
190341
(my_venv) $ python -m pip install --upgrade numpy
191342
343+
Glossary
344+
--------
345+
346+
.. glossary::
347+
:sorted:
348+
349+
IDE
350+
integrated development environment
351+
A program designed to help a software developer write code. An IDE
352+
combines a text editor with features such as syntax highlighting and
353+
checking, debugging capabilities, revision control interfaces and
354+
inbuilt terminal windows.
355+
356+
venv
357+
virtual environment
358+
A lightweight private Python installation with its own set of
359+
Python packages installed.
192360

193361

194362

195-
Exercises
196-
---------
363+
.. Exercises
364+
.. ---------
197365
198-
This week's exercises are designed to ensure that you are set up with the core
199-
tools that you will need for the rest of the module. Exceptionally, there is no
200-
quiz this week as we haven't yet started with the substantive contents of the
201-
module. Nonetheless, this week's exercises are an important baseline. Skipping
202-
them is likely to result in you having to play catchup in the coming weeks.
366+
.. This week's exercises are designed to ensure that you are set up with the core
367+
.. tools that you will need for the rest of the module. Exceptionally, there is no
368+
.. quiz this week as we haven't yet started with the substantive contents of the
369+
.. module. Nonetheless, this week's exercises are an important baseline. Skipping
370+
.. them is likely to result in you having to play catchup in the coming weeks.
203371
204-
.. proof:exercise::
372+
.. .. proof:exercise::
205373
206-
Install Python using the :doc:`FoNS Python installation instructions <fons:python>`.
374+
.. Install Python using the :doc:`FoNS Python installation instructions <fons:python>`.
207375
208-
.. proof:exercise::
376+
.. .. proof:exercise::
209377

210-
Install Git and work through the entire Git, GitHub, and GitHub Classroom
211-
tutorial on the :doc:`FoNS Git instructions webpage <fons:git>`.
378+
.. Install Git and work through the entire Git, GitHub, and GitHub Classroom
379+
.. tutorial on the :doc:`FoNS Git instructions webpage <fons:git>`.
212380
213-
.. proof:exercise::
381+
.. .. proof:exercise::
214382

215-
Install Visual Studio Code using the :doc:`FoNS Visual Studio Code
216-
installation instructions <fons:vscode>`.
383+
.. Install Visual Studio Code using the :doc:`FoNS Visual Studio Code
384+
.. installation instructions <fons:vscode>`.
217385
218-
.. proof:exercise::
386+
.. .. proof:exercise::
219387

220-
With one or two friends from the class, follow the
221-
:ref:`Live Share instructions <vscode-liveshare>`.
222-
Ensure that each of you can start a Live Share session and have the other
223-
successfully join, and that all of you can edit files.
388+
.. With one or two friends from the class, follow the
389+
.. :ref:`Live Share instructions <vscode-liveshare>`.
390+
.. Ensure that each of you can start a Live Share session and have the other
391+
.. successfully join, and that all of you can edit files.

doc/source/2_programs_in_files.rst

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,7 @@ systems, you would type the following:
162162
(my_venv) $ cd src
163163
(my_venv) $ python hello.py
164164
165-
The dollar sign is the command prompt. Its different on some systems, for
166-
example, it's often a greater than sign (`>`) or a percent symbol (`%`). The
167-
text to the left of the command prompt might also be different depending on
168-
which terminal program you are using on which operating system, but we are only
169-
concerned with the commands to the right of the prompt. The first of these,
165+
The first of these commands,
170166
`cd` (*change directory*) switches the current folder to :file:`src`. The
171167
second command actually runs the Python interpreter on :file:`hello.py`. From
172168
within our venv, we can be confident that `python` will refer to the right
@@ -926,11 +922,6 @@ Glossary
926922
A text file containing a sequence of Python statements to be
927923
executed. In Python, program and script are synonymous.
928924

929-
venv
930-
virtual environment
931-
A lightweight private Python installation with its own set of
932-
Python packages installed.
933-
934925
Exercises
935926
---------
936927

0 commit comments

Comments
 (0)