Brought to you by Michael and Brian - take a Talk Python course or get Brian's pytest book

#55: Flask, Flask, Flask, 3x Flask

Published Thu, Dec 7, 2017, recorded Wed, Dec 6, 2017

Sponsored by DigitalOcean: http://digitalocean.com

Brian #1 The Flask Mega-Tutorial, reborn

  • This very popular tutorial, written in 2012, has been rewritten.
  • Miguel Grinberg has rewritten it with the help of a kickstarter campaign.
  • Part 1 of the tutorial is up, and he’s releasing 1 part per week.
  • Want it faster, you can get it all in an eBook right now.
  • A video version is coming in January.

Michael #2: Django 2.0 Released

Brian #3: The Big Ol' List of Rules

  • Flake8 is a popular code linter that combines pyflakes, pycodestyle, and mccabe.
    • pycodestyle is the new pep8 to enforce PEP8 suggestions. These are mostly style guide items, and not actual bugs.
    • pyflakes is more like a traditional linter in that it catches things that are probably oversight or bugs.
    • mccabe is harder to explain, but it generally tells you if your code might be too complicated, using Cyclomatic Complexity.
  • Flake8 produces error codes if your code has problems
    • Ennn and Wnnn for pycodestyle errors and warnings
    • Fnnn for pyflakes errors
    • Cnnn for mccabe errors
  • The The Big Ol' List of Rules is a very nice breakdown of every error, what it means, and has links to other documents where they are defined.
  • Very nice work from Grant McConnaughey

Michael #4: requests-staticmock

  • via Anthony Shaw
  • The Session object allows you to persist certain parameters across requests. It also persists cookies across all requests made from the Session instance, and will use urllib3's connection pooling. So if you're making several requests to the same host, the underlying TCP connection will be reused, which can result in a significant performance increase
  • A Session object has all the methods of the main Requests API.
  • requests-staticmock is a static HTTP mock interface for testing classes that leverage Python requests with no monkey patching!

Brian #5: PEP 557 -- Data Classes have been approved

Very short Example lifted directly from PEP 557 doc.

@dataclass
class C:
    a: int       # 'a' has no default value
    b: int = 0   # assign a default value for 'b'

In this example, both a and b will be included in the added __init__ method, which will be defined as:

def __init__(self, a: int, b: int = 0):
    pass
  • Why not just use attrs? (Also lifted from the pep doc)
    • attrs moves faster than could be accommodated if it were moved in to the standard library.
    • attrs supports additional features not being proposed here: validators, converters, metadata, etc. Data Classes makes a tradeoff to achieve simplicity by not implementing these features.

Michael #6: Quart: 3x faster Flask

  • Python has evolved since Flask was first released around 8 years ago, particularly with the introduction of asyncio.
  • Asyncio has allowed for the development of libraries such as uvloop and asyncpg that are reported (here, and here) to improve performance far beyond what was previously possible.
  • Quart provides the easiest transition for Flask apps to use asyncio as it shares the Flask-API.
  • tl;dr: Upgrading this Flask-pyscopg2 app to a Quart-asyncpg app gives a performance speedup of 3x without requiring a major rewrite or adjustment of the code
  • View methods become async / await methods

Our news

Michael:


Want to go deeper? Check our projects