2012-08-08

2008-11-26

Using python's sorted method with key functions.

Python's "sorted" method allows you to pass a key to it for returning the value to compare by. I'm surprised I didn't end up using this before. I had read about it in the documentation, but hadn't put it to use yet. This is best shown through a short example.

I have a list of dictionaries, the values of which are being displayed to the user in a tabular format. Some of these dictionaries have a key, 'money', which contains a string representation of a monetary value, perhaps with a currency symbol, perhaps not.

We want to display the values from the dictionaries that have the 'money' key at the top of the results list, sorted from lowest to highest. This is not a very difficult problem to begin with, but it's trivially easy in python. The solution looks something like this:



def get_results():
def by_monetary(item):
if item.has_key('money'):
value = to_decimal(item['money'])
return value
else:
return 0

...

return sorted(results, key=by_monetary)

This is going to come in really handy in the future, especially in cases where I have no control over where my data is coming from, or what format it is in. You probably already knew about this, but if you didn't, I hope you find it useful

2008-11-20

Typealyzer

I found out about typealyzer from here. It's a pretty interesting idea, although it seemed to work too fast.

It classified me as an INTP, which is what I tend to get classified as.

2008-11-19

On anarcho-primitivism and the capabilities of humanity.

If you are not familiar with anarcho-primitivism, it is a subset of anarchism which rejects civilization as a whole. People who self-label as anarcho-primitivists tend to also reject technology, "science", and the scientific method - which the view as little more than tools for the compartmentalization of reality into small, easily understandable parts.

I am not going to present an entire overview of anarcho-primitivist philosophy here. Chances are that if you are still reading this post, that you are at least marginally familiar with it. If you are not, some notable anarcho-primitivist authors include Derrick Jensen (whom I have much respect for, although I disagree with quite a few of his conclusions), John Zerzan, Daniel Quinn (whom I also have much respect for, his writing is beautiful and explores some profound concepts and David Watson. Bob Black, while not an anarcho-primitivist himself, is often cited as influential - my views have been influenced quite a bit by his "The Abolition of Work". Some publications that are prominent within anarcho-primitivist circles are Green Anarchy, Species Traitor (the species traitor archives are down the page a bit on that link, the page also includes links to a lot of anarcho-primitivist writing), and Fifth Estate.

There is definitely some interesting reading there. I have admittedly not read through all of it. I would recommend Derrick Jensen's Endgame - not as a complete overview of anarcho-primitivist thought, but as an interesting and thought-provoking read at the very least. Jensen is a very good author, and although it has been a while since I read through the two Endgame books, they definitely made me think, and question my mindset - good stuff.

Ishmael by Daniel Quinn is also a good read - it's presented in an innovative fashion, and his interpretation of the mythology in the Bible's Genesis is quite interesting.

Anyhow, enough overview.

I do not agree with anarcho-primitivist philosophy, although some of the points made by (for example) Jensen and Quinn are valid. The main problems that I have with it have to do with the rejection of technology, science, and civilization as a whole. I am not going to present rebuttal of anarcho-primitivism through direct analysis of the philosophy - this has already been done. Notably, Noam Chomsky (among many others) has pointed out that in order to achieve anarcho-primitivists goals, a mass reduction in human population would be necessary, and that a lot of anarcho-primitivists are, for all intents and purposes, advocating an almost total speciescide (for lack of a better term). The anarcho-primitivists I have talked to have been somewhat reluctant to address this (until I make it clear that I am not interested in calling them monsters for their views), but generally admit that they think that such an event is either very likely, or desirable for the good of all living things.

What I am going to present is an explanation of how I (and many others) view humanity, civilization, technology, etc - with a focus on parts that I feel that are particularly important (and valid), and that anarcho-primitivism is ignoring - specifically parts that deal with what it means to be human.

I feel as though anarcho-primitivism is missing some very large, unavoidable facts about the nature of humans, what we collectively know right now, the extent of what we are able to accomplish, the uniqueness of this point in humanity's history, and the nature of civilization - and although I am glad to see criticism of modern society (which is very much in need of criticism), I almost think that they are doing something close to the opposite of "missing the forest for the trees" - something like "missing the trees for a very specific forest". But I digress. What follows will not all be directly related to anarcho-primitivism, but I feel that it is necessary in order to understand where I'm coming from - it will hopefully become obvious why I view the philosophy as incorrect, or at least as incomplete. I would humbly request that (if you are reading this from the perspective of an anarcho-primitivist, or just for the purposes of getting a bit of brain exercise) you read it in full before rebutting it. It's long, but I hope it's worth the effort.

Standard disclaimers apply - I do not mean to come off as being dismissive, condescending, or arrogant, although I realize that I probably do at times. I am far from being a master of textual communication (or communication otherwise). I do not hold my opinions as being concrete truths - what I consider to be "correct" in matters like these is not set in stone. I believe in questioning everything that you can, and in examining things from as many angles as possible in order to enable yourself to have a complete, valid opinion on something, and do not believe that one should hold their opinions as being infallible. What I am expressing is something that I have put a lot of thought into, but I am human - and I myself may be missing some large, obvious points. I welcome rebuttals and criticism of my viewpoints, and have modified my opinions and mindset because of such criticism many times in the past. Everything that I write here should probably be read as if each sentence was prefixed with "In my opinion," - I would think that should be the default mode of reading political pieces, especially on the internet, but too often people forget about it. There may be areas where I am not thorough enough in defining what I am talking about. If clarifications are needed, I'm sure that they will be made in the comments, and at the bottom of this post.

We humans can be quite stubborn, and I think it is a very sad thing that more people are not willing to question how and what they think. Furthermore, I find the state of a lot of anarchist writing to be largely preaching to the choir, and that saddens me - but that's another topic. Suffice it to say that I think that a lot of writers would benefit from giving Politics and the English Language a good read (wiki link).

Now on to the "fun" stuff.

Like every other living being, humans are unique within nature. Every thing that we are capable of observing, as far as we can tell, has some unique things that make it it. Every living thing, as far as we can tell, has some unique traits that enable its survival - some things share traits with others, but every one has something unique about it. Over the time that we have been examining the world around us (which is probably ever since our brains developed to the point that allowed introspection, perhaps even before that if Bicameralism turns out to be correct), we have been able to identify tons of different traits that enable things to survive. We've noticed enough to create entire classifications for types of matter, to types of life, to types of types of life, and so on. Plants, while considered to be alive, are different than animals, and are able to survive because of different things.

It seems probable that the cause of these different traits are physical laws that deal with what and how things are able to combine, these laws (if we knew what they were in entirety) would be expressions of properties of matter, or of the "building blocks" of matter. Evolution. Things are able to combine in a myriad of ways - over time, they have combined in a myriad of ways - and the combinations that led to stable things stuck. In the case of non-living things, this would be part of the reason that we have planets and stars. In the case of living things, the combinations happen at a bit higher of an abstraction (e.g. instead of different types of elements combining into a molecule, you have DNA/chromosome/other biological mutations that allow for beaks, that are the result of some combination. This is a woefully inadequate description, but I hope the point is clear), and if a combination (more specifically in these cases a mutation) does not cause the death of an individual in a species, it tends to stick around and propagate itself given enough time. Over enough time, species diverge from one another - a point gets reached where there are enough individuals with a specific set of mutations within one species, that they can be considered their own species. More often than not, it seems like the things that differentiate one species from another within the different branches of what we call the "Animal Kingdom", are directly related to survival. I doubt that they are all the time, but it makes sense that they would be most of the time - a random mutation that increases an individuals capability for survival is more likely to stick around and propagate through their offspring than a random mutation that decreases an individuals capability for survival, or one that is neutral to their capability of survival.

As a loose example, plants have the ability to pretty directly harvest the Sun's energy, and to pull nutrients out of the soil; birds have wings and build nests and lay eggs; mammals can have sex and reproduce and have fur for warmth and so on. (Yes, there are exceptions to these "rules").

Humans, as far as I can tell, have two very important traits that allow us to survive. We can manipulate physical objects rather well (that is, we have thumbs and nimble hands), and we are capable of reasoning on a very high level of abstraction. At the base level, this is what enables humans to survive, regardless of the form of organization (or lack thereof) that humans live in.

These abilities are part of what makes us the human species. Without them, we would not be human - and, if we had one of them and not the other, or neither of them, we would not be able to survive in the same manner that we do now. There are no (again, as far as I know) humans that survive without utilizing them.

Now, let's assume that we can put ourselves in the shoes of the hunter-gatherer people of old. Over time, members of our group, however small, are going to make little discoveries through the application of our "survival traits". They might find out that certain types of plants are not good to eat, or they might figure out how to coax fire out of the aether, or they might figure out how to sharpen sticks, or make bows, or boats, or whatever else. They might discover these things on accident, or they might discover them through guessing and trying.

If a discovery is kept and improved upon, what is happening is that new technology is being invented or used, and that people are "practicing science". Everything that is made by humans that is meant to serve a practical purpose is technology. The hammer, the lever, the wheel are all technology. A sharpened stick is technology. Everything that is refined through experimentation can be viewed as science. Figuring out which rocks will sharpen which is science. Figuring out what plants are good to eat is science. Figuring out what animals are good to hunt, and how to hunt them is science.

Each thing that is discovered, each new combination of physical items or ideas, leads to the possibility of new discoveries and combinations. All of our current technology - the computer, cars, electricity, and so on - is based on abstractions and new combinations of previous knowledge.

That is not to say that technological progress is an inherently good thing. There are serious complications with how we (for instance) create energy right now. Technological progress can not be an inherently bad thing either. There is no evil in electricity - it's all over nature. There is no evil in a hammer, or in a walking stick.

I would argue though, that while technological progress is not inherently good or evil, that it is inherent. It is a direct side effect of what makes us human - of what enables us to survive.

Let us think for a moment about what would happen if the anarcho-primitivists goals were accomplished for all of humanity. Let's say that we do face a giant die-off of humanity - we blow ourselves up with war, or get smacked by an asteroid, or ruin the planet to the point that it cannot sustain human life anymore on the levels that it is currently. A few small packs of humans survive - hell, for the sake of this argument, we can assume that everyone who survives considers themselves to be an anarcho-primitivist. Let's assume that they're prepared for the event, and are capable of surviving indefinitely. What would happen?

After some period of time, say a few hundred generations, there will be no real memory of the way that we live today, except perhaps as mythology. Languages will either be entirely different, or just starting to re-emerge depending on how the original survivor humans felt about verbal "symbolic communication". The reasons that anarcho-primitivists have now for advocating what they do will no longer exist - humans are largely small groups of individuals, living in a hunter-gatherer type of situation, presumably in pure harmony with nature and with no division of labor, and all that stuff.

Unless we have, at that point, turned into an entirely different species, the series of discoveries that led to modern civilization would be rediscovered as humans play around with things. Assuming that there is not a permanent taboo against utilizing new discoveries to make survival easier, these discoveries will be used. Eventually, at least one group of people will discover agriculture again, and will start farming and using their capabilities for physical manipulation and abstract reasoning to influence the form of Nature around them for the purposes of making survival easier.

This wouldn't happen in one day, or one year, or one century. Possibly not even in one millennium, but it would happen eventually. By the time humans were discovering enough that their groups populations would be expanding, the situation would be rather interesting. We would have tribal groups of people, who had only ever been exposed to their elders, and perhaps another tribe here and there. Tribes that were separated by a great physical distance would have developed differing cultures, languages, and habits. Tribes would probably have a pretty large variance in how they lived, and in what tools and discoveries they utilized to enable their living. A lot of this would be dependent on physical area - fishing poles are not a great survival tool compared to spears if you're nowhere near an ocean or large body of water, and just the opposite if you're nowhere near the plains.


If one of these groups figured out how to make a boat, and travelled farther than anyone their tribe was aware of anyone travelling before, they would eventually run into a group of other humans that was different from them in every way except for base physical appearance, almost in the same way that we are different than other apes in every way except for base physical appearance. How would these two groups of people even recognize each other as people? Sure, they both make sounds - but lots of animals make sounds. Sure, they've both got two legs and two arms, but other animals look like that as well. There would be no means of communication, probably no observable similarities, and more likely than not they would not recognize each other as the same species - how could they?

If one group, or multiple groups, had figured out agriculture, and had started to use up more land for the support of their group, and they ran into another, totally alien, group of people - they probably wouldn't even recognize them as people.

What I'm trying to get at is that if we did see a mass reduction in the population of humanity, and humans ended up having the hunter-gatherer lifestyle as the only really viable means (by choice or otherwise) for survival, that given enough time we would see a repeat of history up to this point - the same discoveries would be made eventually, the same growth of religion and mythologies and cultures, the same clash of cultures and warring, the same discovery of electricity, etc. We would end up doing all the things that anarcho-primitivists rail against in modern society again, and would quite possibly end up in the same situation that we have now.

Now, we are currently in a very interesting time. It has become apparent in the last three or four decades that a lot of how we live is harmful to other species, and harmful to their ecosystems, harmful to everything - eventually (and in many cases, right now) including us. The most interesting thing about this, in my opinion, is that we weren't even capable of knowing that we were screwing things up so badly until the last three or four decades. There's been warning signs, and some speculation for probably a few hundred years - but no real connection linking our actions with the "warning signs", and no data to back up the speculation. This is an incredibly short amount of time in human history.

I suppose that the critical response to those last few sentences would be that civilization has only existed for a very short time in human history. That's true, however I do not think that it is because humans weren't making "new technology", or because they had no "scientism" to speak of, or because they weren't attempting to understand the world around them - I think that it is for an entirely different reason, namely that of what is discoverable with a certain bank of knowledge.

For each new thing that a person, or group of people discovers, there is generally ways to use that new knowledge to discover a bunch more new things. Some discoveries open the door to a lot of new knowledge and applications, some do not. If you track humanities capability for communication, and humanities "body of knowledge" throughout history, you'll see that it follows an exponential curve very closely. The ability for humans to communicate across differing cultures and languages allows groups of people to share their discoveries, and over time this starts to snowball. You can't exactly discover the practical uses of fossil fuel without having any idea what a knife is - some discoveries primarily allow us to further investigate the nature of the world around us. Having the insight to start planting food is significantly easier than figuring out fire. Discovering electricity is significantly easier than figuring out that planting food can make your life easier - this is, of course, assuming that you have never seen anything like that before. By the time we figured out electricity, we had a huge bank of data about the world around us to come up with theories from, and there were still tons of what we would now call "crackpot" theories about the world.

It is, at least partially, the difficulty of discovering the prerequisites to agriculture, and then figuring out agriculture that caused humanity to live without it for so long. Possibly, the discovery was entirely chance.

Back to what we know now - again, it is practically undeniable that our current means of energy production is harmful - not only to us, but to everything around us. We've only been aware of this for perhaps twice as long as I've been alive - and it's only become apparent to the average person in the last few years, as far as I can tell. We also are now capable of producing energy in a way that does not just rape the Earth with no regards to other forms of life. We know (for example), that energy is not used up, just transferred - and we have a giant yellow source of energy blasting the Earth every day in the form of the sun. We know how to utilize the wind and the water to create energy, etc.

Furthermore, we can communicate globally instantaneously, and we are more than capable of recognizing other groups of humans as humans, regardless of their culture. Some groups of humans even have a concept of rights that living beings have just for being there. Younger generations of people have cultures that are based less on their immediate surroundings, and more on the other members of their generation - globally. We can transport things globally with ease, we can set up organizations, temporary or not, and impromptu or not, to deal with anything.

We (and here when I say we, I mean humanity as a whole) have the ability to have our cake and eat it too. From the ability standpoint, we are entirely capable of providing for every single person on this planet if they want it. We have plenty of ways already of providing energy, homes, etc that are not based on methods that push us closer to ecological disaster. We have tons of people finding out new ways to do things that have been, for a long time now, accomplished through methods that are not sustainable. We are, for the first time in recorded history, technically capable of having civilization (as in, not being hunter-gatherers) without violent coercion. Our technology, and "science" is the enabler of that.

What is holding us back is not that we have civilization, and is not technology, it is not science - these are side effects of what make us human. What is holding us back are economic structures, power structures, and cultures that came to rise before we were capable of large-scale organization and communication without them (which are also, partially side effects of what makes us human, but they are not required for human survival. This hints somewhat at Marx's theories about the progression of human forms of societal organization). This is not to say that I think that we could just jump into an anarchist utopia, but we are not held back by how capable we are of communicating, or how capable we are of getting places anymore - and really, communication and transportation are the base things that are needed for humans to function in groups larger than one anyhow, whether it be in groups of six or six-billion - the main difference there is scale.

Emacs Tidbit #3

For those of you who have blogs hosted on blogger (like this one), and use emacs, I'm sure you've run into this - writing a post in emacs with auto-fill mode on leads to an ungodly amount of reformatting needed because of the insertion of hard line breaks.

If you weren't already aware of it,

longlines-mode
solves this problem.


longlines-mode is an interactive compiled Lisp function in `longlines.el'.
(longlines-mode &optional arg)

Toggle Long Lines mode.
In Long Lines mode, long lines are wrapped if they extend beyond
`fill-column'. The soft newlines used for line wrapping will not
show up when the text is yanked or saved to disk.

If the variable `longlines-auto-wrap' is non-nil, lines are automatically
wrapped whenever the buffer is changed. You can always call
`fill-paragraph' to fill individual paragraphs.

If the variable `longlines-show-hard-newlines' is non-nil, hard newlines
are indicated with a symbol.

This, in combination with the ability to post stuff to blogger through email, makes using emacs with your blogger account a bit easier.

One more thing - never, ever, while in the blogger "new post" editor, hit Ctrl-P, unless you really want to publish. It can be awfully frustrating.

2008-11-12

A little userscript for reddit.

If you read this and reddit, and the "my reddits" dropdown in the top-bar was too big for you (it scrolled past the end of my screen), here's a little userscript to make it a bit nicer.

You'll need to be using greasemonkey and firefox, of course.

2008-11-11

Emacs Tidbit #2

Ever make a throwaway buffer in some random directory that you didn't intend on saving, but then ended up wanting to save - but not in that directory?
M-x set-visited-file-name

Is what you want.

From the docs:

set-visited-file-name is an interactive compiled Lisp function in `files.el'.
(set-visited-file-name filename &optional no-query along-with-file)

Change name of file visited in current buffer to filename.
This also renames the buffer to correspond to the new file.
The next time the buffer is saved it will go in the newly specified file.
filename nil or an empty string means mark buffer as not visiting any file.
Remember to delete the initial contents of the minibuffer
if you wish to pass an empty string as the argument.

The optional second argument no-query, if non-nil, inhibits asking for
confirmation in the case where another buffer is already visiting filename.

The optional third argument along-with-file, if non-nil, means that
the old visited file has been renamed to the new name filename.


This just saved me a few commands, hope you find it useful, fellow emacs-learners. Don't forget to follow the link to files.el and read the code for the function.

2008-11-09

Creating dynamic client-side image maps with javascript/jQuery

I recently ran into an odd problem - I needed a dynamic client-side image map. Dynamic as in "the areas will be in the right spot regardless of how large the image is client-side".

I've been working on a site for the house I live in, which creates a lot of various forms of media. We decided to offload video hosting for videos we make to youtube, since we can't afford hosting costs for streaming video at this point in time. Youtube offers a pretty neat chromeless player that you can control through javascript, and the guy who does most of our design work whipped up a pretty nice-looking television with buttons image to use as the player. Given that he's not exactly very tech-savvy, it seemed like using an image map (which I haven't really heard much about for years) would be an appropriate way to make the player

Now, the problem with image maps is that since the coords attribute of the area tags is based off of pixels, you can't easily use an image that you don't know what dimensions it will end up being on the clients browser.

This is one of the situations I've run into where the solution probably would have been obvious if I had gone to college, but I enjoy solving problems anyhow - I got out the graph paper and pencil, drew a few boxes with points in them, and it became apparent that if I represented the coords for the areas as ((x/image_width)/(y/image_height)) instead of just (x/y), that it would be pretty easy to modify for any image size.

Javascript to the rescue:


//the height and width of the image on the server
CircleButton.prototype.base_width = 800;
CircleButton.prototype.base_height = 600;

//stores information about where buttons are as the ratio of
//(x/base_width)/(y/base_height)
function CircleButton(x, y, diameter, func) {
//express x,y,diameter as ratios instead of pixels.
this.x = x / this.base_width;
this.y = y / this.base_height;
this.diameter = diameter * ((this.base_width / this.base_height)
/ (this.x / this.y));
this.func = func;

this.adjust_coords = function(width, height) {
//coordinates for an image of (width, height) on the client
return [parseInt(width * this.x),
parseInt(height * this.y),
parseInt(this.diameter)];

}

this.tag_string = function(width, height) {
var coords = this.adjust_coords(width, height);
return '<area shape="circle" coords="' + coords.join() + '" href="javascript:' + this.func + '"/>';
}
}


This only works for circular buttons, but would be easy to modify for any of the other shape options (I only need circular buttons at the moment).

To use this on a page, you need to

  1. Initialize some buttons with the coords that they would have in the image on the server
  2. Find the image's width and height on the client
  3. Generate the area tags with the previously found width and height
Like so:

function setup_map() {
var base_buttons = [new CircleButton(525, 127, 15, 'ytplayer.playVideo();'),
new CircleButton(584, 153, 15, 'ytplayer.pauseVideo();')];

//size of the map image on the client
var map_width = $('#my-image').width();
var map_height = $('#my-image').height();

$('#my-map').empty();
$('#my-image').removeAttr('usemap');

var the_map = document.getElementById('my-map');

for (var i = 0; i < base_buttons.length; i++) {
(function(b) {
s = base_buttons[b].tag_string(map_width, map_height);
//we have to append to the innerHTML like this because of the way
//jQuery appends stuff
the_map.innerHTML += s;
})(i);
}

$('#my-image').attr('usemap','#my-map');
}


Assuming markup like this:


<div id="video" class="content">

<map id="my-map" name="my-map">
</map>

<img id="my-image" src="images/my-image.png"/>
<object width="48%" height="52%" id="video-player"
allowscriptaccess="always"
data="http://youtube.com/apiplayer?enablejsapi=1&playerapiid=ytplayer">
</object>
</div>


The calls to empty and removeAttr can probably be removed if, unlike me, your image and map are not residing in a jQuery tab. We have to append to the innerHTML of the map because of a quirk with how jQuery does appends - as far as I can tell, it appends to an empty div - and area tags aren't valid inside a div, so the browser ends up not displaying them. That was a bit frustrating to figure out.

Also, if you're like me, and the image and map are residing in a jQuery tab, you'll want to bind setup_map to the click attribute of the tab with a timeout like so:


$("a[href='#my-tab']").click(function() {
//we have to wait a little bit for the player
//image to load, otherwise it reports a width of 0
setTimeout('setup_map()', 100);
});


Just about any amount of delay should work, 100 milliseconds is unnoticeable, 500 would work if you wanted to be really safe

Image maps don't get much action these days, probably because when they were made, there wasn't much of a use for them - and now, there are other ways to accomplish what they were generally used for. I do feel that they were quite appropriate for making a custom player with youtubes chromeless player, and it was fairly straightforward to allow for differing image sizes on the client side. Hooray!