-
Notifications
You must be signed in to change notification settings - Fork 0
/
WeightedTuple.py
147 lines (111 loc) · 3.81 KB
/
WeightedTuple.py
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
#!/usr/bin/env python
from BMovieConstants import *
import random
#//**********************************************************************
#//
#// WeightedTuple
#//
#// This class has a choice( ) function that allows for tracking
#// most-recently selected items and making sure it doesn't choose the
#// same item within a certain threshold of choices.
#//
#// The threshold is calculated to be half the length of the list
#// of unique choices rounded up.
#//
#// There really isn't anything tuple-ish about this class, but it
#// is used like a plan tuple in the data structures.
#//
#//**********************************************************************
class WeightedTuple( object ):
def __init__( self, values ):
if type( values ) is list:
self.weighted = True
self.values = [ ]
for i in range( 0, len( values ) - 1, 2 ):
self.values.extend( [ values[ i ] ] * values[ i + 1 ] )
try:
uniqueItems = len( set( values ) )
except:
uniqueItems = len( values )
if uniqueItems > 4:
self.maxHistory = uniqueItems // 4 + 1
elif uniqueItems == 4:
self.maxHistory = 1
else:
self.maxHistory = 0
elif type( values ) is tuple:
self.weighted = False
self.values = list( values )
try:
uniqueItems = len( set( values ) )
except:
uniqueItems = len( values )
if uniqueItems > 4:
self.maxHistory = uniqueItems // 4 + 1
elif uniqueItems == 4:
self.maxHistory = 1
else:
self.maxHistory = 0
self.mru = list( )
def choice( self ):
while True:
result = random.choice( self.values )
if result not in self.mru:
break
self.mru.append( result )
if len( self.mru ) > self.maxHistory:
self.mru = self.mru[ 1 : ]
return result
def __len__( self ):
return len( self.values )
def __getitem__( self, key ):
return self.values[ key ]
def __repr__( self ):
return self.repr( self )
def repr( self, target ):
#print( 'type: ', end='' )
#print( type( target ) )
if type( target ) is WeightedTuple:
result = 'WeightedTuple('
if target.weighted:
result += self.weighted_repr( target.values )
else:
result += self.repr( target.values )
result += ')'
elif type( target ) is str:
result = "'" + target + "'"
elif type( target ) is int:
result = wordTypeDescriptions[ target ]
elif isinstance( target, tuple ):
result = '('
for i in target:
result += self.repr( i ) + ','
result += ')'
elif isinstance( target, list ):
result = '['
for i in target:
result += self.repr( i ) + ','
result += ']'
else:
result = repr( target )
return result
def weighted_repr( self, target ):
repeat = 1
oldValue = ''
result = '['
#print( 'type: ', end='' )
#print( type( target ) )
for i in target:
#print( 'list element type: ', end='' )
#print( type( i ) )
#print( i )
if i == oldValue:
repeat += 1
else:
result += self.repr( i ) + ',\n' + str( repeat ) + ','
repeat = 1
oldValue = i
result += self.repr( i ) + ',' + str( repeat ) + ']'
#print( 'result: ', end='' )
#print( result )
return result