def test_generate_one_one(self): options = {"boss": "One to One"} rando = Randomizer(tempdir=self._get_tmp_path()) b64 = rando.generate_seed("kh2", options=options) import base64 # Make sure it's valid base64 base64.decodebytes(b64)
async def test_simple_scan(self): randomizer = Randomizer(seed=3284) await self.system_clock_fast_forward(speed_multiplier=20) commutator, error = await self.login('oreman', "127.0.0.1") self.assertIsNotNone(commutator) self.assertIsNone(error) clock = modules.get_system_clock(commutator) self.assertIsNotNone(clock) miner_1 = modules.get_ship(commutator, ShipType.MINER.value, "miner-1") self.assertIsNotNone(miner_1) scanner = modules.PassiveScanner.get_by_name(miner_1, "perceiver") self.assertIsNotNone(scanner) spec = await scanner.get_specification() self.assertIsNotNone(spec) ship_position = await miner_1.get_position() spawned_asteroids: List[types.PhysicalObject] = [ await self.administrator.get_spawner().spawn_asteroid( position=randomizer.random_position( center=ship_position, radius=2 * spec.scanning_radius_km * 1000), composition=types.make_resources(ice=100, metals=32), radius=randomizer.random_value(5, 20)) for i in range(1000) ] # Take 10 seconds to scan scanned_objects = await TestCase.scanning(scanner, clock, 10000) for candidate in spawned_asteroids: distance = ship_position.distance_to(candidate.position) if distance <= spec.scanning_radius_km * 1000: self.assertIn(candidate.object_id, scanned_objects)
def get(self): randomizer = Randomizer() pattern = randomizer.get_pattern() left = randomizer.get_number() operator = randomizer.get_operator() right = randomizer.get_number() self.captcha = Captcha(pattern, left, operator, right)
def test_generate_all_xemnas(self): options = {"selected_boss": "Xemnas", "boss": "Selected Boss"} rando = Randomizer(tempdir=self._get_tmp_path()) b64 = rando.generate_seed("kh2", options=options) import base64 # Make sure it's valid base64 base64.decodebytes(b64)
def run(event, context): # Test out S3 upload capability url = 'https://raw.githubusercontent.com/mozilla/http-observatory-dashboard/master/httpobsdashboard/conf/sites.json' random_site = Randomizer(url) scanner = ObservatoryScanner() scan_result = scanner.scan(random_site.next()) logger.info(scan_result)
def __init__(self, driver, logger): super(Facebook, self).__init__() self.driver = driver self.int_to_res = {1: '240p', 2: '360p', 3: '480p', 4: '720p'} self.r = Randomizer('facebook') self.logger = logger self.error_count = 0
class Netflix(object): """docstring for Netflix""" def __init__(self, driver, logger): super(Netflix, self).__init__() self.driver = driver self.r = Randomizer('netflix') self.logger = logger def login(self): try: self.driver.get("https://www.netflix.com/browse") user, password = open("credentials.txt", "r").read().splitlines() user_xp = '//*[@id="appMountPoint"]/div/div[2]/div/div/form[1]/label/input' self.driver.find_element_by_xpath(user_xp).send_keys(user) pass_xp = '//*[@id="appMountPoint"]/div/div[2]/div/div/form[1]/div[1]/label/input' self.driver.find_element_by_xpath(pass_xp).send_keys(password) sign_xp = '//*[@id="appMountPoint"]/div/div[2]/div/div/form[1]/button' self.driver.find_element_by_xpath(sign_xp).click() hassan_xp = '//*[@id="appMountPoint"]/div/div/div[2]/div/div/ul/li[3]/div/a/div/div' self.driver.find_element_by_xpath(hassan_xp).click() except: self.logger.log("Netflix: Login Failed") time.sleep(2) self.login() time.sleep(4) def play_random_video(self): self.driver.get("https://www.netflix.com/browse") time.sleep(3) pane_num = random.randint(1, 4) vid_num = random.randint(1, 4) vid_xp = '//*[@id="title-card-%d-%d"]/div/div[1]' % (pane_num, vid_num) try: self.driver.find_element_by_xpath(vid_xp).click() time.sleep(2) div = self.driver.find_element_by_class_name('jawBone') a_tag = div.find_elements_by_tag_name('a')[1] a_tag.click() except: self.logger.log("Netflix: Video playing Failed") self.driver.get("https://www.netflix.com/browse") time.sleep(3) self.play_random_video() play_time = self.r.get_play_time() time_str = "%dm,%ds" % (play_time / 60, play_time % 60) self.logger.write('Netflix', time_str, 'Auto') self.logger.log("Netflix: Playing video with resolution Auto for %s" % time_str) time.sleep(play_time) def random_play(self): self.login() while 1: self.play_random_video() wait_time = self.r.get_wait_time() self.driver.get("chrome://settings/") self.logger.log("Netflix: Waiting for %dm,%ds" % (wait_time / 60, wait_time % 60)) time.sleep(wait_time)
def __init__(self, seed, initial_t, positive_examples, annealer): self.randomizer = Randomizer(seed) self.annealer = annealer self.T = initial_t self.positive_examples = positive_examples # self.hyp = self.annealer.find_initial_hypthesis(data) self.hyp = self.annealer.initial_hypothesis() self.creation_time = datetime.datetime.now()
def __init__(self, seed, initial_t, data, annealer): self.randomizer = Randomizer(seed) self.annealer = annealer self.T = initial_t self.data = data # self.hyp = self.annealer.find_initial_hypthesis(data) self.hyp = self.annealer.initial_hypothesis() self.creation_time = datetime.datetime.now()
def test_digit(self): """ Does it return the digit character class? """ metacharacter = CharacterClass.digit test = Randomizer.integer(maximum=9) self.assertIsNotNone(re.search(metacharacter, str(test))) self.assertIsNone(re.search(metacharacter, Randomizer.letters())) return
def test_read_all_zexion(self): seedfn = os.path.join(os.path.dirname(__file__), "testdata", "zexion.json") if os.path.exists(os.path.join(self._get_tmp_path(), "test")): shutil.rmtree(os.path.join(self._get_tmp_path(), "test")) rando = Randomizer(tempdir=self._get_tmp_path(), tempfn="test") b64 = rando.read_seed("kh2", seedfn=seedfn) import base64 # Make sure it's valid base64 base64.decodebytes(b64)
def test_group(self): """ Does the group method add parentheses? """ sample = Randomizer.letters() exp = Group.group(sample) self.assertEqual("(" + sample + ")",exp) matched = re.search(exp,sample+Randomizer.letters()).groups()[0] self.assertEqual(matched, sample) return
def Seed(self, seed=None): """ Initializes the random number generator with the given seed. Keyword arguments: seed -- The seed with which to intialize the random number generator. """ randomizer = Randomizer() self.Randomizer, seed = randomizer.intialize(seed) return [seed]
def test_single_digit(self): """ Does it return an expression to match only one digit? """ expression = Numbers.single_digit test = str(Randomizer.integer(maximum=9)) two_many = str(Randomizer.integer(minimum=10)) self.assertIsNotNone(re.search(expression, test)) self.assertIsNone(re.search(expression, two_many)) return
def test_two_digits(self): """ Does it return an expression to match exactly two digits? """ expression = Numbers.two_digits test = str(Randomizer.integer(minimum=10,maximum=99)) fail = random.choice([str(Randomizer.integer(0,9)), str(Randomizer.integer(100,1000))]) self.assertIsNotNone(re.search(expression, test)) self.assertIsNone(re.search(expression, fail)) return
def test_string_end(self): """ Does it return the end of string metacharacter? """ metacharacter = Boundaries.string_end word = Randomizer.letters() expression = word + metacharacter text = Randomizer.letters() + word self.assertIsNotNone(re.search(expression, text)) self.assertIsNone(re.search(expression, text + Randomizer.letters())) return
def test_zero_or_one(self): """ Does it return the zero-or-one quantifier? """ substring = Randomizer.letters() text = Randomizer.letters() expression = text + Quantifier.zero_or_one("(" + substring + ")") text_1 = text + substring * random.randint(1,100) text_2 = text + substring * random.randint(1,100) self.assertIsNotNone(re.search(expression, text_1)) self.assertEqual(re.search(expression, text_2).groups()[0], substring) return
def test_zero_or_more(self): """ Does it return the kleene star? """ substring = Randomizer.letters() text = Randomizer.letters() complement = text + Randomizer.letters_complement(substring) expression = text + Quantifier.zero_or_more('(' + substring + ')') text_1 = text + substring * random.randint(0, 10) + Randomizer.letters() self.assertIsNotNone(re.search(expression, complement)) self.assertIsNotNone(re.search(expression, text_1)) return
def test_preceded_by(self): "Does it match a substring with a prefix?" name = 'preceded' body = Randomizer.letters() sub_string = Randomizer.letters() prefix = Randomizer.letters() expression = Group.named(name, Group.preceded_by(prefix) + sub_string) text = body + prefix + sub_string match = re.search(expression, text) self.assertEqual(match.group(name), sub_string)
def test_word_boundary(self): """ Does it add word-boundaries to the expression """ word = Randomizer.letters() expected = r'\b' + word + r'\b' expression = Boundaries.word(word) bad_word = word + Randomizer.letters() text = ' '.join([Randomizer.letters(),word,Randomizer.letters()]) self.assertIsNone(re.search(expression, bad_word)) self.assertIsNotNone(re.search(expression, text)) return
def test_one_or_more(self): """ Does it return the one-or-more metacharachter? """ character = random.choice(string.letters) complement = Randomizer.letters_complement(character) text = Randomizer.letters() + character * random.randint(1,100) + Randomizer.letters() expression = character + '+' self.assertIsNone(re.search(expression, complement)) self.assertIsNotNone(re.search(expression, text)) return
def test_not(self): ''' Does it convert the string to a non-matching class? ''' sample = Randomizer.letters(maximum=10) complement = Randomizer.letters_complement(sample) expression = CharacterClass.not_in(sample) self.assertEqual(LEFT_BRACKET + '^' + sample + RIGHT_BRACKET, expression) self.assertIsNone(re.search(expression, sample)) self.assertIsNotNone(re.search(expression, complement)) return
def test_named(self): """ Does the named method create a named group? """ name = Randomizer.letters() sample = Randomizer.letters() text = Randomizer.letters() + sample + Randomizer.letters() exp = Group.named(name=name, expression=sample) expected = '(?P<' + name + '>' + sample + ")" self.assertEqual(expected, exp) matched = re.search(exp, text).groupdict()[name] self.assertEqual(sample, matched) return
def test_followed_by(self): """ Does it match strings followed by a pattern? """ body = Randomizer.letters() sub_string = Randomizer.letters() suffix = Randomizer.letters() text = body + sub_string + suffix name = 'followed' expression = Group.named(name, sub_string + Group.followed_by(suffix)) match = re.search(expression, text) self.assertEqual(match.group(name), sub_string)
def test_digits(self): "Does it match one or more digits?" expression = Group.named(name='digits', expression=Numbers.digits) first = "{0}".format(random.randint(0,9)) rest = str(random.randint(0,1000)) test = first + rest self.assertIsNotNone(re.search(expression, test)) match = re.search(expression, test) self.assertEqual(match.group('digits'), test) mangled = Randomizer.letters() + test + Randomizer.letters() match = re.search(expression, mangled) self.assertEqual(match.group('digits'), test) return
def test_class(self): ''' Does it convert the string to a character class? ''' sample = Randomizer.letters() expression = CharacterClass.character_class(sample) self.assertEqual(LEFT_BRACKET + sample + RIGHT_BRACKET, expression) sub_string = random.choice(sample) complement = Randomizer.letters_complement(sample) self.assertIsNotNone(re.search(expression, sub_string)) self.assertIsNone(re.search(expression, complement)) return
def __init__(self, width, height, level, gravity, seed): self.state = 1 self.randomizer = Randomizer(seed) self.current_tetromino = self.randomizer.get_tetromino() self.next_tetromino = self.randomizer.get_tetromino() self.grid = Grid(width, height) self.score = Score() self.level = level self.gravity = gravity self.threshold = min(level * 10 + 10, max(100, level * 10 - 50)) self.is_dropping = False self.delay = None self.timer = Timer(60)
def test_string_start(self): """ Does it have return a string start metacharacter? """ metacharacter = Boundaries.string_start expected = '^' self.assertEqual(expected, metacharacter) word = Randomizer.letters() expression = Boundaries.string_start + word text = word + Randomizer.letters() self.assertIsNotNone(re.search(expression, text)) self.assertIsNone(re.search(expression, " " + text)) return
def test_exactly(self): """ Does it return the repetition suffix? """ repetitions = Randomizer.integer(minimum=1, maximum=5) repeater = Randomizer.letters() expected = "{" + "{0}".format(repetitions) + "}" quantifier = Quantifier.exactly(repetitions) self.assertEqual(expected, quantifier) expression = "(" + repeater + ")" + quantifier text = Randomizer.letters() + repeater * (repetitions + Randomizer.integer(minimum=0)) self.assertIsNotNone(re.search(expression, text)) self.assertEqual(re.search(expression, text).groups(), (repeater,)) return
def test_get_json_with_stub_randomizer(self): stub_randomizer = Randomizer() def get_pattern(): return 1 def get_number(): return 1 def get_operator(): return 1 captcha_controller = CaptchaController() stub_randomizer.get_pattern = get_pattern stub_randomizer.get_number = get_number stub_randomizer.get_operator = get_operator captcha_controller.randomizer = stub_randomizer self.assertTrue('{"left":"ONE", "operator":"+", "right":"1"}', captcha_controller.to_json())
def test_not_preceded_by(self): ''' Does it create a negative look-behind expression? ''' prefix = Randomizer.letters() expr = Group.not_preceded_by(prefix) self.assertEqual(L_PERL_GROUP + "<!" + prefix + R_GROUP, expr) text = Randomizer.letters(minimum=5) is_preceded_by = prefix + text self.assertIsNone(re.search(expr + text, is_preceded_by)) self.assertIsNotNone(re.search(expr + text, text)) return
def test_not_followed_by(self): """ Does not_followed_by create a negative lookahead assertion? """ prefix = Randomizer.letters(maximum=5) suffix = Randomizer.letters_complement(prefix) expr = Group.not_followed_by(suffix) text = Randomizer.letters() self.assertEqual(L_PERL_GROUP + '!' + suffix + R_GROUP, expr) self.assertIsNone(re.search(text + expr, text + suffix)) self.assertIsNotNone(re.search(text + expr, text)) return
def __init__(self, n, seed=None): """ Initializes the Fast Mutex Algorithm. The fast mutex algorithm has an array of log(n) gadgets each of depth log(n). We maintain both a list of processes and a hash table containing a list of process ids spinning on each register. """ self.rand = Randomizer(n, seed) self.n = n self.logn = int(ceil(log(n, 2))) self.gadgets = 2 * self.logn self.reg_per_gadget = self.logn self.mem = -1 * np.ones((self.gadgets, self.reg_per_gadget)) self.writers = -1 * np.ones((self.gadgets, self.reg_per_gadget)) self.spinners = { i: {j: [] for j in range(self.reg_per_gadget)} for i in range(self.gadgets) } self.proc_list = [ Process(i, self.rand, self.gadgets, self.reg_per_gadget) for i in range(n) ] #self.scheduler = SingleScheduler(self.proc_list, k = None) #self.scheduler = KRoundRobinScheduler(self.proc_list, k = None) self.scheduler = KRRRandomScheduler(self.proc_list, k=None) self.quiet = set(self.proc_list) self.spin = set() self.seek = set() self.crit = set() self.rmr = 0
def test_get_random_int(self): min = 1 max = 100 num = Randomizer.get_random_int(min, max) self.assertIsNotNone(num) self.assertTrue(num >= min) self.assertTrue(num <= max)
def test_get_random_float(self): min = 1.25 max = 87.0124 num = Randomizer.get_random_float(min, max) self.assertIsNotNone(num) self.assertTrue(num >= min) self.assertTrue(num <= max)
async def test_spawn_asteroids(self): """Check that spawn.asteroid request returns asteroid id""" self.assertTrue(await self.system_clock_play()) randomizer = Randomizer(2132) spawner = self.administrator.spawner for i in range(100): now = await self.system_clock_time() asteroid = await spawner.spawn_asteroid( position=randomizer.random_position(rect=types.Rect( -1000, 1000, -1000, 1000), min_speed=0, max_speed=1000), composition=types.make_resources(ice=100, metals=32), radius=10) assert asteroid is not None assert asteroid.position.timestamp.usec() >= now
def randomize(self): clean_iso_path = self.settings["clean_iso_path"].strip() output_folder = self.settings["output_folder"].strip() self.settings["clean_iso_path"] = clean_iso_path self.settings["output_folder"] = output_folder self.ui.clean_iso_path.setText(clean_iso_path) self.ui.output_folder.setText(output_folder) if not os.path.isfile(clean_iso_path): QMessageBox.warning( self, "Clean ISO path not specified", "Must specify path to clean your Wind Waker ISO (USA).") return if not os.path.isdir(output_folder): QMessageBox.warning( self, "No output folder specified", "Must specify a valid output folder for the randomized files.") return seed = self.settings["seed"] seed = self.sanitize_seed(seed) if not seed: self.generate_seed() seed = self.settings["seed"] self.settings["seed"] = seed self.ui.seed.setText(seed) self.update_settings() options = OrderedDict() for option_name in OPTIONS: options[option_name] = self.get_option_value(option_name) max_progress_val = 20 self.progress_dialog = RandomizerProgressDialog( "Randomizing", "Initializing...", max_progress_val) try: rando = Randomizer(seed, clean_iso_path, output_folder, options) except TooFewProgressionLocationsError as e: error_message = str(e) self.randomization_failed(error_message) return except Exception as e: stack_trace = traceback.format_exc() error_message = "Randomization failed with error:\n" + str( e) + "\n\n" + stack_trace self.randomization_failed(error_message) return self.randomizer_thread = RandomizerThread(rando) self.randomizer_thread.update_progress.connect( self.update_progress_dialog) self.randomizer_thread.randomization_complete.connect( self.randomization_complete) self.randomizer_thread.randomization_failed.connect( self.randomization_failed) self.randomizer_thread.start()
class Simulated_annealing_learner: def __init__(self, seed, initial_t, data, annealer): self.randomizer = Randomizer(seed) self.annealer = annealer self.T = initial_t self.data = data # self.hyp = self.annealer.find_initial_hypthesis(data) self.hyp = self.annealer.initial_hypothesis() self.creation_time = datetime.datetime.now() def simulated_annealing(self, create_plots, positive_examples, output_directory, threshold, alpha): assert (0 < alpha < 1) and (0 < threshold) and (self.T > 0) iter_counter = 0 p = None if create_plots: # Initial hypothesis self.hyp.plot_transitions( 'hyp_%d ; E_%s' % (iter_counter, self.annealer.metric_calc(self.hyp, positive_examples)), output_directory) while self.T > threshold: iter_counter += 1 info("# ITERATION COUNTER =", iter_counter) info("Current temperature:", self.T) H_tag = self.annealer.get_random_neighbor(self.hyp, self.data) delta = self.annealer.energy_difference_a_minus_b( H_tag, self.hyp, positive_examples) info("Delta =", delta) if delta < 0: p = 1 else: p = math.exp(-delta / self.T) if p >= self.randomizer.get_prng().random(): info("Changing hypothesis\n") self.hyp = H_tag else: info("Didn't change hypothesis\n") energy = self.annealer.metric_calc(self.hyp, positive_examples) if create_plots: self.hyp.plot_transitions( 'hyp_%d ; E_%s' % (iter_counter, energy), output_directory) self.T *= alpha info("CHOSEN HYPOTHESIS:\n", self.hyp) return self.hyp, positive_examples def logger(self, create_plots, positive_examples, output_directory, threshold, alpha): info("# APPLYING LEARNER ON THE FOLLOWING POSITIVE EXAMPLES: %s" % ','.join(positive_examples)) info("\nInitial temperature:", self.T, ", Threshold:", threshold, ", Alpha:", alpha) info("\n# INITIAL HYPOTHESIS:\n", self.hyp) info("\n") return self.simulated_annealing(create_plots, positive_examples, output_directory, threshold, alpha)
def test_non_digit(self): """ Does it return the anything-but-a-digit metacharacter? """ metacharacter = CharacterClass.non_digit test = str(Randomizer.integer(maximum=9)) self.assertIsNone(re.search(metacharacter, test)) return
def test_decimal_point(self): """ Does it return a decimal point literal? """ metacharacter = Numbers.decimal_point test = random.uniform(0,100) self.assertIsNotNone(re.search(metacharacter, str(test))) self.assertIsNone(re.search(metacharacter, Randomizer.letters())) return
def test_empty_string(self): "Does it match only an empty string?" name = 'empty' expression = Group.named(name, FormalDefinition.empty_string) empty = '' not_empty = Randomizer.letters() match = re.search(expression, empty) self.assertEqual(empty, match.group(name)) self.assertIsNone(re.search(expression, not_empty)) return
def __init__(self, n, iterations, r_count, crossing_prob): # Количество вершин графа self.n = n # Вероятность того, что при селекции будет выполнено скрещивание self.crossing_prob = crossing_prob # Мощность популяции self.r_count = r_count # Количество итераций алгоритма self.iterations = iterations # Популяция, т. е. набор решений задачи self.r = [] #self.r.append([i for i in range(n - 1, 0 - 1, -1)]) self.r.append([i for i in range(0, n)]) # Инициализируем случайными величинами for i in range(1, n): self.r.append(Randomizer.swap_list_elements(self.r[i - 1], 5)) if (n == 10): self.distances = [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 0, 1, 2, 3, 4, 5, 6, 7, 8], [2, 1, 0, 1, 2, 3, 4, 5, 6, 7], [3, 2, 1, 0, 1, 2, 3, 4, 5, 6], [4, 3, 2, 1, 0, 1, 2, 3, 4, 5], [5, 4, 3, 2, 1, 0, 1, 2, 3, 4], [6, 5, 4, 3, 2, 1, 0, 1, 2, 3], [7, 6, 5, 4, 3, 2, 1, 0, 1, 2], [8, 7, 6, 5, 4, 3, 2, 1, 0, 1], [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]] else: # Матрица расстояний между вершинами графа self.distances = [[0 for i in range(n)] for j in range(n)] # Граф неориентированный, поэтому d[i][j] = d[j][i] for i in range (0, n): for j in range (i + 1, n): # Задаем случайное значение длины пути r = random.randint(1, 50) self.distances[i][j], self.distances[j][i] = r, r#int(r), int(r) # Здоровье популяции self.health = [] # Вычисляем здоровье начальной популяции for el in self.r: self.health.append(self.get_health(el)) # Позиции, с которых делали мутации self.mutation_pos = [1 for i in range(n)] # Лучшие решения за каждую итерацию self.best_solutions = []
def test_string_boundary(self): """ Does it add boundaries to match a whole line? """ substring = Randomizer.letters() expression = Boundaries.string(substring) expected = "^" + substring + "$" self.assertEqual(expected, expression) self.assertIsNotNone(re.search(expression, substring)) self.assertIsNone(re.search(expression, ' ' + substring)) return
def get_positive_examples_for_exactly(randomizer, min_zeros_per_positive_example, max_zeros_per_positive_example, min_sample_for_each_n, max_sample_for_each_n, n1, n2): return [ get_binary_representation(Randomizer(randomizer.seed), i, j).get_binary_representation(shuffle=True) for i, j in make_list_of_set_pairs_for_determiner_EXACTLY( (n1, n2), min_sample_for_each_n, max_sample_for_each_n, min_zeros_per_positive_example, max_zeros_per_positive_example) ]
def test_alternation(self): """ Does it match alternatives? """ name = 'or' # this might fail if one of the terms is a sub-string of another # and the longer term is chosen as the search term terms = [Randomizer.letters() for term in range(random.randint(10, 100))] expression = Group.named(name, FormalDefinition.alternative.join(terms)) test = terms[random.randrange(len(terms))] match = re.search(expression, test) self.assertEqual(test, match.group(name)) return
def get_randomizer(self, data_path): """ Construct a Randomizer instance from the parsed arguments and the given data path. :param data_path: The cards.json data path. :type data_path: str :return: A randomizer instance. :rtype: Randomizer """ return Randomizer(data_path, self.args.sets, self.args.number, self.args.weights, self.args.counts, self.args.include, self.args.exclude, self.args.filter_types, self.args.events, self.args.landmarks)
def test_m_to_n(self): """ Does it return the expression to match m-to-n repetitions """ m = Randomizer.integer(minimum=5) n = Randomizer.integer(minimum=m+1) substring = Randomizer.letters() quantifier = Quantifier.m_to_n(m,n) expression = '(' + substring + ')' + quantifier self.assertEqual("{" + str(m) + ',' + str(n) + '}',quantifier) text = Randomizer.letters() + substring * Randomizer.integer(m, n) complement = (Randomizer.letters_complement(substring) + substring * Randomizer.integer(0,m-1)) too_many = substring * Randomizer.integer(n+1, n*2) self.assertIsNotNone(re.search(expression, text)) self.assertIsNone(re.search(expression, complement)) self.assertEqual(re.search(expression, too_many).groups(), (substring,)) return
def __simulate_with_data(self, quantifier_type, additional_parameters_to_persist, data, initial_temperature, threshold, alpha): positive_examples = [ get_binary_representation(Randomizer(self.randomizer.seed), set_a, set_b) for set_a, set_b in data ] output_directory = self.create_output_directory( quantifier_type, additional_parameters_to_persist, positive_examples, initial_temperature, threshold, alpha) annealer = DFA_Annealer(self.randomizer.seed) learner = Simulated_annealing_learner(self.randomizer.seed, initial_temperature, data, annealer) final_hyp = learner.logger(self.create_plots, positive_examples, output_directory, threshold, alpha)[0] return output_directory, final_hyp, positive_examples
def test_one_hundreds(self): """ Does it match values from 100-199? """ number = "{0}".format(random.randint(100,199)) low_number = str(random.randint(-99,99)) high_number = str(random.randint(200,500)) float_number = str(random.uniform(100,199)) text = Randomizer.letters() + str(random.randint(100,199)) name = 'onehundred' expression = re.compile(Group.named(name, Numbers.one_hundreds)) self.assertIsNotNone(re.search(Numbers.one_hundreds, number)) self.assertIsNone(re.search(Numbers.one_hundreds, low_number)) self.assertIsNone(re.search(Numbers.one_hundreds, high_number)) # it only checks word boundaries and the decimal point is a boundary self.assertIsNotNone(re.search(Numbers.one_hundreds, float_number)) # it needs a word boundary so letters smashed against it will fail self.assertIsNone(re.search(Numbers.one_hundreds, text)) return
def __try_option(self, dfa, data, chosen_option): """ Checks whether a chosen neighbor is valid, i.e: 1) Accepts all pairs in data 2) Doesn't reduce a state from an only-state machine If one of the above doesn't hold, None is returned. Otherwise, the chosen neighbor is returned. """ info("Random neighbor to be checked:", chosen_option.__name__) if len(dfa.states) <= 2 and chosen_option == self.__remove_final_state: info("Neigbor will cause dfa to have 0 states.") return None neighbor = chosen_option(dfa) # Check whether the neighbor dfa recognizes all couples in data # If not, return old dfa (i.e don't use neighbor). Otherwise, return neighbor for set_a, set_b in data: bin_rep = get_binary_representation( Randomizer(self.randomizer.seed), set_a, set_b) if not neighbor.recognize(bin_rep): info("Neighbor didn't recognize one of the pairs in data: %s" % bin_rep) return None info("Neighbor is valid.") return neighbor
import os import pickle import uuid from multiprocessing.pool import Pool import pathlib import numpy as np from randomizer import Randomizer from printer import info, set_up_logging from run_single_simulation import SingleSimulationRunner, run_dir, get_binary_representation _BASE_SEED = 100 _RANDOMIZER = Randomizer(_BASE_SEED) def make_list_of_set_pairs_for_quantifier_between( at_least_ones, at_most_ones, min_size_of_universe, max_size_of_universe, number_of_positive_examples, add_examples_which_are_all_ones_of_these_lengths=[]): """ Returns pairs, each of which will later be transformed into a binary string, which represents set membership. In each pair: 1) First element is the universe: a set {0,...,M}, where M is a random number in [min_list_size, ..., max_list_size]. 2) Second element is a random subset {0,...,K}, where K is in [at_least, ..., at_most]. More precisely, K is in [at_least,..., M] if M < at_most. Thus generally speaking, the result is multiple instances of the generalized quantifier, each taken at random from a random size universe. Numeric example: at_least=3 at_most=6
class Youtube(object): """docstring for Youtube""" def __init__(self, driver, logger): super(Youtube, self).__init__() self.driver = driver self.logger = logger self.r = Randomizer('youtube') self.recursion_depth = 5 def get_random_channel(self): #tech_users = ["marquesbrownlee","duncan33303","unboxtherapy","LinusTechTips"] pydata = ["PyDataTV", "jacobschwarz"] user = random.choice(pydata) y = "https://www.youtube.com/user/%s/videos" % user return y def get_time_in_sec(self, time_string): tup = map(int, time_string.split(':')) if len(tup) == 2: return tup[0] * 60 + tup[1] else: return tup[0] * 3600 + tup[1] * 60 + tup[2] def get_video_url_time_list(self): channel_url = self.get_random_channel() soup = get_soup(channel_url) try: vid_grid = soup.find('ul', id='channels-browse-content-grid') vid_panes = vid_grid.find_all('li') except: if self.recursion_depth == 0: return ["https://m.youtube.com/watch?v=2MpUj-Aua48"] self.get_video_url_time_list() urls_times = [] youtube_prefix = "https://www.youtube.com" for li in vid_panes: a = li.find('a') if a is None: continue href = a['href'] time_string = li.find('span', {'class': 'video-time'}).get_text() url = youtube_prefix + href t = self.get_time_in_sec(time_string) urls_times.append((url, t)) return urls_times def disable_autoplay(self, driver): xpath = '//*[@id="autoplay-checkbox"]' self.driver.find_element_by_xpath(xpath).click() return def get_quality_button(self): elems = self.driver.find_elements_by_class_name('ytp-menuitem') elems = [ x for x in elems if x.get_attribute('aria-haspopup') == 'true' ] for elem in elems: x = elem.find_element_by_class_name('ytp-menuitem-label').text if x == 'Quality': return elem def select_resolution(self, res_level): try: sb = self.driver.find_element_by_css_selector( '.ytp-button.ytp-settings-button') sb.click() time.sleep(1) qual = self.get_quality_button() actions = webdriver.ActionChains(self.driver) actions.move_to_element(qual) actions.click() for i in range(res_level + 1): actions.send_keys(Keys.UP) actions.send_keys(Keys.RETURN) actions.perform() except: if self.recursion_depth == 0: self.random_play() self.logger.log("Youtube: Resolution selection Failed") self.recursion_depth -= 1 time.sleep(5) self.select_resolution(res_level) return def fetch_current_resolution(self): try: sb = self.driver.find_element_by_css_selector( '.ytp-button.ytp-settings-button') sb.click() time.sleep(1) qual = self.get_quality_button() res = qual.find_element_by_class_name('ytp-menuitem-content').text print "Resolution:", res # res = self.driver.find_element_by_xpath('//*[@id="movie_player"]/div[21]/div/div/div[5]/div[2]/div/span').text sb = self.driver.find_element_by_css_selector( '.ytp-button.ytp-settings-button') sb.click() return res except: if self.recursion_depth == 0: self.random_play() self.logger.log("Youtube: Resolution fetch Failed") self.recursion_depth -= 1 time.sleep(3) self.fetch_current_resolution() def check_ad(self): try: self.driver.find_element_by_class_name('videoAdUiBottomBar') except: return # Get ad time try: time_elem = self.driver.find_element_by_class_name( 'ytp-time-duration') ts = time_elem.text self.logger.write('YoutubeAd', ts, 'Auto') self.logger.log("Youtube: Playing Ad with resolution Auto for %s" % ts) time.sleep(self.get_time_in_sec(ts)) except: if self.recursion_depth < 0: self.random_play() time.sleep(2) self.recursion_depth -= 1 self.check_ad() def play_video(self, url, time_sec, res_level): self.driver.get(url) time.sleep(3) self.check_ad() #self.disable_autoplay(self.driver) self.select_resolution(res_level) time.sleep(2) res = self.fetch_current_resolution() play_time = min(self.r.get_play_time(), time_sec) time_str = "%dm,%ds" % (play_time / 60, play_time % 60) self.logger.write('Youtube', time_str, res) self.logger.log("Youtube: Playing video with resolution %s for %s" % (res, time_str)) time.sleep(play_time) def random_play(self): self.urls_times = self.get_video_url_time_list() while 1: self.recursion_depth = 5 if random.random() < 0.2: self.urls_times = self.get_video_url_time_list() url, time_sec = random.choice(self.urls_times) self.play_video(url, time_sec, self.r.get_resolution()) self.driver.get('chrome://settings/') wait_time = self.r.get_wait_time() self.logger.log("Youtube: Waiting for %dm,%ds" % (wait_time / 60, wait_time % 60)) time.sleep(wait_time)
def __init__(self, driver, logger): super(Youtube, self).__init__() self.driver = driver self.logger = logger self.r = Randomizer('youtube') self.recursion_depth = 5
#!/usr/bin/env python from randomizer import Randomizer url = 'https://raw.githubusercontent.com/mozilla/http-observatory-dashboard/master/httpobsdashboard/conf/sites.json' random_site = Randomizer(url) class TestRandomizer(): def test_randomizer_string(self): assert isinstance(random_site.next(), str) def test_randomizer_fqdn(self): assert "." in random_site.next() def test_randomizer_random(self): result1 = random_site.next() result2 = random_site.next() assert result1 != result2
def test_get_number_should_return_between_1_and_9(self): randomizer = Randomizer() self.assertTrue(randomizer.get_operator() >=1 and randomizer.get_operator() <= 9)
def randomize(self): clean_iso_path = self.settings["clean_iso_path"].strip() output_folder = self.settings["output_folder"].strip() self.settings["clean_iso_path"] = clean_iso_path self.settings["output_folder"] = output_folder self.ui.clean_iso_path.setText(clean_iso_path) self.ui.output_folder.setText(output_folder) if not os.path.isfile(clean_iso_path): QMessageBox.warning(self, "Clean ISO path not specified", "Must specify path to clean your Wind Waker ISO (USA).") return if not os.path.isdir(output_folder): QMessageBox.warning(self, "No output folder specified", "Must specify a valid output folder for the randomized files.") return seed = self.settings["seed"] seed = self.sanitize_seed(seed) if not seed: self.generate_seed() seed = self.settings["seed"] self.settings["seed"] = seed self.ui.seed.setText(seed) self.update_settings() options = OrderedDict() for option_name in OPTIONS: options[option_name] = self.get_option_value(option_name) options["custom_colors"] = self.custom_colors permalink = self.ui.permalink.text() max_progress_val = 20 self.progress_dialog = RandomizerProgressDialog("Randomizing", "Initializing...", max_progress_val) if self.bulk_test: failures_done = 0 total_done = 0 for i in range(100): temp_seed = str(i) try: rando = Randomizer(temp_seed, clean_iso_path, output_folder, options, permalink=permalink, cmd_line_args=self.cmd_line_args) randomizer_generator = rando.randomize() while True: next_option_description, options_finished = next(randomizer_generator) if options_finished == -1: break except Exception as e: stack_trace = traceback.format_exc() error_message = "Error on seed " + temp_seed + ":\n" + str(e) + "\n\n" + stack_trace print(error_message) failures_done += 1 total_done += 1 print("%d/%d seeds failed" % (failures_done, total_done)) try: rando = Randomizer(seed, clean_iso_path, output_folder, options, permalink=permalink, cmd_line_args=self.cmd_line_args) except TooFewProgressionLocationsError as e: error_message = str(e) self.randomization_failed(error_message) return except Exception as e: stack_trace = traceback.format_exc() error_message = "Randomization failed with error:\n" + str(e) + "\n\n" + stack_trace self.randomization_failed(error_message) return self.randomizer_thread = RandomizerThread(rando) self.randomizer_thread.update_progress.connect(self.update_progress_dialog) self.randomizer_thread.randomization_complete.connect(self.randomization_complete) self.randomizer_thread.randomization_failed.connect(self.randomization_failed) self.randomizer_thread.start()
def __init__(self, driver, logger): super(Netflix, self).__init__() self.driver = driver self.r = Randomizer('netflix') self.logger = logger
from player import Player from blocks import Block from bullets import Bullet from randomizer import Randomizer from particles import Particle from powerup import Powerup pygame.init() # Window and main game variables width = 400 height = 720 screen = pygame.display.set_mode((width, height)) pygame.mouse.set_visible(0) done = False rand = Randomizer() # Background image background = pygame.image.load("img/bg.gif") background = pygame.transform.scale(background, (width, height)) background_rect = background.get_rect() # Score area font = pygame.font.SysFont('Calibri', 30, True, False) font_color = pygame.Color('white') score = 0 score_width = 380 score_height = 50 score_area = pygame.Surface((score_width, score_height)) score_area_shadow = pygame.Surface((score_width + 8, score_height - 10)) score_color = (80, 30, 145)
def test_get_pattern_should_return_1_or_2(self): randomizer = Randomizer() self.assertTrue(randomizer.get_pattern() >=1 and randomizer.get_pattern() <= 2)
def plot_mdl_differences_for_determiner_exactly( randomizer, min_num_zeros_per_positive_example, max_num_zeros_per_positive_example, num_repeat, min_sample_for_each_n, max_sample_for_each_n, minimum_n, maximum_n): all_results = compute_average_energy_difference_exactly_minus_init_hyp( randomizer, min_num_zeros_per_positive_example, max_num_zeros_per_positive_example, num_repeat, min_sample_for_each_n, max_sample_for_each_n, minimum_n, maximum_n) plot_heatmap( False, minimum_n, maximum_n, '$E\\left(DFA^{EX}\\left(n_1, n_2\\right)\\right) - E\\left(DFA^{ALL}\\right)$' + '\nLower is Better', os.path.join( 'figures', 'average_energy_difference_exactly_%d_%d.png' % (min_sample_for_each_n, max_sample_for_each_n)), maximum_n, all_results) if __name__ == '__main__': # info('\n'.join(str(item) for item in sorted(compute_mdl_differences(1, 20).items(), # key=lambda pair: pair[1]))) import uuid randomizer = Randomizer(uuid.uuid1()) min_n, max_n = 1, 20 # repeat_all_of_the_exactly(1, 5, min_n, max_n) for number_of_examples_for_each_n in range(1, 15): plot_mdl_differences_for_determiner_exactly( randomizer, 0, 100, 10, number_of_examples_for_each_n, number_of_examples_for_each_n, min_n, max_n)