Today I learnt Python (kind of)

By | February 1, 2014

I’ve been wanting to learn a scripting language for a while as it seemed like it might be a useful skill. So I planned to get around to it this year and was tossing up between Perl, Python and JavaScript (each for various reasons). Yesterday I needed to a set of test scripts, based on a some test cases in a simple simple spreadsheet. The tests are all very samey, and normally I’d create the scripts by 

  1. Writing the boilerplate text (TestNG XML)
  2. Run some fairly simple concatenation/substitution functions in Excel, to populate the the boilerplate for each test case.
  3. Create the 6 files.
  4. Copy+Paste the generated XML into the appropriate files, while toggling various filters.
  5. Hope I don’t have to do it again soon. <sigh/>

The quantity of tests cases (~180) wasn’t a problem. It was the need to shuffle text from one place to another, and the likelihood that new use cases would want to be introduced periodically. So I thought, time to give Python a try…

I’d read a lot of positive things about Python, so was looking forward to seeing what it was all about. I already use Notepad++, so that was my (initial…) IDE. I knew I needed to read and write to a file and do some string handling so I skipped the manual and went straight to this random tutorial that seemed to cover the basics, and a solution was coming together quite quickly. I’ll admit, it’s a pretty simple language. Except there’s two things I wasn’t a fan of.

  • Absence of line delimiters
  • Absence of braces

The delimiters, isn’t so bad. In face I think you can still drop semi-colons, they’re just not necessary. But in place of braces, Python uses indentation. That is a block of code – be it a function, loop whatever – is only identified by it’s level of indentation relative to everything else. Now as this article points out – I’d normally do this anyway, but there is something reassuring seeing a couple of braces. But getting out of the comfort zone is part of the fun of trying something new. I settled on using tabs (although I know it’s not ideal) and within a couple of hours (and numerous looks at Stack Overflow) thought I was about done.

Then came the debugging… For some reason, my script would only generate 1 of the 6 files I needed. And I spent way too long trying to work out why. Generation of the 6 files shared 100% of the same code, so I eventually look to IDLE got my error message that way:

utf8′ codec can’t decode byte 0xa0

So, my source data had some non-breaking spaces in it, that weren’t getting encoded/decoded properly. I easily fixed the problem now I knew it was an encoding issue, and the script ran without trouble. (Though I’ve still got an encoding issue in my new script files, to resolve…) But I generate new scripts in about 3 seconds, if new test cases are required. So while I’m not an expert, I now know I can use Python for tasks like this in future.

Leave a Reply

Your email address will not be published. Required fields are marked *