top of page

Python isn't just for programmers


Python is a very friendly language that has caught on in much more than just web development or scientific processing - it's almost everywhere now. Its readable, clean syntax helps even non-technical people to understand what's going on in the code they're looking at and this helps a lot when you're just getting started with this kind of interaction with the digital world.

Spyhce is supporting the first DjangoGirls event here in Cluj-Napoca, and we had a mentor meeting a few days ago where we discussed about how more people, especially ladies - for this event, could get interested in programming. That got me thinking that a lot of people only look at programming from a professional perspective, asking "how can I turn this skill into a job?", when there's a lot of other very interesting question that remain mostly unasked, like "how can I solve a practical problem that I have using code?", or "how can I integrate code in my regular activities / job?".

That's why I decided to take this opportunity to finish this article that Julia, our marketing specialist, has been badgering me to write, as it seems to be a nice response to one of the latter questions above.

About an year ago my parent's house in Alba Iulia was left empty, after more than 50 years of someobody staying there day-to-day. Because of this guarantee that somebody was always there, there was never a lock on the gate (something pretty uncommon in urban Romania), and also, more importantly for this article, our local scout group used some of the available storage space for all their equipment.

So a lock had to be placed on the gate, and a system had to be devised where the scouts could have uncomplicated access to their storage spaces. Of course, this could have been solved by placing a classical lock on the door and distributing some keys around, but I'm technically trained and I found this a good opportunity to ask myself "how can I solve a practical problem that I have using code?".

The idea seemed (and actually is) pretty simple - have some sort of electronic lock on the door, connect that to the Internet and give family and scouts access to a web system accessible via mobiles to unlock the gate.


A bit of googling quicly led me to an electromagnetic lock from a local provider, who luckly also sold compatible power supplies and relays at decent rates (250RON / 60EUR for the whole setup). There's minimum electrical knowledge required when picking and choosing these components to make sure that they're electrically compatible, but that's about all you need and you're set.

For the electronics I had to choose between the ubiquous Arduino and RaspberryPI (just because I had one of both laying around) and of course I went for the RaspberryPI, as I wanted to play around with Python for this project. An added bonus for RaspberryPI is that it has integrated network support and I didn't have to order an additional component.

I messed up when picking out the relay (don't ask) so, in true maker spirit, I had to improvise with a cheap transistor to get approprate electrical levels for the relay to open the gate.


Then, finally, I got to writing some Python. First, in order to open the relay driving the gate, I got to writing a very small, nice "Hello world!" level program for the RaspberryPI that went like this:

# relay.pyimport RPi.GPIO as GPIO

# initializing pin 12 for output with 0 (low)
GPIO.setup(12, GPIO.OUT, initial=GPIO.LOW)# set pin 12 to 1 (high), activate relay
GPIO.output(12, GPIO.HIGH)# wait for two seconds. anything more than 8 seconds at the time will fry the lockimport time
time.sleep(2)# finish and clean up; this will automatically drive pin 12 to 0 (low)

For the next piece of the puzzle, this Python script needs to be called somehow from the Internet by whoever needs to open the gate. Luckly, the scout group in Alba Iulia also has an app they use to manage everything (coincidentally also written in Python, and also written by me), so the public-facing interface would be in that Django app.

This left two problems to be solved: how to address the RaspberryPi from the Django app server and how to actually call the script once we're there.

For the former, a few things needed to be considered: the RaspberryPi is conected on a private network that goes to the Internet via a router with a dynamic IP address, in a neighborhood where short power outages are pretty common.

This required a robust, self-recovering solution that wasn't hindered by NAT and routers, and I finally went with a VPN connection from the RaspberryPi to the server. This provided me with a stable private IP address to the RaspberryPi, and also a way to reconnect after the power went out, and setting up an OpenVPN on a VPS is an one-hour deal.

For the last part, I had to run the script on the RaspberryPi from the Django app on our server. For this purpose I turned to a very nice Python package called fabric. There's some boilerplate but essentially I have a Django view that calls something like this:

# fabfile.pyfrom fabric.api import run, env, cd
from django.conf import settings

def open_gate():with cd("/home/pi"):

        env.password = settings.GATEKEEPER_CONNECTION_PASSWORD

        run("sudo python")

Bottom line

The take-away from this project / article should be that when (learning) Python is concerned, there are more questions to be asked than how to turn that knowledge into a job?. Different forms of programming are becoming ubiquous in all kinds of work environments and the ability to code provides greater opportunities for non-programmers in their own projects or by adding values to the jobs they are already holding. This "new" world is more about understanding how things work at a conceptual level and less about writing a lot of code, and that's where programming languages as Python are great :)

52 views0 comments

Recent Posts

See All


A NEW AND FAST PACKAGE MANAGER There is a new kid on the block, and long story short, he is quite AWESOME. We are talking about YARN, a new package manager that wants to solve a few problems that have

Quick SSH access

HOW TO NOT GET LOST IN THE FOREST OF SSH SERVERS YOU HAVE TO WORK WITH For 8vance, one of the more complex projects that we're working on here in Cluj, we have a growing infrastructure - about 15 serv


bottom of page