Lecture 7

Notebooks & Context Managers

MCS 275 Spring 2024
Emily Dumas

View as:   Presentation   ·   PDF-exportable  ·   Printable

Lecture 7: Notebooks & Context Managers

Reminders and announcements:

  • Homework 1 feedback in Gradescope
  • Homework 3 will be posted Thursday afternoon

Python interfaces

  • REPL — Enter statements one by one. Each result is immediately printed.
  • 🟨 Notebook — Blocks of code and formatted text. Running a code block displays the last expression.
  • Script mode — Text file full of code, runs all at once. By default there is no output.

What notebooks look like

MCS 275 uses notebooks for homework, worksheets, and project descriptions, so you've seen these before. But you usually see a version converted to HTML.

How to use notebooks

Several options:
  • Google Colab — Web tool to create, edit, run notebooks. Need a Google account. Can save or download notebooks.
  • Other online provider, e.g. Kaggle, CoCalc
  • Jupyter — Software you install locally to create, edit, run notebooks. Browser shows the UI. Previously called IPython.
  • VS Code — Has an extension for handling notebook files.

Jupyter install instructions

Most users can install Jupyter using pip:

python3 -m pip install notebook

Then run the interface with:

python3 -m notebook

Of course, you need to replace python3 with your interpreter name.

Using Colab / Jupyter

A few of the many keyboard shortcuts:

  • shift-enter — run the current cell
  • escape — switch from cell editing to navigation
  • a — in nav mode, add a new cell ABOVE this one
  • b — in nav mode, add a new cell BELOW this one
  • dd — in Jupyter, in nav mode, delete current cell (colab has a delete button, and a different shortcut)
  • m — in Jupyter, in nav mode, make current cell a Markdown (text) cell

Jupyter pitfalls

You see the whole notebook, but the Python interpreter only sees code cells you run.

Output from cells can be saved with the notebook, making it hard to tell which cells have been executed.

Jupyter server program keeps running until you stop it (closing browser does not do that).

Markdown

Text cells (Colab) or markdown cells (Jupyter) contain formatted text. When editing, formatting is specified with a language called Markdown.

# Heading level 1
## Heading level 2
### Heading level 3

* Bullet list item
* Another **excellent** item

1. *Numbered* list item
1. Another numbered list item

You can also do math: $\int_0^\infty e^{-x^2} \, dx$

Links: [text to display](https://example.com)
    

Will this function always close the file?

def file_contains_walrus(fn):
    """Return True if "walrus" is a line of file `fn`"""
    fileobj = open(fn,"r",encoding="UTF-8")
    for line in fileobj:
        if line.strip() == "walrus":
            fileobj.close()
            return True
    return False        

Currently, in CPython (the usual interpreter): Yes.

In CPython, local variables are deleted as soon as a function returns. Deleting a file object closes the file.

But this isn't a language guarantee!

Common pattern

  1. Acquire a resource (e.g. open file)
  2. Use it to do something
  3. Release resource (e.g. close)

Possible bug: something might prevent step 3 from being reached (e.g. exception or return in step 2). Resource may not be released at all.

Better file I/O

Use with block to ensure automatic file closing.

with open("data.txt","w",encoding="UTF-8") as fileobj:
    fileobj.write(...)
    fileobj.write(...)
    # other write operations...
print("At this point, the file is already closed")

Extra bonus: you can see exactly what part of the program needs the open file.

Cleanup guarantee

A file opened using a with block will be closed as soon as execution flow leaves the block even if it does so due to an exception or return.

Recommendation

Always open files using with, and make the body as short as possible.

Think of files like refrigerators: Open only if needed, and for the shortest time possible.

Context managers

The with statement is not just for files; it can be used for any acquire-use-release situation.

Any class that is a context manager can be used after with in the same way as open().

A context manager is a class with special methods:

  • __enter__ to perform setup
  • __exit__ to perform cleanup

Examples

Context managers are appropriate for:

  • Network connections
  • Database connections
  • Locks
  • Any limited or exclusive access right
  • Temporary setup or changes that must be reverted

Context manager protocol

  • __enter__(self):
    • Performs setup
    • Return value is assigned to the name after as in with statement.
  • __exit__(self,exc_type,exc,tb):
    • Performs cleanup.
    • The arguments describe any exception that happened in the with block.

Expect each method to be called exactly once.

Built-in context managers

Some examples (listed as class - resource)

  • open - Open file
  • threading.Lock - Thread-exclusive right
  • urllib.request.urlopen - HTTP connection
  • tempfile.TemporaryFile - Temporary file (deleted after use)

References

Revision history

  • 2023-01-31 Finalization of the 2023 lectures this was based on
  • 2024-01-23 Initial publication