Posterous theme by Cory Watilo

Filed under: Python

Struggling with Python

My struggle with the Python Language has been ongoing for a few weeks now. Prior to this experience, I have virtually no experience in any programming language, save HTML/CSS, and a little bit of PHP, if you call reverse engineering valid experience. As far as the HTML, I don't consider programming, although I'm sure people with a lot more CSS knowledge and talent would disagree with me; but I'm speaking to my abilities here.

Anyway, I've been having a lot of fun trying to pick up what I can and solve the "puzzle" that's set before me at work. I've been digging through books, tutorial sites, python.org, the help manual, forums, friends, and strangers in an attempt to get past one issue that I've been facing. I was able to write the "bottom" 3/4s of the code in relatively short time - let's just say it was easy enough... my problem was the initial decision point. 

To make a long story short:

I have an input field which can be a number such as "99,999" or "200' BELOW ..." - it comes in as a string. I needed to first check if it is a number, then do something with that; and then go into the "string" evaluations (bottom portion of the code). 
I went through several different methods of trying this - never could come up with a valid output... I bounced ideas off my boss, my co-workers, several people I know that code in other languages - as sounding boards - and came up with even more ideas/theories to try, all to no avail. I finally hit up a messageboard [http://www.python-forum.org] and posted a message there which was quickly answered by a few different people with different things to try that I hadn't come across. I tried them and they each accomplished a part of what I needed.

Frustrating. 

I mean I'm still having fun trying to solve the puzzle - but I'm just about exhausted from this one issue... So I'm sitting at home tonight, just taking it easy and catching up on some DVR material... when it hits me... 

Without boring you with the silly details, I went back to the basics and did a simple if-else statement and ended up solving the issue - I'm so excited, thrilled, and happy - yet feel silly because it was such a simple fix that I never even thought of. I don't know if this is common among "programmers" - but I will always remember, from now on, to look at the simple stuff first!

Glad to have found all the resources I have thus far - and I'm sure I'll need them again (SOON) in the future. 

What is that thing they say?... K.I.S.S. - keep it simple, STUPID!

Python/Model Builder > the first 2 weeks

Modelsample

I've been working in both Python and Model Builder, since my python script(s) reside in a model. I’ve been pretty impressed thus far with both – though I’m still right in the middle of newbie central… I find myself digging through all the books I have, as well as the interwebs…  I've even joined the Python Users Group Mailing list in hopes of picking up on things I need help with, and to be able to ask questions when needed. (The list does get more Spam than any other list I'm apart of; maybe because it's so big?? But they really need to do something about that!)

 

At this point, my goal is simply to rewrite the VBA code into usable Python so my toolbox will run in Arc10; eventually, I’d like to rewrite the entire model in Python (less moving parts, less dependency on Arc versions – plus, the script can then be executed on a virtual server).

 

At the end of our python class, I had a major complaint that we shouldn’t have used the last afternoon going over python that wasn’t directly related to Arc – well, last week I found out why she had the class set up that way….

 

When it comes down to using python to pound through your data/tables, Regular Expressions are inevitably necessary… I have been backward and forward through that section of the book, and now hope that I can find a class that picks up with Python where that class left off. While I’m using python in my model, at present I’m using a Field Calculator with python code, which is awesome because I don’t have to worry about setting up a loop to run through each row – just an FYI… There are some limitations of doing it that way, but for the record, you CAN import modules while in Field Calculator – tested this last week. I’m down to the knitty-gritty in the first implementation of code, but I’m stuck on one issue – the “DEPTH_FROM” field in the Property_Tract_Status table, in some instances, has words (which I’m successfully parsing through) and in others there are numbers.

 

You’d think the rows with numbers would be easy to deal with numbers, right? Well, there’s a huge catch… The numbers have commas as thousand’s separators… effectively creating what’s known as a “tuple” in python.

 

My hope is that I can use the split function and then strip the comma, then recompile into a number and then proceed through the rest of my code – I started this on Friday, but so far have been unsuccessful… 

 

I found an IDE that I think far superior to PythonWin  and IDLE > it’s called PyScripter (http://code.google.com/p/pyscripter/) < it’s freeware, and it works like a champ – it lets you navigate through the functions, objects, and imported modules. I'm now using it exclusively; although, I usually have all three IDEs open (PyScripter, PythonWin, & IDLE), since using IDLE for “help()” is the fastest/easiest way to get said help (on things/functions of python outside of ArcPy)…