Swizzling in Python

Story Time

Ever come across a Python library with some kind of shortcoming that you know you can fix, but don’t want to fork, fix, pull-request, wait five weeks, then update in order to get moving? Are you even too lazy to do just the first two steps and point your dependency manager at your own fork? Don’t worry, you can probably fix it in code.

Right now I’m writing a Slack bot library that will support, in addition to the commonplace single command/response paradigm, a more conversational paradigm for multi-step commands and workflows. So, like a good programmer, I set up my module structure, my setup.py, and got moving. This was after a little bit of proof-of-concept code sketching, in which I came up with the general flow of connecting with the official Slack client for python, aptly named python-slackclient. So, I ported the flow into my library’s code, with some nicer structure, fired up a virtualenv, ran python setup.py develop, and then wrote a little wrapper script to invoke the client.

Boom, error. Weird, right? My proof-of-concept had worked fine…

After looking at Slack’s code, I found that it was cleansing an exception from the websocket-client library, where it was having problems locating my system’s Root Certificate Authority file. That’s a problem I don’t really want to deal with right now. I have a feeling it has to do with the path magic that python setup.py develop does, so I’m willing to let it go. Nonetheless, I’m still stuck with it not working, and no way to tell the Slack client to get its head out of its ass and find the Root CA file for the websocket client.

Or is there?

Investigation

The websocket-client method, create_connection takes a URL by default but also takes some kwargs, one of which is sslopt, a dictionary of values describing SSL options, including the location of the Root CA file. Additionally, the ssl standard library includes methods for retrieving the location of said file. But, again, the calling of create_connection is within the Slack client’s code! What do I do?

Swizzle that shit!

Swizzling, for the uninitiated, is the same thing as monkey-patching, which is to say, replacing at runtime the implementation of a method on a class. Alas, it’s a little more complex in this case, since the method I want to swizzle is on the class of an object, an instance of which is held by the Slack client. So, I ended up using a subclass of the client and overloading the initializer to dig into the client’s state and swizzle the method in question.

Check it out!

Note that you have to bind the newly-defined method to the class, meaning that if you only have a reference to an instance, you need to use the __class__ attribute to get the class itself. Don’t forget self as the first argument; this is important for the method to bind correctly to the class/instance.

Oh, so, by the way, that Slack bot library I’m working on can be found here: SlackBorg.

Pizza Time: Olive Bomb

Time for another pizza! This one was a lot of fun, mostly because it was so briny and tasty. If you like salty stuff, this pizza is for you.

Photos

Olive Bomb, Before
Olive Bomb, After

Recipe

Materials

  • Pizza dough (again, I’ll be posting a recipe to this soon!)
  • Some kind of herb (I had parsley)
  • Anchovy paste
  • Olive Oil
  • Garlic cloves (one or two) sliced very very thin.
  • Kalamata Olives, pitted, then sliced or chopped
  • Castelvetrano Olives, pitted, then sliced or chopped
  • Mozarella Cheese (I use Belgoioso gems, but any mozz will do. Stay away from very wet varieties)

Preparation

Preheat your oven and pizza stone/steel as high as it goes. Finely chop up your herb of choice, and whip together with your olive oil and anchovy paste. If you omit the anchovy paste, probably add a little salt. Let sit for a little bit while you spread/toss/roll out your dough into your pizza shape. Give your dough a nice massage with your finger tips – just poke and poke with your outstretched fingers, until the surface is full of depressions like a golf ball. Spread your anchovy herb oil over the crust. Evenly distribute your cheese and olives. Throw that in the oven and bake until you’ve got some nice brown on your crust.

🍕

Pizza Time: Red Pie

I love making pizza. I don’t know if I’ve shared that very much, but between fermenting a good dough, assembling good ingredients, and putting on the heat, I love making pizza.

I’m going to try and start sharing my pizzas, mostly in before/after photos but also as partial recipes. I’m not much of a recipe writer, but I’ll do my best.

Photos

Red Pie, Before
Red Pie, After

Recipe

Makes one pie.

Ingredients

  • Pizza dough
    • (I’ll be posting my dough recipe soon enough; the real key overall is to let it rise/ferment at least twice as long as you think it should)
  • Tomato sauce
    • Go for the high-quality stuff, it matters.
  • Mozarella cheese
    • I go for the little balls, but you could just as easily get a big ball and tear it up. Err toward less moisture; avoid pre-shredded bags.
  • Parmesan Cheese
    • Real parmesan, please, or any good hard Italian cheese.
  • Basil (Fresh!)
  • Kosher Salt

Composition

Preheat your oven as high as it goes. Make sure your baking stone or steel is in there. My oven gets to 550°F, which does pretty well.

Toss, push, or roll out your dough. It’s up to you. Make sure you use enough flour to keep it from sticking. I usually just stretch it out on my peel by hand, giving a little toss if needed.

With your fingertips, poke at the part of the dough you want stuff to go on (so, all but the edges). When you’re done, it should be covered in faint depressions all around. This helps prevent bubbling and also gives some dimensionality to the crust surface for your sauce to hang out in.

Spread your sauce. Just straight up tomato sauce. No herbs, no nothing. All things considered you’ll probably use a quarter to a half of a cup, but feel free to go nuts. If you see a lot of dough through your spread sauce, it’s too little. If it’s all red, it’s too much.

Generously sprinkle the kosher salt across the tomato sauce.

Take your mozzarella balls (or torn mozzarella) and sparingly place on the pizza. The sauce is the star here, not the cheese. Grate some parmesan over the pizza, it should look like your front lawn after the first flurry of the winter.

Toss that pizza in the oven! While it’s baking, take some basil leaves, stack them together, and roll them up like a cigar. Then, slice up a bunch of thin slices of that cigar to get a nice chiffonade.

The pizza is done when the crust is a nice brown and the red of the sauce has deepened a little bit – that means the sugars in the tomato have caramelized, which is what makes for the great flavor.

Take your pizza out of the oven, grate some more parm over it, and sprinkle your basil chiffonade on it as well, while it cools.

Once cooled, slice and enjoy.

🍕!

Please

I wrote this the other day on a dare from a friend of mine. It only works in Bash (and I use ZSH) but I think it’s funny anyway.

Just:

  • Make sure you have bash-preexec installed
  • Put .please.py in your home directory
  • Put the contents of the Gist’s .bashrc in your .bashrc

New Site Who Dis

Decided to flip up my style a bit. Using the Poole-based theme for Jekyll, aptly named Hyde. Check it out.

Not much new here, at least nothing longform. I’ll have some kind of stupid update soon, probably.