Home » Python (page 7)

Python

Read-Only Properties in Python

Python descriptors are a fairly powerful mechanic, especially for creating properties. The built-in property descriptor/decorator works very well for defining properties, but they have a small weakness in my eyes: it doesn’t have defaults for simple usage. Yes, not supplying the property class with all methods does have the decent default of not allowing that usage, but I mean that ...

Read More »

Python: Selecting certain indexes in an array

A couple of days ago I was scrapping the UK parliament constituencies from Wikipedia in preparation for the Graph Connect hackathon and had got to the point where I had an array with one entry per column in the table. import requests   from bs4 import BeautifulSoup from soupselect import select   page = open("constituencies.html", 'r') soup = BeautifulSoup(page.read())   ...

Read More »

Explicit Currying in Python

Last week, I talked about currying a little bit, and I’ve been thinking about how one could do in languages that don’t have it built in. It can be done in Java, but the type of even a two-parameter function would be something like Function<Integer, Function<Integer, Integer>>, which is bad enough. Imagine 3- or 4-parameter functions. There’s also the fact ...

Read More »

Eventually Correct: Async Testing With Tornado

Async frameworks like Tornado scramble our usual unittest strategies: how can you validate the outcome when you do not know when to expect it? Tornado ships with a tornado.testing module that provides two solutions: the wait / stop pattern, and gen_test.   Wait / Stop gen_test Further Study Wait / Stop To begin, let us say we are writing an ...

Read More »

Python: matplotlib hangs and shows nothing (Mac OS X)

I’ve been playing around with some of the matplotlib demos recently and discovered that simply copying one of the examples didn’t actually work for me. I was following the bar chart example and had the following code: When I execute this script from the command line it just hangs and I don’t see anything at all. Via a combination of ...

Read More »

PyMongo And Key Order In Subdocuments

Or, "Why does my query work in the shell but not PyMongo?" Variations on this question account for a large portion of the Stack Overflow questions I see about PyMongo, so let me explain once for all. MongoDB stores documents in a binary format called BSON. Key-value pairs in a BSON document can have any order (except that _id is ...

Read More »

Want to take your WEB dev skills to the next level?

Grab our programming books for FREE!

Here are some of the eBooks you will get:

  • PHP Programming Cookbook
  • jQuery Programming Cookbook
  • Bootstrap Programming Cookbook
  • Building WEB Apps with Node.js
  • CSS Programming Cookbook
  • HTML5 Programming Cookbook
  • AngularJS Programming Cookbook