A Django-Style Admin Interface For Pyramid

Photo of Chichen Itza pyramid by Jimmy Baum on Unsplash

Pyramid Logopyramid_formalchemy provides a CRUD interface for Pyramid based on FormAlchemy. Which is to say it looks at your SQLAlchemy models and automagically generates an HTML interface to create, read, update and delete instances of those models from the database, very much like Django’s admin interface. That sounds pretty cool, doesn’t it? In this post I will demonstrate how to get it up and running and then start customizing the forms.

Software screen capture

Making An Inherited API Pythonic

Image of Redis and Python logo

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 exists, expire, and ttl can be used against any key in the datastore, while commands get, set, and incr, can only be applied to strings, and hget, hkeys, 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 String, List, Set, SortedSet, and Hash.

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.


Tutorial: Posting to Facebook from a Django App

Django logo

This tutorial is the first part of a series of tutorials that build a complete Django application, codenamed procrastination automation. The tutorial on Python Social Authentication can be considered a preface to this series – if you would like an introduction into using social authentication with Python or Django, check it out.

Some time ago, I saw a diagram that showed how content originates from 4chan (or was it 9gag?), then gets reused by Reddit, then gets reposted on Digg, and ends up on Facebook. Don\’t google it, it is using a very ugly image for the metaphor. But the idea is that thousands of people are viewing things on one subset of social resources and reposting on another subset, where this content becomes the source of entertainment and news for more viewers.

Literally millions of man-hours are spent daily to transfer images of cat-based memes from Reddit to Facebook. This is a perfect opportunity for automation.

Let\’s build an app that will allow a person who would ordinarily browse Reddit a few hours a day and repost pretty much every link from a favourite subreddit to Facebook, set a personal re-poster that will automatically forward those updates from Reddit to his Facebook wall for him, impressing his friends with his immense social presence, and saving his precious time.