Showing posts with label windows. Show all posts
Showing posts with label windows. Show all posts

Tuesday, May 26, 2009

Console programs

There are many programs that run in console environments. They are more often seen on Linux than other systems, but that doesn't mean they are not useful on Windows. My *nix favorite console programs are

  • alpine: e-mail client for the console (easy to use and with a ton of features)
  • midnight commander: file organizer
  • vim: text editor
  • gnuplot: open source plotting program
These are quite complicated also when it comes to user interfaces, and are very usable programs.

If you are a little less ambitious, making simple console programs with "menus" is quite simple. A database handling program may have the following menu:

MENU:
1. REGISTER NEW FIELD
2. EDIT FIELD VALUES
3. HELP

SELECT MENU ITEM >> ......

Easy enought to use, eh? You want to read the help file, hit 3 on your keyboard followed by enter. Next post will show how to build a quite useful program using a console interface of this type.... (not uncommon in apps from the 1970's and 1980's)!

Sunday, May 24, 2009

Avoid the *.py extension when calling scripts under Windows

If you'd like to make your scripts run by typing

myscript args

instead of

myscript.py args

you can wrap the python script inside a *.bat file. Say you want to run myscript.py and it takes two arguments, make the following batch DOS file:

myscript.py %1 %2

The percentage+number things play the role of sys.argv in Python such that command line arguemnts are piped through to the Python script.

Friday, May 22, 2009

Time registering command line tool in Python

Often, I want to write a one-liner to a file to register information. I do my time tracking this way, for example. First, you need Python to get data from the command line. Say you want to track number of hours spent on a project at work and get this into a tab-delimited file that you can open in a spreadsheet later (Excel, Calc, etc). You want a utility where you simply write the following at the command prompt: 

regtime projectname hours

and it should automatically make the following line in your "spreadsheet":

date projectname hours

In order to do this, we need to get the current date in Python. Luckily, there is a nice module called time that allows us to do that in a simple fashion. First, import it as follows: 

from  time import strftime as getdate
currentDate=getdate('%Y-%m-%d')

What this does is that it gets the current local time used on the computer and writes a text string with the date:

currentDate='2009-05-22'

We also need to get the command line arguments projectname and hours into Python.
For this we need the list sys.argv.

from sys import argv as arguments
projectname=argv[1]; hours=argv[2];

Ok, lets summarize with a script that takes the two command line arguments and writes the "spreadsheet" line to the screen:

from sys import argv as cla 
from time import strftime as getDate 
myString = getDate('%Y-%m-%d')+'\t'+cla[1]+'\t'+cla[2]+'\n' 
print myString

The result of running this script with arguments "Command line tool" and 1 is shown below:

2009-05-22      Command line tool       1

Next post will show how to write this at the end of a text file. 

Some comments on the above code:
a) the character \t means "insert tab in text string"
b) the character \n means "insert line break"
c) the string '%Y-%m-%d' formats the date to ISO standard
d) adding string elements works in the obvious way: 'foo'+'bar'='foobar'