Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

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

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.

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()