bor.borygmus

A programming weblog by Hao Lian. • A long walk through an angry forest. • A series of memory leaks brought on by senility.

Let’s say, one day, you become insane and want to authorize your application to access a user’s data on Twitter. WITH OAUTH!

Twist!

A brief recap: OAuth authenticates an application. It ensures that a sequence of HTTP requests belong to exactly one person, which is hugely important if you’re providing a service API like Twitter is. Here we’ll present the OAuth with signed callback URLs, which is how Twitter provides OAuth for desktop applications—applications that might not have web browsers but nevertheless need to get the Special OAuth Pat on the Back and let the user allow it to access his data. And to do so, you do this:

You’ll grab a request token. With it, you’ll ask the user to log into Twitter and get a special PIN. This PIN is pasted into your application. You then trade your request token for an access token, with which you can perform unspeakable evil.

A first attempt at precisely this:

Jump.
[(2w & 4d ago) .]
[(1mo & 1w ago) .]

Let’s talk about SQLAlchemy, __init__(), and __repr__()!

It’s another post about Python. And SQLAlchemy. There’s got to be a way to monetize this.

Let’s say we have this, because we are young powerful virile men and women capable of great sexy things.

user_table = Table(
    'user', meta.metadata,
    Column('id', Integer, primary_key=True),
    Column('name', Unicode(100), unique=True),
    Column('strength', Integer))

class User(object): pass

mapper(User, user_table)

If you are lost, maps are on the table in the corner to your left. No, not the sparkly table, that’s Django’s. The solid wooden table. Yes, that one.

Now we journey out into the world, using our new User class.

>>> User(name=u'Gosh McMuffin', strength=10)
[snip]
TypeError: __init__() got an unexpected keyword argument 'name'

Ah. SQLAlchemy is not Magic School Bus ORM. You never defined an __init__() on User. Neither did our mapper() call. And this makes sense: We don’t want a function that monkeypatches an entire class. It gives into The Temptation of Globals; any monkeypatching we do is only available in that thread and we just shot down the chances of anybody reusing our code without growing a few more gray hairs. It’s rude. And, worst of all, it’s un-Pythonic. What to do, what to do.

Jump.
[(1mo & 1w ago) .]
[(3mo & 4d ago) .]

GNU Emacs 23 has been out for a while now (Windows, atomized.org’s OS X builds from Emacs’ source control). It’s the little features that count, as you know.

  • Tramp now works on Windows.

  • The mode-line-buffer-id face seems to no longer dynamically inherit from the standard mode-line face: For example, if your mode line’s active background is green and inactive is gray, then on Emacs 22 the buffer-id (the place where your buffer name is written) would change backgrounds along with the mode line as you shunted back and forth. In Emacs 23, I had to choose one background for mode-line-buffer-id, finding no other way of replicating 22’s behavior.

  • Not a feature, but: If you open the electric buffer list (M-x electric-buffer-list) you’ll see the column headers “CRM” in a curious serif typeface. The letters correspond to Current, R-something, and Modified, marking out which buffers are what at a glance, but that little corner is the only place in Emacs I know of where a different typeface as been used.

  • customize-face seems to be unable to override color-theme, no matter where you put customize-face in your initialization script. I shrugged, opened up customize-face, and saved all my colors from deep-blue and my own tweaks into a file. Tentative goodbye, color-theme library.

  • New window decoration icon on Windows! The window decoration icon is what I call the cute little 16x16 thing sitting at the top left of all Explorer windows.

  • A little graphics bug: the refreshing stutters if you hold down Ctrl-N or Ctrl-P and scroll line by line. This is forcing me to navigate by search (As It Should Be Done), so let’s pull out an overall plus from this.

  • python-mode highlights built-ins!

Hooray!

Update There is now a fancy website by David Caldwell written in SVG for Mac OS X builds. (via Hacker News, where a fantastic thread about building Emacs on OS X is taking place)

[(5mo & 1w ago) .]

If you’ve been reading along on the Atom feed, hello! And thank you! And also if you’ve been noticing paragraphs that seem out of place in certain grumbles, that’s probably because they are. I’ve been marking paragraphs as asides in classes with a corresponding fancy, three-line CSS, along the lines of what Sphinx does. So if certain paragraphs seem disjointed, suck it up! And sorry. HTML 5’s <aside> can’t get here fast enough.

[(6mo & 2w ago) .]

It’s time now to write about two of my favorite pieces of software ever written: Georg Brandl’s Sphinx and Pylons’ Pylons. Normally, these get along like a house on concrete, which is to say very well. A Pylons web application is a well-behaved, standard Python package that pays all the taxes: a setup.py, no module magic like pre-modern Django, and imports as expected. However, if you have ever used the highly useful Sphinx autodoc extension and have pointed it at a module in your Pylons app, you may have run into a traceback that looks like this:

[snip]
File "[snip]/autodoc.py", line 1019, in can_document_member
  return (isdescriptor(member) and not
File "[snip]/autodoc.py", line 203, in isdescriptor
  if hasattr(getattr(x, item, None), '__call__'):
File "[snip]/paste/registry.py", line 137, in __getattr__
  return getattr(self._current_obj(), attr)
File "[snip]/paste/registry.py", line 194, in _current_obj
  'thread' % self.____name__)
TypeError: No object (name: url) has been registered for this thread

Two questions should emerge immediately: What is paste.registry? And why is it throwing a traceback?

Jump.
[(6mo & 2w ago) .]

WebMynd has a great little post about extending SQLAlchemy to back Amazon’s Simple Storage Service. I wasn’t even aware of MapperExtension until now. If you love reading about all the little nooks and crannies of SQLAlchemy, check this out. I also just realized that the Mike Bayer who commented on bor.borygmus’s first grumble is the same person who created SQLAlchemy, which is marvelous.

[(6mo & 3w ago) .]