/usr/share/pyshared/pyepl/vr/randomconstrained.py is in python-pyepl 1.1.0-3build3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | # PyEPL: vr/randomconstrained.py
#
# Copyright (C) 2003-2005 Michael J. Kahana
# Authors: Ian Schleifer, Per Sederberg, Aaron Geller, Josh Jacobs
# URL: http://memory.psych.upenn.edu/programming/pyepl
#
# Distributed under the terms of the GNU Lesser General Public License
# (LGPL). See the license.txt that came with this file.
"""
"""
import random
def demutify(m):
"""
"""
if isinstance(m, Mutable):
return m.get()
return m
def mutapply(func, *targs, **dargs):
"""
"""
return Mutable(lambda *targs, **dargs: func(*map(demutify, targs), **dict(zip(dargs.iterkeys(), map(demutify, dargs.itervalues())))), *targs, **dargs)
class MutableMethod:
"""
"""
def __init__(self, obj, attr):
"""
"""
self.obj = obj
self.attr = attr
def __call__(self, *targs, **dargs):
"""
"""
return Mutable(lambda meth, *targs, **dargs: getattr(meth.obj.get(), meth.attr)(*map(demutify, targs),
**dict(zip(dargs.iterkeys(), map(demutify, dargs.itervalues())))),
self, *targs, **dargs)
class Mutable:
"""
"""
def __init__(self, *targs, **dargs):
"""
"""
if len(targs) or len(dargs):
self.set(*targs, **dargs)
else:
self.set(NotImplemented)
def __repr__(self):
"""
"""
return "<Mutable: %s>" % repr(self.get())
def __str__(self):
"""
"""
return str(self.get())
def __getattr__(self, name):
"""
"""
if name == "__coerce__" or name == "__hash__":
raise AttributeError
o = self.get()
a = getattr(o, name)
if callable(a):
return MutableMethod(self, name)
else:
return Mutable(lambda obj, name: getattr(obj, name), o, name)
def __lt__(self, other):
"""
"""
return Mutable(lambda a, b: a.get() < demutify(b), self, other)
def __le__(self, other):
"""
"""
return Mutable(lambda a, b: a.get() <= demutify(b), self, other)
def __eq__(self, other):
"""
"""
return Mutable(lambda a, b: a.get() == demutify(b), self, other)
def __ne__(self, other):
"""
"""
return Mutable(lambda a, b: a.get() != demutify(b), self, other)
def __gt__(self, other):
"""
"""
return Mutable(lambda a, b: a.get() > demutify(b), self, other)
def __ge__(self, other):
"""
"""
return Mutable(lambda a, b: a.get() >= demutify(b), self, other)
def __len__(self):
"""
"""
return len(self.get())
def __iter__(self):
"""
"""
return iter(self.get())
def __nonzero__(self):
"""
"""
return bool(self.get())
def contains(self, other):
"""
"""
return Mutable(lambda a, b: demutify(b) in a.get(), self, other)
def length(self):
"""
"""
return Mutable(lambda x: len(x.get()), self)
def get(self):
"""
"""
return self.func(*self.targs, **self.dargs)
def set(self, func, *targs, **dargs):
"""
"""
if callable(func):
self.func = func
self.targs = targs
self.dargs = dargs
elif isinstance(func, Mutable):
self.func = func.func
self.targs = func.targs
self.dargs = func.dargs
else:
self.func = lambda x: x
self.targs = (func,)
self.dargs = {}
class AssertionBlockItem:
"""
"""
def operate(self):
"""
"""
pass
class RandomFloatItem(AssertionBlockItem):
"""
"""
def __init__(self, target, min, max):
"""
"""
self.target = target
self.min = min
self.max = max
def operate(self):
"""
"""
self.target.set(random.uniform(demutify(self.min), demutify(self.max)))
return True
class RandomIntItem(AssertionBlockItem):
"""
"""
def __init__(self, target, min, max):
"""
"""
self.target = target
self.min = min
self.max = max
def operate(self):
"""
"""
self.target.set(random.randint(demutify(self.min), demutify(self.max)))
return True
class RandomChoiceItem(AssertionBlockItem):
"""
"""
def __init__(self, target, seq):
"""
"""
self.target = target
self.seq = seq
def operate(self):
"""
"""
self.target.set(random.choice(demutify(self.seq)))
return True
class RandomSampleItem(AssertionBlockItem):
"""
"""
def __init__(self, target, population, k):
"""
"""
self.target = target
self.population = population
self.k = k
def operate(self):
"""
"""
self.target.set(random.sample(demutify(self.population), demutify(self.k)))
return True
class AssertionItem(AssertionBlockItem):
"""
"""
def __init__(self, condition):
"""
"""
self.condition = condition
def operate(self):
"""
"""
return bool(demutify(self.condition))
class AssertionBlock(AssertionBlockItem):
"""
"""
def __init__(self):
"""
"""
self.items = []
def operate(self):
"""
"""
self.randomize()
return True
def randomize(self):
"""
"""
go = True
while go:
go = False
for item in self.items:
if not item.operate():
go = True
break
def randomFloat(self, min = 0.0, max = 1.0):
"""
"""
r = Mutable()
self.items.append(RandomFloatItem(r, min, max))
return r
def randomInt(self, min, max):
"""
"""
r = Mutable()
self.items.append(RandomIntItem(r, min, max))
return r
def randomChoice(self, seq):
"""
"""
r = Mutable()
self.items.append(RandomChoiceItem(r, seq))
return r
def randomSample(self, population, k = None):
"""
"""
if k == None:
k = len(population)
r = Mutable()
self.items.append(RandomSampleItem(r, population, k))
return r
def assertion(self, condition):
"""
"""
self.items.append(AssertionItem(condition))
def subBlock(self):
"""
"""
r = AssertionBlock()
self.items.append(r)
return r
|