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

#21: Python has a new star framework for RESTful APIs

Published Thu, Apr 13, 2017, recorded Wed, Apr 12, 2017

This episode has been sponsored by Rollbar. Get a special offer via http://rollbar.com/pythonbytes

#1 Brian: profile and pstats — Performance Analysis

  • Doug Hellman is working on the Python 3 MOTW series that was so successful for Python 2.
  • Recent edition is profile and pstats, for profiling parts of your code you may have concerns with and finding out where the slow bits are.

#2 Michael: API Star by Tom Christie

  • A smart Web API framework, designed for Python 3.
  • A few things to try right away:
        $ pip3 install apistar
        $ apistar new --template minimal
        $ apistar run
        $ apistar test
    
  • API Star allows you to dynamically inject various information about the incoming request into your views using type annotation.
    • e.g.
          def show_query_params(query_params: http.QueryParams):
              return {
                  'params': dict(query_params)
              }
      
  • You can instead set the status code or headers by annotating the view as returning a Response
    def create_project() -> Response: ...
    
  • Parameters are automatically passed into views from routes (annotations!):
        def echo_username(user_id: int):
            return {'message': f'Welcome, user {user_id}!'}
    
  • Performance: Faster than sanic!

#3 Brian: Yes, Python is Slow, and I Don’t Care

  • Optimize for your most expensive resource. That’s YOU, not the computer.
  • Choose a language/framework/architecture that helps you develop quickly (such as Python). Do not choose technologies simply because they are fast.
  • When you do have performance issues: find your bottleneck
  • Your bottleneck is most likely not CPU or Python itself.
  • If Python is your bottleneck (you’ve already optimized algorithms/etc.), then move the hot-spot to Cython/C
  • Go back to enjoying getting things done quickly

#4 Michael: A Quick Introduction: Hashing

  • Article by Gerald Nash
  • Hashing is a method of determining the equivalence of two chunks of data.
  • A cryptographic hash function is an irreversible function that generates a unique string for any set of data.
  • Example
        import hashlib as hash
        sha = hash.sha256()
        # Insert the string we want to hash
        sha.update('Hello World!')
        # Print the hexadecimal format of the binary hash we just created
        print(sha.hexdigest())
        # 4d3cf15aa67c88742e63918825f3c80f203f2bd59f399c81be4705a095c9fa0e
    
  • Know when to choose “weak” hashes vs. strong ones
  • Straight hashes are not enough for security (e.g. passwords). Use passlib and be done.

#5 Brian: Wedding at Scale: How I Used Twilio, Python and Google to Automate My Wedding

  • gspread to access a google spreadsheet of guests and phone numbers
  • SMS guests with twilio
  • replies handled by a flask app
  • gathered accept/decline/didn't reply statistics
  • reminder texts
  • food selections and replies and reminders, all handled by Python

# 6 Michael: python-alexa: A Python framework for Alexa Development

  • by Neil Stewart
  • Ordered an amazon assistant.
  • Before it arrived, I had challenged myself to develop something for it
  • Project: VoiceOps, interact with an AWS account, such as telling me how many running and stopped instances there is or what RDS databases are in an account
  • Wanted a framework that would make Alexa development super easy.
  • Decided a new framework was needed: python-alexa
  • python-alexa on github
  • echo shim for testing without hardware

Our news:

Michael: Just added full text search (including within videos) to Talk Python courses.

Brian: Netflix chaos engineering interview on Test & Code


Want to go deeper? Check our projects