Do What I Mean


Table of Contents

I use Python for work and play. I make money with Python, and I have fun with it.

There's excellent documentation for Python itself and for many of the Python packages. But how do you use Python in practical, day-to-day use? Automate the boring stuff provides a thorough overview of the basics for beginners. This pages shows how I use Python. There are many other possibilities, but this one works for me.

1 Second best?

Many consider Python 'the second best language for anything'. PHP may be the leading open source language for the web, Java may dominate the enterprise, R may be the dedicated language for data science, and bash may be the go to language for shell scripting, but in all these cases Python is at least a viable alternative.

What this means is that by learning Python you can be competitive in all these areas. And learning Python is relatively easy, because it is considered to be very beginner-friendly (while at the same time being powerful enough to intrigue the most advanced hackers).

But don't get carried away, because just learning Python (the programming language) is not enough. To be productive in any area, you will have to learn the appropriate package(s) as well.

The only area I can think of where Python is not at home, is the web client. There are client-side implementations like Skulpt, but these are still rather obscure. Javascript rulez (sic) in the browser. And with it, its elegant asynchronicity and use of callbacks. Both work in Python (the former since v3.4 through asyncio), but they are not as common.

2 History

Python was originally developed by Guide van Rossum who also was its BDFL for decades. But on July 12, 2018 he indicated he is going to step down.

Version 2.7 of Python has long been my default, but a couple of years ago I switched to version 3, which rectified some of the technical debt that had built up in version 2, at the cost of loosing backwards compatibility.

3 Pythonic

To describe something as clever is not considered a compliment in the Python culture. Alex Martelli - Python Cookbook (O’Reilly)

When working with Python, you will often encounter the phrase pythonic, indicating if something is 'proper python' or not. But what does this mean?

PEP20, or the Zen of Python, describes guiding principles for Python's design:

  • Beautiful is better than ugly.
  • Explicit is better than implicit.
  • Simple is better than complex.
  • Complex is better than complicated.
  • Flat is better than nested.
  • Sparse is better than dense.
  • Readability counts.
  • Special cases aren't special enough to break the rules.
  • Although practicality beats purity.
  • Errors should never pass silently.
  • Unless explicitly silenced.
  • In the face of ambiguity, refuse the temptation to guess.
  • There should be one –and preferably only one– obvious way to do it.
  • Although that way may not be obvious at first unless you're Dutch.
  • Now is better than never.
  • Although never is often better than right now.
  • If the implementation is hard to explain, it's a bad idea.
  • If the implementation is easy to explain, it may be a good idea.
  • Namespaces are one honking great idea – let's do more of those!

Note that the Zen of Python uses terms like "is better than" and "beats", indicating that these are relative, rather than absolute, values. So, flat may be better than nested, but in specific conditions you may still use nesting.

PEP8 contains the Python style guide. Here you will find conventions about layout, whitespace, etc.

The Hitchhiker’s Guide to Python contains an extensive section on writing great code.

To me, constructs like list comprehension and generators feel very Python-specific, although they also occur in other languages.

4 Packages

Pipenv, as its name suggest, combines pip and virtualenv:

Pipenv is primarily meant to provide users and developers of applications with an easy method to setup a working environment. It harnesses Pipfile, pip, and virtualenv into one single command. It features very pretty terminal colors.

Pipenv simplified my Python workflow significantly. Instead of creating a virtual environment, activating it, running pip install, etc., I just do pipenv shell and I'm in business.

Here's a list of the packages I use most:

5 Web development

5.1 With Flask

Web projects differ widely, so there's not one size fits all web framework for me. What I need is just the basics that can be extended with anything the project at hand asks for. Flask is my go to package for web development.

Flask is a microframework for Python based on Werkzeug, Jinja 2 and good intentions. Flask is fun and easy to set up.

Out of the box, Flask just provides minimal http request/response, routing and template support. This means that Flask is unopinionated, but also that, for anything that's not completely trivial, you need to add extensions. Most of my projects need at least SQL Alchemy, WTForms and security. For convenience, I have bundled these in {{% attention %}} Barrel, which optionally also includes admin, REST and datatables modules.

The most advanced part of Barrel is the db module, which provided CRUD-operations and relation decorators. I am working on making that a separate package as SQLAngelo. More on that later.

5.2 With Django

6 Testing

6.1 Unit testing

Python has many unit testing frameworks, like PyTest and Nose2. But I prefer the default unit testing, because Python provides it out of the box, and it satisfies all of my requirements.

6.2 Functional testing

For functional (or behaviorial) testing I prefer Behave that uses Gherkin to describe behavior in (near) natural language.

6.3 Utilities

7 Tips

  • For REPL there is no standard like ipython, but in some cases I prefer ptpython.
  • The command python -m site

8 Packages to look into

©2019 otech-nl