Everything I know about Python...

Learn to Write Pythonic Code!

Check out the book Writing Idiomatic Python!

Discuss Posts With Other Readers at discourse.jeffknupp.com!

Improve Your Python: the with Statement and Context Managers

Of all of the most commonly used Python constructs, context managers are neck-and-neck with decorators in a "Things I use but don't really understand how they work" contest. As every schoolchild will tell you, the canonical way to open and read from a file is:

with open('what_are_context_managers.txt', 'r') as infile:
    for line in infile:
        print('> {}'.format(line))

But how many of those who correctly handle file IO know why it's correct, or even that there's an incorrect way to do it? Hopefully a lot, or else this post won't get read much...

Read on →

Python Dictionaries

Aside: one thing I dislike about the official Python documentation is that only a small percentage of entries have example code. We should change that...)

One of the keys to becoming a better Python programmer is to have a solid grasp of Python's built-in data structures. Using the structured format below, today you'll learn what a dict is, when to use it, and see example code of all of its member functions. I have some other data structures in the works, so this may turn into a little series.

Read on →

Flask and SQLAlchemy Magic

For my first "real" post after my extended leave, I wanted to cover something fun (well, fun if you like writing libraries with Flask and SQLAlchemy...). In one of my projects at work recently, I was tasked with turning a client-facing Flask app that I had written into a framework/library that made creating new "sub-applications" as easy as possible. One pattern that had worked well in the original app was the use of Flask-SQLAlchemy, both for interacting with a new set of database models and in connecting to existing databases owned by other teams. One existing database, in particular, was heavily used by the app and would likely be used a good deal by any "sub-apps" created on the framework.

The database in question was essentially our company's "main" database, with about 100 tables (each with thousands or millions of records). This is a common, and somewhat frustrating situation when writing internal libraries: you know clients are going to need to use some resource, but you're not exactly sure how or what parts of it. How do you create an interface to that? With SQLAlchemy specifically, does that mean each sub-project will need to describe all of the tables it needs to use from the "main" database, leading to repeated, boilerplate code? Of course not! All it takes is a little magic.

Read on →

Sorry for the Silence. I'm Back!

I'm Sorry/I'm Back

My second daughter was born on October 27th. I'll be honest: I haven't read email sent to jeff@jeffknupp.com in almost three months. In fact, I haven't done anything besides my day job, sleeping, and spending time with my girls for the past three months. Throw in a medical condition (that has been resolved) and you can see why the past few months have been difficult for me. I know that I'm making excuses, but I wanted to add some context about my silence.

Thanks to my incredible wife and daughters, I now have the chance to be active online once again. For those of you who emailed me and didn't receive a response, I'm terribly sorry. I promise I will get to every single one in the weeks ahead. If you asked for a free copy of the book, you'll get one. It's going to take me a bit of time to catch up with everything, but it will get done.

Read on →

Web Analytics