Thursday, April 2, 2015

Lobotomizing bash autocomplete

I hate the smart tab-completion functionality that comes with bash on Ubuntu (and others), and much prefer that just show me files & directories.

To disable it, just put this in your .bachrc:

complete -r

Monday, May 19, 2014

So you ditched your iPhone for an Android device ...

No, not me, I certainly haven't gotten rid of my iPhone.  Even if I didn't love the newish looks of iOS 7 (and I do love it), I'm much too lazy to learn a new UI without major payoff.

But my brother DID ditch his iPhone, and now sending him a text message is a pain, b/c my iPhone defaults to iMessage.  (There's even a lawsuit!)

I've found two threads discussing fixes for this; I haven't tried either, and haven't heard back from my brother, so I'm posting both in the hopes that one works.


(I) The easy method (from https://discussions.apple.com/thread/5079636?start=30&tstart=0)

Step 1

Step 2

Log in with your Apple ID and password.

Step 3

Select the product you need to de-register (your iPhone, for example).

Step 4

Click ‘Unregister’.

Step 5

You’ll see the message ‘Are you sure you want to unregister and delete this product?’ – Click ‘Unregister’ again.
NOTE: if you’ve got any other iMessage-capable devices registered to the same Apple ID that you no longer have access to, unregister these now.

(II) The slightly harder method, from https://discussions.apple.com/thread/5157687

The long and short of my fix was 1) going to my iphone Settings under "Messages" and assuming iMessage is set to "on" scroll down to where it says "Send & Receive" and click on that. It showed my phone number along with other email and Apple related addresses. Deselect the phone number now associated with my Android device as well as any email messsages from which I didn't want to send/receive iMessages (in my case I still want to receive iMessages at email addresses associated with my iPad and MacBook). After doing those steps, close out of Settings.
 Then 2) go to the itunes/apple page and change your password at the following page: https://appleid.apple.com

When you change the password on your account it supposedly deauthorizes the phone number and other addresses just deselected addresses from iMessage.


Wednesday, March 26, 2014

Wednesday quickie: summing numbers in bash

care of stackoverflow, here's how you sum a bunch of numbers in Bash (or, well, awk):
 
   awk '{s+=$1} END {print s}'

Tuesday, September 17, 2013

Bash as occult text: starting a Unix daemon

So you've written your own Unix daemon, and now you need a start script to stick in /etc/init.d

As a socially responsible coder, you know your daemon needs to run under tightly policed user, like www or therebemonstershere.  So your init script will look something like this:

  su - therebemonstershere -c " { /usr/local/bin/mydaemon & } "

You'd also like to keep track of its pid, so you can kill your daemon later via /etc/init.d/mydaemon stop

  su - therebemonstershere -c " { /usr/local/bin/mydaemon & } ; echo \$! > /var/run/mydaemon.pid"

But this isn't enough: what about stdout and stderr?

  su - therebemonstershere -c " { /usr/local/bin/mydaemon > /var/log/mydaemon.log 2>&1 & } ; echo \$! > /var/run/mydaemon.pid"

Looking good!  But there's one more catch: when your terminal dies, your daemon will get a HUP signal and die too.   Probably in a week or so when you finally kill that xterm and forgot any of this happened.

  su - therebemonstershere -c " { nohup /usr/local/bin/mydaemon > /var/log/mydaemon.log 2>&1 & } ; echo \$! > /var/run/mydaemon.pid" 

Wednesday, February 13, 2013

Protip: disabling accidental text resizing in Firefox

Ubuntu, at least in its Pangolin incarnation, has this clever skeuomorphic touchpad behaviour: when you scroll with a glancing gesture, the scrolling continues for a bit after your fingers have left the touchpad, like flicking a playing card across a table.  It feels natural.

However, it often results in my Firefox fonts becoming randomly resized, as I frequently begin scrolling to the top of a page, then change my mind and highlight the URL bar via ctrl+L, and in that instant when the skeuomorphic scroll is happening and the ctrl key depressed, Firefox zooms.

The easy fix it to disable Firefox' ctrl+mousewheel zoom behaviour.

Tuesday, December 18, 2012

Using a web server for deferred computation

Bioinformatics lends itself to lengthy, offline computations on large datasets like Next-generation Sequencing runs, but I imagine any number of problems would follow the submit-form, check back in 10 minutes approach.

Often, web frameworks make this hard to do without resorting to secondary processes and message queues, because their response function doesn't close the socket until it returns.  (Eg, Django's views.py functions return Response objects).  Python's BaseHTTPServer / BaseHTTPRequestHandler combo turns out to make this easy:
 
import BaseHTTPServer
from SocketServer import ForkingMixIn
import socket

class Handler(BaseHTTPServer.BaseHTTPRequestHandler):

   def do_POST(self):
                path = self.path
                msg = ...
                self.send_response(200)
                self.send_header("Content-type", "text/html")
                self.end_headers()
                self.wfile.write(msg)
                self.wfile.close()
                self.connection.shutdown(socket.SHUT_RDWR)
                self.connection.close()

                long_computation() 


class ForkingHTTPServer(ForkingMixIn, BaseHTTPServer.HTTPServer):
  """
  Handle requests in a separate process.
  """
  pass

server = ForkingHTTPServer((host,port), handler_factory(override))
server.serve_forever()

The key is this bit:
   self.connection.shutdown(socket.SHUT_RDWR)
   self.connection.close()

Once you shutdown and close the socket, the HTTP client is released and you can run as lengthy a computation as you like.

Thursday, May 10, 2012

Through the JVM looking-glass.

So you've got a binary file full of integers in bog-standard x86 format, just a straight list of unsigned short ints.  And you need to read them into a Java ArrayList.  Sounds simple enough.

Now, you're an experienced C programmer, so you know you'll have to deal with endianness: the JVM is big-endian, and x86 is little-endian.  Every time you find 0xff00 in your file, you need to flip those bytes around to read 0x00ff.  So you write a simple loop -- in pseudocode,

int accumulator = 0;
while( b = file.readByte() ) {
   accumulator = accumulator | b;
   accumulator = accumulator << 8;
}

And low and behold, 0xff00 becomes ... -1.

You might have guessed it already, but the problem is that Java doesn't support unsigned integers and that first byte, 0xff, is read as -1 per 2's complement.

Now, if you can visualize all that bit-shifting and or'ing, you might not see this as a problem -- the 0xff will just be OR'ed into your short, then shifted.  This is not what happens.

Instead, our byte b, which has been interpreted as -1, is silently coerced into a short -- 0xff becomes 0xffff, which is then OR'ed with 0x0000 to yield 0xffff, aka -1.  Our next byte, 0x00, is also coerced and OR'ed to no effect.

[Why anyone would want a signed byte is a whole 'nother question -- next time I want to store a value between -128 and 127, I guess I know what to use.]

The fix is to undo the 2's complement -- to turn -1 into 255, like so:

int accumulator = 0;
while( b = file.readByte() ) {
   short s = (short) b;
   if( s < 0 ) {
      s = s + 0x100;
   }
   accumulator = accumulator | s;
   accumulator = accumulator << 8;
}

That's how you read a single unsigned short in Java.  Just so intuitive, don't you think?  Compare with Python:

   s = struct.unpack("B", file.read(1))

Thursday, February 2, 2012

Embedding iPython, redux

I've posted about iPython as an embedded debugger before; it turns out that the syntax has changed a bit:
  import IPython
  IPython.embed()

Friday, January 20, 2012

hdiutil and OS X data recovery

Your hard drive fails, so the first thing you do is grab dd and pull a byte-by-byte copy of the drive's contents:
   dd if=/dev/bad_drive of=bad_drive.img conv=noerror,sync

Problem is, if the drive was from a Mac and you want to use a recovery tool like Data Rescue or Drive Warrior, you'll probably need to attach your drive image as a loopback device, and double-clicking the file to mount it doesn't work when the filesystem is fragged.

Enter hdiutil:
   hdiutil attach -nomount bad_drive.img

Wednesday, December 21, 2011

Resuming truncated 'cp's

I'm sure this isn't too hard to do w/ dd, but let's say you've got some slow network disk mounted under /mnt/crappy_SMB_share and were 100 gigs into a 101G file when the Windows server went down, and now you want to finish off that last gig.

  cd /mnt/crappy_SMB_share
  curl -C - -O file:///original/file/location

from here.

Tuesday, September 27, 2011

AirPlay: streaming from iTunes and your iPhone to a Linux box

So I've got a Debian machine as a WiFi access-point / file-server, and I decided to plug some speakers in and stream music from my iPhone.

I tried Erica Sadun's AirPlayer, a Python script that, as it turns out, implements version 2 of the AirPlay protocol -- the one that supports video and images.  What it DOESN'T support is sound, at least not without the appropriate crypto key.

Which led me to Shairport, vessel of the secret sound-support sauce.  Shairport only implements AirPlay v1, which can't handle images of any kind.  Which means that the video portion of YouTube can be streamed from my iPhone to AirPlayer, and the audio portion to Shairport, but I have to choose one.  Huzaah.



This worked well enough for iTunes, but iOS, ie, my iPhone, didn't work until I disabled IPv6 on my Linux box.

Friday, June 3, 2011

Yet more Crescent City


Those giant game-of-jacks pieces are called dolos.

Wednesday, May 4, 2011

Another in a series of brief tests of Wolfram Alpha

If you've spent the last couple of years behind a few inches of lead, protecting you from the nerd-radiation of the Wolfram|alpha launch,well, you should read up on it.  It claims to be, not a search engine, but something smarter, a compendium of systematic data (temperature records, physical constants, business data, you name it), and the algorithms necessary to process that info.

I've been disappointed thus far.  I tried another reasonable-seeming query today: "kinetic energy of a ring with mass 1 kg and radius 1m rotating at 100 rpm"

(If you're wondering, I was hanging out by a centrifuge, which just begged the question, ya know?)

And what did Alpha have to say? "and (English word)"

It recognized the word "and" as English. That's it.

The answer didn't show up until the 8th hit in google's results, but there it was,

   E = 0.5 * I * ω
   I = m r2

etc etc.  Granted, I still have to do the math, but at least it's something.

Friday, January 28, 2011

The lively faces of poverty

At the risk of sounding like Tom Friedman, the most striking thing about Saigon was the bustling, crowded buzz of the place -- the way every square inch of public space was somehow occupied by a person, streets completely filled with Vespas and every sidewalk a public market.

The average Vietnamese lives on about $3,100 per annum, less than half what her counterpart in China makes, so we're talking about a very poor country.  But that poverty doesn't manifest itself as obvious misery--not where we visited, at least.  Instead, you see this:
This Hanoi street is lined by key-cutting carts, as could be found on every other street corner in Saigon.  These are industrious, hard-working people, but in the US, copying keys must occupy 30 minutes out of the day of a Home Depot employee; the rest of the time, he's off stocking shelves while the machine rests in the corner.  American labor is simply too expensive to waste waiting for customers; instead, each worker has a whole stock of capital goods (the key-cutting machine, a forklift, etc) at his command, keeping him occupied each and every minute.

In Saigon, you can find any food or service you need, right there on the sidewalk, because in Saigon, you can make a living cutting keys for one blocks-worth of people.  It's actually kinda nice if you don't mind crowds ...

Thursday, December 30, 2010

The future is already here – it's just not very evenly distributed. (An open letter to AT&T)

Dear AT&T,

Attached you'll find a picture my wife and I took on a boat traveling through Vietnam's Mekong Delta.  Our abilities with our new camera are admittedly lacking, so it's hard to make out the orange life vests lining the boat's ceiling and green jungle in the background, but then, this is my blog, not a court of law.  The real crux of this photo is the 5 bars you see on my iPhone: I had good reception throughout the Delta -- much, much better than I get in America's second most density populated big city*.
AT&T, I've loved my iPhone these past 2 years, but long for a phone call that doesn't end mid-sentence, and will be thrilled to move to a network that can compete with the 3rd world the moment my contract expires.
  -- kieran

* - These kinds of comparisons hinge on the definition of terms like "big city," of course, but scan through Wikipedia's density stats and look for a city you recognize that isn't NYC.  And if you haven't tried AT&T in San Francisco, don't bother.

Thursday, October 14, 2010

Konichiwa

Hello from Osaka. God knows what's going on with that very uncomfortable rhino up there, but one thing I'll say for Japan: it's dense.

That shot out the window of a train is exurban, far-out Osaka. No sprawl in sight: dense, multi-story housing and ... woods.

Sunday, August 1, 2010

OpenOffice makes me appreciate Firefox' search function.

The search dialog that appears when you hit Command- [or Control-, depending upon OS] F is a bit of UI that's improved greatly but subtly in Firefox: instead of a separate dialog appearing, an embedded interface shows up at the bottom of the page.

This means the dialog location is consistent and predictable, but more importantly, it means that I can always search by hitting Command-F.  Under OpenOffice, Command-F toggles between opening and closing the search dialog; if I was searching for something earlier, I've now CLOSED the dialog rather than opening it.  Very annoying.

Thursday, July 22, 2010

Quick link: shameless self-promotion edition

In the interest of raising the site's Google Pagerank, and as a plug for my wife, who did the site design, here's a link to my sister-in-law's optometry site, Lake Vision Optometry, located in the verdant, exotic lands of Riverside, CA.

Wednesday, July 21, 2010

The Eee, Ubuntu, and Songbird

After years reading about super-cheap Netbooks, I finally picked one up for a friend, though ASUS' Eee PC 1201t skates pretty close to the line separating Netbooks from Notebooks with its 1.6ghz AMD CPU, 12" LED LCD screen and 250G drive.  All for $380!

Don't try to get an OS on the beast, though.  That'll be tough.  After determining that MeeGo doesn't support the Eee's Radeon GPU and finding EasyPeasy kinda clunky, I decided to put Ubuntu 10.04 on it. Unfortunately, Canonical has mostly dropped support for USB installers, and this little machine has no DVD drive, leaving me to pull off bits of cheap Taiwanese plastic until I hit hard-disk.

Now that Ubuntu's loaded, however, I couldn't be happier.  All the hardware -- Wifi card, webcam, everything -- was supported without any tinkering, and Ubuntu looks great.

I do miss iTunes, though.  Songbird (installed via Skyzim's deb) is a gorgeous replacement, so long as you don't want to listen to any w4a files from the iTunes store -- which I do. This thread explains the fix, though it's tough for the command-line-phobic. After installing the various gstreamer libraries, you'll want to open your favorite editor and add one line to /usr/bin/songbird:

export SB_GST_NO_SYSTEM=1