3/8/2022: Advanced Language Constructs

A collection of notes to go over in class, to keep things organized.

NOTES:

I’ll try to have a break every hour or so – ping me if I forget!

Issues from this week

I usually find a number of sticking points, confusions, etc, when reviewing your work – and taking your questions.

This week – not so much! congrats!

So we can take this time to answer questions – what are you still unsure of?

Fire Away!

And/or we can do a little live code review.

Break Time!

10min break:

Metaprogramming

This week’s topic is “Advanced Language Constructs”.

In particular, we talk about Decorators, Context Managers, and recursion.

Recursion is more a technique than a language feature – and a very powerful one.

Any questions on recursion? – you should have all seen it a bit last quarter.

Decorators and Context Managers are a little different – they both use language features: the with keyword and the @decoration syntax. And you’ve all been using them some already.

But actually making new ones requires what’s broadly known as “metaprogramming”.

Metaprogramming sounds really fancy – but in its simplest form, it’s pretty straightforward. But you need to know the tools available.

So let’s dig deeper into that now:

https://uwpce-pythoncert.github.io/ProgrammingInPython/modules/MetaProgramming.html

(Again from the Course One Notes)

If time: let’s write a class decorator to make it easy to save dataclasses into Mongo:

Examples/lesson09/class_decorator

Break Time!

10min break

This week’s assignment: let’s take a look.

One of the key parts is creating a context manager to manage the database connection – we saw this a couple weeks ago in Luis’ example.

But before you can do that – you need to think about how the database connection is managed – in many of the examples, and in many of your solutions (and ours) – the database is created at the top of a module. So it gets started up right at module import – making it very hard to control how it happens.

So let’s look at that first.

Example for Mongo in:

Examples/lesson09/mdb.py