[Tutor] Objects & Classes...

Max Noel maxnoel_fr at yahoo.fr
Mon Jan 17 23:43:09 CET 2005


On Jan 17, 2005, at 22:02, Chad Crabtree wrote:

> (why would an attri bute need to be a separate class?)

	So that the points or Karma (depending on the generation system) cost 
of the Attribute can be calculated without having to resort to 
procedural programming.

	You'd be then able to get the Karma cost of all the attributes by 
using a line such as:

return sum([att.getKarmaCost() for att in attributes])

	The following is a preliminary Attribute class I dug up from my hard 
drive, made for the BeCKS creation system. Feel free to use bits of 
it...




#!/usr/bin/env python

class Attribute:
     """Shadowrun character attribute."""
     def __init__(self, baseRating = 1, racialMod = 0, augCyber = 0, 
augMagic = 0):
         """Create an Attribute."""

         # Initialize the class attributes...
         self._baseRating = 1
         self._racialMod = 0
         self._augCyber = 0
         self._augMagic = 0

         self.setBaseRating(baseRating)
         self.setRacialMod(racialMod)
         self.setAugCyber(augCyber)
         self.setAugMagic(augMagic)


     def getRating(self):
         """Return the attribute's modified Rating."""
         return self._baseRating + self._augCyber + self._augMagic


     def getBaseRating(self):
         """Return the attribute's base Rating (unmodified by magic or 
cyber)."""
         return self._baseRating


     def setBaseRating(self, baseRating):
         """Set the attribute's base Rating. baseRating must be an int 
equal to or greater than max(1, 1 + racial mod)."""
         if type(baseRating) != int:
             raise TypeError, "Attribute rating must be an int."
         if baseRating < 1:
             # Attribute rating can't be lower than 1.
             baseRating = 1
         if baseRating < 1 + self._racialMod:
             # Attribute rating can't be lower than 1 + racial mod.
             baseRating = 1 + self._racialMod
         self._baseRating = baseRating


     def getRacialMod(self):
         return self._racialMod


     def setRacialMod(self, racialMod = 0):
         """Set the racial modifier for the attribute. racialMod must be 
an int."""
         if type(racialMod) != int:
             raise TypeError, "Racial modifier must be an int."
         self._racialMod = racialMod
         # Re-set the base Rating to enforce the racial limits
         self.setBaseRating(self._baseRating)


     def getAugCyber(self):
         return self._augCyber


     def setAugCyber(self, augCyber):
         """Set the cyber augmentation(s) for the Attribute. augCyber 
must be an int."""
         if type(augCyber) != int:
             raise TypeError, "Cyber augmentation must be an int."
         self._augCyber = augCyber


     def getAugMagic(self):
         return self._augMagic


     def setAugMagic(self, augMagic):
         """Set the magic augmentation(s) for the Attribute. augMagic 
must be an int."""
         if type(augMagic) != int:
             raise TypeError, "Magic augmentation must be an int."
         self._augMagic = augMagic


     def getKarmaCost(self):
         """Return the BeCKS Karma cost of the Attribute."""
         rating = self.getBaseRating()
         racialMod = self.getRacialMod()
         minr = max(1, 1 + racialMod)
         maxr = 6 + racialMod
         if rating <= maxr:
             # Could (will) be optimized for speed, but the formula is 
much clearer this way
             cost = sum([2*i for i in range(minr + 1, rating + 1)])
         else:
             cost = sum([2*i for i in range(minr + 1, maxr + 1)]) + 
sum([3*i for i in range(maxr + 1, rating + 1)])
         return cost



if __name__ == '__main__':
     # Run a simple test procedure
     atr = Attribute()
     print atr.getRating()
     atr.setRacialMod(1)
     print atr.getRating()
     atr.setAugCyber(1)
     atr.setAugMagic(1)
     atr.setBaseRating(3)
     print atr.getBaseRating(), atr.getRating()
     print 'Karma total cost', atr.getKarmaCost()
     del atr




-- Max
maxnoel_fr at yahoo dot fr -- ICQ #85274019
"Look at you hacker... A pathetic creature of meat and bone, panting 
and sweating as you run through my corridors... How can you challenge a 
perfect, immortal machine?"



More information about the Tutor mailing list