Archive for the ‘compositions’ Category

Fast, recursion depth safe, list flattening for Python

Monday, February 28th, 2011

I’ve been using Python at work a lot recently (yeah, I know, took me long enough) and find­ing it to be fan­tas­ti­cally use­ful for all sorts of things.

Anyway, I found myself need­ing a mech­a­nism for flat­ten­ing nested con­tainer objects and a quick google search led me to a Right Foot In arti­cle on var­i­ous flat­ten algo­rithms. Seeing as I needed some­thing to work on con­tain­ers that aren’t tuples or lists and I’m not very good at leav­ing well enough alone, I went about try­ing to come up with my own method.

Here’s what I’ve got so far:

def flatten_to_list(something):
    if not hasattr(something, '__iter__'):
        return [something]
    retlist = list(something)
    i = 0
    while i < len(retlist):
        while hasattr(retlist[i], '__iter__'):
            if not retlist[i]:
                retlist.pop(i)
                i -= 1
            else:
                retlist.insert(i, retlist[i].pop(0))
        i += 1
    return retlist

In at least one test case, my method appears to be faster than the best method in the Right Foot In arti­cle. I’m still wrestling with name­space issues relat­ing to the use of timeit but I’ll try to get some actual speed num­bers and test cases up later.

UPDATE: I am an idiot. My code was faster because my test case exploited a degen­er­ate case for the algo­rithm; the code below, which is much closer to the orig­i­nal at Right Foot In, is faster.

def flatten_to_list(something):
    if not hasattr(something, '__iter__'):
        return [something]
    retlist = list(something)
    i = 0
    while i < len(retlist):
        while hasattr(retlist[i], '__iter__'):
            if not retlist[i]:
                retlist.pop(i)
                i -= 1
            else:
                retlist[i:i + 1] = retlist[i]
        i += 1
    return retlist

Will O’ The Wisp (cocktail recipe)

Sunday, January 16th, 2011

Continuing my ear­lier work in the realm of mixol­ogy, I would like to present my sec­ond cock­tail inven­tion. The inven­tion of this cock­tail is the result of play­ing around with gin, Chartreuse, St. Germain, and var­i­ous other liqueurs in an attempt to make some­thing tasty. This cock­tail earns its name, Will O’ The Wisp, from its faint, ghostly, green color.

Ingredients:

  • 3/4 oz Green Chartreuse
  • 3/4 oz St. Germain
  • 3/4 oz White Vermouth
  • 1 dash Lavender Bitters
  • 3 oz Dry Gin
  • 1/4 oz Herbsaint or Absinth

Recipe:

  1. Pour the Chartreuse, St. Germain, Vermouth, Bitters, and Gin into a cock­tail shaker
  2. Pour the Herbsaint/Absinth into a chilled cock­tail glass
  3. Swirl the cock­tail glass, coat­ing the sides, and pour­ing off any excess Herbsaint/Absinth
  4. Add ice to the cock­tail shaker and shake
  5. Pour shaken cock­tail into the pre­pared glass

Notes:
The Herbsaint/Absinth can be skipped, though I don’t rec­om­mend it. The Lavender Bitters are also optional but very highly rec­om­mended. I would rec­om­mend stir­ring this cock­tail (instead of shak­ing) as the result­ing bev­er­age has a very dif­fer­ent appearance.

Ironhide (cocktail recipe)

Thursday, October 28th, 2010

Some num­ber of weeks ago, hav­ing dis­cov­ered Maraschino liqueur, I con­cocted a vari­a­tion on my beloved Manhattan. I have been mean­ing to write up the recipe here for a while but had been stuck on com­ing up with a name; I needed a name befit­ting a strong whiskey cock­tail with con­nec­tions to myself and the color red. Having thought for quite a while and come up with a bunch of names that were already taken by other cock­tails, I set­tled on nam­ing the drink after the griz­zled, old Autobot, Ironhide. So, with­out fur­ther ado:

Ironhide recipe:

  • 3 oz Bourbon whiskey
  • 3/4 oz Maraschino
  • 1/4 oz grenadine
  • 2 dashes Angostura bitters
  • 1 Maraschino cherry

Serve shaken or stirred with ice in a cock­tail glass; gar­nish with the Maraschino cherry.

Notes:

I have mostly been drink­ing these on the rocks, which is an accept­able but infe­rior vari­a­tion. Being a Manhattan-like cock­tail, up-high is really the cor­rect way to serve an Ironhide.

Corn syrup based grena­dine or “maraschino” cher­ries will ruin the fla­vors imparted by the bit­ters and Maraschino; you will make a bet­ter cock­tail by skip­ping the grena­dine and cherry than using cheap, fake ones.

Painting: Round 1

Friday, September 10th, 2010

Having been inspired by an artist friend of mine, I have taken up paint­ing as a hobby. I am, as yet, still hor­ri­bly out of prac­tice at art, lack­ing skill in the pro­duc­tion of art, and incred­i­bly slow, but I am hav­ing a won­der­ful time. My life is really quite busy so it’s hard to find time but I’ve been try­ing to carve out time as I can, and paint­ing is a far more ful­fill­ing pas­time than watch­ing tele­vi­sion or similar.

Anyway, with­out fur­ther ado, here are a few of the pieces that I’ve put together so far:


Unsticking Stuck LCD Pixels: A Script

Tuesday, December 8th, 2009

As I just recently sug­gested I might do, I have made a script for auto­mat­i­cally gen­er­at­ing color cycling ani­ma­tions of arbi­trary sizes and speeds.

The script can be used at: http://blog.gwax.com/files/rgbanim.php

The script accepts the para­me­ters height, width, and delay, so it can also be used in more inter­est­ing ways. Feel free to play around (if you crash it, let me know).

Ex.: http://blog.gwax.com/files/rgbanim.php?width=100&height=100&delay=7

Or, bet­ter still:

- Width - Height - Delay

For the curi­ous amongst you, the rel­a­tively sim­ple code follows:

< ?php
if(isset($_GET['delay']) && is_numeric($_GET['delay']))
	$delay = max((int)$_GET['delay'], 1);
else
	$delay = 10;
 
if(isset($_GET['width']) && is_numeric($_GET['width']))
	$width = max(min((int)$_GET['width'], 1920), 1);
else
	$width = 320;
 
if(isset($_GET['height']) && is_numeric($_GET['height']))
	$height = max(min((int)$_GET['height'], 1080), 1);
else
	$height = 240;
 
$out = new Imagick();
$out->newImage($width, $height, '#FF0000', 'gif');
$out->setImageDelay($delay);
$out->newImage($width, $height, '#00FF00', 'gif');
$out->setImageDelay($delay);
$out->newImage($width, $height, '#0000FF', 'gif');
$out->setImageDelay($delay);
 
header('Content-type: image/gif');
echo $out->getImagesBlob();
?>

A post-apocalyptic beginning

Friday, September 25th, 2009

I just came up with what I think is a good first line for a book and a not quite so good sec­ond line:

“Our par­ents used to ask each other if they remem­ber where they were when a man first walked on the moon or when the Berlin Wall fell; we used to ask each other where we were when the World Trade Center fell; now, if we can find any­one to ask, we ask where we were when the world ended.”

“I remem­ber where I was when the world ended; I was in a McDonald’s, and I didn’t even like McDonald’s.”

Griffon Sketch

Friday, August 21st, 2009

A sketch of a griffin standing in watch over a savanna

A sketch of a grif­fin stand­ing in watch over a savanna

This is fur­ther art sketched on my N810 while idle in life. I’ve always found griffins (which I appar­ently can’t always spell cor­rectly) to be fan­tas­ti­cally cool myth­i­cal beasts, right up there with hydras, I’d say. This image is of a grif­fin up on high observ­ing its savanna hunt­ing grounds. I kind of feel like griffins, were they to exist, would have been native to Africa; you could say that my land­scape in this image has African savanna influ­ences (ish).

SkyzCastle Sketch

Friday, August 21st, 2009

Sketch of a mountain top castle under siege from flying machines

Sketch of a moun­tain top cas­tle under siege from fly­ing machines

First off, I have to admit that I don’t know why I decided to give this file a file­name of “SkyzCastle” except that it must have seemed appro­pri­ate at the time that I made it. I’m about a month behind on post­ing images so I really can’t say what my moti­va­tions were at the time. This sketch is, I would say, ok at best, but I am try­ing to adopt a pol­icy of post­ing my art here. In case it isn’t clear, this is a cas­tle on a moun­tain top that is under siege/assault from bal­loons and ornithopters.

Lighthouse Sketch

Friday, August 21st, 2009

Sketch of a landscape with lighthouse made on my N810

Sketch of a land­scape with light­house made on my N810

Another image from the depart­ment of sketches that I’ve made on my N810 while pass­ing time *cough*in meetings*cough*. This, as should likely be obvi­ous, is a sketch of a light­house on a cliff above some rocky waters near some rel­a­tively peace­ful ocean waters. Enjoy.

Boat Sketch

Monday, July 6th, 2009

Sketch of a small sailboat made  on my N810

Sketch of a small sail­boat made on my N810

This is a small sketch of a per­son sail­ing a small boat that I made on my N810’s touch­screen while pass­ing some time recently. I’m rather pleased by this lit­tle sketch so I thought that I’d toss it up here.

The Uncyclopedia game

Sunday, November 13th, 2005

In my free time, I’ve been play­ing with the won­der­ful Uncyclopedia a whole bunch. For those poor souls out there that don’t know about Uncyclopedia, it’s basi­cally Wikipedia but wrong; that and it’s the twelfth won­der of the world. I’ve con­tributed a lot to a few arti­cles and a bit to a lot of articles.

Some arti­cles which I’ve con­tributed heav­ily to include (but are not lim­ited to):

And a cou­ple of arti­cles that I really like but haven’t con­tributed much, if any, to:

In con­clu­sion, Uncyclopedia is awe­some and every­one should use it more and con­tribute to it a bunch.

Nonsense as a passtime

Sunday, October 17th, 2004

I’ve come up with a won­der­ful new passtime for when I to get bored in class or oth­er­wise dur­ing the day, writ­ing non­sense. It’s a ton of fun to write and then it’s kind of fun to read, although it lacks more than very short range coher­ence. Here is a short excerpt:

…to soon coon moon toon buf­foon inter­pre­tive dance romance chance clance manse ran­cid meat spon­ta­neous gen­er­a­tion swirl twirl curl diver­gence charge up a hill at the enemy who has entrenched in the supe­rior posi­tion no mat­ter we have more troops and your dog is expend­able or per­haps your dog is expand­able mandible click-clack giant ant fly­ing into the man­tis spongi­form boast defeat in the realm of the man­i­fest des­tiny col­lapse from sense to gib­ber­ing like four golf carts which we eat to cut down on excess pro­tein put the spoon in your mouth…

Maybe I’ll put more up later, but for now it serves to illus­trate a point. Of course, feel free to call it crap, it very well may be, but I enjoy the act of writ­ing it. It’s mainly just word play and ran­dom word asso­ci­a­tion mixed with a bit of stream of consciousness.

Things I found on my hard drive

Saturday, October 2nd, 2004

I was root­ing around my hard dri­ves look­ing for amus­ing things to play with and I came across a cou­ple of things I wrote in the past. One night I was dri­ving around at 3 AM and then wrote me up a short lit­tle thing that I called At 3 AM. Another one I found was some­thing that I wrote about hav­ing a Bad Day where one or two bad things hap­pened to me and then I exag­ger­ated a bunch of other stuff on top of it. The third, and last, thing that I found was some­thing that I started writ­ing which was meant to be an exper­i­ment in writ­ing with a lot of side phrases in Parentheses.

I make no asser­tions about the qual­ity of these com­po­si­tions, but the first two are kind of fun and the third is kind of dif­fer­ent. So yeah, stuff I wrote; enjoy.

Taco Haiku

Saturday, January 24th, 2004

Suddenly it came
I want to have a taco
With guacamole

“1… 2… 4!”

Thursday, January 15th, 2004

For those of you that get the ref­er­ence, I appol­o­gize; to the rest of you, I also appol­o­gize. This entry exists for two rea­sons, the first is to put “1… 2… 4!” some­where else on the inter­net (it has already shown up in the web­comics Underverse and Ketchup Popsicle) and the sec­ond is to give you all a good exam­ple of how bro­ken my sense of humor really is.

So here it goes: “1… 2… 4!” is my favorite joke. There is no more to the joke; what is in quo­ta­tion, is all that there is. If you don’t get the it, your only real option is to think about it until you fig­ure out what makes it so funny or just give it up as not funny. There is no addi­tional con­text and it is only an inside joke inso­far as a few of my friends also find it humor­ous. Chances are pretty good that you don’t get it, won’t get and will just end up irri­tated with me. It’s just kind of sur­re­al­is­tic that way.

And yes, I am seri­ous, this is my favorite joke.