Bot Diary, part 1

Chatbots are the hot topic of conversation among developers and investors alike right now. Advances in natural language processing (NLP) and wide adoption of a few messaging platforms like Facebook Messenger, Slack, Snapchat and Whatsapp have presented developers with a unique opportunity. Would you rather navigate to, say, Nordstrom’s website, find the shoe section, figure out the categories, search, choose a pair, add them to a cart, etc., or would you rather message the Nordstrom’s bot: “I need a pair of brown dress shoes, size 10 1/2, leather, and I like Johnson and Murphy” and have it respond with a selection which you can order from simply by tapping? Yeah, thought so. Me too.

Chatbots have a number of advantages. The chat platform (FB for example) knows who you are so that whole create account/auth business can be dispensed with. You can save your personal details like shipping address and payment methods once, and have them shared with companies whose bots you interact with. You don’t have to learn the organization of a new website with a unique information flow. You don’t even have to remember their domain name, and there are still plenty of opportunities for branding and positive customer interactions.

Continue reading

Validate json models with swagger and bravado

Originally published at https://medium.com/@betz.mark/validate-json-models-with-swagger-and-bravado-5fad6b21a825

If you design and implement APIs for a living then you’re probably already familiar with swagger. Swagger is a specification for defining API endpoints and the model objects they transact. Once you have a swagger definition of your API written in either json or yaml you can do quite a few useful things with it: generate HTML documentation on the fly; generate client and server code; generate a postman collection for endpoint testing; and more to the point of this piece, use the spec to validate incoming objects at request time, resulting in meaningful error responses to clients.

To demonstrate the json validation capabilities we will create a very simple API for managing my fleet of dream cars, and then implement it using flask in python 2. Validation will be performed against our swagger spec using the bravado-core package from Yelp. The spec itself will be written in yaml, since it’s a little less verbose than json and will make for easier reading. To begin we can add some boilerplate and then define the objects that our API will make use of:

Continue reading

De-mystifying python decorators

Originally published at https://medium.com/@betz.mark/de-mystifying-python-decorators-7c50c66fc180

When I first started programming in python I was confused by decorators. I don’t remember where I first encountered them, but it was probably in a framework like flask, and probably looked something like this:

@app.route("/")
def hello():
    return "Hello World!"

This example comes from the flask documentation, and shows how a function is added to the flask application’s routing table. After the module containing the definition above is imported the flask framework will know how to route requests to “/” to the “hello” function. So how does this work?

Let’s simplify that example even further and pick it apart:

@foo
def bar():
    print "In bar"    
    return
Continue reading

Cross-thread event dispatching in python

Originally published at https://medium.com/@betz.mark/cross-thread-event-dispatching-in-python-fc956446ad16

This article will discuss dispatching events across threads in python 2, using a parallel file downloading utility as a working example. Before we dive into that let’s spend a couple of minutes talking about python 2 and threads in general. Mention the two things together in a sentence and many developers will respond with puzzlement. “Python isn’t multi-threaded,” they say, ending with an invocation of the dreaded GIL, or Global Interpreter Lock. This is incorrect. Python is multi-threaded. What python is not is ̶c̶o̶n̶c̶u̶r̶r̶e̶n̶t̶ parallel (thanks to Tomasz P for setting me straight on the difference, see his response). There’s a difference.

Python can run lots of threads, but because of the GIL the python interpreter can only run one thread at a time. That means that if a thread wants to do work it has to acquire the interpreter lock, which it can only do when another thread doesn’t already have it. The GIL is the synchronization primitive that serializes requests to the interpreter from different threads. So splitting some work into multiple threads doesn’t actually get it done any faster. We can see this clearly in a simple example:

Continue reading