Mostly Linux & Python syntax notes and hyperlinks.

Monday, May 15, 2017

python: Read in generic lookup table from tab-separated text file with field-names on top line

not beautiful yet, but here's a start:

lookup={}
fd=open("tab_separated_fields.txt", 'r')
try:
    headers=fd.readline()
    names=headers.split()
    num_fields=len(names)
    print "%d fields in %s" % (num_fields,names)

    for row, line in enumerate(fd,1) :
        print "row %d: line=%s" % (row,line)
        if len(line) > 10:
            values=line.split('\t')
            if len(values) == num_fields:
                lookup[str(row)] = {names[i]:(values[i]).strip() for i in range(num_fields)}
            else:
                print "row %d doesn't have %d values" % (row,num_fields)
finally:
    fd.close()
print lookup

Next, see if csv reader makes it better.
Actually, csv.DictReader.
e.g https://github.com/pargery/csv_utils2/blob/master/SimpleCSVReporter.py

Friday, April 21, 2017

Mediocre Windows Batch Script to Measure Time

Well, it uses clock time, and would be more useful as a called subroutine, but this is what I've got so far:

rem Test Timing

rem You might need to set this in later versions and also use !time 

setlocal enabledelayedexpansion

set start_time=%time%
echo start_time is %start_time%

pause
rem Replace above 'pause' with activity to be timed (approx).

set end_time=%time%

rem Now calculate time difference


echo started %start_time%
echo ended %end_time%


rem To get a substring, do this:
rem    %variable:~num_chars_to_skip,num_chars_to_keep%

rem Here we also prepend "1" to 2-digits and then subtract 100 
rem   to avoid numbers like "02"
rem   which can cause errors in the windows batch interpreter.

set sh=1%start_time:~0,2%
set eh=1%end_time:~0,2%
set sm=1%start_time:~3,2%
set em=1%end_time:~3,2%
set ss=1%start_time:~6,2%
set es=1%end_time:~6,2%
set sms=1%start_time:~9,2%
set ems=1%end_time:~9,2%

echo sh %sh%, eh %eh%, sm %sm%, em %em% and sms %sms%, ems %ems%
pause

set /A start_secs=(%sh%-100)*3600 + (%sm%-100)*60 + (%ss%-100)
set /A end_secs=(%eh%-100)*3600 + (%em%-100)*60 + (%es% - 100)

set /A start_ms=(%start_secs%)*100 + (%sms% - 100)
set /A end_ms=(%end_secs%)*100 + (%ems% - 100)

set /A diff=%end_ms% - %start_ms%

echo %start_ms% to %end_ms% is %diff% milliseconds 

pause

Lots of "pause"s to check how it's going.
You can take them out.
Better versions welcome, too.