Saturday, October 8, 2016

Ping spikes on Mac OS X

Problem: After (I think) an update my Mac began to show a strange pattern of network latency: A ping to my router would show a string of 1ms trip times punctuated periodically by a cluster of 400-1000ms latencies. I first realized something was wrong when I was playing counter strike and my ping would go crazy - on a server I frequent my typical ping used to be 20ms, but now it would register as 190ms, 400ms and so on. I could verify this was not a internet provider issue by pinging my router from a different computer and getting a steady string of 1ms latencies.

The solution was remarkably hard to find even though it seemed to be a common problem faced mostly by Mac users. A common response was to turn off anti-virus software, and I knew this was not the correct answer.

Solution: From the thread here (look for the answer by "eyepaq") it seems to be a bad setting or combination of settings in network preferences which can be reset by deleting the file "/Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist" and rebooting.

I was a little surprised at how happy I was that I could play CS:S again - not that I play so much, it was just such an annoying problem.

Sunday, March 6, 2016

Recording terminal sessions

All coders have some use for screen casting a terminal session, usually to demo a command line tool. The old way I did this (when I migrated to a Mac) was to use Quicktime's screen recording feature, which really is superb. Then I learned of asciinema. Asciinema was very novel for me because it looked like a movie, when played through the slick player they have, but you can just go an select the text that is 'playing' and copy paste it into a terminal. Amazing!

Reading a little deeper into asciinema's docs we find that Linux actually has built in infrastructure for recording terminal sessions - with timestamps! - and also for then playing those sessions back. You use script to record the keystrokes and responses and scriptreplay to play them back! Magic!

But there is more. There is a Python package called TermRecord that packages these utilities and then dumps the output to a standalone html file! This is most convenient for sharing demos etc because everyone has access to a web browser and you can also set up the file as part of a webpage.

Thursday, February 11, 2016

gh-pages: .nojekyll

I had the devil of a time trying to get a custom website to show up on github pages. It was sphinx generated documentation and I had put it on the website under a directory called manual. It was all borked. Not only did the nice stylesheet not show up, none of the images showed up either. I tried a whole bunch of things, but finally, from a hint here, it turns out that you need to add an empty file named .nojeykll in the root directory, and that allows you to add your own custom projects.

Friday, February 5, 2016

Python - Click

Python excels at making it quick and simple to convert your one-off scripts (or so you thought) into easy to use command line driven programs. The package that made me fall in love with this process was docopt (my notes here, here and here).

A year after I started to use docopts a colleague (two colleagues actually) introduced me to the Python click package. I was initially reluctant to migrate an existing code base to docopt, but I decided to give it a spin and have not looked back.

There was a recent post comparing argparse, docopt and click shared by another colleague, perhaps prompted by an internal debate about the merits of the different options available.

One thing that gets missed in these comparisons is more specific and complicated use cases. I just found a simple, specific use case where Click blows the other two away in terms of managing complexity and improving readability:

My application requires me to pass in key, value pairs in the command line that serve as metadata for a system. The keys for the metadata are constrained to come from a predefined list. Now, how would you do this in argparse? In docopts?

In click, it's very, very straightforward.

import click
keys = ['key1', 'key2', 'key3']
@click.command()
@click.option('--meta', multiple=True, type=(click.Choice(keys), str), help='Metadata')
def cli(meta):
  """Print the supplied metadata values"""
  print meta

This code will validate the metadata keys, will accept multiple entries for --meta and will present you with a list of tuples. It is very terse and very maintainable.

In this respect, I think Click is very far in advance of the other Python packages available for constructing commmand line interfaces.

Monday, January 18, 2016

Python: Getting position in a compressed file

Problem: Python's gzip module is awesome, but poses a problem when we wish to report reading progress along a compressed file: .tell() returns the total bytes read but we usually only have the compressed size of the total file available, e.g. via os.path.getsize.

Solution: One trick is probably to extract the original size of the gzipped file from the last 4 bytes in the file, but, there are interesting caveats to this. Another method, slightly more principled, is to use .raw.fileobj.tell which, interestingly, reports the position on the compressed file, rather than the uncompressed stream.

Thursday, January 14, 2016

git config core.fileMode false

Problem: After moving data from a Time machine backup to a new Mac (EDIT) Actually, I copied the data to a NTFS drive and then back to a Mac, so the blame goes to NTFS, all files in a repository show up as changed files under git, even though a diff proves that their contents are unchanged.

Solution: From a tip here, it turns out that it's related to file mode (and other metadata) changes which may occur during such a restore operation (for example the UID of the user may change, I guess) the solution is to tell git to ignore the file mode


git config core.fileMode false