Using Python PyPy in a virtual environment

Sometimes we need to test if our code also works with PyPy implementation of Python. Assuming you have already installed it in your system, first find out where it is installed:

then you need mkvirtualenv to create a virtual environment that will use this runtime:

That’s it! You can now use this virtual environment to run your Python application using PyPy environment.

Getting started with BBC MicroBit and Python

A few days ago I had the great opportunity to attend an event organised in collaboration with Python Software Foundation, a few primary school teachers and hosted by Computing at School, in London. The meeting was organised by Yvonne Walker (from CAS) and Nicholas Tollervey (PSF). The aim of the meeting was for teachers and developers to meet and discuss the opportunities offered by MicroPython on the BBC micro:bit. During the event a BBC micro:bit board was loaned to each person for the purpose of developing Python scripts, MicroPython itself or educational resources for the BBC micro:bit. Nicholas made it very clear that there is an NDA in place until the device is delivered to the kids and explained what we could or couldn’t do.

computing_at_school_microbit_reduced

The Board

bbcfullbleed

The board is a 4 x 5 cm device with an ARM Cortex-M0 processor, accelerometer and magnetometer sensors, Bluetooth and USB connectivity, a display consisting of 25 LEDs, two programmable buttons, and can be powered by either USB or an external battery pack (source: https://en.wikipedia.org/wiki/Micro_Bit ).

Flashing the firmware

Once you get a new board, it probably doesn’t have a proper firmware and application flashed. I suggest you to download the Python MicroBit REPL from this repository: https://github.com/ntoll/microrepl
All you need to do is to connect the board to your computer, using a micro-USB cable. The device will be mounted as a volume. At this point, drag & drop the file called firmware.hex into the mounted volume. The firmware will be flashed and during the operation you will see a yellow led flashing.

Using MicroPython micro:bit REPL

To start writing some Python code on micro:bit you first need to clone this repository

once you have cloned the repository, you need to install the Python dependencies (I suggest you to do it from inside a virtualenv)

start the MicroPython REPL

and the Python shell will open, so you can start writing commands, like this one

BBC micro:bit MicroPython Editor

Typing all the Python commands directly into the shell can be a bit difficult. You can use a very nice and dedicated editor to write code and produce the compiled application for the micro:bit. All you need to do is clone this repository

Open the file named editor.html with your browser and start writing your code. When your code is done, you can generate the .hex file clicking on Download button. To load the compiled application you just need to drag & drop the .hex file to the mounted device, exactly like you did the first time to flash it. If you need a reference for all the methods and libraries available, you can consult the official documentation here http://microbit-micropython.readthedocs.org/en/latest/index.html

References

Fix encfs under OSX after upgrading to 10.11.2

After having upgraded OSX to 10.11.2 on my MacBook, I noticed that my encfs volume didn’t mount after reboot. I tried to run the script manually and all I got was this error:

I quickly found that was a common problem caused by a new version of Boost being installed: https://github.com/Homebrew/homebrew/issues/46254

To fix it, you just need to reinstall encfs using this command

 

Getting a free SSL certificate from Letsencrypt and configuring it on Nginx with automatic renewal

Finally Letsencrypt went to public beta and I really couldn’t wait to use it on my VPS (where this blog is hosted). Until few days ago I was using a free SSL certificate from StartSSL. The service is nice and I’m grateful to them for this important resource they are providing for free, but it must be said that their renewal procedure isn’t one of the most user friendly.

For people who don’t know the service yet, Letsencrypt not only gives free SSL certificates, they also provide a command line tool that people can use to request a new certificate or to renew an existing one. This means that you don’t have to worry anymore if/when your certificate expires, you can set a crontab command and have the certificate automatically renewed for you.

Client installation

To request a SSL certificate you need to install their command line utility. Unless it has already been packaged for your distribution, for the moment it’s much easier to get it from git as they explain in their installation instructions:

Getting the SSL certificate

There are a few different options available to request a certificate, but the easiest one is to use the –webroot option, specifying the document root of your website so that the client will be able to put there a verification (temporary) file that will be served to the remote service and used as verification method. In my case I only needed this command:

Please note that I had to specify both www.andreagrandi.it and andreagrandi.it as domains, otherwise it would have been invalid when requesting just andreagrandi.it resources.

Configuration files and certificates installation

The command above will save all the configuration under /etc/letsencrypt/ and all the generated certificates under /etc/letsencrypt/live/www.andreagrandi.it/*.pem (all the *.pem files here are symbolic links to the current certificate). If you are using Nginx the only files you need are fullchain.pem and privkey.pem and you can set them in your Nginx configuration using these two parameters:

In case you want to have a look at my full Nginx configuration file, as reference, you can find it here https://gist.github.com/andreagrandi/8b194c99cd3e77fdb5a8

Automatic renewal

The last thing to be configured is a crontab rule to call the script every… 2 months. Why 2 months? Letsencrypt SSL certificates expire in 3 months. Usually SSL certificates are valid at least for 1 year, but Letsencrypt decided to make it 3 months to incentivate the automation of the renewal. I set it to 2 months, so if anything goes wrong I still have plenty of time to do it manually. To edit crontab for root user execute crontab -e and add this line:

Just a final note. You may have noticed that this website presents an SSL certificate issued by COMODO. That’s because I have CloudFlare in front of my website and that’s how their SSL strict option works (at least for free plans).