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()
Tuesday, March 4, 2008
Sunday, March 2, 2008
Embed Win32 resources in C# programs
This article explains all ...
http://www.codeproject.com/KB/cs/embedwin32resources.aspx
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 ()
FSharp - Discovering
I must admit that after Python, It has been difficult for me to find another
language which was as attractive as Python.
I think F# will be this one.
language which was as attractive as Python.
I think F# will be this one.
#light
open System
let print x = printfn "%A" x
let sub a b = a - b
let halfway a b =
let dif = b -a
let mid = dif/2
let z = mid + a
z
let y = (fun x y -> x + y) 1 2
let rec fib x =
match x with
| 1 -> 1
| 2 -> 1
| x -> fib (x-1) + fib (x-2)
let UnAnApres =
DateTime.Now + new TimeSpan(365,0,0,0,0)
let print_List l =
List.iter print_string l
print_string "\n"
let shortHand = ["apples"; "pairs"]
shortHand |> printf "%A"
read_line ()
Wednesday, February 6, 2008
Performance Counters - Python
The module win32pdh allows to retrieve performance counters on Windows host
Here is an example published on ASPN.
You will have to use "win32pdhutil.browse" to translate counters names
in your language.
Here is an example published on ASPN.
You will have to use "win32pdhutil.browse" to translate counters names
in your language.
import win32pdh
import win32pdhutil
#Use win32pdh, Kevan Heydon, 2006/06/23
#http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496815
#win32pdhutil.browse()
def uptimeServeur(serveur='Python'):
path = win32pdh.MakeCounterPath( ( serveur, 'Système', None, None, 0, "Temps d'activité système") )
query = win32pdh.OpenQuery()
handle = win32pdh.AddCounter( query, path )
win32pdh.CollectQueryData( query )
seconds = win32pdh.GetFormattedCounterValue( handle, win32pdh.PDH_FMT_LONG | win32pdh.PDH_FMT_NOSCALE )[ 1 ]
return "Serveur : %s - Uptime: %d seconds" %(serveur,seconds )
print uptimeServeur()
Sunday, February 3, 2008
HTA - Python - Sorting Table
This example shows a method to sort a html table in Python within a HTA file
The things to note are:
- Inserting dynamically within a url the name of the list to be display.
- How to sort a list by index
To run HTA files, simply copy this peace of code with the extension '.hta'
and of course Python must be installed (ActiveState Python will do the job)
The things to note are:
- Inserting dynamically within a url the name of the list to be display.
- How to sort a list by index
To run HTA files, simply copy this peace of code with the extension '.hta'
and of course Python must be installed (ActiveState Python will do the job)
<html>
<head>
<TITLE>HTML Application Example</TITLE>
<HTA:APPLICATION ID="HTAEx" APPLICATIONNAME="HTAEx" ICON="e.ico" WINDOWSTATE="normal">
</head>
<body onLoad="Initialisation()">
<table><tr><td valign="top"><div id="menu">Menu</td><td><div id="content">contenu</div></td></tr></table>
<script language="Python">
from win32com.client import GetObject
import os
import cPickle as pickle
import base64
import operator
sorteddata = [0]*10
donnee = [('Name','Town','Age'),('Albert','Paris',46),
('Robert','Denver',49),
('Grace','Monaco',18),
('Catherine','Palaiseau',25)]
services = {}
ChaineConnexion = r"WinMgMts:\\%s\%s"
#Menu
menu="""
<p>Menu Principal</p>
<center>
<select name="choices" onChange="SelectOption()">
<option>%s</option>
<option value="service">Service</option>
<option value="process">Process</option>
</select>
</center>
"""%('-'*40)
def getValue(id):
#return document.getElementById(id).value
return document.getElementById(id).getAttribute("value")
def getElt(id):
return document.getElementById(id)
def setValue(divid,data):
elt = document.getElementById(divid)
elt.innerHTML = data
def Pointer():
document.body.style.cursor = "hand"
return
def Default():
document.body.style.cursor = "default"
return
def sortTable(idcol,table):
obj = decodeObj(table)
titre = list(obj[0])
data = obj[1:]
index = titre.index(idcol)
sorteddata[index] = 1 - sorteddata[index]
data.sort(key=operator.itemgetter(index),reverse=sorteddata[index])
data.insert(0,titre)
t = formatTable(data)
setValue('content',t)
def decodeObj(s):
objects = base64.decodestring(s)
return pickle.loads(objects)
def encodeObject(obj):
data = base64.encodestring(pickle.dumps(obj))
return data.__repr__()
def formatTable(data):
tablestr = encodeObject(data)
titre = data[0]
numitem = len(titre)
lnktitre = ["""<span onclick="sortTable('%s',%s)" \
onmouseover='Pointer()' \
onmouseout='Default()'>%s</span>"""%(t,tablestr,t) for t in titre]
table = "<table border='1'>"
table += "<tr>" + ("<td>%s</td>"*numitem)%tuple(lnktitre) + "</tr>"
for item in data[1:] :
table += "<tr>" + ("<td>%s</td>"*numitem)%item + "</tr>"
table += "</table>"
return table
def getAllObjects(serveur = ".", espaceDenom = "root\cimv2", classe = "Win32_Service"):
c = GetObject(ChaineConnexion%(serveur,espaceDenom))
objects = c.ExecQuery(r"select * from %s"%classe)
return objects
def getService():
data = [('Caption','State')]
services = getAllObjects()
for svc in services:
data.append((svc.caption,svc.state))
setValue('content',formatTable (data))
def getProcess():
data = [('Handle','Name','CommandLine')]
process = getAllObjects(classe="Win32_Process")
for proc in process:
data.append((int(proc.Handle),proc.Name,proc.CommandLine))
setValue('content',formatTable (data))
def SelectOption():
services[getValue('choices')]()
services['service'] = getService
services['process'] = getProcess
def Initialisation():
setValue('content',formatTable(donnee))
setValue('menu',menu)
</script>
</body>
</html>
Subscribe to:
Posts (Atom)