Mocking Python Files

Monday, 29 June 2009

Just a quick note on testing python; I needed to test a method the reads in from a file. To do that I could either created a temp file or use stringio. Stringio is easier and quicker so I used that, here's how:


def mockfile(text):
return StringIO.StringIO(text)

class Tester1(unittest.TestCase):
def setUp(self):
self.filehandle = mockfile("""
gen , G1 , 101 , 450 , 50 , U20""")
self.components = read(self.filehandle)

def tearDown(self):
self.filehandle.close()
self.filehandle = None
self.components = None

def test_name(self):
self.assertEqual(set(self.components.keys()),
set("G1"))


class Tester2(unittest.TestCase):
def test_a(self):
components = read(mockfile(""))
self.assertEqual(len(components),0)


Hopefully I this is the first in a series of posts on practical TDD. Watch this space

Test Driven Development (pros and cons)

Tuesday, 23 June 2009

I am a fan of TDD (Test Driven Development), for those who don't know TDD here is the places to read about it:

Test Driven Development by Kent Beck

The Three Rules Of TDD by UncleBob
  1. You are not allowed to write any production code unless it is to make a failing unit test pass.
  2. You are not allowed to write any more of a unit test than is sufficient to fail; and compilation failures are failures.
  3. You are not allowed to write any more production code than is sufficient to pass the one failing unit test.
Three Monkeys of Test-Driven Development by Ashutosh Nilkanth



You basically make sure everything works before doing anything else; and you only do one thing at a time. It has the great advantage that you can change whatever you want in your code and you can be sure it is as good as before. This allows you to aggressively refactor. Being able to refactor without fear of breaking something should leave you code easier to maintain and easier to add new features. This will hopefully overcome the time it has taken to write all those tests in the first place.

A few things make TDD more difficult:
  • Randomness (Non-Deterministic Methods)
  • User Interfaces
  • External Code (including Databases)
It is for this reason that I find it difficult to use TDD for all my coding. Now those who support it say that you can mock up facade classes for the external code including random number generation meaning you can test only the bits you want.

My other problem it it seems to break my flow of thinking. I want to type out a load of different ways of doing things and see which one I like better.



I think TDD has it's place but there is a need to do another kind of programming. Exploratory programming; one where you do not know what you want to end up with. You just chuck out a load of code and see how it runs. This is great if you have a nice fast development cycle, i.e. interpreted high level language. I would say that it get exponentially more complicated to do exploratory programming as the total number of lines of code increases. This gives a natural limit to the kind of things that can be accomplished with this technique. I believe it is best suited for tracers and prototypes (see The Pragmatic Programmer). In this case you quickly see the shape of a small part of your program and begin to see what things (modules, classes) you need in the full version. Once you have got a section of code working you can write it properly (with TDD) meaning you live by the rules of TDD and throw one away (Mythical Man Month).

Various Musings on Time Travel

Tuesday, 16 June 2009

Here is a old (from about 2003) e-mail I write. Enjoy; It would be good to hear some comments on it. My opinions have changed a bit now but I stand by some of it.



Hello,

I have just read your webpage ‘Various Musings on Time Travel’ (admittedly only once – I feel something like that should be read a few times). I thought that your analogy of a flip book for a 4th dimension was perfect: I could now explain 4 dimensions to a young teenager. I’m very surprised that you didn’t carry it on for the rest of the discussion; especially on a multi-verse. As with any good article, most of the questions I had were answered or at least mentioned, and I’m left with more questions than I started with.

DETERMINISM
I have only one negative comment on your article, which is of determinism, you didn’t seem to look into the consequence very much. Starting with a definition: I believe that the laws of nature are so strictly defined, that given all information about one point in time, and enough processing power, we could work out everything about the next instance in time. I also believe that to do such a thing is impossible [1]. Thus we have the situation as follows: you have free-will: no one can predict what you will do, you fully believe that you can make any choice, AND that of determinism [2]. Randomness is caused by everything we don’t take into account when doing an experiment; hence there are still ‘random’ events.

SINGLE TIME LINE
In a world with only one timeline and assuming that we can go back in time – this will cause us to realise that we don’t have free will. We cannot kill our father as we didn’t: we can only do what has already happened. Hence, you cannot change the past. The reason that we haven’t seen any of these changes (seen any time travellers) is that we haven built anything to receive the energy/mass (signal/traveller). No paradoxes, but then again no free will and no where near as much ‘fun’ as a multi-verse.

I didn’t think that you covered the idea of physically not being able to make the decision. Not some strange co-incidence not letting it happen.

The stuff above I have thought through before, the stuff below has quite a lot more assumptions and flaws.

MULTI VERSE
Let’s describe a multi-verse: we have another dimension. We have the, infinite sized axes: x, y, and z of the 3d drawing on the page of the flipchart. We have all the infinite number of flipchart pages representing time. And we have an infinite number of these flipcharts that represent every universe in the multi-verse. A five dimensional space; infinite in all directions.

Movement through the first 3 dimensions is by transferral of energy, the 4th is mostly automatic (time travel changes this). Movement through the 5th, well lets look at one possibility that you didn’t discuss.

We could move when ever something happens. For instance, a coin lands on heads, hence, we are in the universe where the coin landed on heads. By changing stuff we are simply moving to the flipchart where that happens (in the same way that we automatically move to a new instance in time). We are moving through the 5th dimension.

Determinism almost becomes a moot point in a multi-verse with time travel. We can predict what will happen in the universe that we are in, but we can move between universes so there is not a lot of point.

When we time travel to another universe we can change things; we can add and remove matter; kill the ‘version’ of our grandfather; whatever we want. But we are simply in the universe where that happened.

THOUGHT EXPIRIMENT
Thought experiment time. We have a time-mahcine that can freely move about in both the 4th and 5th dimension. Let’s only look at 2 universes, A and B. We will look at them from 3 points of reference: from Bob’s, from A’s, and from B’s. Each point of reference will be from the chronological order of the one perceiving it.

Bob
Bob is in A in 2050; he time travels so that he is in A in 2000. He kills his mother called Janet; hence he is in B in 2000. The act of killing brought about the change.

A
Bob’s mother, Janet, gives birth to him in A in 2020. He time-travels out of this universe.

B
The Bob from A arrives in 2000, kills Janet, who is not his mother. Hence Janet does not give birth.

From here Bob can travel to A where his mother is alive or to B where the person he thinks is his mother never gave birth.

LOTTERY NUMBERS
It’s the same with telling yourself winning lottery numbers. The fact that information has travelled back in time will cause the movement to a new universe. The likelihood is that the movement of information will cause different numbers on the lottery machine so it it’s not a lot better than guessing. The more chaotic a system the less accurately the information from time travel.

In essence you are using one universe to compute an approximation of the other one. The universe is almost the same (bar the information that travelled back) and you know what happened in the future of the very similar one.

VIOLATE LAWS OF NATURE
If there are an infinite number of dimensions, which cover everything happening, surely we can move to ones where the laws of nature don’t apply. The only reason we haven’t is that we cannot produce a situation where we violate them. If we produced them then we would be in such a place.

You are only able to think if you are alive. By thinking, you are in a universe where you are not dead. Hence, you will only be able to think in the universes where you didn’t die and hence you will think that you are living forever. There will be some universes where you don’t die and that’s the only ones where you will perceive. Do something that will definitely kill you according to the current laws of physics. Does this mean that you will move to a universe which ignores the laws of physics? Shoot yourself in the head to find out.

MOVEMENT DIFFICULTIES
Note that we might not be able to move through the 5th dimension very easily. It might take beings as long as it took them to create a time machine to create a 5th dimension machine, it may not be possible at all. If that is true then we have the case where we can kill our mother and live in a world where we no one gave birth to us. It’s not a paradox if you say that the other universe exists but we can’t get to it. The laws of conservation only apply to closed systems, we cannot observe all the other universes and hence we may gain matter and energy from other universes.

CONCLUSION
Note that the only difference between a universe and a multi-verse is the ability to move in the 5th dimension: something that can only be proved by time travel. I have removed most of the paradoxes you have described but only by defining things or assuming things. I haven’t discovered anything.

My head is buzzing; every time I write about this stuff I come up with so many counter arguments to my thoughts that I find it hard to express anything of value. I hope you like my musings.

[1] My reasoning is that. We cannot build a computer fast enough to perfectly emulate (NOT estimate) itself and do other some stuff. Inside the universe (or even multi-verse) we cannot predict everything that will happen without dedicating the entire universe to working it out.

[2] I know that it is a leap-of-faith, but please, just follow the line of thinking.

Running a country

I don't think this this would work on a large scale, but at the community/village level I see no problem.

I think the country should be run by teams of interested experts. If you have to make a law for cars you need to decide who is the main interested parties and find a representative group from each. These are mediated by a non-voting person. The decisions are made when 75% of people agree, it should be about reaching a consensus. To aid this process people need to know how to change their mind. Our ideas belong to us and become deeply personal; this blocks change.

As an addition to this the entire process should be public. Transcripts and doccuments available.

Laws should be temporary by default. They revert without action. Laws should be guidelines given with examples.

A friend once told me that he hopes the Liberal Democrats never have a good idea. His reasoning is that they don't have a realistic chance of being elected so wont be able to implement it, yet the two partied that do get elected can't use it either, they would be copying. Hence if the Liberal Democrats have a good idea it cannot happen.

To change we would need to see a massive shift in teaching. People need to know how to work together towards an idea. Too many meetings are about convincing people that your idea is better than theirs. Edward de Bono has written extensively on this.

In praise of the internet

I do not think that a democracy is the best political structure, I think it's the least easy to corrupt. Twitter et. al. has done a wonderful job of making news creation public and non-professional. It's difficult to moderate or to stop and once people accept it as the norm it will continue to change things for the better.

People have more power to reach the world now. The web managed, by bringing everyone onto one place, to form groups; which is a good thing in this case. You might expect everyone to become homogenised by the internet, we all have access to the same global media. But what happened is far greater; you can find groups of people with the same interests as you.

Someone who wants to discuss, for no other reason than that it interests them, the details of making a better shoe, voting system, teaching method. You find people with a love of almost everything.

Three things I looked up recently:
  1. The speed of gravity (wave propagation)
  2. New ways to tie shoelaces
  3. Backpacking without carrying much
Not only did I find these things (and more) but there was more than one website that told me more than I wanted to know about each.

Genetic Algorithms in Python

Thursday, 11 June 2009

Following on from before here is some resources for programming GA in python. The top one looks like the easiest to understand and hence is my preferred one.
As a side-note here is a list of neural network resources in Python:
http://www.answermysearches.com/four-free-neural-network-libraries-for-python/195/

I'm thinking or reviving one of my old projects; to recreate critters from aijunkie. I have quite a few ideas I would like to add to them once it's up and running.

Genetic Algorithms and Traveling Salesman

Wednesday, 10 June 2009

My friend is doing some work with Genetic Algorithms. He's trying to optimize a kind of traveling salesman problem. Here is the problem:

You have N sources and M destinations, it takes a specific cost to transport goods between the two. Each has a max and min capacity and you have to make sure all of the source gets to a destination in the least possible cost. (in reality there is a lot more to it but that's enough for me to explain what I want to)

He has coded it as a list of integers. The index into that list is the source and the value is the destination.
---------------------------------------
Source | 1 | 2 | 3 | 4 | ... | N |
---------------------------------------
Destination | 3 | 2 | 9 | 9 | ... | 6 |
---------------------------------------
From the about you can see that all goods from both source 3 and 4 go to destination 9.

It wasn't working as well as the client wanted so we worked through some modifications.

1. Change the crossover method. He was using single point crossover, I thought this would cause sections of solutions to remain fixed for too long. i.e. you would be limited to having the same beginning sections of genome as one of the randomly selected initial individuals. I suggested occasionally he selects a random genes (here being one integer in the list) from the parents. This way they still get about 50% from each parent but which ones that get are random (rather than from the beginning or from the end).

2. Change the mutation method. He was mutating by changing a random source to a random number. As he pointed out, this is likely to cause infeasible solutions. Instead we decided that swapping two sources might be a better method. I think this will help particularly in later epochs (generations). Note that you still need the old mutation to bring back removed destinations.

I did also suggest hill climbing the best solutions at the end of the GA. But as there are no continuous variables I'm don't think this would work.

It might also have been an idea to group good partial solutions which his current encoding method doesn't exploit to it's potential. For example, it might be that all good solutions have the same source/destinations links as each other for a sources 2,6,17,29. It would be nice if the GA could allow these to be close together on the genome so that they stay as a unit. We could partition the GA by selecting chunks of sources and solve each separately, then use the best 10 from each partition to make a new combined solution with improved starting points.

One interesting part that he has already used was to penalize the solutions that don't transport all the goods or transport too much. He does this by an exponential so that solutions that heavily break the rules ave very heavily punished. I did suggest that it might be a good idea to change the punishment factor with the number of generations such that you allow the rules to be bent at the start but not at the end.

We shall have to see how it works.

Evolved Virtual Creatures

Tuesday, 9 June 2009

A brief summary of Evolved Virtual Creatures by Karl Sims.

Creatures are made and evolved to perform simple tasks. These include jumping, swimming or following. "the optimization determines the creatures morphologies as well as their control systems" i.e. they have both a physical and mental representation in DNA.

Physically they are made of cuboid blocks of varying dimensions with joints connecting them. Each block can have sensors or effectors. The effectors are simply a force applied to a specific joint causing motion. The sensors include the angle of a joint, photo-detectors or contact sensors.

This physical structure is stored as a directed graph. E.g. if the body node was connected to itself 10 times it would make a snake like creature. Each node of the graph has its own brain so that body parts can learn to have their own controls. In addition to that there is the centralized control which can link any of the parts to any other.




I would love to see a distributed computing version of this program. You could imagine each computer as a segregated island (galapagos island) where different species can develop. By downloading new creatures from other computers you can see how they cope in this new habitat.

To make it more interesting as an end user it would be good to be able to direct the evolution. Give the control to treat them like pets: providing training grounds, change food scarcity, introduce or remove predators.

If that gained interest as a free version then the paid one could be the genetics lab where you can make your own creatures. Additional sensors and effectors could be added (and sold) and the spread through the population (both intro and inter island/computer) would be a wonderful piece of research.

Blocks of brains could be made that perform certain tasks and introduced in the way that new sensors and effectors are added. To reduced computational load thinking (i.e. computation) could use up energy.

hackers and libertarians

Just a quick post to this:
Code Free or Die(): Why Hackers Are so Often Libertarians
by Daniel Franke. His idea is that because libertarians do not want to be controlled by anyone or anything we learn how to take control. If you own a computer that means learning to hack it.

Evolution

Monday, 8 June 2009

Evolution is an accidental consequence of our universe. If you have an object that can produce slightly modified copies of itself, you have evolution. The strange part is the modifications can be random changes.

It happens with stories, people make up a story but each time it is told it gets changed. It might be changed because people forget parts, or they think a different version is better. The story evolves to become what people want to hear.

It's important to note that it is not just the story that affects how good it will be but the environment around it: the people who hear it and the people who pass it on. People like different things.

It also happens with life; In fact we have evolved a fantastic method for evolving. This method is called genetic evolution. We are coded, like blue-prints, into a chemical called DNA. This allows us to replicate in an imperfect way (organisms are rarely exact clones). Mostly these changes do nothing, sometimes they are problematic, very occasionally they are beneficial. Anything that gets a benefit will be able to reproduce (copy) more times, meaning they will cover a higher percent of the population.

Sexual reproduction is another improvement in evolution. It's as if we combined two good stories or two good car blue-prints to make another.

As we continue to rush through the landscape of evolution history on earth we can see that certain events that seem to be detrimental can be beneficial. Fluke events can shape an entire lineage. Things don't have a plan. We are not forced to follow our genes.

Python Date and Time

Friday, 5 June 2009

Same project, different problem. This time the problem is dates and times. Specifically converting between them. These two small functions do just that.

# parse_date :: Date -> String
# e.g. parse_date("2001-12-31")
def parse_date(date):
return datetime.date(*strptime(date, "%Y-%m-%d")[0:3])

# str_date :: String -> Date
def str_date(date):
return date.strftime("%Y-%m-%d")

P.S. Pleac is a wonderful site, it aims to have a load of common code for a variety of languages, good to get practical tips as well as learn a new language.

Combining Python Dictionaries

I had a small task where I had to take two existing dict and combine them by adding together the values of any elements that already exist. Here's how I did it.



def dict_combine(dict1, dict2, combiner):
"""takes two dictionaries and combines them
such that the value is the result of:
result[a] = combiner(dict1[a],dict2[a])"""
result = {}
dict1_keys = set(dict1.keys())
dict2_keys = set(dict2.keys())

# keys uniuqe to dict1 are included unchanged
for x in dict1_keys - dict2_keys:
result[x] = dict1[x]

# keys uniuqe to dict2 are included unchanged
for x in dict2_keys - dict1_keys:
result[x] = dict2[x]

# keys in both dictionaries get combined then included
for x in dict1_keys & dict2_keys:
result[x] = combiner(dict1[x],dict2[x])

return result

def test_dict_combine():
dict1 = dict(a=1, b=2, c=3)
def adder(a,b):
return a + b
assert dict_combine(dict1,dict1,adder) == dict(a=2, b=4, c=6)
test_dict_combine()

Testing Python Code

Just a quick test that parsing python works...


#!/usr/bin/python

from time import strptime, strftime
from datetime import date
import datepyth
from datepyth import Money, RepeatEntry, FractionalEntry, SingleEntry
from datepyth import weekly, monthly, yearly
from decimal import Decimal

# parse_lines :: [Entry] -> (String)
def parse_lines (getline):
database = []
for line in getline:
if len(line.strip()) != 0:
database.append(parse(line))
return database

Pretty Print Source Code in Blogger

First job get nice looking code. Modify your template by going to: Layout->Edit Template. Find the bit between the end of 'head' and start of 'body'. this is what you need to change. follow the instructions as of the first link below.

Here is the links I used to find it out.

First Post!

Thought it was about time.