Hot Topic Spamming Failures - I follow a friend into a Hot Topic store in the mall. Saw some cool merchandise. Decided to buy something. The salesperson asked me a bunch of questions, ...
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.
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?
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.
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.
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.
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.
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.