Using cron as an alarm clock
Yesterday I left my Archos at work, which I usually use as an alarm, so I had to improvise. I settled on using cron to play some music very loudly in the morning. My first thought was to use mplayer for this, but then I realised that I didn't have it installed on my server, so I went for MPD instead.
The first thing to do, an entry in the crontab, looks innocent enough:
15 08 * * * /usr/local/bin/alarmsound
The alarmsound script is also pretty simple, but with a devastating effect...
#!/bin/zsh
mpc pause
mpc clear
mpc add "Music/DragonForce/Inhuman Rampage/Through the Fire and Flames.ogg"
amixer set Master 100%
mpc play
mpc | grep -q "repeat: off" && mpc repeat
Now simply mute the sound with amixer set Master 0%, turn up the volume on your speakers (I have pretty good speakers, so I only went for 50% or so), and fall asleep. The next morning, you will be pleasantly awakened by a storm of sound that will cause you to fly out of your bed to mute it.
I don't think any alarm has ever gotten me out of bed faster.
Turing machines in Python
Lately I've been working through the recommended reading list for computer science at York and, naturally, Turing machines get mentioned in these books, being the computing machines. Today, something finally clicked and I understood how they work and how to program them. The result is a Python module for constructing and using Turing machines.
The module can be found on my github account, along with all my other stuff.
Before writing this module I designed a simple Turing machine on paper to increment and decrement binary numbers an arbitrary number of times to calculate simple addition and subtraction problems. For example:
Input tape: <10>+++-#
Output tape: <100>////#
What has been calculated there is 2 + 3 - 1, and you can see it has found the correct answer of 4. I shall firstly explain how this particular Turing machine works, then I'll explain how it was implemented using my module.
The Machine
The machine, hereinafter referred to as M increments and decrements an input binary number, N, as dictated by a series of +'s and -'s. M uses an alphabet of 8 symbols, plus a special blank symbol, which fills every cell not occupied by another symbol (as is the case for all Turing machines). It is assumed that the tape extends infinitely to the left and the right of the input expression, that M starts with the read/write mechanism pointing at the cell containing the > symbol, and that M starts in the ready state.
Alphabet
The alphabet used by M is as follows:
0 = binary zero
1 = binary one
> = lower end of number
< = upper end of number
+ = increment number
- = decrement number
/ = no operation
# = halt
The alphabet could actually be contracted, as the < and > symbols are not required (only being present for convenience), and could be replaced by a few more state transitions. In fact, the # and possibly / could also be removed, reducing to an alphabet of four symbols. Additionally, if I chose to represent numbers in unary rather than binary, an alphabet of three symbols could therefore be used. However, for now, I shall use the eight-symbol variant.
State transitions
The state transitions are the key to the machine, the following diagram shows how the machine behaves in each state for every symbol it encounters in that state. This diagram could perhaps be contracted, though I have not tried.
As you can see, if a < symbol is reached before a 1 when decrementing N (ie: subtracting 1 from 0), the machine halts. This machine only works for positive integer values of N (and zero, when incrementing).
Just a note on the notation used in the diagram for state transformations, "A/B,C" is used, where A corresponds to the symbol read, B corresponds to the symbol written, and C corresponds to the direction moved. A downside of using a slash as a separator between A and B means that, when a slash is read and written, the string "///" appears. Additionally, if A is missing (as is the case when moving from the grow state to the reset state), the symbol read is the blank symbol.
Knowing alphabet and transitions
It is trivial to see how it works. Here is an example showing 1 + 1 - 2= 0.
<1>+--# symbol read '>', state 'ready'
<1>+--# symbol read '+', state 'ready'
<1>/--# symbol read '>', state 'inc'
<1>/--# symbol read '1', state 'inc'
<0>/--# symbol read '<', state 'inc'
10>/--# symbol read ' ', state 'grow'
<10>/--# symbol read '1', state 'reset'
<10>/--# symbol read '0', state 'reset'
<10>/--# symbol read '>', state 'reset'
<10>/--# symbol read '/', state 'ready'
<10>/--# symbol read '-', state 'ready'
<10>//-# symbol read '/', state 'dec'
<10>//-# symbol read '>', state 'dec'
<10>//-# symbol read '0', state 'dec'
<11>//-# symbol read '1', state 'dec'
<01>//-# symbol read '1', state 'reset'
<01>//-# symbol read '>', state 'reset'
<01>//-# symbol read '/', state 'ready'
<01>//-# symbol read '/', state 'ready'
<01>//-# symbol read '-', state 'ready'
<01>///# symbol read '/', state 'dec'
<01>///# symbol read '/', state 'dec'
<01>///# symbol read '>', state 'dec'
<01>///# symbol read '1', state 'dec'
<00>///# symbol read '>', state 'reset'
<00>///# symbol read '/', state 'ready'
<00>///# symbol read '/', state 'ready'
<00>///# symbol read '/', state 'ready'
<00>///# symbol read '#', state 'ready'
However, even for such a simple calculation, 29 steps are required.
The Implementation
My class does most of the work, so all that needs to be done is to provide information about the desired machine, and provide an input. The basic code to create and run a Turing machine is as follows:
from turing import *
M = Turing(tape, states, haltstates, initialstate, transitions, alphabet, tapepos, blanksymbol)
M.run()
# At this point, the finished tape can be read:
print(M.tape)
Turing() parameters
Many of the parameters are fairly self-descriptive, however I will explain all of them:
- tape = an input tape to the machine, given as a string. This will be expanded to the left and right as required.
- states = a list of state names that the machine can be in.
- haltstates = a list of those states in states which should cause the machine to halt.
- initialstate = the name of the initial state of the machine.
- transitions = a dictionary showing state transitions (explained below).
- alphabet = a list of allowed symbols for this machine.
- tapepos = the starting tape position, given as a string index for tape (eg, tapepos = 0 corresponds to the first entry in tape).
- blanksymbol = the symbol used to fill newly-added cells in the tape, must be one character, by default set to a space. If not in alphabet, it will be added automatically.
Now, for the transitions dictionary. This consists of entries in the forms:
"state-symbol" : ["newsymbol", "newstate", "direction"],
"state-" : ["newsymbol", "newstate", "direction"]
Where the state-symbol syntax matches that symbol in the given state, and the state- syntax matches the blank symbol in the given state. newsymbol is the symbol to replace the current one on the tape with, newstate is the name of the state to enter, and direction is the direction to move in ("R" or "L").
Implementation of incdec
You are now fully armed to use my Turing class, and so I give you the example of my incdec Turing machine. The machine definition section which follows is the most interesting, particularly the state transition dictionary which, you should check for yourself, contains all of the transitions described in the previous diagram:
states = ["ready", "inc", "dec", "grow", "reset", "halt"]
haltstates = ["halt"]
alphabet = ["0", "1", "<", ">", "+", "-", "/", "#"]
transitions = {"ready->" : [">", "ready", "R"],
"ready-/" : ["/", "ready", "R"],
"ready-#" : ["#", "halt", "L"],
"ready-+" : ["/", "inc", "L"],
"ready--" : ["/", "dec", "L"],
"inc->" : [">", "inc", "L"],
"inc-<" : ["1", "grow", "L"],
"inc-/" : ["/", "inc", "L"],
"inc-0" : ["1", "reset", "R"],
"inc-1" : ["0", "inc", "L"],
"dec->" : [">", "dec", "L"],
"dec-<" : ["<", "halt", "R"],
"dec-/" : ["/", "dec", "L"],
"dec-0" : ["1", "dec", "L"],
"dec-1" : ["0", "reset", "R"],
"grow-" : ["<", "reset", "R"],
"reset-0" : ["0", "reset", "R"],
"reset-1" : ["1", "reset", "R"],
"reset->" : [">", "ready", "R"]}
After the machine definition, the rest of the code is trivial in comparison:
tape = "<1>+--#"
incdec = Turing(tape, states, haltstates, "ready", transitions, alphabet, tape.index(">"))
incdec.run()
print("Input tape: " + tape)
print("Output tape: " + incdec.tape)
print("Last state: " + incdec.states[incdec.mystate])
print("Symbols read: " + str(incdec.symbols))
As a side effect of this whole endeavour, I have proven to myself that Python 3 is a Turing-complete language :)
An edit
I have reduced incdec to an alphabet of five symbols, removed the grow state, and added a new state to indicate an overflow has occurred on decrementing, ovrflw. New state transition diagram follows (colour coded :))
Now it is assumed that the starting position of the tape is somewhere in the number. Additionally, this diagram is very pretty with red for halt states and blue for the initial state, and I have reduced the number of arrows by clumping state transitions that act on the same pair of states together.
No more libetc for me
I've been meaning to get rid of libetc for a while now, as it causes a bus error when used with the latest xorg-server package. My solution to that until now was to start xorg with LD_PRELOAD= startx but now my solution is a little more drastic and a little less hacky; I've removed it entirely.
For those who don't know, libetc is a handy little library that redirects all requests for files in ~/.* to ~/.config/*, massively tidying up your dotfiles, at the expense of sticking a library in LDPRELOAD. I've been using it quite happily for a while, witha patch I wrote, that rather than using ~/.config uses $XDGCONFIGHOME, and sports the ability to redirect some requests to $XDGDATAHOME and $XDGCACHE_HOME.
I've been putting off deleting it as, well, there was a lot of stuff in ~/.config. Then yesterday it hit me, for the vast majority of things in ~/.config, I haven't edited the configuration at all. So I could just delete them. That got the number of files down massively, then it was a simple matter of trial and error to figure out which stuff needed to remain in ~/.config, and which stuff went straight in ~. The whole process took me less than an hour; hardly the gruelling thing I expected it to be.
Who the hell do you think I am?
I just thought I'd give a quick update on my recent anime-watching and manga-reading, as it's not exactly often that I update what's on the about page. Possible spoilers follow, though I have tried to avoid any.
Anime
Tengen Toppa Gurren Lagann
Also known as just Gurren Lagann, it's your typical mecha anime: giant robots, impossible feats, epic fights, a magic-like energy source… except this one brings in manliness and fighting spirit, too. It's only 27 episodes long, and I watched it across two days. I quite enjoyed it, and would recommend it.
It also has some great quotes, like "Don't believe in yourself. Believe in me who believes in you!"
School Days
This one is a must-watch, it's follows Itō Makoto's life through a period of time (about a year, I think) of his school life, and how he changes in that time. It may not sound very good or interesting from that description, but believe me, it is incredible. At a mere 12 episodes, you can't really refuse on the grounds of it being a waste of time if you don't enjoy it.
School Days started out as a visual novel, which has about twenty possible endings, depending on the choices you make when going through. Obviously, the anime shows only one of these endings. If you enjoy the anime, I suggest you also acquire the visual novel, something which I intend to do soon.
Umineko no Naku Koro ni
Another visual novel adaptation, Umineko is another great story. I personally am enjoying this more than School Days, but that could be because half the fun of Umineko is trying to solve the mystery. The series is split into four story arcs, one for each arc of the visual novel. The arcs are independent of each other, and tell different stories though key features are the same.
Umineko no Naku Koro ni is the story of the Beatrice the Golden Witch, come to collect her payment for lending Ushiromiya Kinzo a mountain of gold to rebuild the Ushiromiya family after it was ruined by an earthquake. In the process, she kills all of the Ushiromiya family and their servants, apparently using magic. The aim of the game is to solve the mystery and figure out how it was all done without magic, and, more importantly, who did it!
I have a theory, which is currently falling apart as I play through episode two in the visual novel, but I'm working on it...
5 Centimeters per Second
This is a film, and follows the life of Takaki Tōno over a period of two decades. He and Shinohara Akari form a fast friendship at their elementary school and, when she moves away, he never quite gets over it. The story follows how this affects his relationships with others in his life, and is quite sad. This is another very good story.
Manga
I've just got in to manga, so this list is very short for now.
The Ring
An anime adaptation of Ringu, and is pretty much identical to the film. I have yet to read the actual novel, so I don't know how close it is to that. Being the first manga I have actually read fully, I definitely enjoyed it. I have tried to get into manga before by reading it online, but that never worked for me. Same failing as ebooks, really: it should be on paper, but isn't, so I can't enjoy it.
The Ring is (have you been living under a rock if I need to explain this?) a story about an evil girl, killed and who projects her evil into a cursed videotape and kills all who watch it, exactly seven days after they have watched it. Obviously, the manga isn't quite as scary as the film...
Haml and Sass
Recently I discovered (thanks to a thread on the Arch forums) Haml and Sass. Both are Ruby-based domain-specific languages for web development; Haml is intended for (X)HTML (but can be used for any XML, as I discovered), whereas Sass is intended for CSS.
A little wary at first, I decided to investigate them. I am glad I did. I replaced my XHTML template with a Haml template, and my CSS files with some SCSS ones. Haml looks a lot more simple than XHTML, but Sass looks a bit more complex than CSS, which is because Sass is a superset of CSS. A valid CSS stylesheet is also valid Sass code, which means you have a powerful way of producing CSS files. Sass adds some nice features, while still retaining the ability to just type in plain CSS when you need to.
As you can see, the Haml is much simpler and shorter than the corresponding XHTML, whereas the Sass is probably longer, but has more functionality.
In the following sections I'll just give you a very brief overview of Haml and Sass. If you want to learn or use them, look at their respective websites.
Haml
Haml uses indentation to indicate where things are and, unless state otherwise, everything is a div. Consider for example the following XHTML snippet:
<div class="test">
<p>Lorem ipsum dolor sit amet</p>
</div>
Hardly the most complex bit of XHTML, but even for this small example, Haml is a clear winner:
.test
%p Lorem ipsum dolor sit amet.
One line and many characters shorter.
Sass
Sass is a superset of CSS, so I'll have to give a slightly more complex example here:
div.error {
font-weight: 600;
font-size: 2em;
color: #FF0000;
}
div.error span.serious {
font-weight: 700;
font-size: 2.5em;
}
Again, not the most complex of CSS, though the Sass will look pretty complex at first glance, as I'm trying to cram in as many features of Sass as I can to show you the power:
@mixin font($weight : "", $size : "", $color : "") {
@if $weight != "" { font-weight: #{$weight}; }
@if $size != "" { font-size: #{$size}; }
@if $color != "" { color: #{$color}; }
}
div.error {
span.serious { @include font(700, 2.5em); }
@include font(600, 2em, #FF0000);
}
Ok, what's that stuff, I hear you cry. Well, firstly I defined a mixin. Mixins are the thing I like most about Sass, they are CSS generating functions. Basically, it takes up to three values, a font weight, size, and colour, and generates the appropriate CSS. If no weight/size/colour is given, no weight/size/colour CSS is produced in my example. Secondly, you can see how I have nested the span.serious within the div.error. Sass lets you structure your styling like this to clearly see what properties something has. Thirdly, I have used the mixin I defined earlier to generate the font property CSS for the error div and serious span.
This example isn't very clear, but have a look at the SCSS making up my website now, you'll get a better feel for how things fit together.
Haml for XML
It didn't take me long to discover that Haml can be used for any form of XML. I have, until earlier today, had a complex mixture of PHP and XML generating my FOAF file which was, to be honest, a convoluted way to get out of writing XML (it's not my favourite of languages). Translating it to Haml was wonderful, see for yourself:
So, where will I go to next with my Hamling and Sassing? Well, I'm thinking about dropping Phatso and creating a better framework for myself, that has a good template engine. That would let me generate templates for the gallery and lifestream stuff with Haml, as well as remove the PHP from the current template file. I'm thinking about extending my website's Makefile-in-progress to generate static pages out of everything but the lifestream and gallery pages, so reduce processing time for other pages to almost nothing.
I think I'll be playing around with my website for a few days yet, Haml and Sass have temporarily re-kindled my interest in developing it.









