Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #4198 > unrolled thread
| Started by | Peter Otten <__peter__@web.de> |
|---|---|
| First post | 2011-04-30 15:06 +0200 |
| Last post | 2011-04-30 13:28 +0000 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: how to do random / SystemRandom switch Peter Otten <__peter__@web.de> - 2011-04-30 15:06 +0200
Re: how to do random / SystemRandom switch Matthias Kievernagel <mkiever@Pirx.sirius.org> - 2011-04-30 13:28 +0000
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2011-04-30 15:06 +0200 |
| Subject | Re: how to do random / SystemRandom switch |
| Message-ID | <iph1gl$5jh$1@solani.org> |
Matthias Kievernagel wrote: > In my top-level script I want to select if my program > is to use random.xxx functions or the random.SystemRandom.xxx > ones. All the other modules shouldn't know about that > switch and simply use > import random > ... > return random.randint(1, 6) > ... > for example. You can inject the SystemRandom instance into the sys.modules cache: >>> import random >>> random <module 'random' from '/usr/lib/python2.6/random.pyc'> >>> sr = random.SystemRandom() >>> import sys >>> sys.modules["random"] = sr Then use it in the other modules: >>> import random >>> random <random.SystemRandom object at 0x1acdb60> Another approach is to monkey-patch the random module: import random sr = random.SystemRandom() random.randrange = sr.randrange random.randint = sr.randint ...
[toc] | [next] | [standalone]
| From | Matthias Kievernagel <mkiever@Pirx.sirius.org> |
|---|---|
| Date | 2011-04-30 13:28 +0000 |
| Message-ID | <iph2q4$ped$1@speranza.aioe.org> |
| In reply to | #4198 |
Peter Otten <__peter__@web.de> wrote: > Matthias Kievernagel wrote: > >> In my top-level script I want to select if my program >> is to use random.xxx functions or the random.SystemRandom.xxx >> ones. All the other modules shouldn't know about that >> switch and simply use >> import random >> ... >> return random.randint(1, 6) >> ... >> for example. > > You can inject the SystemRandom instance into the sys.modules cache: > >>>> import random >>>> random > <module 'random' from '/usr/lib/python2.6/random.pyc'> >>>> sr = random.SystemRandom() >>>> import sys >>>> sys.modules["random"] = sr > > Then use it in the other modules: > >>>> import random >>>> random > <random.SystemRandom object at 0x1acdb60> > > Another approach is to monkey-patch the random module: > > import random > sr = random.SystemRandom() > random.randrange = sr.randrange > random.randint = sr.randint > ... > Thanks a lot. That's what I was looking for. I'll give both a try. Regards, Matthias Kievernagel
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web