-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathitems.py
More file actions
107 lines (102 loc) · 3.11 KB
/
items.py
File metadata and controls
107 lines (102 loc) · 3.11 KB
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
import pdb
import rstr
import utils
class Item:
names = {}
def __init__(self, s=None, **keys):
self.s = s
@classmethod
def from_str(cls, s):
pass
def evaluate(self, s):
for k in Item.names.keys():
if k in s:
s = s.replace(k, str(Item.names[k]))
# if s in Item.names:
# return Item.names[s]
return eval(s)
def generate(self):
pass
def __str__(self):
if hasattr(self, "s"):
return self.s
class Int(Item):
def __init__(self, name, low, high, s=None, **keys):
"""
correspond to the input value between two spaces
name: str
name of variable
low/high : str
min / max (inclusive)
"""
self.name = name
self.low = low
self.high = high
self.keys = keys
Item.__init__(self, s)
@classmethod
def from_str(cls, s):
name, low, high = s.split(",")
return cls(name, low, high, s=s)
def generate(self):
low, high = self.evaluate(self.low), self.evaluate(self.high)
value = utils.rng.integers(low, high+1)
Item.names[self.name] = value
return str(value)
def __str__(self):
if hasattr(self, "s"):
return self.s
class Float(Int):
"""float value of [low, high) with digit numbers after decimal point
"""
@classmethod
def from_str(cls, s):
name, low, high, digit = s.split(",")
return cls(name, low, high, s=s, digit=digit)
def generate(self):
low, high = self.evaluate(self.low), self.evaluate(self.high)
s = str(utils.rng.uniform(low, high))
sp = s.split(".")
s = sp[0]+"."+sp[1][:int(self.keys["digit"])]
Item.names[self.name] = float(s)
return s
class Perm(Item):
"""permutation of [low, low+1, ..., high-1, high]
"""
def __init__(self, low, high, s=None):
self.low = low
self.high = high
self.s = s
@classmethod
def from_str(cls, s):
low, high = s.split(",")
return cls(low, high, s=s)
def generate(self):
low, high = self.evaluate(self.low), self.evaluate(self.high)
return " ".join(map(str, (utils.rng.permutation(high-low+1) + low).tolist()))
class Str(Item):
def __init__(self, pattern):
"""
pattern : str
regular expression pattern
s.t.
[a-zA-Z]{10}
[a-z]{10,Name}
(value of Name will be substituted runtime)
You cannot specify complicated pattern which does not exist in above example
"""
self.pattern = pattern
self.s = pattern
@classmethod
def from_str(cls, s):
return cls(s)
def generate(self):
pattern = self.pattern
if "{" in pattern:
p, tmp = pattern.split("{", 1)
val, other = tmp.split("}", 1)
l = []
for item in val.split(","):
l.append(str(self.evaluate(item)))
pattern = p + "{" + ",".join(l) + "}" + other
return rstr.xeger(pattern)