1. Ostensibly about Magic: the Gathering, this applies to just about anything.

     
  2. Generate a file filled with ASCII/junk

    I needed to create some junk files to test out a procedure for sharding a large file into smaller files.

    The solution: combine dd, /dev/urandom, and base64.

    Example:

    dd if=/dev/urandom bs=1M count=2 | base64 >random.txt
    

    bs=1M and count determine how much data dd will read out. This example would be 2M of base64.

    For smaller amounts:

    head -c10 /dev/urandom | base64

    Some people like to use this to generate random passwords.

     
  3. 10:31 14th Jun 2013

    Notes: 1

    Tags: pythonhttp

    http GET, POST in Python 2.x

    Use urllib and urllib2 for making HTTP requests. Don’t use httplib.

    httplib is no fun

    There are some dumb limitations:

    • HTTPConnection can only be used once
    • You can only call getresponse() on an HTTPResponse once
    • There’s no HTTPRequest object as such; you throw all your args into a single HTTPConnection.request() call.

    There are one or two little gotchas:

    ### NO NO NO
    conn = httplib.HTTPConnection("www.example.com/foo")
    ### yes
    conn = httplib.HTTPConnection("www.example.com")
    resp = conn.request("GET", "/foo")
    

    All in all httplib was cumbersome for interactive development.

    A friend speculated that this was a thin wrapper around the C APIs. I don’t have anything against C, but this is Python.

    urllib and urllib2 instead

    urllib and urllib2 have what you need. urllib has quote(), which you want if you want to URL encode a string. More convenient is urlencode(), which takes a map or sequence, and outputs the usual foo=bar%20baz&quux=frotz.

    urllib2 supplies a Request object. The constructor is a bit of a bear, but most of it is optional; you can call add_data() or add_header() for to add stuff later.

    After that, call urllib2.urlopen() with your Request object. The Response object is a file-like. The body is the file, and methods like info() give you headers, et al.

    caveat

    It seems like on such as a 403 you’ll get an exception (HTTPError) and your response will be empty. IMHO this is dumb. An exception should be in the case of a malformed, incomplete, or otherwise bogus response. A 403 is a legit response. It may have headers, etc. I should be able to look at that through the response object, not forced to paw through exception info trying to find the right fields.

     
  4. rsync

    If you can’t remember the command line arguments to rsync, take heart. Start with something like

    rsync -av src dest
    

    and work your way from there.

     
  5. Re-indent in vim

    You can re-auto-indent (?) some text in Vim with =. (It works with ST2, as well.) This can save you a bunch of manual > and/or <.

    It’s seems close to useless with Python, but otherwise it can be very useful.

     
  6. 16:06 3rd Aug 2012

    Notes: 2

    Tags: sshshell

    ssh aliases

    I always forget this, so here is the most basic of basics.

    Right now, you’re typing this:

    ssh foo@home.bar.com
    

    You’d rather type this:

    ssh home
    

    Add this to ~/.ssh/config:

    Host home
        HostName home.bar.com
        User foo
    

    ssh home should now work as expected.

     
  7. This is a ruby gem which allows you to monitor files and perform some action when they change. You limit which files you want to monitor with a regex. You can configure which actions guard performs yourself, using their DSL, or by installing one of the many plugins.

    If all you want to do is run a shell command, guard-shell will suffice.

    An example Guardfile, roughly:

    guard :shell do
      watch /(.*)\.mkd/ do |m|
        `markdown #{m[1]}.mkd >#{mkd[1]}.html`
      end
    end
    

    That’s about all you need to get started. Whenever you alter foo.mkd, guard will invoke the command line above, which will output a corresponding foo.html.

    Obviously you could use this for compiling, running unit tests, or whatever you like.

     
  8. git, ssh, and push

    It’s actually super easy to use git and ssh to pull repos between your machines. Pull is the operative word, though. More on that in a bit.

    Create a repo on machine A, whose hostname for this example is desktop.local. We’ll clone from this repo at first.

    mkdir -p ~/src/my-repo
    cd ~/src/my-repo
    git init
    

    On machine B, hostname laptop.local, we clone the repo:

    mkdir -p ~/src
    cd ~/src
    git clone ssh://desktop.local/~src/my-repo
    

    Now your laptop has a copy of my-repo and pulls from desktop.local.

    If we want to be able to pull from laptop.local, we’ll need to tell desktop.local about it.

    git remote add origin ssh://laptop.local/~src/my-repo
    git branch --set-upstream master origin/master
    

    That should do you.

    Now, you can set up a scenario where you push to a repo, but it will work differently. You won’t be able to work in that repo; it’s purely for pushing and pulling, not committing. That’s a bit like github except your stuff doesn’t have to be public, and is instead secured behind ssh on your private machine.

     
  9. Holy mother of crap. I forget this one all the time and it drives me up a wall.

    This works in bash and zsh:

    declare -f
    

    Or

    typeset -f
    

    This will list all the functions defined in your shell, including the source.

     
  10. watch

    Sometimes you want to monitor what changes are happening to a file or the output of a command. The poor man’s way to do it is via something like

    while true; do echo foo; sleep 1; done
    

    Fortunately, there exists a simpler way:

    watch -n 1 'echo foo'
    

    It appears to ship with Linux, but not Mac OS 10.7. Homebrew has it, however.