pysort.py
by
admin
—
last modified
2007-09-27 23:00
Sorter efter dato og tid.
Size 1.8 kB - File type text/python-sourceFile contents
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import datetime
Liste = """;14-11-2007,07:00 - 08:45,None,100
;21-11-2007,07:00 - 08:45,None,100
;12-12-2007,07:00 - 08:45,None,100
;19-12-2007,07:00 - 08:45,None,100
;26-12-2007,07:00 - 08:45,None,100
;02-01-2008,07:00 - 08:45,None,100
;20-02-2008,07:00 - 08:45,None,100
;27-02-2008,07:00 - 08:45,None,100
;05-03-2008,07:00 - 08:45,None,100
;12-03-2008,07:00 - 08:45,None,100
;12-09-2007,17:00 - 18:30,None,104
;19-09-2007,17:00 - 18:30,None,104
;26-09-2007,17:00 - 18:30,None,104"""
def mktid(dato="dd-mm-yyyy", tid="hh:mi" ):
"""returns datetime object"""
dd , mm , aar = map( int, dato.split( "-" ) )
hh, mi = map( int, tid.split( ":" ) )
return datetime.datetime(aar, mm, dd, hh, mi)
def sort_dato( x, y ):
"""Compare dates in format dd-mm-yyy,hh:mi"""
dato = x.split( "," )[ 0 ]
tid = x.split( "," )[ 1 ].split( "-" )[ 0 ]
X=mktid(dato=dato, tid=tid )
dato = y.split( "," )[ 0 ]
tid = y.split( "," )[ 1 ].split( "-" )[ 0 ]
Y=mktid(dato=dato, tid=tid )
return cmp( X, Y )
if __name__ == "__main__":
Page = """Content-type: text/html
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<b>
<a href="http://www.4guysfromrolla.com/webtech/082201-1.shtml">Howto run python on ASP</a>
<br>
<a href="http://support.microsoft.com/kb/276494">Using Python Scripts with IIS</a>
</b>
<pre>
<a href="http://www.eksperten.dk/spm/798330">Svar på http://www.eksperten.dk/spm/798330</a>
<h2>Før sortering</h2>
"""
print Page
print Liste
liste=[ line[1:] for line in Liste.split("\n") ]
liste.sort( cmp=sort_dato )
print "<h2>Resultat af sortering</h2>;%s" % "\n;".join( liste )
print "<hr><h2>KildeTekst</h2>"
print open("/srv/www/cgi-bin/pysort.py").read()
print "</pre></html>"
Click here to get the file