Wednesday, November 20, 2013

NoneTypes

Let's say we have a function do_something(item). The parameter item is a class that has an attribute called thing. When this function is called, it needs to perform an operation on item.thing so long as item is not None, and thing is a string. My first thoughts on how to do this were to check if item exists, then check if thing is an instance of a string, in that order. The first line of my code looked something like:

<the things needing done> if item.thing and isinstance(item.thing, str) else <don't do stuff>

My reasoning was something like: Well if item is not None, then it has a thing attribute, so if item.thing returns false then item must be None! Well...turns out this isn't the case, and this mistake cost me two whole marks on Test 2. If item is None, then trying to do anything with any attribute of item will throw an AttributeError, because NoneTypes have no attributes. In order for a bool(item.thing) call to return False, item.thing has to be explicitly initialized to None, or 0, or an empty string, etc. Looking back, this was such a stupid mistake, and it would've been fine if I had just had item instead of item.thing in the first check.

One thing I've noticed about the sample solutions for recursive functions is that they only ever check if the parameter itself exists, not any attributes of the parameter. For whatever reason I've been writing code that tries to avoid passing None as a parameter recursively, but also having to deal with the case that None is passed explicitly from outside the function. Had I noticed this earlier and incorporated it into my own code, the above mistake probably wouldn't have happened =\

Wednesday, November 6, 2013

Regular Expressions

Our second assignment was the first time I had dealt with regular expressions. As always, I was looking forward to working with some new concepts after I had read through the assignment handout. A regular expression (regex for short) is a series of characters that can be interpreted by an appropriate class into a set of rules. These rules are used to determine if a string belongs to the regex, or if it doesn't. Other than a fun programming exercise, I didn't really see any practical applications of this. Turns out that regexes can be very useful in aiding the interpretation of a string. If you need your program to read files, but treat different types of strings in different ways, comparing sections of your file to a list of regexes can help you classify them for proper handling. Another use of regexes might be in detecting cheat codes in games. Instead of having a "Cheats" menu option, which would lead to a screen where the user can input codes in a textbox of some kind, consider continuously scanning the user input during normal gameplay. If a series of inputs matches the general "cheat code" regex, then said inputs can be compared to a list of cheats, and the appropriate one activated.

As far as the assignment itself, I was kind of disappointed. After scrambling within the last few days to get through the first assignment, I gave myself plenty of time to work on this one. The thing is, this assignment was easy. Once you understand the concept of regexes, and the set of rules described in the handout, the actual coding was fairly simple. The rules for the various operators were practically written in pseudo-code, ready to be translated into Python syntax. I feel kinda bad saying that, because I know there were many people who struggled with this assignment, but this is just my opinion. It is not my intention to make anyone feel inferior (because you aren't), I just wanna write a blog post =\