Rockstar Programmer or Poet

I’ve learned a few programming languages in my time. Sometimes for a particular project but also occasionally just for fun. So I was interested to find out that someone I knew had invented a programming language where the code looks like song lyrics; it’s called Rockstar.
What are song lyrics but a form of poetry and I like writing poetry so how could I resist. The best way to learn any language is of course to write a program in it, so I chose a simple example and had a go.

It was a fascinating experiment. Choosing a simple enough program to write made me aware of how many things I take for granted in most languages, things like bitwise operations or string manipulation. Writing this* into Rockstar led to a detailed study of the syntax. I made some guesses as to how things should work where the documentation wasn’t clear. I’ve followed some of the official example code and sometimes not. Using null as equal to zero and then incrementing it felt so wrong I just had to find another way.
Despite all this I found getting a ‘working’ Rockstar program is the easy part. Rewriting it to be sufficiently poetic took some time, including developing some of my own coding conventions which just shows how poetic code really is.

I’ll also admit to cheating slightly; the language as specified doesn’t have a random number function and I wanted one. So (based on C’s rand function) I’ve assumed a function that returns a random integer between zero and whatever maximum is available. I figure gambling is fairly rock star so that is an acceptable extension (I hope).
Because that function isn’t defined in my program I’ve added a way of showing that too. I hope I’ve also used comments in an interesting way.

Bring on Chance (my own invention)

Time takes my life and my tears
While my life is as strong as my tears
Put my life without my tears into my life

Give back my life

Simply takes a smile and a wave
Put a wave without a smile into the world
Build it up
Put Time taking Chance, the world into your life
Give back your life with a smile

Dreaming takes my hopes and my fears and my smiles and my tears
Shout my hopes
Whisper my fears
Shout my smiles
Whisper my tears

The sunshine was frightening
The shade was my compatriot
Your hopes were everything
My idea was wilder (no more than this)
Dreaming taking “I am thinking of a number between”, the sunshine, “and”, the shade
Put Simply taking the sunshine, the shade into my heart
While my idea is stronger than your hopes
Say “Take a guess.”
Listen to your dream
Build your hopes up
If your dream is smaller than my heart
Whisper “Your guess is too low.”
Take it to the top

If your dream is bigger than my heart
Whisper “Your guess is too high.”
Take it to the top

If my heart is your dream
Break it down

(and breathe)

If my heart is your dream
Dreaming taking “Good job!”, “You guessed my number in “, your hopes, ” guesses!”

If my heart ain’t your dream
Say “Nope. The number I was thinking of was ”
Whisper my heart
(no more)

*Obviously working out what this program actually does is left as an exercise for the reader

Advertisements

Wrapping Text With ImageMagick

I’ve already explained that I wanted to create an image slide for a slideshow from some information in a spreadsheet. I decided to use ImageMagick and, in particular, the Wand library for python to help me.

As with all theatre projects this one is collaborative. The original concept for this was James’ and he’d created some examples by hand*. Each slide has some fixed elements and then information specific to the event. Using those he’s created some template images that contain the fixed elements, there are a number of templates depending on the type of event.

Basic SlideEach one has basically the same layout and the same (wavy) line which delineates where I can draw the event elements onto the slide. Then I add the date and time and the title of the event and who is presenting it. All of that is straightforward with either a Caption, or a Text element.

Image AreaThe more challenging part is the lower half of the slide. The spreadsheet contains a link to an event specific image in Google Drive, so I download the image data directly. I want the image to fit into, at most, roughly half of the remaining area, so I resize it keeping the aspect ratio but capping the height and width.

with Image(blob=image_data) as img:
    size = "{}x{}".format(max_width, max_height)
    img.transform(resize=size)
    img_height = img.height # save height
    img_width = img.width # save width
    main_img.composite(img, left=left, top=top)

Slide With Image

Now I want to display the description wrapped around the slide. ImageMagick will easily draw all sorts of amazing text effects (on one line) but text wrapping is more of a challenge. You can use a Caption to wrap text inside a rectangle, but my rectangle now has a corner taken out of it. Also I was asked to truncate the text at a sensible point (e.g. the end of a sentence) if it was too big to fit.

This is the point for some funky arithmetic. Firstly create a Drawing and set the Font you want to use on it. Now text is either going to be next to the picture (a narrow line) or underneath it (a wide one), so using the font size and the height available, and of course the actual image height and width, calculate how many lines you can fit of each width. Now we just need to split the text into that many lines each of the right length.

The simplest way to split the text is to use the python textwrap module but that requires you to specify a fixed maximum number of characters and my font isn’t monospaced. Instead I use the Draw module’s get_font_metrics to do the same job. It requires a dummy image for some reason.

words = para.split(" ")
lines = []
with Image(height=10, width=10) as img:
  line = words.pop(0)
  for word in words:
    line_width = draw.get_font_metrics(img, 
      line + " " + word).text_width
    if line_width < max_width:
      line = line + " " + word
    else:
      lines.append(line)
      line = word
  if line != "":
    lines.append(line)
return lines

Using this I can build up a list of lines of text. Firstly the narrow ones and then the wide ones, discarding any text that won’t fit. Then I can draw them onto the image line by line. Giving me a finished slide.Complete Slide

 

*James is also responsible for the spreadsheet that holds all this data. You should ask him about it, it’s awesomely clever.

From Spreadsheet to Slideshow with a Raspberry Pi

As well as actually spending time playing in the theatre I also occasionally get involved in various computer related projects for one. This particular project was to help set up a rolling slideshow of “coming soon” events that we can display in the foyer of the theatre.

Of course this project lends itself to a raspberry pi so I got mine out and started work. Everybody (it seems) creates a slideshow on their raspberry pi, so there are many solutions to choose from. In this case (you’ll see why in a minute) I wanted a slideshow that looped through pictures in a directory and automatically coped with pictures being added and deleted. For now I’m using Eye of Gnome which does this admirably but this can easily be replaced in future.

So where are the pictures coming from? Well that’s where this gets interesting. The list of upcoming events is kept and managed in a Google Spreadsheet so I needed to take this information and turn it into a set of images for the slideshow. As the information in the spreadsheet is regularly updated I wanted to write a python script that could be run regularly by a cron job to pick up changes and create the new slideshow images.

Google provide an API to Google Drive that allows access to files and file information and they provide a python library to access this.
I followed the instructions here to configure a developers account and then grant access to the APIs you need (in this case Drive and Sheets). I decided to use the OAuth for Server to Server authentication to avoid the need for human interaction, so the other thing I needed to do was to make sure that the service account has access to the files it needs. I did this by sharing the relevant files and directories in Drive with the service account’s email address. I only needed to grant view access to the files as we’re only ever reading the data.

Then on the pi I needed to install a few libraries, the Google API of course, a date parsing module and Image Magick to help create the images.

sudo apt-get install python-pip
sudo pip install --upgrade google-api-python-client
sudo apt-get install python-dev
sudo pip install ciso8601
sudo apt-get install libmagickwand-dev
sudo pip install Wand

Once that’s done I could write the script to download the data. The Google API documentation is full of example code so it’s easy to get this working.
Firstly I retrieve the modified time of the spreadsheet, using the Drive API, to see if it’s changed since we last ran. If it has then I download the data from the spreadsheet and check each row to see if it’s been updated (each row has a unique identifier and a last updated time-stamp). For each updated row I delete any existing image and generate a new one.
Finally I record locally the most recently updated time of the rows so I know when to start from next time.

Note (because it surprised me) that the latest updated time of a row might actually be later than the modified time of the file as there’s sometimes a couple of minutes delay before the file modified time is updated.

That really was all there was to it. Well, okay, there’s actually a few other things going on too, but that’s for another post perhaps.