def provide(): logging.info("Requesting the data...") try: with open('COMPANIES.pkl', 'rb') as json_file: COMPANIES = pickle.load(json_file) with open('COMPANY_DICT.pkl', 'rb') as json_file: COMPANY_DICT = pickle.load(json_file) except: logging.info("--> Getting data the first time...") hs.get_data("tmp.pdf") content = hs.read_pdf("tmp.pdf") COMPANIES, COMPANY_DICT = hs.get_companies(content) with open("COMPANIES.pkl", "wb") as write_file: pickle.dump(COMPANIES, write_file) with open("COMPANY_DICT.pkl", "wb") as write_file: pickle.dump(COMPANY_DICT, write_file) hs.get_data("tmp.pdf") content = hs.read_pdf("tmp.pdf") NEW_COMPANIES, NEW_COMPANY_DICT = hs.get_companies(content) removed, added = hs.compare_sets(COMPANIES, NEW_COMPANIES) res_share = hs.compare_share(COMPANY_DICT, NEW_COMPANY_DICT) if removed or added or res_share: hs.send_payload(WEBHOOK, removed, added, res_share) else: logging.info("No change in sets") # Update local cache if removed or added or res_share: with open("COMPANIES.pkl", "wb") as write_file: pickle.dump(NEW_COMPANIES, write_file) with open("COMPANY_DICT.pkl", "wb") as write_file: pickle.dump(NEW_COMPANY_DICT, write_file) logging.info("Setting new data as standard")
def test_resource_rules(app, views, client): api = Api(app) api.add_resource( base_rule='/widget/<id>', base_view=views['widget'], alternate_rule='/widgets', alternate_view=views['widget_list'], ) get_response = client.get('/widget/1') assert get_response.status_code == 200 assert helpers.get_data(get_response) == { 'id': '1', } post_response = helpers.request( client, 'POST', '/widgets', {}, ) assert post_response.status_code == 201 assert post_response.headers['Location'] == 'http://localhost/widget/2' assert helpers.get_data(post_response) == { 'id': '2', }
def main(): print("Fetching training dataset..") data = get_data('data/data_movies_tr.json') #[:100] X, Y = split_text_rating(data) print("Creating features from training set..") vectorizer = CountVectorizer(tokenizer=LemmaTokenizer(), lowercase=True) vectors = vectorizer.fit_transform(X) #Train classifiers t0 = time.time() params = {'trainX': vectors, 'trainY': Y} knnclf = train(KNeighborsClassifier(n_neighbors=40), params, 'knn') svmclf = train(svm.SVC(kernel='poly', C=5, degree=4, coef0=2), params, 'svm') nnclf = train( MLPClassifier(max_iter=500, alpha=0.01, activation="logistic", learning_rate="invscaling", hidden_layer_sizes=(10, 10, 110)), params, 'neural network') nbclf = train(MultinomialNB(), params, 'naïve Bayes') dtclf = train(tree.DecisionTreeClassifier(), params, 'decision tree') vclf = train( VotingClassifier(estimators=[('knn', knnclf), ('svm', svmclf), ('nn', nnclf), ('nb', nbclf), ('dt', dtclf)], voting='hard'), params, 'ensemble') t1 = time.time() dt = t1 - t0 print("Training all classifiers took %.2fs.\n" % (dt)) #Test classifiers print("Fetching testing dataset..") data = get_data('data/data_movies_ts.json') #[:100] X, Y = split_text_rating(data) print("Creating features from testing set..") vectors = vectorizer.transform(X) t0 = time.time() params = {'testX': vectors, 'testY': Y} test(knnclf, params, 'knn') test(svmclf, params, 'svm') test(nnclf, params, 'neural network') test(nbclf, params, 'naïve Bayes') test(dtclf, params, 'decision tree') test(vclf, params, 'voting ensemble classifier') t1 = time.time() dt = t1 - t0 print("Testing all classifiers took %.2fs.\n" % (dt))
def main(): from_time, intervals = parse_input(get_data(day=13)) best_id, wait_time = find_interval_with_smallest_wait_time_for(from_time, intervals) print(f"Take bus {best_id} to wait just {wait_time} minutes.") print(f"Puzzle solution: {best_id * wait_time}") _, ids_with_pos = parse_input_with_positions(get_data(day=13)) congruences = [(-pos, bus_id) for bus_id, pos in ids_with_pos] t = solve_congruences(congruences) print(f"At time {t} the buses will all beautifully line up.")
def main(): live_points = read_input(get_data(day=17)) after_6_steps = update_n_steps(live_points, 6) print(f"After 6 steps, there are {len(after_6_steps)} cubes active.") live_points = read_input(get_data(day=17), dim=4) after_6_steps = update_n_steps(live_points, 6) print( f"After 6 steps in four dimensions, there are {len(after_6_steps)} cubes active." )
def check_by_input_file(input_name): input_file, expected_output = helpers.get_data("test/data/cli", input_name) output = assert_run(input_file) assert output == expected_output, "Incorrect scores for '{0}'.".format( input_name)
def __init__(self) -> None: print_app_info() self.browser: object = make_browser(headless=False) self.browser.get(get_defaults("url", "login")) self.scrape_data_amount: int = get_inputs(self.browser) self.filters: dict = get_filters(self.browser) self.result_links: list = get_links( self.browser, self.scrape_data_amount) self.scraped_details: dict = get_data( self.browser, self.result_links, self.scrape_data_amount) self.scraped_details["filters"] = self.filters quit_browser(self.browser) file_name = "./ScrapedData/ScrapedData_{}".format( time.strftime("%d_%b_%Y_%H_%M", time.localtime())) make_json(self.scraped_details, f"{file_name}.json") make_json_to_csv( f"{file_name}.json", f"{file_name}.csv" ) print_app_end()
def test_ai_view_range(): """Check if only move the enemies in the check range.""" EMPTY_MAP = np.zeros((11, 11), dtype=np.int64) data = get_data() # when ai in check range he see me data['check_range'] = 6 data['how_far_see_ai'] = 3 w = Worldmor(**data, rows=11) EMPTY_MAP[5, 5] = PLAYER + w.to_direction(1) + w.to_gun( GUN_B) + w.to_bullets(100) + w.to_health(1) # enemy which can see me, should move to me EMPTY_MAP[ 5, 2] = ENEMY_B + w.to_health(1) + w.to_bullets(100) + w.to_gun(GUN_B) # Not see me can do random walk EMPTY_MAP[ 5, 9] = ENEMY_B + w.to_health(1) + w.to_bullets(100) + w.to_gun(GUN_B) w.put_map_to_game(EMPTY_MAP) w.set_pos_row(5) w.set_pos_col(5) w.set_mid_row(5) w.set_mid_col(5) w.do_one_time_moment() # check the map assert (w.get_map_to_save()[5, 10] % 100 == ENEMY_B or w.get_map_to_save()[5, 8] % 100 == ENEMY_B or w.get_map_to_save()[4, 9] % 100 == ENEMY_B or w.get_map_to_save()[6, 9] % 100 == ENEMY_B or w.get_map_to_save()[5, 9] % 100 == ENEMY_B) assert w.get_map_to_save()[5, 2] % 100 == GRASS assert w.get_map_to_save()[5, 3] % 100 == ENEMY_B assert w.get_map_to_save()[5, 5] % 100 == PLAYER
def test_check_range(): """Check if only move the enemies in the check range.""" data = get_data() # when ai in check range he see me data['check_range'] = 4 data['how_far_see_ai'] = 10 w = Worldmor(**data, rows=11) EMPTY_MAP[5, 5] = PLAYER + w.to_direction(1) + w.to_gun( GUN_B) + w.to_bullets(100) + w.to_health(1) # enemy in of range of check - move near to me EMPTY_MAP[1, 5] = ENEMY_B + w.to_health(1) # enemy out of range of check - not move EMPTY_MAP[10, 5] = ENEMY_B + w.to_health(1) w.put_map_to_game(EMPTY_MAP) w.set_pos_row(5) w.set_pos_col(5) w.set_mid_row(5) w.set_mid_col(5) w.do_one_time_moment() # check the map assert w.get_map_to_save()[10, 5] % 100 == ENEMY_B assert w.get_map_to_save()[1, 5] % 100 == GRASS assert w.get_map_to_save()[2, 5] % 100 == ENEMY_B assert w.get_map_to_save()[5, 5] % 100 == PLAYER
def print_testing(): data, data2 = helpers.get_data("./training/") fd = process(data, data2) predict = model.sess.run(model.decoder_prediction, fd) for j, (inp, pred, out) in enumerate( zip(fd[model.encoder_inputs].T, predict.T, fd[model.decoder_targets].T)): # print ("input {} : ".format(inp)) # print ("predict {} : ".format(pred)) # print ("out {} : ".format(out)) print '\n' print "input" print(inp) for data in inp: if (data - 2 >= 0 and data - 2 < 257): sys.stdout.write(get_word(data - 2) + " ") sys.stdout.flush() print '\n' print "predict" print(pred) for data in pred: if (data - 2 >= 0 and data - 2 < 257): sys.stdout.write(get_word(data - 2) + " ") sys.stdout.flush() print '\n' print "target" print(out) for data in out: if (data - 2 >= 0 and data - 2 < 257): sys.stdout.write(get_word(data - 2) + " ") sys.stdout.flush()
def test_shoot_when_can(): """Check if enemy shoot if they can shoot.""" data = get_data() # when ai in check range he see me data['check_range'] = 4 data['how_far_see_ai'] = 10 w = Worldmor(**data, rows=11) EMPTY_MAP[5, 5] = PLAYER + w.to_direction(1) + w.to_gun( GUN_B) + w.to_bullets(100) + w.to_health(1) # enemy out of range of check range not move, have big gun with a maximum set shoot range EMPTY_MAP[ 10, 5] = ENEMY_B + w.to_health(1) + w.to_gun(GUN_E) + w.to_bullets(100) w.put_map_to_game(EMPTY_MAP) w.set_pos_row(5) w.set_pos_col(5) w.set_mid_row(5) w.set_mid_col(5) w.do_one_time_moment() # not change enemy not in move range assert w.get_map_to_save()[10, 5] % 100 == ENEMY_B assert w.get_map_to_save()[5, 5] % 100 == PLAYER # move near to the check range w.down() # enemy can shoot and shoot to me and kill me (1 health) so check return value and map after game time moment. assert w.do_one_time_moment() == -1 # not change enemy not in move range assert w.get_map_to_save()[10, 5] % 100 == ENEMY_B assert w.get_map_to_save()[6, 5] % 100 == BLOOD
def test_go_for_player_ai(): """Test if ai go for player right if the probability is 100%.""" EMPTY_MAP = np.zeros((11, 11), dtype=np.int64) data = get_data() data['go_for_player_ai_prob'] = 1 data['go_for_gun_ai_prob'] = 0 data['go_for_health_ai_prob'] = 0 data['go_for_bullets_ai_prob'] = 0 # not see but can move to player, this have some not 0 probability data['how_far_see_ai'] = 0 w = Worldmor(**data, rows=11) EMPTY_MAP[4, 4] = PLAYER + w.to_direction(1) + w.to_gun( GUN_B) + w.to_bullets(100) + w.to_health(1) # test enemy to move nearest gun in see EMPTY_MAP[ 4, 10] = ENEMY_B + w.to_health(1) + w.to_gun(GUN_B) + w.to_bullets(100) w.put_map_to_game(EMPTY_MAP) w.set_pos_row(4) w.set_pos_col(4) w.set_mid_row(5) w.set_mid_col(5) w.set_how_fast_ai_is(0) # dont want to wait w.do_one_time_moment() # Enemy have the gun new? assert w.get_map_to_save()[4, 10] % 100 == GRASS assert w.get_map_to_save()[4, 9] % 100 == ENEMY_B
def test_generate_map_only_with_enemies(): """Set probability of enemies to 1 test there are only bullets and wall for distance which is far as view range. In view range is protection zone. """ data = get_data() # test map generator for large map data['rows'] = TEST_SIZE data['enemy_max_prob'] = 1 data['health_max_prob'] = 0 data['guns_max_prob'] = 0 data['bullets_max_prob'] = 0 # this do bullets probability to 1 data['enemy_max_prob'] = 1 data['enemy_distance_divider'] = 1 data['enemy_start_probability'] = 1 data['view_range'] = 0 w = Worldmor(**data) wall_check = WALL + w.to_health(100) print(w.get_map_to_save()) for er, i in enumerate(w.get_map_to_save()): for ec, j in enumerate(i): if er == w.get_pos_row() and ec == w.get_pos_col(): continue else: # there can be walls(generator logic are inside) or blood (some bonus with very small probability) # But there are no pharmacies, bullets, enemies and guns assert (j == wall_check or ENEMY_B <= j % 100 <= ENEMY_E or j == 2)
def test_many(client): response = helpers.request( client, 'PUT', '/parents/1', { 'id': '1', 'name': "Updated Parent", 'children': [ {'id': '1'}, {'id': '2'}, ], }, ) assert helpers.get_data(response) == { 'id': '1', 'name': "Updated Parent", 'children': [ { 'id': '1', 'name': "Child 1", }, { 'id': '2', 'name': "Child 2", }, ], }
def test_killing_enemy(): """Test if I can kill enemy and catch the points.""" w = Worldmor(**get_data(), rows=4) # add player direction, health, gun and bullets set_code = PLAYER + w.to_direction(1) + w.to_gun(GUN_E) + w.to_bullets( 100) + w.to_health(1) EMPTY_MAP[2, 0] = set_code EMPTY_MAP[1, 0] = ENEMY_B + w.to_health(1) w.set_pos_row(2) w.set_pos_col(0) w.set_mid_row(2) w.set_mid_col(0) w.put_map_to_game(EMPTY_MAP) code = w.get_map_to_save()[2, 0] assert code == set_code # Kill the enemy w.shoot() assert w.do_one_time_moment() == 0 code = w.get_map_to_save()[1, 0] # blood after kill, it must be visible? assert code == BLOOD + w.to_visible(1) # collect the points w.up() assert w.do_one_time_moment() > 0 # check pick up and move assert w.get_pos_row() == 1 assert w.get_pos_col() == 0 # Test field of position before are grass with visibility 1 assert w.get_map_to_save()[2, 0] == 100
def test_move_and_pickup_for_gun(): w = Worldmor(**get_data(), rows=4) # add player direction, health, gun and bullets set_code = PLAYER + w.to_gun(GUN_B) + w.to_bullets(1) + w.to_health(1) EMPTY_MAP[2, 0] = set_code EMPTY_MAP[1, 0] = GUN_3 w.set_pos_row(2) w.set_pos_col(0) w.set_mid_row(2) w.set_mid_col(0) w.put_map_to_game(EMPTY_MAP) code = w.get_map_to_save()[2, 0] assert code == set_code w.up() assert w.do_one_time_moment() == 0 code = w.get_map_to_save()[1, 0] # add some bullets, and no other code change assert w.get_bullets(code) == 1 assert w.get_health(code) == 1 assert w.get_gun(code) == GUN_3 assert code % 100 == PLAYER # check pick up and move assert w.get_pos_row() == 1 assert w.get_pos_col() == 0 # Test field of position before are grass with visibility 1 assert w.get_map_to_save()[2, 0] == 100
def test_destroy_wall_position(): """Test shoot after shoot before move to position of wall and test move to wall before shoot in one time moment""" w = Worldmor(**get_data(), rows=4) # add player direction, health, gun and bullets EMPTY_MAP[2, 0] = PLAYER + w.to_direction(1) + w.to_gun( GUN_B) + w.to_bullets(100) + w.to_health(1) EMPTY_MAP[1, 0] = WALL + w.to_health(1) EMPTY_MAP[0, 0] = WALL + w.to_health(1) w.set_pos_row(2) w.set_pos_col(0) w.set_mid_row(2) w.set_mid_col(0) w.put_map_to_game(EMPTY_MAP) # destroy wall and move w.shoot() w.up() assert w.do_one_time_moment() == 0 assert w.get_pos_row() == 1 assert w.get_pos_col() == 0 # move cant and then destroy wall w.up() w.shoot() assert w.do_one_time_moment() == 0 assert w.get_pos_row() == 1 assert w.get_pos_col() == 0 # Try if the wall wal destroyed, move there w.up() assert w.do_one_time_moment() == 0 assert w.get_pos_row() == 0 assert w.get_pos_col() == 0
def main2(): y,X = get_data() # standardize features X_std = np.copy(X) X_std[:,0] = (X[:,0] - X[:,0].mean()) / X[:,0].std() X_std[:,1] = (X[:,1] - X[:,1].mean()) / X[:,1].std() ada = AdalineGD(n_iter=15, eta=0.01) ada.fit(X_std, y) plot_decision_regions(X_std, y, classifier=ada) plt.title('Adaline - Gradient Descent') plt.xlabel('sepal length [standardized]') plt.ylabel('petal length [standardized]') plt.legend(loc='upper left') plt.tight_layout() plt.savefig(PIC_LOC + 'adaline_2.png', dpi=300) plt.close() plt.plot(range(1, len(ada.cost_) + 1), ada.cost_, marker='o') plt.xlabel('Epochs') plt.ylabel('Sum-squared-error') plt.tight_layout() plt.savefig(PIC_LOC + 'adaline_3.png', dpi=300) # plt.show() plt.close()
def test_move_right(): w = Worldmor(**get_data(), rows=4) assert w.get_pos_row() == 2 assert w.get_pos_col() == 2 w.right() w.do_one_time_moment() assert w.get_pos_row() == 2 assert w.get_pos_col() == 3
def test_update(client): update_response = helpers.request(client, "PATCH", "/widgets/1/2", {"id_1": "1", "id_2": "2", "name": "Qux"}) assert update_response.status_code == 204 retrieve_response = client.get("/widgets/1/2") assert retrieve_response.status_code == 200 assert helpers.get_data(retrieve_response) == {"id_1": "1", "id_2": "2", "name": "Qux"}
def test_playground(): all_tiles = parse_input(get_data(day=20)) graph = build_tile_fit_graph(all_tiles) print(max(graph.degree(node) for node in graph.nodes())) print(min(graph.degree(node) for node in graph.nodes())) assert len(find_corner_tiles(all_tiles, graph)) == 4
def test_go_for_bullets_when_not_have(): """Test enemy go for bullets after shoot last bullets to player and then continue shooting. Test can fail if the damage of the base gun change. """ EMPTY_MAP = np.zeros((11, 11), dtype=np.int64) data = get_data() data['go_for_player_ai_prob'] = 0 data['go_for_gun_ai_prob'] = 0 data['go_for_health_ai_prob'] = 0 data['go_for_bullets_ai_prob'] = 1 data['how_far_see_ai'] = 8 w = Worldmor(**data, rows=11) # Dont die for first shoot but second can EMPTY_MAP[5, 4] = PLAYER + w.to_direction(1) + w.to_gun( GUN_B) + w.to_bullets(100) + w.to_health(12) # test enemy to move nearest bullets in see EMPTY_MAP[5, 6] = ENEMY_B + w.to_health(1) + w.to_gun(GUN_B) + w.to_bullets(1) EMPTY_MAP[5, 9] = BULLET w.put_map_to_game(EMPTY_MAP) w.set_pos_row(5) w.set_pos_col(4) w.set_mid_row(5) w.set_mid_col(4) w.set_how_fast_ai_is(1) # dont want to wait # should shoot to me w.do_one_time_moment() assert w.get_map_to_save()[5, 4] % 100 == PLAYER assert w.get_health(w.get_map_to_save()[5, 4]) < 12 assert w.get_map_to_save()[5, 6] % 100 == ENEMY_B assert w.get_bullets(w.get_map_to_save()[5, 6]) == 0 # no bullets let go for some w.do_one_time_moment() w.do_one_time_moment() assert w.get_map_to_save()[5, 7] % 100 == ENEMY_B w.do_one_time_moment() assert w.get_map_to_save()[5, 8] % 100 == ENEMY_B w.do_one_time_moment() assert w.get_map_to_save()[5, 9] % 100 == ENEMY_B assert w.get_bullets(w.get_map_to_save()[5, 9]) > 0 # Lets go back and kill the player w.do_one_time_moment() w.do_one_time_moment() assert w.get_map_to_save()[5, 8] % 100 == ENEMY_B w.do_one_time_moment() w.do_one_time_moment() assert w.get_map_to_save()[5, 7] % 100 == ENEMY_B w.do_one_time_moment() w.do_one_time_moment() assert w.get_map_to_save()[5, 6] % 100 == ENEMY_B w.do_one_time_moment() w.do_one_time_moment() assert w.get_map_to_save()[5, 4] % 100 == BLOOD
def main(): tiles = parse_input(get_data(day=20)) graph = build_tile_fit_graph(tiles) corner_tiles = find_corner_tiles(tiles, graph) print( f"The product of the four corner tiles is {math.prod(corner_tile.id for corner_tile in corner_tiles)}" )
def test_retrieve(client): response = client.get('/widgets/1') assert response.status_code == 200 assert helpers.get_data(response) == { 'id': '1', 'name': "Foo", 'description': "foo widget", }
def available_after_ordered(index, temp_order): menu_item = menus[index] order_item = helpers.get_data(menu_item, temp_order) if order_item: if order_item['qty'] - menu_item['qty'] == 0: return False return True
def main(): data = get_data(day=6) ans = sum_all_unique(data) print(f"The answer is {ans}.") ans = sum_all_present(data) print(f"Achtually, it is {ans}.")
def test_arg(client, token): response = client.get('/widgets?id_token={}'.format(token)) assert helpers.get_data(response) == [ { 'id': '1', 'owner_id': 'foo', }, ]
def test_list(client): response = client.get("/widgets") assert response.status_code == 200 assert helpers.get_data(response) == [ {"id_1": "1", "id_2": "2", "name": "Foo"}, {"id_1": "1", "id_2": "3", "name": "Bar"}, {"id_1": "4", "id_2": "5", "name": "Baz"}, ]
def create_plot(doc): """Create a static plot.""" doc.clear() # doc.theme = Theme(filename=join(getcwd(), 'theme.yaml')) sp = StockPrices() figure = plot(5, PLOT_WIDTH, PLOT_HEIGHT, get_data(sp, SELECTED, 5)) doc.add_root(figure)
def main(): print("Fetching training and testing datasets..") ntr = 5000 nts = 1000000 train_data = get_data('data/data_cds_sub_' + str(n) + '_tr.json') test_data = get_data('data/data_cds_sub_' + str(nts) + '_ts.json') trainX, trainY = split_text_rating(train_data) testX, testY = split_text_rating(test_data) print("Creating features from training set..") vectorizer = HashingVectorizer(tokenizer=LemmaTokenizer(), lowercase=True, n_features=2**18, alternate_sign=False) # Count tr_vectors = vectorizer.fit_transform(trainX) params = { 'trainX': tr_vectors, 'trainY': trainY, 'testX': testX, 'testY': testY, 'vectorizer': vectorizer } knnclf = train_and_test(KNeighborsClassifier(n_neighbors=40), params, 'knn') svmclf = train_and_test(svm.SVC(kernel='poly', C=5, degree=4, coef0=2), params, 'svm') nnclf = train_and_test( MLPClassifier(max_iter=500, alpha=0.01, activation="logistic", learning_rate="invscaling", hidden_layer_sizes=(10, 10, 110)), params, 'neural network') nbclf = train_and_test(MultinomialNB(), params, 'naïve Bayes') dtclf = train_and_test(tree.DecisionTreeClassifier(), params, 'decision tree') train_and_test( VotingClassifier(estimators=[('knn', knnclf), ('svm', svmclf), ('nn', nnclf), ('nb', nbclf), ('dt', dtclf)], voting='hard'), params, 'ensemble')
def test_api_prefix(app, views, client): api = Api(app, '/api') api.add_resource('/widgets', views['widget_list']) response = client.get('/api/widgets') assert helpers.get_data(response) == [ { 'id': '1', }, ]
def main(): data = get_data(day=4) passports = to_password_dicts(parse_passports(data)) num_present = sum(1 for passport in passports if is_present(passport)) print(f"There are {num_present} passports with all fields present.") num_valid = sum(1 for passport in passports if is_valid_passport(passport)) print(f"There are {num_valid} valid passports")
def test_worldmor_code_convertors(): w = Worldmor(**get_data(), rows=4) code = 123456789876 assert w.get_direction(code) == 3 assert w.get_gun(code) == 12 assert w.get_bullets(code) == 456 assert w.get_health(code) == 789 assert w.get_visible(code) == 8 test = (76 + int(w.to_direction(3)) + w.to_gun(12) + int(w.to_bullets(456)) + w.to_health(789) + w.to_visible(8)) assert code == test
def test_baseline(client): parent_response = client.get('/parents/1') assert helpers.get_data(parent_response) == { 'id': '1', 'name': "Parent", 'children': [], } child_1_response = client.get('/children/1') assert helpers.get_data(child_1_response) == { 'id': '1', 'name': "Child 1", 'parent': None, } child_2_response = client.get('/children/2') assert helpers.get_data(child_2_response) == { 'id': '2', 'name': "Child 2", 'parent': None, }
def test_limit_offset_offset_truncate(client): response = client.get('/limit_offset_widgets?offset=5') assert helpers.get_data(response) == [ { 'id': '6', 'size': 3, }, ] assert helpers.get_meta(response) == { 'has_next_page': False }
def test_limit_offset_filtered_offset(client): response = client.get('/limit_offset_widgets?size=2&offset=1') assert helpers.get_data(response) == [ { 'id': '5', 'size': 2, }, ] assert helpers.get_meta(response) == { 'has_next_page': False }
def main(): input_str = get_data(day=19) grammar, messages = read_data(input_str) regexp = grammar_to_regexp(grammar) pattern = re.compile(regexp) num_match = sum(check_message(pattern, message) for message in messages) print(f"There's {num_match} messages matching the rule.") res = part2(grammar, messages) print(f"There's now {res} matching messages with the new rules.")
def update_period(attr, old, new): """Update selected period.""" spinner.css_classes = ['loader-spinner'] overlay.css_classes = ['loader-overlay'] _data = get_data(sp, selected, period_selector.active) layout.children[2] = plot(period_selector.active, plot_width, plot_height, _data) spinner.css_classes = [] overlay.css_classes = []
def main(): start_numbers = [int(num) for num in get_data(day=15).split(",")] number_2020 = get_nth_number(start_numbers, 2020) print(f"The 2020th number is {number_2020}") start = time.time() number_300000000 = get_nth_number(start_numbers, 30000000) duration = time.time() - start print(f"The 30000000th number is {number_300000000}") print(f"This took {duration}.")
def train(self, x, y): # Make sure that x and y have the same number of instances assert x.shape[0] == len(y), \ "Training failed. x and y must have the same number of instances." dataset = hp.get_data(x, y) self.init_freq = hp.get_frequency(dataset) self.tree = self.induce_decision_tree(dataset) # set a flag so that we know that the classifier has been trained self.is_trained = True return self
def test_header(client, token): response = client.get( '/widgets', headers={ 'Authorization': 'Bearer {}'.format(token), }, ) assert helpers.get_data(response) == [ { 'id': '1', 'owner_id': 'foo', }, ]
def test_list(client): response = client.get('/widgets?user_id=foo') assert helpers.get_data(response) == [ { 'id': '1', 'owner_id': 'foo', 'name': "Foo", }, { 'id': '3', 'owner_id': None, 'name': "Public", }, ]
def test_page(client): response = client.get('/page_widgets?page=1') assert helpers.get_data(response) == [ { 'id': '3', 'size': 3, }, { 'id': '4', 'size': 1, }, ] assert helpers.get_meta(response) == { 'has_next_page': True }
def test_page_default(client): response = client.get('/page_widgets') assert helpers.get_data(response) == [ { 'id': '1', 'size': 1, }, { 'id': '2', 'size': 2, }, ] assert helpers.get_meta(response) == { 'has_next_page': True }
def test_limit_offset_offset(client): response = client.get('/limit_offset_widgets?offset=2') assert helpers.get_data(response) == [ { 'id': '3', 'size': 3, }, { 'id': '4', 'size': 1, }, ] assert helpers.get_meta(response) == { 'has_next_page': True }
def test_create_client_id(app, views, client): api = Api(app) api.add_resource('/widgets', views['widget_list'], views['widget']) response = helpers.request( client, 'POST', '/widgets', { 'id': '100', }, ) assert response.status_code == 201 assert response.headers['Location'] == 'http://localhost/widgets/100' assert helpers.get_data(response) == { 'id': '100', }
def test_create(client): response = helpers.request( client, 'POST', '/widgets', { 'name': "Qux", 'description': "qux widget", }, ) assert response.status_code == 201 assert response.headers['Location'] == 'http://localhost/widgets/4' assert helpers.get_data(response) == { 'id': '4', 'name': "Qux", 'description': "qux widget", }
def test_many_falsy(client): test_many(client) response = helpers.request( client, 'PUT', '/parents/1', { 'id': '1', 'name': "Twice Updated Parent", 'children': [], }, ) assert helpers.get_data(response) == { 'id': '1', 'name': "Twice Updated Parent", 'children': [], }
def test_update(client): update_response = helpers.request( client, 'PATCH', '/widgets/1', { 'id': '1', 'description': "updated description", }, ) assert update_response.status_code == 204 retrieve_response = client.get('/widgets/1') assert retrieve_response.status_code == 200 assert helpers.get_data(retrieve_response) == { 'id': '1', 'name': "Foo", 'description': "updated description", }
def test_single(client): response = helpers.request( client, 'PUT', '/children/1', { 'id': '1', 'name': "Updated Child", 'parent': {'id': '1'}, }, ) assert helpers.get_data(response) == { 'id': '1', 'name': "Updated Child", 'parent': { 'id': '1', 'name': "Parent", }, }
def test_id_cursor_default(client): response = client.get('/id_cursor_widgets') assert helpers.get_data(response) == [ { 'id': '1', 'size': 1, }, { 'id': '2', 'size': 2, }, ] assert helpers.get_meta(response) == { 'has_next_page': True, 'cursors': [ 'WzFd', 'WzJd', ], }
def test_id_cursor_sorted_default(client): response = client.get('/id_cursor_widgets?sort=size,-id') assert helpers.get_data(response) == [ { 'id': '4', 'size': 1, }, { 'id': '1', 'size': 1, }, ] assert helpers.get_meta(response) == { 'has_next_page': True, 'cursors': [ 'WzEsIDRd', 'WzEsIDFd', ], }
def test_list(client): response = client.get('/widgets') assert response.status_code == 200 assert helpers.get_data(response) == [ { 'id': '1', 'name': "Foo", 'description': "foo widget", }, { 'id': '2', 'name': "Bar", 'description': "bar widget", }, { 'id': '3', 'name': "Baz", 'description': "baz widget", }, ]
def test_create(client): response = helpers.request(client, "POST", "/widgets", {"id_1": "4", "id_2": "6", "name": "Qux"}) assert response.status_code == 201 assert response.headers["Location"] == "http://localhost/widgets/4/6" assert helpers.get_data(response) == {"id_1": "4", "id_2": "6", "name": "Qux"}
import numpy as np import matplotlib.pyplot as plt from adjust_subpads import adjust_subpads from helpers import get_data pad = 2 run = 304 img = 1 run_bkgd = 401 img_bkgd = 0 do_debug = False fixpad = 0 figname = "test" imData = get_data(pad, run, img) imData_bkgd = get_data(pad, run_bkgd, img_bkgd) (imData_polar, onPad, rr, tt, \ twotheta_deg1, fr1, offset) = adjust_subpads(imData, pad, \ fixpad=fixpad, do_debug=do_debug, \ figname=figname) (imData_polar, onPad, rr, tt, \ twotheta_deg2, fr2, offset) = adjust_subpads(imData_bkgd, pad, \ fixpad=fixpad, do_debug=do_debug, \ figname=figname) (imData_polar, onPad, rr, tt, \ twotheta_deg3, fr3, offset) = adjust_subpads(imData-imData_bkgd, pad, \ fixpad=fixpad, do_debug=do_debug, \ figname=figname) plt.plot(twotheta_deg1, fr1, 'b-', label="orig")
def test_retrieve(client): response = client.get("/widgets/1/2") assert response.status_code == 200 assert helpers.get_data(response) == {"id_1": "1", "id_2": "2", "name": "Foo"}
from helpers import get_data from helpers import filter_by_airline, filter_by_species, filter_by_airport, sort_by_criteria # store the datarows in memory incidents = get_data() # this function is the only one that app.py needs to know about def go_incidents(species="", airline="", airport="", sortby="newest"): matched_rows = [] datarows = incidents # first, filter if airline: filteredrows = filter_by_airline(airline, incidents) if airport: filteredrows = filter_by_airport(airport, incidents) else: # by default, search by species filteredrows = filter_by_species(species, incidents) # then, sort and return the result # remember to pass in filteredrows, not incidents return sort_by_criteria(sortby, filteredrows) def print_record_count(): print("There are", len(incidents), "rows.")
plain_mail_templates.append(jinja2.Template(template_text)) html_mail_templates = [] for filename in html_filenames: html_text_file = open(template_path + '/' + filename, 'r') template_text = html_text_file.read() html_text_file.close() html_mail_templates.append(jinja2.Template(template_text)) #images that we need to send images = [] images.append(['images/scicast_logo.png', '<@sci_logo>']) print "Getting All Users..." all_users = helpers.get_data("https://scicast.org/users/index?role=None&traded_since=None", userpass['api_username'], userpass['api_password']) import pdb;pdb.set_trace() for winner in winner_list: #pick a random template for the thank you template_index = random.randint(0, len(plain_mail_templates)-1) plain_template = plain_mail_templates[template_index] html_template = html_mail_templates[template_index] text_rendered = plain_template.render(username = winner["username"]) html_rendered = html_template.render(username = winner["username"]) subject = "Thank you from the SciCast team" to_addr = helpers.get_email_from_username(all_users, winner["username"]) to_addr_check = helpers.get_email_from_userid(all_users, winner["user_id"]) if to_addr != to_addr_check: print "error with address " + to_addr + ".... continuing" continue
def check_by_input_file(input_name): input_file, expected_output = helpers.get_data("test/data/cli", input_name) output = assert_run(input_file) assert output == expected_output, "Incorrect scores for '{0}'.".format(input_name)
import sys, os import pickle import numpy as np from helpers import findind, get_data, get_pads, get_calib if __name__=="__main__": pad = 0 r_start = 380 r_end = 400 imData_bkgd = get_data(pad, 401, 0) # read calibration values and subpad locations x0, y0, D, rr, tt, twotheta_deg = get_calib(pad) pads = get_pads(pad) Npads = pads.shape[0] # read XRonly run numbers and loop over runs = np.genfromtxt("XRonly_runnumber.txt").astype(int) data = {} for run in runs: data[run] = {} img = 0 while True: try: imData = get_data(pad, run, img)-imData_bkgd except IOError: break ny, nx = imData.shape ft = np.zeros(len(tt)) ft_count = np.zeros(len(tt))