Python not that wierd

Steve Lamb grey at despair.rpglink.com
Tue Aug 1 13:24:40 EDT 2000


    OK, work finally let me go long enough for me to apply the lessons that
were plowed into my head by this newsgroup over the discussion of "Perl is
Worse".  First off, I want to thank everyone for the conversation.  I did
learn a lot about the language and how to think in it.  I'm sure my Perl roots
will show for a while but I think I know understand Python a lot better than I
did before and will approach what I consider its quirks better armed to figure
them out so they stop being quirky to me.  If I had offended anyone in the
process, I do apoligize.

    For those curious, here is the final for of the parsing of the dice code.
Yes, I incorporated a lot of what was written here, but now that I see it in
use I am quite pleased with how well it works and I think that was really what
thumped me the most upside my head to sit down, shut up, and learn.

  def parse_diecode(self):
    diematch = re.match(r'^(?:(\d{1,2})|)d(\d{1,3})(?:([\+\-]\d{1,2})|)$',self.diecode,re.I)
    if diematch:
      self.number = int(diematch.group(1) or self.number)
      self.sides = int(diematch.group(2) or self.sides)
      self.modifier = int(diematch.group(3) or self.modifier)
    else:
      raise BadDieCode

    self.number, self.sides and self.modifier are set when the current
instance of the class is created.  In this class it is 1, 6 and 0
respectively.  In derived classes that can change.  The roll routines will, of
course, change, which is part of the reason why I was having problems with
having a list defined as "None".  Of course I was completely ignoring that it
was in the __init__ so I can make it what I want per class.  

    Finally, I am debating one more refinement to the regex to make the
portion after the d optional as well.

1d6
1d6+1
1d
1d+1
d6
d6+1

    And for the "that's neat" factor.

d
d+1

    Ah, now on to parsing text and figuring out how to remangle my logic
there.  It is, of course, regex happy as any good Perl programmer would have
it.  I had the diecode parsing as part of the main loop but wanted to make it
part of the base class so the class could be reused elsewhere.  That means
recognizing the diecode in the main loop but not parsing it there.

    Anyway, thanks again for the discussion to all.  I might post being grumpy
about something I stumbled across but hopefully it won't be anything that
lengthy.

-- 
         Steve C. Lamb         | I'm your priest, I'm your shrink, I'm your
         ICQ: 5107343          | main connection to the switchboard of souls.
-------------------------------+---------------------------------------------



More information about the Python-list mailing list