Markdown Previews

Say you want to edit file.md in your favorite text editor but want to see a preview of the resulting HTML in Safari. On Mac OS X. Get yourself a new bash shell and run the following commands:

brew install multimarkdown
brew install fswatch

echo '#!/bin/bash
multimarkdown --full "file.md" > \
/tmp/file.html && \
open -g /tmp/file.html' > /tmp/rebuild.sh \
&& chmod +x /tmp/rebuild.sh; \
fswatch -o "file.md" | xargs -n1 /tmp/rebuild.sh

Now every time you save file.md, fswatch will run the rebuild script, passing the file through multimarkdown and handing the resulting HTML file to the open command. The -g flag in open keeps Safari from jumping to the foreground every time the script runs.

Safari has the nice feature that if you click on a link to http://example.com while http://example.com is already loaded in some tab, it will switch to that tab and reload the page. Not sure if Chrome does the same, but there's probably a setting or extension for that.

Note that there's a newline after the first line in the string we're passing to echo.

Who needs a dedicated live previewing markdown editor, anyway?

mailbot.py

Wrote a python3 script that looks for specifically formatted emails with photo attachments and uploads said photos to a web-server accessible folder. The script then replies to the emails with the URL or URLs of the image or images, and deletes the emails from the server. A simple, self-hosted, alternative to existing photo uploading services. The script is available as a gist. At the moment is pretty simplistic and special cased. If I find that mailbot needs to deal with other sorts of messages, I'll probably generalize it.

Copy Current Safari Page Link for Day One

Put together a Safari Service in Automator that generates a nice Markdown link to the current page for the Day One journaling app. It copies the link to the system clipboard and launches Day One. Very handy for quickly writing a note about the current website. Once the service is installed it can be bound to a keyboard shortcut through the Keyboard preference pane in System Preferences. I use Option-D.

Fortnight

The word "fortnight" became less common than the phrase "two weeks" in American English in 1906, but persisted as the more popular term in British English until 1977.

I'm not drawing any particular conclusions here. Just thought it was interesting. I wondered when the word "fortnight", so common in the writings of Jane Austen and her contemporaries, went out of fashion. Take a look at this for reference. Note the 1906 crossing of the green and orange lines. Those represent the two terms in the American English corpus. Also note the crossing of the blue and red lines in 1977. Those lines represent the words in British English publications.

Fortnight seems to still be pretty common in British English. It is used in relation to various fortnightly payments that are popular in Britain and related Commonwealth countries.

I say we make an effort to revive the word in America within the fortnight.

See what I did there?

Hopping Out of the Airlock

EVA is not so simple as jumping in a space suit and heading out the door.

Extra-Vehicular Activity (EVA) on the International Space Station (ISS) is a pretty tricky business. The suits astronauts wear these days take time and require help to put on. But here is a point that is not always considered: the atmosphere inside the space suit is not the same as the one on the ISS.

The air in the habitable compartments of the ISS is kept in a pretty comfortable state. The pressure is about the same as here on Earth at sea level. The gas content is also normal: a mix of Oxygen and Nitrogen (plus some Carbon Dioxide). The space suit is different, though. The pressure is lower, about 1/3 of an atmosphere, and it is pure Oxygen (along with any Carbon Dioxide and water vapor coming from the astronaut). The pressure has to be lower to allow the astronaut inside to move their arms and legs: if it were higher, the suit material would be too stiff to move. The astronaut survives the lower pressure because the actual Oxygen content is pretty much the same as that on Earth. So it isn't like they go up a mountain where the air pressure is low and the Oxygen content is low also. The pressure might be low, but the Oxygen is just as it should be.

Ask any diver what happens when a person quickly goes from high pressure to low pressure: the bends. Not something people would want to experience, especially not in space. There are plenty of other things to worry about up there. And lots of lovely sights to see. So astronauts need a way to gradually transition to the no-Nitrogen low-pressure conditions inside the space suit.

One way to manage the transition involves a process that takes two and a half to four hours out of the astronaut's morning. They breathe pure oxygen for a while before going out, with some vigorous exercise thrown in, to slowly pull the Nitrogen out of their bodies. It is difficult work on top of an already difficult day, and apparently not much fun.[1, 2, 3]

The alternative is to "campout" in the airlock overnight. The atmosphere is gradually adjusted while the astronauts sleep through the night. This way they are ready to head out about an hour sooner than they would otherwise. Imagine shaving an hour off your four hour commute. I'm sure you'd be thrilled too! [4]

Of course, this makes one wonder what would happen in an emergency (I blame that Gravity trailer). It seems that it would be impossible to rush the EVA procedure. The emergency repair that was done on the ISS just before Expedition 35 headed home was in planning for a few days at least. Chris Cassidy and Tom Marshburn went out to fix an ammonia coolant leak. I haven't been able to find anything on rapid emergency response for the ISS beyond hopping in a Soyuz capsule and punching out. That's a cramped, bumpy, but reliable ride back to Earth.

See also: NASA Spacesuit Engineer talks Space with Students on YouTube.

Piwik-StatusBoard Bridge

An easily-customizable Piwik-StatusBoard bridge: GitHub Gist.

Wrote this Django views.py function to bridge my Piwik analytics system to my iPad StatusBoard. It generates a graph of the number of visits to each of my two sites over the past few days. The default is 10, but I actually found that 11 is a nice number. It should be fairly easy to adapt this code to display other piwik based data. It uses the Piwik Python API to communicate with Piwik and Python's own JSON library to process the input and generate the output.

Shuffle Classical Music in iTunes

You can use the grouping field in the iTunes metadata to conveniently shuffle classical music.

iTunes has a few modes of shuffling music. These can be accessed from the Controls⇒Shuffle menu. The old standards are album and song shuffling. There is also grouping shuffling. This seems to shuffle tracks that have no grouping as regular songs. When it encounters music that does have all the same grouping, however, it treats these tracks as albums, and plays them through before jumping to the next thing. Classical music albums often have separate tracks for movements, but also multiple overall pieces (symphonies, for example). All of the tracks in a single symphony on an album can be given the same grouping: "Beethoven: Symphony #1 (LSO)". You are now at liberty to select the classical genre, turn on shuffle, and have a long stream of classical music with the various symphonies and concertos intact.

Other uses of the grouping field may be found around the Internet. The most notable one is using it as a sub-genre for building Smart Playlists.

Counting Digits

A problem came up: how to count the number of digits in an integer? There were three solutions that I could think of: division, strings, and logarithms.

  • You can divide the integer by 10 until you get to zero. The number of divisions is equal to the number of digits.
  • You can convert the number to a string and get the length of the string.
  • You can take the ceiling of the base 10 log of the number.

In order to figure out which of these methods works best, I wrote a short python script. Starting with a 9 digit number, I ran the number through 1,000 iterations of each method, got the average calculation time, added another digit, and repeated indefinitely. The division method started showing significant decreases in speed at about 200 digits. The string method broke down at about 400 digits. The logarithm method ran with no decrease in calculation speed up to 47,387 digits. The process of adding an extra digit to the number took significantly more time than the 1,000 iterations of the digit count. Not wanting to let my computer's all day calculation spree go to waste, I present these findings and the algorithm here. Your mileage may vary, depending on the log implementation.

def numdigits(n):
    return int(math.ceil(math.log(n, 10)))

Core Animation Zooming

At first pass, this seems like a fairly simple problem: How do you zoom to mouse position in Core Animation? I suppose looking at it now, once I've figured it, out it seems quite straightforward.

The problem lies in the fact that I had a complicated layer structure last time I tried to do this. The first time I tried to arrange the layer hierarchy for my sequential image viewing program, I had far too many layers. I had a root layer, then there was a zoom layer, which would theoretically just zoom. Then there was a layer for panning. Finally there was a layer encasing the images, and then the images themselves. This turned out to be a housekeeping nightmare.

The simple solution is to use two layers. The required root layer, and a layer to contain the images. The image layout is done with respect to the images layer. The panning of the images is handled by changing the position of the images layer. The zooming is handled by applying a scaling transform to the images layer. Simple!

There's still a trick to get things to work just the way I wanted. In order to have the layer zoom in and out while centered on the mouse cursor, I redefine the scroll wheel function for the containing window. Here I figure out the coordinates of the mouse point in the layer being zoomed. It does not matter if the coordinates are outside the layer. I then create a new anchor point by dividing the coordinates I just figured out by the width and height of the layer. This gives me a unit length, rather than a pixel length (damn Apple for making things this complicated; I still don't see why they need to have two different coordinate schemes). Finally, I save the frame of the layer I want to zoom, set up a transaction without actions, set the new anchor point, reset the old frame, and end the transaction. I have to save the frame because changing the anchor point moves the layer, so to change the anchor point in place I reset the frame back to what it was. Now I can apply the scaling transform to the layer however I wish and move on with my life.

I wish I had a guide like this back when I was first trying to solve this problem half a year ago, so maybe this will help somebody else. Know a simpler way to do it? Drop me a line!

Counting Divisors

A little over a year ago I started working on Project Euler problems. Used them as a handy way of learning Python. About that time I ran into a problem that stumped me completely. Problem 12 wanted to know the first triangle number with more than 500 divisors.

A triangle number of order (n) is defined as the sum of the arithmetic sequence from 1 to n, so triangle(3)=1+2+3=6 and triangle(7)=1+2+3+4+5+6+7=28. Calculating a triangle number is trivial. Figuring out how many divisors it has is a bit more challenging. I was trying various things that first time with caching the number of divisors for various numbers as you go up so that you don't have to calculate it again. Honestly, I don't really remember exactly what I did, but I remember that it was big and it took far more than the 1 minute maximum that any Project Euler problem should take. It took so long that I didn't even have the patience to let it run until it found a solution.

I skipped the problem then and let it sit undone for a long time. Until today. I fired up my trusty editor and busted out a solution. And it worked. And I'm thrilled. Not least because the code I came up with for finding the divisor takes all of 4 lines and only breaks the 1 second execution time mark at around n=1e+13 . Here's the code:

def divisors(n):
    total = 2
    for i in xrange(2, int(math.sqrt(n)) + 1):
        total += 2 if n % i == 0 else 0
    if math.sqrt(n) == math.floor(math.sqrt(n)):
        total -= 1
    return total

This works for n greater than 1.

Blackberry Dial Tone

In case anybody was wondering, the little sequence of dial tones that the Blackberry Curve 8330 makes when you hit send is the dual-tone number sequence 7933409.

I recorded the sound, analyzed it in Audacity, and found the following main frequency peaks:

  • 859 1207
  • 856 1480
  • 703 1481
  • 707 1479
  • 781 1217
  • 939 1340
  • 852 1481

These come fairly close to the following standard frequency pairs:

  • 852 1209
  • 852 1477
  • 697 1477
  • 697 1477
  • 770 1209
  • 941 1336
  • 852 1477

Which match to the keypad numbers 7933409.

Not sure what all of this means, but there's is a version of the sound generated from sequences of pure sine tones of the second set of frequencies, mixed together, and then passed through a low pass filter at a cutoff of 1600 to eliminate the clicks between silence and sound.

So 7933409 it is.