Thursday, March 27, 2008

Simple Tree : Python

I needed a simple example on using list in Python for
a Python tutorial :
This sample shows a way to diplay a hierachical list

tree=["Serveurs",
["Dakar",
["System",
"Banque",
"Reseaux",
['R1',
'R2']],
"Dublin",
"Denver",
"Douala",
["Queue"]]]

hidedNodes = []


def displayTree(tree,n=0):
ident = " "
nextitem = None
for item in tree:
if not isinstance(item,list):
try:
nextitem = tree[tree.index(item)+1]
except:
pass
if isinstance(nextitem, list):
print n*ident+"+" + item
if item in hidedNodes:
if nextitem[0] != 'x':
nextitem.insert(0,'x')
else:
try:
nextitem.remove('x')
except:
pass
else:
print n*ident+" "+ item
else:
if not nextitem[0] == 'x':
n = n + 1
displayTree(nextitem,n)
n = n -1


def hideNode(node):
hidedNodes.append(node)

def showNode(node):
hidedNodes.remove(node)

def test():
print "TEST 1 : Display all"
displayTree(tree)
print ""
print "TEST 2 : Hiding Douala,Dakar"
map(hideNode,['Dakar','Douala'])
displayTree(tree)
print ""
print "TEST 3 : Showing Dakar"
showNode('Dakar')
displayTree(tree)
print ""
print "TEST 4: Hide Reseaux"
hideNode('Reseaux')
displayTree(tree)

if __name__ == "__main__":
test()


Tuesday, March 4, 2008

Learning F# - List Concatenation

My first attempt with lists in F# :-)

let lofl = [[1;2];[3;4];[5;6]]

let rec concat ll =
match ll with
| [] -> []
| h :: t -> h @ concat t

printf "%A" (concat lofl)
read_line()

Sunday, March 2, 2008

Embed Win32 resources in C# programs

This article explains all ...
http://www.codeproject.com/KB/cs/embedwin32resources.aspx

ADO - Python

Querying ACCESS with Python


from win32com.client import Dispatch as CreateObject

conn = CreateObject("ADODB.Connection")
mdb = r"C:\northwind.mdb"
conn.Open("Driver={Microsoft Access Driver (*.mdb)}; DBQ=%s;"%mdb)


def execRequest(sql):
r,t = conn.Execute(sql )
cust = "<table>\n"
while not r.EOF:
s = "<tr>"
for item in r.Fields:
if item.Name == "ContactName" or item.Name == "Address":
s+= "<td>%s</td>"%(item.Value)
s += "</tr>\n"
cust += s
r.MoveNext()
cust += "</table>\n"
return cust

cust = execRequest("select * from customers")

conn.Close()


Sunday, February 24, 2008

WMI - FSharp

WMI test is my 'Hello World' :-)

#light
open System
open System.Management

let getLogicalDisk () =
let query = "select * from win32_logicaldisk where drivetype = '3'"
let objMgt = new ManagementObjectSearcher(query)
let result = objMgt.Get()
for info in result do
let caption = info.GetPropertyValue("Caption").ToString()
let size = info.GetPropertyValue("Size").ToString()
printf "%2s : %15s Ko\n" caption size

getLogicalDisk ()

read_line ()