Python not that wierd

Alexander Williams thantos at gw.total-web.net
Thu Aug 3 05:54:27 EDT 2000


"Alex Martelli" <alex at magenta.com> writes:

> 10-sided solids (which as far as I recall can't be fully regular, i.e.
> Platonic) are just not as neat as (fully-regular, platonic) 20-sided

Nope, not fully regular, but close enough to dance with; I rather like
the dual-pyramidial design, but I've always been a neophile.

> OK.  A sum of arbitrary dice would no doubt be slightly awkward to handle
> anyway -- syntax is easy, but then you have to apply it.  Having more
> generality than is actually used is often fine, but simplicity must never
> be allowed to suffer for that...

Because its absolutely crucial I inject some Pythonization into my
comments on this thread, here's the source for the core of my current
dice-rolling system.

===

'''Library for simulating dice-rolls.

Designed to be used by "from dice import *"

Exported classes:
    Die(sides):
    DieList([Die ...]): 
        
Exported functions:
    d4, d6, d8, d10, d12, d20, d100:

'''

import whrandom, string


class DieList:
    def __init__(me, dieList=None):
        # dieList should be a list of instances of the Die class
        me.dieList = []
        me.dieTypes = []
        me.addDice(dieList)
        
    def __call__(me):
        '''Takes no args and returns a list of random ints'''
        outList = []
        for i in me.dieList:
            outList.append(i())
        return outList    

    def __repr__(me):
        return '<DieList of %s>' % me.dieList

    def __str__(me):
        outTypes = {}
        for d in me.dieList:
            dName = str(d)
            try:
                outTypes[dName] = outTypes[dName] + 1
            except KeyError:
                outTypes[dName] = 1

        outList = []

        for d in me.dieTypes:
            dName = str(d)
            if outTypes[dName] == 1:
                outList.append('%s' % d)
            else:
                outList.append('%s%s' % (outTypes[dName], d))
            
        return string.join(outList, ' & ')

    def addDie(me, d):
        me.dieList.append(d)
        me.dieList.sort()
        me.dieList.reverse()

        if d in me.dieTypes: pass
        else: me.dieTypes.append(d)
        me.dieTypes.sort()
        me.dieTypes.reverse()

    def addDice(me, dlist):
        if dlist:
            for d in dlist:
                me.addDie(d)
            
    def values(me):
        outList = []
        for d in me.dieList:
            outList.append(d())
        outList.sort()
        outList.reverse()
        return outList    

    def __add__(me, other):
        tmp = other.dieList[:]
        tmp.extend(me.dieList)
        return DieList(tmp)

    __radd__ = __add__
        

class Die:
    def __init__(me, sides):
        '''Class to encapsulate basic die operations.

        Die(sides) returns an instance of a Die with sides sides.'''
        
        me.sides = sides


    def __call__(me):
        '''instance() returns a random value between 1 and me.sides,
        inclusive'''

        return whrandom.randint(1, me.sides)


    def roll(me):
        '''roll() is just an alias for __call__'''
        me.__call__()


    def __repr__(me):
        return '<d' + str(me.sides) + ' Object>'


    def __str__(me):
        return 'd' + str(me.sides)


    def __cmp__(me, other):
        return cmp(me.sides, other)


    def __hash__(me):
        return id(me)
        

d4, d6, d8, d10, d12, d20, d100 = Die(4), Die(6), Die(8), Die(10), Die(12), \
                                  Die(20), Die(100)


-- 
Alexander Williams (thantos at gw.total-web.net)           | In the End,
  "Blue Jester needs food."                             | Oblivion
  "Blue Jester needs fuku-wearing cuties."              | Always
  http://www.chancel.org                                | Wins



More information about the Python-list mailing list