Tuesday, April 03, 2007

Utility programs 1

Ok so I said I was going to talk about function key parsing and python dictionaries, and I will... eventually. I got off on a small tangent though and decided that it would be beneficial to start out talking about external utility programs first. There are probably many uses for a nice organized function key data set, both inside Vr and out. But lets start small. I love little command line utilities that take the place of actually working, as a matter of fact if I have a motto it would be "It beats working" and the more times I have reason to say that on any give day the better. Here is an example...

When I am working with ortho's there are times that I would like to have the photo centers plotted in a file. Because almost everything in Vr is stored in easy to read, easy to interpret text files it is a piece of cake to go into the opp files and pull out the xyz coordinates in any text editor. But do that on 100 separate files and you are bordering on work. What if you could just run a simple little command line utility that would give you a space separated listing of the photo name and it's coordinates. That my friend "beats working". Let's think through this program a few lines at a time, when it's all said and done I'll post the final code on the repository web site.

First for the preliminaries


# Lets start out with a docstring to explain what the program is for.
'''
Extracts photo center coordinates from all .opp files in a directory
and sends
the output to the screen, which can then be redirected into
a text file.

'''
# For now just trust me that these are the modules we'll need.
import os,sys,string,glob
# It's always a good idea to put in a usage function. In this case since
# (like it says) there won't be any arguments, will force the usage string
# to print if the user enters any argument at all.
def usage():
....'''
....Explanation text to print if anything is entered as an argument.
....'''
....print '\nUsage: python opp2txt.py'
....print 'There are no arguments, it will just look in the current '
....print '
directory for files with a .opp extension, strip the '
....print '
coordinates out of them and output them to the screen'
....print '\n\nListings can be redirected into a file.'
....print 'eg: python opp2txt.py > photos.txt'

....sys.exit(1)
# This is the main part of the program. First we'll make sure no
# arguments were entered. sys.argv is the command itself the program
# name is always the first argument, if there is only 1 thing in the
# list then no arguments besides the program name were passed.
if len(sys.argv)==1:
....print 'no code yet run\npython opp2txt.py -h\nfor help'
else: usage()

Ok that's it for now. Copy the above to a python file called opp2txt.py , replace the indent holders (....) with your normal indentation and run it with an argument and it will describe where we are headed. You need python installed and in the path. Type "python opp2txt.py -h" and the usage should print.

If you want to get a better idea what argv does just create a python script that contains nothing except
import sys
print sys.argv
Then run the script with different (or no) arguments.
More to come...


No comments:

For anyone interested in trying VrPython for the first time or if you are early in the game, I suggest going to the earliest posts and working forward. I use VrPython every day for many wonderful things, needless to say it will change and could potentially damage a file. Any risk associated with using VrPython or any code or scripts mentioned here lies solely with the end user.

The "Personal VrPython page" in the link section will contain many code examples and an organized table of contents to this blog in a fairly un-attractive (for now) form.