Migrating scripts
- November 30th, 2005
- Posted in Tech
- Write comment
In the midst of the non-stop action at work today (I was watching Return of the Jedi) I decided to move my old conversions.py script over to conversions.pl. My clumsy nature with python may well have been the cause of the overuse of if, and else – but in the end conversions.py lived as a 100+ line menu driven script.
That’s right, I said menu. You run the script, it pukes a menu with a host of options – do you want to go from kilowatts to horsepower? Make your selection, and then you get to input your value, and hit enter again. Rejoice, for now you have your new value, and new unit of measure! Awesome, only not really at all.
My issue with implementing any command-line mojo revolved around my inability (I strongly doubt this is python’s fault) to pass numerical values as numerical values through sys.argv. Options, being strings, were not an issue – but for the life of me I could not manage to pass a number as an integer, just a string.
Enter perl, and ./conversions.pl fc 32 returns 0! All in 28 lines. The only reason it takes 28 lines is because I felt like using a seperate line for each key/value pair in the hash containing my conversion factors. Instead of a different if statement for each individual possibility, I have one if checking for the first command line argument, in my conversion factors hash. The best part is the ease with which I can make a CGI script out of this, and you’ll probably find that I do this shortly.
I’ll upload and link the code for both conversions.py and conversions.pl for you knowledgable folks to pick apart.
Here’s a tweak: http://widebrant.org/conversions.pl
I am totally lost! You sound intelligent though!! How many more days??? This last month may be the longest in history!
Take care and stay safe and alert!
Love,
Mom
heading to aggieland for nat’s b-day. 21 and legal in all states!!
way to post some nerd mumbo-jumbo on my birthday! you didn’t even say anything about your little sis turning 21! TWENTY-ONE! enjoy your python. (that’s the only word I comprehended out of that whole post). jerk.
Typically I think of Python as being more readable than Perl, but in this case the Perl version happens to be much nicer, though that is mostly due to the use of the hash in Perl vs. the tons of if statements in the Python version.
Nice script!