Created
January 3, 2017 20:02
-
-
Save NoahTheDuke/6e8c88275c5e743fba971549d8b70759 to your computer and use it in GitHub Desktop.
A simple example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
from bisect import bisect | |
from itertools import accumulate | |
class Struct: | |
def __init__(self, alist): | |
self.alist = alist | |
def main(): | |
example = Struct([1, 2, 3, 4, 5, 6, 7, 8, 9]) | |
arguments = { | |
'arg_1': 2, | |
'arg_2': 5, | |
} | |
apply_functions(example, arguments) | |
def apply_functions(data_struct, arguments): | |
funcs = [func_1, func_2, func_3] | |
weights = [1, 2, 3] | |
func_weights = list(accumulate(weights)) | |
probability = bisect(func_weights, random.random() * func_weights[-1]) | |
funcs[probability](data_struct, **arguments) | |
print(data_struct.alist) | |
def func_1(data_s, arg_1, **kwargs): | |
print('func_1:', arg_1, kwargs) | |
def func_2(data_s, arg_2, **kwargs): | |
print('func_2:', arg_2, kwargs) | |
def func_3(data_s, arg_1, arg_2, **kwargs): | |
print('func_3:', arg_1, arg_2, kwargs) | |
alist = data_s.alist | |
temp = alist[:arg_1] + alist[arg_2:] | |
point = random.randint(0, len(temp)) | |
data_s.alist[:] = temp[:point] + alist[arg_1:arg_2] + temp[point:] | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment