Test Data Generation with Python

My latest task has me writing a utility to generate test data in XML format. My language of choice for this task is Python. At first, I got some push back from management. They told me they thought Python could not handle XML. Ha.

I am starting with the basics. I am using the ElementTree that comes in the Python standard library. It is really easy to parse an XML file in Python. Just takes a few lines of code for ElementTree. The actual parsing is delegated down to a parser program. Expat is such a program that ships with Python.

It is also easy to find or iterate through nodes of interest in the XML. The parsing puts the data in a tree. ElementTree gives you methods to search and destroy. I got hung up for a second because there did not seem to be a method to clone a node. That's okay. I rolled my own.

You need to make sure to do a deepcopy() from the copy module. That way you get the whole node. Then you can append() it to the tree. Make sure to write() the resulting in memory tree back out to an XML file to persist your changes.

Next up I need to figure out which values I need to manipulate in my cloned nodes. Good times I tell you. Good times. No wonder the new guys straight out of college prefer Python for their tools.

Microsoft Virtual Academy

I was planning to complete a Microsoft Virtual Academy (MVA) course on Python. Specifically I was interested in the Developing Web Applications with Python and Flask. Upon review of the topics for the course, I thought it might not be what I really want. I want to write apps. Not so much web apps.

So I perused the MVA courses related to Python. I tried out the Intro to Python. After viewing the first module, I think it might be too rudimentary for me. The presenters were interesting. Their links were out of date. Don't know why. They hosted their stuff on Github. It should still be out there.

Learned that there are a lot of flavors of Python: CPython, JPython, IronPython, PyPy. Not exactly sure what version I have been learning. Maybe these are specific to Microsoft Windows or something. Of course the MVA recommends you use Visual Studio to do your development. So far I have gotten used to the Python Shell that comes with Python 3.4.

One week left until the local Python Hackathon. I think my skills need more practice. Even the MVA guys say you need to practice to learn. Is it time to implement the game of Othello? Or maybe go full blast a try to code up a mini-roguelike?

Python Battleship

I implemented a crude version of the game of Battleship in Python. This is all about x and y coordinates. How should one store such a pair of coordinates? I chose a tuple. A tuple in Python is a set of comma separated numbers. Normally they are programmed within parentheses. I just happened to represent the ship coordinates using tuples. Then I put those tuples in a list.

The good thing about tuples is that you access the elements of a tuple like you would a list, with brackets. A zero based index goes in the bracket. The values in the tuple are immutable. Once set, you cannot change them. This is fine as the ships in Battleship are not changing during the game.

Previously I had learned to use the int() function to convert a string to an integer. Well now I know there is also a str() function to convert an integer to a string. Useful information. Where to go next? I might implement a simple encryption method in Python. Or I might go full tilt and try to code the game of Othello. Still might also want to investigate a Python library. So much to learn. So little time.

Missing From Python

This week I have been concentrating on the features that are present in the Python programming language. However I have noticed some omissions in Python that other programming languages have. One is the case statement. There is no such thing in Python. You got to do a bunch of if else statements.

Another thing that does not seem to be supported out of the box is graphics. Sure you can pipe some characters to the console output. But there are no bitmapped graphics that I can see. Luckily there are popular add on packages that supplement this shortcoming.

I have read about PyGame which will give me graphics and sounds. And there are other choices too I presume. My last big project is going to be implementing the game of battleship. Unfortunately I did not play this game as a kid. I expect that is it something akin to Minesweeper. Let's see how hard that will be using Python.

Python Pass By Reference

I implemented a bare bones version of Tic Tac Toe using the Python programming language. Since my last post, I added logic so that the computer made moves during its turn. The computer AI is not too intelligent. It just follows a preset path, and skips over spots that are taken on the board. The goal here is to learn Python, not figure out the Tic Tac Toe bot.

During this exercise, I needed to finally figure out whether parameters passed to a function are by value or reference. The answer is not so simple. Let's get one thing out of the way first. If you pass a string to a function, it cannot be changed as that type is immutable. But let's discuss passing mutable types to a function.

When you pass a variable, you are supplying a name that is associated to an object. That name is like a reference. The function gets its own reference that is separate from the formal parameter name. However the version that the function has can be used to reference and change the object it points to. So that is kind of like passing by reference.

You can reassign the local function reference to some other object. However the original variable (reference) that the caller has is unchanged by this reassignment. Confused? I guess you got to play with it a bit to understand what the heck is going on here. I will just treat this as pass by reference with some caveats.

Python Tic Tac Toe

I am in the middle of implementing a Tic Tac Toe game using the Python programming language. So far I can draw a board with ASCII graphics. I also get user moves and show them on the board. I can detect whether the latest move is a winning move. Next I need to implement the computer moves. That will require some rudimentary artificial intelligence.

Although I did not have to use it, I figured I should try two dimensional arrays to represent the board. In Python, it would actually be a two dimensional list. The creation of such a beast is not intuitive. I found some weird ways to specify it. However I decided to start simple and create a list with brackets. Then I appended lists to that main list using append(). Then I append the actual moves that sub-list.

The good news is accessing the two dimensional list is just as you would an array: myList[x][y]. I learned a couple other things today. Looks like you have to enclose expressions in parentheses for your if statements. And when there are compound expressions with parentheses themselves, wrap the whole thing in extra parentheses.

I did take advantage of a neat feature in Python. You can return more than one value from a function. Just do a return value1, value2. The caller can use the syntax global1, global2 = fxn(). You don't see that syntax in many (any?) other languages. Now back to tic tac toe implementation.

Python Hangman

I wrote a hangman style game using the Python programming language. This required me to use the skills I learned so far, and stretch a bit to learn more. One thing I picked up was that you can use the keyword elif to act like else if. Saves a few characters.

Also made use of a kind of for loop. You can use for i in range(n) to make i loop from 0 to n-1. Very handy to enumerate the indexes of a string. Speaking of a string, you can use a zero based index in brackets after the string variable name to access individual characters in the string.

The "in" keyword has other uses. You can check whether an item is in a list by using myItem in myList. That is pretty handy. Finally the values for a boolean variable are written True and False. Yes. You must use capital letters to start those keywords.

Now that I got hangman out of the way, it is time to reach for the stars. Next I think I shall implement tic tac toe. That will require a bit of artificial intelligence. But nothing too deep. I might even use the algorithm my book taught me so I can concentrate on the Python programming aspect.

Python Functions

It is pretty easy to write functions in Python. You use the keyword "def". Then you put the name of your function followed by parentheses. Optional argument names go inside the parentheses. After that you place a colon.

The rest of the indented code after the colon is the body of your function. The function can optionally return a value with the return keyword. Now there is variable scope business that I have read about and have not played with. More on that later I presume.

Calls to the function can be made after it has been defined. You just reference the function name followed by parentheses. You put any actual values for the parameters within the parentheses. And you can assign the return value of a function, if any, to the left hand side of the function call.

The colon seems to be standard fare in Python. You put it after the function declaration. You also put if after conditional checks and looping constructs. I am going to be kicking it into high gear next, writing a non-trivial game of hangman in Python.

Python Lists

Time to write my second program in Python. This one will need to hold a bit of data. So I decide to use an array. Ooops. Python does not seem to have an array. But they do have lists. All right. Set a variable name equal to a comma separate list in brackets. Then I have a list.

The list is accessed with a zero-based index in brackets. You can create an empty list with just brackets. Then you can add items using the append() function. The list items do not need to be the same type. But you cannot access an entry in the list that was not created yet. That would cause an error.

I also need some random number generation. I import the random package for help with that. Then I can use the randrange() function which takes the upper and lower bounds of the range for the random integer that is returned.

Next up I am going to learn how to create a function in Python.

Invent With Python

I have been interest in learning the Python programming language for a while. My college had offered a class to teach you Python. But I have not seen the class offered in the last two years or so. Finally I have figured it is time to take matters into my own hands and learn the language by myself.

To help with this, I got a copy of the book Invent Your Own Computer Games with Python. I call it Invent with Python for short. I read through the book over the last month or two. I took a lot of notes. But I did not work through any exercises. Bad move. When I sat down to write my first program, I was clueless.

Now it is time to really learn this language. I did complete my first program with the help of Google. My plan is to write the games that were covered in the book. But I will write them on my own without looking at the source code in the book.

My company is sponsoring a Python hackathon in less than two weeks. Time to really get up to speed so I can complete. All right. I can use the input() function to grab data from the user. That returns a string. Need to convert it to a numeric value using int() before comparing to other integers. I can also use the print() function to do my output.

This is going to be a long ride.