CMS 3D CMS Logo

Public Member Functions | Private Member Functions | Private Attributes

RandomServiceHelper::RandomNumberServiceHelper Class Reference

List of all members.

Public Member Functions

def __init__
def countSeeds
def getNamedSeed
def insertSeeds
def populate
def resetSeeds
def setNamedSeed

Private Member Functions

def __containsSeed
def __psetsWithSeeds

Private Attributes

 _lockedSeeds
 _randService

Detailed Description

_RandomNumberServiceHelper_

Helper class to hold and handle the Random number generator service.

Provide both user level and WM APIs.

Revision: "$Id: RandomServiceHelper.py,v 1.4 2008/06/10 19:49:49 ewv Exp $"
Version   "$Revision: 1.4 $"
Author:   Dave Evans
Modified: Eric Vaandering

Definition at line 8 of file RandomServiceHelper.py.


Constructor & Destructor Documentation

def RandomServiceHelper::RandomNumberServiceHelper::__init__ (   self,
  randService 
)

Definition at line 22 of file RandomServiceHelper.py.

00023                                   :
00024         self._randService = randService
00025         self._lockedSeeds = []
00026 


Member Function Documentation

def RandomServiceHelper::RandomNumberServiceHelper::__containsSeed (   self,
  psetInstance 
) [private]
_keeper_

True/False if the psetInstance has seeds in it

Definition at line 27 of file RandomServiceHelper.py.

00028                                          :
00029         """
00030         _keeper_
00031 
00032         True/False if the psetInstance has seeds in it
00033 
00034         """
00035         if psetInstance == None:
00036             return False
00037         if not isinstance(psetInstance,CfgTypes.PSet):
00038             return False
00039         seedList = getattr(psetInstance, "initialSeedSet", None)
00040         if seedList != None:
00041             return True
00042         seedVal = getattr(psetInstance, "initialSeed", None)
00043         if seedVal != None:
00044             return True
00045         return False
00046 

def RandomServiceHelper::RandomNumberServiceHelper::__psetsWithSeeds (   self) [private]
_psetsWithSeeds_

*private method*

return the list of PSet instances with seeds in them

Definition at line 47 of file RandomServiceHelper.py.

00048                               :
00049         """
00050         _psetsWithSeeds_
00051 
00052         *private method*
00053 
00054         return the list of PSet instances with seeds in them
00055 
00056         """
00057         svcAttrs = [getattr(self._randService, item, None)
00058                     for item in self._randService.parameters_()
00059                     if item not in self._lockedSeeds]
00060 
00061         #print svcAttrs
00062 
00063         return filter(self.__containsSeed, svcAttrs)
00064 

def RandomServiceHelper::RandomNumberServiceHelper::countSeeds (   self)
_countSeeds_

Count the number of seeds required by this service by
summing up the initialSeed and initialSeedSet entries
in all PSets in the service that contain those parameters.

Definition at line 65 of file RandomServiceHelper.py.

00066                         :
00067         """
00068         _countSeeds_
00069 
00070         Count the number of seeds required by this service by
00071         summing up the initialSeed and initialSeedSet entries
00072         in all PSets in the service that contain those parameters.
00073 
00074         """
00075         count = 0
00076 
00077         for itemRef in self.__psetsWithSeeds():
00078             #  //
00079             # // PSet has list of seeds
00080             #//
00081             seedSet = getattr(itemRef, "initialSeedSet", None)
00082             if seedSet != None:
00083                 count += len( seedSet.value())
00084                 continue
00085             #  //
00086             # // PSet has single seed
00087             #//
00088             seedVal =  getattr(itemRef, "initialSeed", None)
00089             if seedVal != None:
00090                 count += 1
00091 
00092             #  //
00093             # // PSet has no recognisable seed, therfore do nothing
00094             #//  with it
00095         return count
00096 

def RandomServiceHelper::RandomNumberServiceHelper::getNamedSeed (   self,
  psetName 
)
_getNamedSeed_

This method returns the seeds in a PSet in this service. Returned

- *psetName* : Name of the pset containing the seeds

Definition at line 139 of file RandomServiceHelper.py.

00140                                     :
00141         """
00142         _getNamedSeed_
00143 
00144         This method returns the seeds in a PSet in this service. Returned
00145 
00146         - *psetName* : Name of the pset containing the seeds
00147 
00148         """
00149         pset = getattr(self._randService, psetName, None)
00150         if pset == None:
00151             msg = "No PSet named %s belongs to this instance of the" % (
00152                 psetName,)
00153             msg += "Random Seed Service"
00154             raise RuntimeError, msg
00155 
00156         seedVal = getattr(pset, "initialSeed", None)
00157         if seedVal != None:
00158             return [pset.initialSeed.value()]
00159 
00160         seedSet = getattr(pset, "initialSeedSet", None)
00161         if seedSet != None:
00162             return pset.initialSeedSet
00163 

def RandomServiceHelper::RandomNumberServiceHelper::insertSeeds (   self,
  seeds 
)
_insertSeeds_

Given some list of specific seeds, insert them into the
service.

Length of seed list is required to be same as the seed count for
the service.

Usage: WM Tools.

Definition at line 164 of file RandomServiceHelper.py.

00165                                  :
00166         """
00167         _insertSeeds_
00168 
00169         Given some list of specific seeds, insert them into the
00170         service.
00171 
00172         Length of seed list is required to be same as the seed count for
00173         the service.
00174 
00175         Usage: WM Tools.
00176 
00177         """
00178         seeds = list(seeds)
00179         if len(seeds) < self.countSeeds():
00180             msg = "Not enough seeds provided\n"
00181             msg += "Service requires %s seeds, only %s provided\n"
00182             msg += "to RandomeService.insertSeeds method\n"
00183             raise RuntimeError, msg
00184 
00185         for item in self.__psetsWithSeeds():
00186             seedSet = getattr(item, "initialSeedSet", None)
00187             if seedSet != None:
00188                 numSeeds = len(seedSet.value())
00189                 useSeeds = seeds[:numSeeds]
00190                 seeds = seeds[numSeeds:]
00191                 item.initialSeedSet = CfgTypes.untracked(
00192                     CfgTypes.vuint32(*useSeeds))
00193                 continue
00194             useSeed = seeds[0]
00195             seeds = seeds[1:]
00196             item.initialSeed = CfgTypes.untracked(
00197                 CfgTypes.uint32(useSeed)
00198                 )
00199             continue
00200         return
00201 

def RandomServiceHelper::RandomNumberServiceHelper::populate (   self,
  excludePSets 
)
_populate_

generate a bunch of seeds and stick them into this service
This is the lazy user method.

Optional args are names of PSets to *NOT* alter seeds.

Eg:
populate() will set all seeds
populate("pset1", "pset2") will set all seeds but not those in
psets named pset1 and pset2

Definition at line 202 of file RandomServiceHelper.py.

00203                                      :
00204         """
00205         _populate_
00206 
00207         generate a bunch of seeds and stick them into this service
00208         This is the lazy user method.
00209 
00210         Optional args are names of PSets to *NOT* alter seeds.
00211 
00212         Eg:
00213         populate() will set all seeds
00214         populate("pset1", "pset2") will set all seeds but not those in
00215         psets named pset1 and pset2
00216 
00217         """
00218 
00219         import random
00220         from random import SystemRandom
00221         _inst = SystemRandom()
00222         _MAXINT = 900000000
00223 
00224         #  //
00225         # // count seeds and create the required number of seeds
00226         #//
00227         newSeeds = [ _inst.randint(1, _MAXINT)
00228                      for i in range(self.countSeeds())]
00229 
00230 
00231         self._lockedSeeds = list(excludePSets)
00232         self.insertSeeds(*newSeeds)
00233         self._lockedSeeds = []
00234         return
00235 

def RandomServiceHelper::RandomNumberServiceHelper::resetSeeds (   self,
  value 
)
_resetSeeds_

reset all seeds to given value

Definition at line 236 of file RandomServiceHelper.py.

00237                                :
00238         """
00239         _resetSeeds_
00240 
00241         reset all seeds to given value
00242 
00243         """
00244         newSeeds = [ value for i in range(self.countSeeds())]
00245         self.insertSeeds(*newSeeds)
00246         return
00247 
00248 

def RandomServiceHelper::RandomNumberServiceHelper::setNamedSeed (   self,
  psetName,
  seeds 
)
_setNamedSeed_

If a specific set of seeds is needed for a PSet in this
service, they can be set by name using this method.

- *psetName* : Name of the pset containing the seeds

- *seeds*    : list of seeds to be added, should be a single seed
for initialSeed values.

Definition at line 97 of file RandomServiceHelper.py.

00098                                             :
00099         """
00100         _setNamedSeed_
00101 
00102         If a specific set of seeds is needed for a PSet in this
00103         service, they can be set by name using this method.
00104 
00105         - *psetName* : Name of the pset containing the seeds
00106 
00107         - *seeds*    : list of seeds to be added, should be a single seed
00108         for initialSeed values.
00109 
00110         """
00111         pset = getattr(self._randService, psetName, None)
00112         if pset == None:
00113             msg = "No PSet named %s belongs to this instance of the" % (
00114                 psetName,)
00115             msg += "Random Seed Service"
00116             raise RuntimeError, msg
00117 
00118         seedVal = getattr(pset, "initialSeed", None)
00119         if seedVal != None:
00120             pset.initialSeed = CfgTypes.untracked(
00121                 CfgTypes.uint32(seeds[0])
00122                 )
00123 
00124             return
00125         seedSet = getattr(pset, "initialSeedSet", None)
00126         if seedSet != None:
00127             #  //
00128             # // Do we want to check the number of seeds??
00129             #//
00130             #if len(seeds) != len( seedSet.value()): pass
00131             pset.initialSeedSet = CfgTypes.untracked(
00132                 CfgTypes.vuint32(*seeds))
00133             return
00134         #  //
00135         # // No seeds for that PSet
00136         #//  Error throw?
00137         return
00138 


Member Data Documentation

Definition at line 22 of file RandomServiceHelper.py.

Definition at line 22 of file RandomServiceHelper.py.