Stavros' Stuff

Angry rants of programming and other things.

Calling single-argument methods in Django templates

No longer are your methods confined to bare calls!

One of my pet peeves when it comes to Django is that you can’t call methods that require arguments in templates. While this is fine most of the time, it does mean that you need to have one property or method per call you want to make, which sometimes gets very cumbersome.

I needed a way to define various dynamic permissions that are calculated at runtime (for irrelevant reasons, Django’s permissions framework wasn’t a good fit), and writing properties like can_register, can_add_tags, can_subscribe got tedious. These tended to be defined all over the place, rather than in one central spot, and it was hard to add more checks without cluttering the classes.

I would much prefer to have a single method (let’s call it can()) that accepted a string with the permission I wanted to check, and return True or False, depending. This is easy to do in the views, but templates would never be able to call it with an argument.

However, since Django can do dictionary-style attribute lookups, I could add a dictionary interface over the method, and allow

Continue reading…

Get a text when there's motion in your house

Who needs friends when you have an Arduino?

Like every person, I have a burning desire to know who’s in my house when I’m not. A few months ago, I decided that I had had enough of the uncertainty of my extradomicilial activities, and that I needed to do something about it. I realized that I had two options. The first option would be to hire someone to be in my house 24/7, but that would get a bit embarrassing when I wanted to watch reruns of Desperate Housewives. The other option would be a motion sensor that texts me when it detects motion.

Luckily, this proved really easy to do with an Arduino. All I needed to get was

Continue reading…

How to safely store your bitcoins

Keep your money close and your bitcoins closer

Let me tell you a story about how this fool and his money were soon parted. About a year ago, I added Bitcoin payments to Dead Man’s Switch, because it seems that people who want to send messages after their death are also the sort of people who don’t like using actual money.

One or two people decided to use that payment method, probably mostly because it was the only thing they could buy with their newly-mined bitcoins, and I was the proud owner of a whole bitcoin, worth about $20 at the time. However, storing it was a bit of a problem, as I would either have to trust a third party with it or use the official client, and store the entire multi-gigabyte blockchain on my computer.

You can probably see where this is going. I didn’t want to store a bunch of gigabytes for $20, so I left my coins on

Continue reading…

I found the Holy Grail of backups

Finally, encrypted, fast offsite backups

If you’ve been reading my ramblings for a while, you will remember that I have been looking for a good way to back up my files, without much luck. My requirements are:

  • Encrypted (on the original machine).
  • Fast/bandwidth-efficient (deduplication is a plus).
  • Open-source.
  • Cheap.

My files are mostly code and photos, which aren’t really critical (the critical files are stored in repositories and multiple computers), but losing all my photos wouldn’t be too great. That’s why any backup solution I wanted had to be cheap enough to justify the cost.

Every single piece of software I have ever tried fails in at least one of the above. I currently use SpiderOak, which is everything except open-source, so that fails too.

However, my quest is finally over. I have discovered the Holy Grail of backups, and its name is

Continue reading…

Arduino-powered irrigation system

Control your field’s irrigation system with your mobile

A few weeks ago, I was showing my dad my latest project, the Arduino RF remote control, and he lamented the fact that I still hadn’t made an irrigation system remote control for him. We have a few acres of fields that need irrigation, which is done with a system of pipes from a central water pump. At that point, however, the fact that he needed a remote control for the pump was as news to me as it is to you, so I asked him what he needed exactly.

He told me he needed a system that would allow him to call or text a phone number and turn the pump on and off remotely, as well as something that would text him if there was a problem that

Continue reading…

Mozilla Persona accounts with disposable email addresses

Mozilla Persona, now with extra disposable email address

If you follow the news, you’ve hopefully heard of Mozilla Persona, the wonderful new way of centralizing logins on the web. In a nutshell, Mozilla Persona works the same way as the “log in with Facebook” you see on many sites, except that, instead of Facebook, you use your email account to log in. The great thing is that sites never have your password, and you only need to have one password for every site on the web (Persona uses your email address as your username).

One downside of the default implementation is that you have one account per email on the default bridge (this isn’t strictly true, but adding more accounts is cumbersome and not meant to be done for every site). This is a problem for me, as I use the excellent 33mail service to create disposable email addresses for every site I visit.

Fortunately, there is a way to use disposable email addresses as usernames on Persona. Here’s how:

Continue reading…

Remotely controlling IR/RF devices with an Arduino

Control your household devices: Arduino edition

If you’ve been following this site, you’ll know that I’ve been going through lots of alternatives in trying to achieve the Holy Grail of home automation, which is to get my garage door to open remotely with a Raspberry Pi. To this day, I haven’t managed to do that, but I did get some great guides going.

For example, I managed to turn the Raspberry Pi into an infrared remote and control my TV and AC unit with it, and, after that, the next step was to control RF wall sockets, which was also great.

After all this, I connected my house lights to RF sockets so I can control them from the Pi, but having a Pi running all the time is a bit of a hassle. It requires an SD card, a USB hub for the wifi, it freezes sometimes, etc, so I wanted a solution that was less of a complete PC. The obvious choice was

Continue reading…

Developing with pull requests

Pull requests are fantastic

As you may know, my favorite way of beginning a blog post is with “as you may know, my favorite way of beginning a blog post is with ‘as you may know’”. As you may also know, for the past year or so I’ve been working with Silent Circle, where I’m part the team that develops the website and backend.

The team is composed of roughly seven people now (developers and designers), and, as it has grown, we have been trying things to find a good workflow that allows us to be productive while staying on top of the changes made to the code by coworkers.

This post is about our needs and the development/release workflow we have arrived at to address them.

Continue reading…

Automatically check your Python code for errors before committing

PEP8 or GTFO (but friendlier)

A few days ago, I wrote a post on how you can reject commits that violate PEP8 using git’s pre-receive hooks. This is evaluated on the server, which, in the end, led to many commits that were just fixing PEP8/syntax errors. Wouldn’t it be much better if you checked for errors before you pushed, so you could fix them right there? It would indeed.

To do this, we can use flake8 and git’s pre-commit hooks (I’m assuming you already have flake8 installed, from the previous post). A pre-commit…

Continue reading…

Writing a full-text search engine using Bloom filters

Search engines and Bloom filters: A match made in heaven?

A few minutes ago I came across a Hacker News post that detailed a method of adding search to your static site. As you probably know, adding search to a static site is a bit tricky, because you can’t just send the query to a server and have the server process it and return the results. If you want full-text search, you have to implement something like an inverted index.

How an inverted index works

An inverted index is a data structure that basically maps every word in every document to the ID of the document it can be found in. For example, such an index might look like {"python": [1, 3, 6], "raspberry": [3, 7, 19]}. To find the documents that mention both “python” and “raspberry”, you look those terms up in the index and find the common document IDs (in our example, that is only document with ID 3).

However, when you have very long documents with varied words, this can grow a lot. It’s a hefty data structure, and, when you want to implement a client-side search engine, every byte you transmit counts.

Client-side search engine caveats

The problem with client-side search engines is that you (obviously) have to do all the searching on the client, so you have to transmit all available information there. What static site generators do is generate every required file when generating your site, then making those available for the client to download. Usually, search-engine plugins limit themselves to tags and titles, to cut down on the amount of information that needs to be transmitted. How do we reduce the size? Easy, use a

Continue reading…