[Tutor] i++

Danny Yoo dyoo at cs.wpi.edu
Tue Jun 5 17:37:58 CEST 2007



> I have attached what I got so far if you feel like taking a look, but it 
> is probably nasty by your standards :)

Good, this is exactly what I want.  Yes, there are a few things here that 
you will want to learn how to fix.  Let me point out one or two, and I'm 
sure others here on the list can chime in too.

#####################################
## within definition of char_sheet():

     if month == 1:
         month = "January"
     elif month == 2:
         month = "February"
     elif month == 3:
         month = "March"
     ...
#####################################

Make this a separate function: the essence of this block of code is pretty 
self-contained as a number-to-month-string converter.  If you need more 
information on this, please ask, and we can talk about it more.


There is also a much nicer way to express the code: rather than make it 
conditional logic, just build a data structure that naturally expresses 
the mapping from a number to a string.  There are two data structures that 
fit the above nicely: a "list" and a "hashtable".  For the above, a list 
naturally works out.

     ######################################
     ## pseudocode
     months = ["January", "February", ...]
     monthName = months[monthNumeral - 1]
     ######################################

Do this, and that block of code reduces from twenty-four lines to about 
three.



I'm looking at the number of attributes you're storing as a Player.

###########################################################################
def char_sheet(player, title, character, password, year, month, influence,
                cur_influence, sp, sl, income, funds,
                mid, mistress_name, mistress_sl, club, house, horses,
                regiment, rank, ma, appointment, strength, con,
                max_endurance, cur_endurance, rapier, dagger, sabre,
                cutlass, two_hand):
###########################################################################

All these values "belong" together.  Rather than pass them separately, 
glue them together as a "structure".  In Python, can we use a class to 
glue things together.


For example, if we have a Person with a name and address:

###################################
class Person:
     def __init__(self, name, addr):
         self.name = name
         self.addr = addr

p = Person("scott", "slewin")
print p.name
###################################

Then we can pass a whole Person object around now.


Going back to your code, as it is, if you need to talk about a character, 
you have to carry around all the attributes of your character by hand and 
hope to get the order of the attributes right in char_sheet() and 
char_save() and anything else that does character-related stuff.


Consolidating the attributes in an object will also let you take advantage 
of Python's pickling a lot better.  The pickling code knows how to 
serialize whole objects, so rather than:

#################################################################
def char_save(player, title, character, password, family, influence, 
cur_influence, sl, allowance, funds, mid,
mistress_name, mistress_sl, club, house, horses, regiment, rank, ma, 
appointment, strength, con, cur_endurance,
rapier, dagger, sabre, cutlass, two_hand):
     '''This saves the character to the harddrive'''
     path = "./characters/" + player
     cfile = open(path, "w")
     cPickle.dump(player, cfile)
     cPickle.dump(title, cfile)
     cPickle.dump(character, cfile)
     ## ...
#################################################################


the code will dissolve into something that looks like:

#############################################
def char_save(character):
     """char_save: character -> void"""
     path = "./characters/" + character.player
     cfile = open(path, "w")
     cPickle.dump(character, cfile)
#############################################

Again, a reduction from about eighty lines of code to about four.


There are a few other things to discuss about in the code, but I should 
let you take a look again and respond before going on.  If you have more 
questions, please feel free to ask the list.


More information about the Tutor mailing list