Sunday, June 28, 2009

simple python closure example

Original taken without permission from ScratchPad.


1>>> def counter(start=0, step=1):
2 ... x = [start]
3 ... def _inc():
4 ... x[0] += step
5 ... return x[0]
6 ... return _inc
7 ...
8 >>> c = counter()
9 >>> c()
10 1
11 >>> c()
12 2
13 >>> c()
14 3

Monday, June 22, 2009

changing ubuntu splash screens

1. http://ubuntuforums.org/archive/index.php/t-210268.html
2. http://ubuntuforums.org/showthread.php?t=89916&highlight=grub+splash+screen

Saturday, June 20, 2009

general linux commands

~/.bashrc is used to initialise each new shell instance

to set an environment variable, include a line analagous to

export PYTHONPATH=$PYTHONPATH:/home/david/Desktop/gdata-1.3.3/src

Setting up a mySQL DataBase on uBuntu

http://www.debuntu.org/how-to-create-a-mysql-database-and-set-privileges-to-a-user

'import MySQLdb' failed

http://davidmichaelthompson.com/?p=12

duh, need to install package 'python-mysqldb', 'A Python interface to mySQL'.

Wednesday, June 17, 2009

Configuring Eclipse on Windows to Use With Google App Engine

Google AppEngine Link

Stationary Traffic Waves

Almost everyone in Jozi has experienced it:

You're driving along the ring-road, when suddenly you drive into the back of a traffic jam. 30 blood-boiling minutes later, you emerge out the other side, never the wiser for the cause of your delay.

Well, some mathematicians have modelled the process, which appears to involve information waves: Wired Magazine Article: Phantom Jams.

Tuesday, June 16, 2009

plot f, f', f'' from csv with python & matplotlib


class rawDataObj:

pointCount = 0

colCount = 0
columns = []
colLabels = []

title = ""

# ===========================================

class Series:

def __init__(self):

self.x = []
self.y = []

#~ def __init__(self, xIn, yIn):

#~ if (len(xIn) != len(yIn)):
#~ raise lengthInequalityError, "length inequality"

#~ for zidx in range(len(xIn)):

#~ self.x.append(xIn[zidx])
#~ self.y.append(yIn[zidx])

# ================================

def DbyDX(self, xIn, yIn):

if (len(xIn) != len(yIn)):

raise setSizeInequalityError, 'set size inequality'

else:

for zidx in range(len(xIn) - 1):

xVal = (xIn[zidx + 1] + xIn[zidx]) / 2.0
self.x.append(xVal)

yVal = (yIn[zidx + 1] - yIn[zidx]) / (xIn[zidx + 1] - xIn[zidx])
self.y.append(yVal)

# ===========================================

import re

def loadrawDataObjFromFile(filePathAndName="/home/david/Desktop/ptpdrh.csv", hasHeader=False):

try:
f = open(filePathAndName, 'r')

one = []
two = []

eof = False

retDataObj = rawDataObj()

lineNum = 0
headerLineCount = 2

while (eof == False):

line = f.readline()
eof = (line == "")

if (line != ""):

lineNum = lineNum + 1

elements = re.split(',', line)

# strip newline (\n escape sequence) from last field of line
elements[len(elements) - 1] = elements[len(elements) - 1].rstrip("\n")

# validate column count
if (lineNum > headerLineCount):
if (len(elements) != retDataObj.colCount):
raise inconsistentColumnCountError, ('column count inconsistent at data line ' + (retDataOb.pointCount + 1))

if (lineNum <= headerLineCount):

if (lineNum == 1):

retDataObj.title = line

elif (lineNum == 2):

retDataObj.colCount = len(elements)

for num in range(retDataObj.colCount):
retDataObj.columns.append([])

for label in elements:
retDataObj.colLabels.append(label)

else: # if (lineNum > headerLineCount)

string = ""

for zidx in range(retDataObj.colCount):

string = elements[zidx]

if (len(string) == 0):
retDataObj.columns[zidx].append(None)
else:
retDataObj.columns[zidx].append(float(string))

retDataObj.pointCount = retDataObj.pointCount + 1

f.close()

except IOError:
print "io exception while attempting to load raw data"
print "from file:"
print filePathAndName


return retDataObj

# ===========================================

# ===========================================

from pylab import *

filePath = "/home/david/Desktop/FinData/"
fileName = "RSA_CPI_1981-2008.csv"
filePathAndName = filePath + fileName

dataObj = loadrawDataObjFromFile(filePathAndName, True)

#for y in range(dataObj.colCount - 1):
# plot(dataObj.columns[0], dataObj.columns[y + 1], label=str(dataObj.colLabels[y + 1]))

fig = figure()
fig.canvas.set_window_title(dataObj.title)

# BASE SERIES
axBase = subplot(311)
baseLabel = label=str(dataObj.colLabels[dataObj.colCount - 1])
plot(dataObj.columns[0], dataObj.columns[dataObj.colCount - 1], label="base")
legend(numpoints=1, loc="upper left")

# FIRST DERIVATIVE
d1 = Series()
d1.DbyDX(dataObj.columns[0], dataObj.columns[dataObj.colCount - 1])
axD1 = subplot(312, sharex=axBase)
plot(d1.x, d1.y, label="d/dt")
legend(numpoints=1, loc="upper left")

#SECOND DERIVATIVE
d2 = Series()
d2.DbyDX(d1.x, d1.y)
axD2 = subplot(313, sharex=axBase)
plot(d2.x, d2.y, label="d2/dt2")
legend(numpoints=1, loc="upper left")

#xlabel(dataObj.colLabels[0])

show()

Monday, June 15, 2009

Import & Display CSV Scatter with Python & MatPlotLib

  

class dataObject:

pointCount = 0

colCount = 0
columns = []
colLabels = []

# ===========================================

import re

def loadDataObjectFromFile():

filePath = "/home/david/Desktop/"
fileName = "ptpdrh.csv"
filePathAndName = filePath + fileName

firstLineIsLabels = True

try:
f = open(filePathAndName, 'r')

one = []
two = []

eof = False

retDataObj = dataObject()

isFirstLine = True

while (eof == False):

line = f.readline()
eof = (line == "")

if (line != ""):

elements = re.split(',', line)

elements[len(elements) - 1] = elements[len(elements) - 1].rstrip("\n")

if (isFirstLine == False):
if (len(elements) != retDataObj.colCount):
raise inconsistentColumnCountError, ('column count inconsistent at data line ' + (retDataOb.pointCount + 1))

if (isFirstLine == True):

isFirstLine = False

retDataObj.colCount = len(elements)

for num in range(retDataObj.colCount):
retDataObj.columns.append([])

if (firstLineIsLabels == True):

for label in elements:
retDataObj.colLabels.append(label)

else:

string = ""

for zidx in range(retDataObj.colCount):

string = elements[zidx]

if (len(string) == 0):
retDataObj.columns[zidx].append(None)
else:
retDataObj.columns[zidx].append(float(string))

retDataObj.pointCount = retDataObj.pointCount + 1

f.close()

except IOError:
print "io exception while attempting to load raw data"
print "from file:"
print filePathAndName


return retDataObj

# ===========================================

from pylab import *

dataObj = loadDataObjectFromFile()

titleText = "title"

for y in range(dataObj.colCount - 1):
plot(dataObj.columns[0], dataObj.columns[y + 1], label=str(dataObj.colLabels[y + 1]))

legend(numpoints=1)
xlabel(dataObj.colLabels[0])
ylabel("")
title(titleText)
show()

Howzit my China ?

For some very interesting reading on China, check out Wired News material on China.

Reading this has helped ease any misgivings I may have had about the ANC's pragmatic but possibly sub-optimal approach to foreign relations with the People's Republic of China.

I think there's plenty of room for improvement in terms of maximizing our side of the bargain, but the Chinese are definitely people you want to be involved in dialogue with at all times.

Friday, June 12, 2009

small business rules

- allocate a generous leave allowance. enforce it's all being consumed within a window period.

Thursday, June 11, 2009

Durban Style Curry

1. Put oil

2. Put more oil [more oil than you would think.]

3. Fry the 'seeds' until they pop

here 'seeds' refers to
- jiru
- mustard seed
- cumin
- cardamom
- coriander
- cloves
- cinnamon
- star anise
- fennel seed
- some or other Indian herb

4. Add 'spices' and fry until they brown

here 'spices' refers to

- hot masala
- bay leaves
- chilli
- fresh ginger (finely chopped)
- ...

5. Add onions and garlic, and fry

6. Add tomatoes, other quicker cooking vegetables, potatoes and meat

in South Africa, use only 'up-to-date' brand potatoes

7. Cook on a low heat for a long time (essentially until potatoes are done)

8. Serve on a paper plate with a polysterene cup of brandy and coke

Un-Installing that unsolicited MS.NET FireFox Add-On

In order to un-install the unsolicited MS .NET Mozilla FireFox Add-On that appears after installing the 3.5 framework, follow Brad Abrahms directions:

http://blogs.msdn.com/brada/archive/2009/02/27/uninstalling-the-clickonce-support-for-firefox.aspx

Wednesday, June 10, 2009

Member Field Serialisation for Config Storage

using System;

using System.Reflection;
using System.IO;
using System.Collections;

namespace ConfigTester
{


public class CFG
{
public int pi = 3;
public string word = "tetragrammatron";
public TimeSpan time = new TimeSpan(1,6,6,6);

public string homeDir = @"/home/david/Desktop/";
public string configFName = "config.cfg";

public CFG()
{
}

// ===== ----- ===== ----- =====

public string FieldToSerial(string fieldName, int fieldValue)
{
return string.Format("{0},{1}", fieldName, fieldValue.ToString());
}

public string FieldToSerial(string fieldName, string fieldValue)
{
return string.Format("{0},{1}", fieldName, fieldValue);
}

public string FieldToSerial(string fieldName, TimeSpan fieldValue)
{
return string.Format("{0},{1},{2},{3},{4}", fieldName, fieldValue.Days, fieldValue.Hours, fieldValue.Minutes, fieldValue.Seconds);
}

// ===== ----- ===== ----- =====

public string[] FieldsToSerial()
{
ArrayList lines = new ArrayList();

Type cfgType = typeof(CFG);
FieldInfo[] fieldInfos = cfgType.GetFields();

foreach (FieldInfo fieldInfo in fieldInfos)
{
string name = fieldInfo.Name;
Type fieldType = fieldInfo.FieldType;

if (fieldInfo.FieldType == typeof(string))
{
string val = (string)(fieldInfo.GetValue(this));
lines.Add(this.FieldToSerial(name, val));
}

if (fieldInfo.FieldType == typeof(int))
{
int val = (int)(fieldInfo.GetValue(this));
lines.Add(this.FieldToSerial(name, val));
}

if (fieldInfo.FieldType == typeof(TimeSpan))
{
TimeSpan val = (TimeSpan)(fieldInfo.GetValue(this));
lines.Add(this.FieldToSerial(name, val));
}
}

int fieldCount = lines.Count;

string[] fields = new string[fieldCount];

for (int fieldNum = 1; fieldNum <= fieldCount; fieldNum++)
fields[fieldNum - 1] = (string)(lines[fieldNum - 1]);

return fields;
}

// ===== ----- ===== ----- =====

public void ListFieldInfo()
{

Console.WriteLine();
Console.WriteLine("FIELD INFO");
Console.WriteLine();

Type cfgType = typeof(CFG);
FieldInfo[] fieldInfos = cfgType.GetFields();

foreach (FieldInfo fieldInfo in fieldInfos)
{
string name = fieldInfo.Name;
Console.WriteLine("name: {0}", name);

Type fieldType = fieldInfo.FieldType;
Console.WriteLine("type: {0}", fieldType);

object obj = fieldInfo.GetValue(this);
Console.WriteLine("value: {0}", obj.ToString());

Console.WriteLine();
}

Console.WriteLine("{0} fields", fieldInfos.Length);
}

// ===== ----- ===== ----- =====

public void SerializeFields()
{
string fullFName = this.homeDir + this.configFName;

try
{
FileStream fileStream = new FileStream(fullFName, FileMode.Create);
StreamWriter streamWriter = new StreamWriter(fileStream);

this.SerializeFields(streamWriter);

streamWriter.Close();
}
catch (Exception exc)
{
throw new Exception("ConfigSaveException");
}
}

// ===== ----- ===== ----- =====

public void SerializeFields(TextWriter tw)
{
string[] lines = this.FieldsToSerial();

foreach (string line in lines)
tw.WriteLine(line);
}

// ===== ----- ===== ----- =====
// ===== ----- ===== ----- =====
// ===== ----- ===== ----- =====

// ===== ----- ===== ----- =====

public void LoadSerialised()
{
string fullFName = this.homeDir + this.configFName;

ArrayList lines = new ArrayList();

try
{
FileStream fileStream = new FileStream(fullFName, FileMode.Open);
StreamReader streamReader = new StreamReader(fileStream);

string line;
while ((line = streamReader.ReadLine()) != null)
lines.Add(line);

streamReader.Close();

int fieldCount = lines.Count;

string[] serials = new string[fieldCount];
for (int fieldNum = 1; fieldNum <= fieldCount; fieldNum++)
serials[fieldNum - 1] = (string)(lines[fieldNum - 1]);

this.FieldsFromSerials(serials);
}
catch (Exception exc)
{
throw new Exception("ConfigLoadException");
}
}

// ===== ----- ===== ----- =====

public string StringFromSerial(string serial)
{
return serial;
}

// ===== ----- ===== ----- =====

public int IntFromSerial(string serial)
{
return Convert.ToInt32(serial);
}

// ===== ----- ===== ----- =====

public TimeSpan TimeSpanFromSerial(string serial)
{
string[] vals = serial.Split(',');

if (vals.Length != 4)
throw new Exception("timespan serial has incorrect number of variables");

int days = Convert.ToInt32(vals[0]);
int hrs = Convert.ToInt32(vals[1]);
int mins = Convert.ToInt32(vals[2]);
int secs = Convert.ToInt32(vals[3]);

return new TimeSpan(days, hrs, mins, secs);
}

// ===== ----- ===== ----- =====

public string MatchKey(string key, string[] keys, string[] values)
{
if (keys.Length != values.Length)
throw new Exception("key count must match value count, but does not");

for (int num = 1; num <= keys.Length; num++)
if (key == keys[num - 1])
return values[num - 1];

return null;
}

// ===== ----- ===== ----- =====

public void FieldsFromSerials(string[] serials)
{
// separate names & serialised form of fields retrieved from storage

int fieldCount = serials.Length;

string[] names = new string[fieldCount];
string[] forms = new string[fieldCount];

for (int fieldNum = 1; fieldNum <= fieldCount; fieldNum++)
{
int splitMarker = serials[fieldNum - 1].IndexOf(',');

string name = serials[fieldNum - 1].Substring(0, splitMarker);
string form = serials[fieldNum - 1].Substring(splitMarker + 1);

names[fieldNum - 1] = name;
forms[fieldNum - 1] = form;
}

// scan through this instantiated object

Type cfgType = typeof(CFG);
FieldInfo[] fieldInfos = cfgType.GetFields();

foreach (FieldInfo fieldInfo in fieldInfos)
{
string name = fieldInfo.Name;
Type fieldType = fieldInfo.FieldType;

string serialString = this.MatchKey(name, names, forms);

if (fieldInfo.FieldType == typeof(string))
{
string val = this.StringFromSerial(serialString);
fieldInfo.SetValue(this, val);
}

if (fieldInfo.FieldType == typeof(int))
{
int val = this.IntFromSerial(serialString);
fieldInfo.SetValue(this, val);
}

if (fieldInfo.FieldType == typeof(TimeSpan))
{
TimeSpan val = this.TimeSpanFromSerial(serialString);
fieldInfo.SetValue(this, val);
}
}

}

// ===== ----- ===== ----- =====

} // class
} // namespace