Long ago, we briefly brushed upon the topic of what has made jQuery such a valuable part of the web developer’s toolset for such a long time – namely, a cleaner interface for interacting with the DOM, and the $.ajax abstraction over XMLHttpRequest.
These days, I would go a step farther and discuss how it has positively influenced browser APIs. jQuery offered a way to find elements using their css selectors, and this eventually gave us
document.querySelectorAll. More recently, browser developers have taken another page from jQuery’s playbook and introduced a new, Promise-based API for making asynchronous requests, and so much more – Fetch.
Why go Fetch? Let’s take a look.
Taking an opportunity to look back at some of our most-read posts from this year, in case you missed them the first time, as the last few days of the year slip by us…
William Allen White
My turn! Earlier this year I wrote about a little Twitterbot that I wrote to periodically tweet out snippets of lyrics from songs by the band They Might Be Giants…
Making redis pythonic.
As python programmers we are sometimes faced with using an API that is, well, unpythonic.
Unpythonic? Pythonic? Huh? Have you ever tried running this:
python -m this
Maybe you’re using a C library via ctypes, or you have inherited a collection of functions. Regardless, you find yourself wishing you can use the API in a more idiomatic manner.
This happened to me recently when I started working with redis-py, the defacto standard python interface to redis.
Sidebar: this post is not really about redis, but if you don’t know about it or haven’t given serious thought to using it as a datastore, run right over to http://redis.io/ and read up on this amazing in-memory, key-value store. If you’re familiar with memcached, then you’ll have a general idea of redis, but redis goes well beyond simple key-value pair storage. It also has functionality for storing lists, sets, sorted sets and hashes. Redis is often described as an in-memory data structure server.
In using redis-py, you acquire an instance to a monolithic object that manages a connection pool as well as the underlying redis protocol, offering a method for every redis command. While being feature complete, the object is a bit unwieldy, imho. Typical usage might look like this:
>>> import redis
>>> conn = redis.StrictRedis()
>>> conn.set('foo', 'bar')
>>> print conn.get('foo')
>>> for c in 'this is a test':
... conn.sadd('myset', c)
>>> print conn.smembers('myset')
set(['a', ' ', 'e', 'i', 'h', 's', 't'])
Every command for every data structure is attached to an instance of
StrictRedis. If you look at the redis command set you will see there are some commands that apply to all data structures, while others apply to one data structure or another. For example, commands like
ttl can be used against any key in the datastore, while commands
incr, can only be applied to strings, and
hset can only be applied to hashes.
A picture begins to form for the potential for an abstract base class,
Key, with concrete subclasses
We will explore how to create a more pythonic interface for redis, building off the powerful redis-py implementation, using minimal code by using advanced python attribute access and delegation.
It’s a pleasure to be able to interact with files in the browser at long last, isn’t it? Reading files in without needing to bounce them against the server first opens up a lot of possibilities – and getting progress from a chunked ajax upload is miles away from the indeterminate form uploads of days past.
Last time we touched this subject, I shared an (admittedly rough) jQuery plugin that allowed you to enjoy HTML5 ajax uploading and file reading with the familiar event interface, and convert any element into a drag-and-drop target.
At the request of reader Mateusz, let’s revisit our HUp plugin, and polish it up a little by adding a new feature – the ability to filter files to be read/uploaded by their file size, and/or their mime-type.
A while ago, I made a threat on Twitter that I was going to unfollow any account that wasn’t a bot. On an average day, I’ve been getting as much value out of these algorithmically-generated as I do from human-run accounts, it seems, whether it’s just tweeting out Finnegans Wake or Gravity’s Rainbow, searching the twitter stream for pairs of rhyming tweets in iambic pentameter, replacing the nouns and adjectives in William Carlos Williams’ “This is Just to Say“, or a Markov chain-driven mashup of the King James Bible and Abelson & Sussman’s The Structure and Interpretation of Computer Programs.
There’s a quote from near the beginning of Pynchon’s ‘The Crying of Lot 49’ that’s always resonated with me — “You know what a miracle is. Not what Bakunin said. But another world’s intrusion into this one. Most of the time we coexist peacefully, but when we do touch there’s cataclysm.” The tweets being generated by the bots I follow give me that same sense of tiny little blips of another incongruous universe briefly slipping into ours. I’m also reminded of Wintermute, the AI gone insane in Gibson’s Count Zero, quietly making Cornell-like shadowboxes.
Then, one day I saw this:
…and knew that I had to build my own bot. (more…)