def get_date_avg(file_name): """Returns the average of dictionary's release dates named list""" preferences_dict = reader.read_file(file_name, simple_string_is_enough=False) date_avg = sum(preferences_dict["release dates"]) / len( preferences_dict["release dates"]) return round(date_avg)
def annealing(filename): start = time.time() name, size, M = read_file(filename) s = [i for i in range(size)] s_best = s[:] curr_length = E(s, M) best_length = E(s_best, M) T_start = choose_start_temp(s, M) T = T_start print("Start temperature: " + str(T_start)) k = 0 while T > 0.001: print(str(best_length) + " " + str(T)) T = temperature(k, T, T_start) s_prime = get_random_neighbour(s) s_prime, new_length = opt_2( s_prime, size, M) #perform local search to find best neighbour #new_length = E(s_prime, M) if P(curr_length, new_length, T) >= random.random(): s = s_prime curr_length = new_length if new_length < best_length: s_best = s[:] best_length = E(s_best, M) #print(str(E(s_best, M)) + ", " + str(T)) k += 1 end = time.time() print(best_length) print(s_best) print("Time taken: " + str(end - start)) save_tour(size, best_length, s_best) return best_length, s_best
def main(fname): lam = 30 # Load data y = reader.read_file(fname) edgeSz = int(math.sqrt(np.size(y))) D = reader.gen_D_matrix(edgeSz) guess = np.zeros((np.shape(D)[0], 1)) sol = admm(D, y, guess, lam, 1e-5) (m, n) = np.shape(D) ident = spsp.identity(n) A = lam * D.T @ D + ident orig = np.reshape(y, (edgeSz, edgeSz)) admm_smooth = np.reshape(sol, (edgeSz, edgeSz)) plt.figure(1) plt.suptitle("Smoothing Results") plt.subplot(121) plt.imshow(orig) plt.title("Original Image") plt.colorbar() plt.subplot(122) plt.imshow(admm_smooth) plt.title("ADMM") plt.colorbar() plt.show()
def main(): target_path = parser.parse_args().target # log_file = parser.parse_args().log # check OS if os.name == "nt": # this is Windows, so let's have some sex with path... target_path = target_path.replace("\\", "/") # log_file = log_file.replace("\\", "/") if os.path.isfile(target_path): reader.read_file(target_path) elif os.path.isdir(target_path): walker.walk_through(target_path) else: print(parser.description)
def get_date_ordered(file_name): """This ugly code returns a double sorted title list ordered by release dates. It searches matches in the sorted dates and sorts those slices in titles.""" preferences_dict = reader.read_file(file_name, simple_string_is_enough=False) year = preferences_dict["release dates"] indexes = [x for x in range(len(year))] indexes.sort(key=year.__getitem__, reverse=True) title = [preferences_dict["titles"][x] for x in indexes] match = {} year = sorted(preferences_dict["release dates"], reverse=True) i = 0 while i < len(year) - 1: if year[i] == year[i + 1]: match[i] = 2 j = i + 1 while year[j] == year[j + 1]: match[i] += 1 j += 1 i += match[i] i += 1 for key in match: swap = title[key:key + match[key]] swap.sort(key=str.lower) title[key:key + match[key]] = swap return title
def aco(filename): start = time.time() name, size, M = read_file(filename) #print(M) ants = 10 #int((size * 0.4) // 1) p_start = choose_start_pheromone(size, M) print("Starting pheromone level: " + str(p_start)) P = [[p_start] * size for _ in range(size)] #initialise pheromones matrix best = 0 best_tour = [] for _ in range(500): tours = [[random.randint(0, size - 1)] for _ in range(ants)] #or random.randint(0, size - 1) tour_lengths = [0] * ants for _ in range(size): local_update(tours, tour_lengths, size, M, P, p_start) for i, tour in enumerate(tours): tours[i], tour_lengths[i] = opt_2( tour, size, M) #local optimisation of each tour for i, l in enumerate(tour_lengths): if l < best or best == 0: best = l best_tour = tours[i] update_best(tours, tour_lengths, size, P, best, best_tour) #can tidy this print( str(tour_lengths) + ", " + str(sum(tour_lengths) / float(len(tour_lengths)))) end = time.time() print("500 iterations completed in " + str(end - start)) print(str(tour_lengths) + ", " + str(best)) print(best_tour) save_tour(size, best, best_tour) return best, best_tour
def get_latest(file_name): """Looks up the highest value from dictionayr's release dates named list and returns the corresponding value from dictionary's titles named list. """ game_infos_dict = reader.read_file(file_name, simple_string_is_enough=False) latest_year = max(game_infos_dict["release dates"]) index = game_infos_dict["release dates"].index(latest_year) return game_infos_dict["titles"][index]
def get_most_played(file_name): """Looks up the highest value from dictionary's copies sold lst named list and returns the corresponding value from dictionary's titles named list. """ preferences_dict = reader.read_file(file_name, simple_string_is_enough=False) index = preferences_dict["copies sold lst"].index( max(preferences_dict["copies sold lst"])) return preferences_dict["titles"][index]
def get_genres(file_name): """Returns sorted non redunant genres dict list through type conversions and bubble sort.""" game_infos_dict = reader.read_file(file_name, simple_string_is_enough=False) try: sorted_non_redundant_genres_list = bubble_sort(list(set(game_infos_dict["genres"]))) except TypeError: return "You changed the list to a non iterable type. Don't do that." else: return sorted_non_redundant_genres_list
def process(): graph = convert_to_graph(read_file('input')) positions = [] while graph: vs = filter_vertices(vertices, graph) selected_vertice = vs[0] graph.pop(selected_vertice, None) vertices.remove(selected_vertice) positions.append(selected_vertice) print("".join(positions))
def main(): file_names = [ 'a_example', 'b_should_be_easy', 'c_no_hurry', 'd_metropolis', 'e_high_bonus' ] file_name = file_names[0] map = read_file('resources/' + file_name + '.in') map.distribute_trips() write_cars('results/' + file_name + '.out', map.cars)
def decide(file_name, year): """Returns True if at least one matchins is found with the secound argument.""" game_infos_string = reader.read_file(file_name, simple_string_is_enough=True) try: int(year) except ValueError as err: return "You should have use number: " + str(err) else: if str(year) in game_infos_string: return True else: return False
def main(): ''' Takes in the command line arguments, selects a sorter, and then sorts the URLs. After they've been sorted, the URLs are written out to a file the user has selected (else the default file). ''' (parser, opts, args) = controller() if not opts.filename or not opts.algorithm or not opts.output: parser.print_help() sys.exit(1) try: algorithm = int(opts.algorithm) except: print("ERROR: invalid sorting algorithm, using default") algorithm = 1 filename = opts.filename output = opts.output if algorithm < 1 or algorithm > 8: parser.print_help() sys.exit(1) try: f = open(filename, 'r') unsorted = reader.read_file(f) except IOError as e: handle_io_exception(filename, e) try: ''' Grab the algorithm from the list of imported modules. Why this order? Because we can! ''' alg_list = [ selectionsort, heapsort, mergesort, radixsort, BinarySorter, HeapSorter, InsertionSorter, MergeSorter, ] sorter = alg_list[algorithm - 1] sorted_list = sorter.sort(unsorted) out = open(output, 'w') for url in sorted_list: out.write(url + "\n") except IOError as e: handle_io_exception(output, e)
def get_game(file_name, title): """Returns each properties of the given title in a list""" preferences_dict = reader.read_file(file_name, simple_string_is_enough=False) try: index = preferences_dict["titles"].index(title) except ValueError: return "No such game in the file." preferences = [ "titles", "copies sold lst", "release dates", "genres", "publishers" ] title_properties = [preferences_dict[key][index] for key in preferences] return title_properties
def get_line_number_by_title(file_name, title): """Returns how many EOL characters (\n) are before the the given argument value to be found and raises the return value by one to get the line number. This solution would not cause error. But the assignment clearly instructed me to raise one. """ game_infos_string = reader.read_file(file_name, simple_string_is_enough=True) try: length = game_infos_string.find(title) if length == -1: raise ValueError except ValueError: return "This title is not in the file." return game_infos_string[:length].count("\n") + 1
def open_file(self): files = QFileDialog.getOpenFileNames(self, "Open file", QDir.currentPath(), "Files (*.csv *.txt)") for file in files[0]: if (file not in self.data_sets): data = read_file(file) if (data is not None): self.data_sets[file] = data self.settings.setValue("work_dir", QFileInfo(file).absolutePath()) self.output_widget.add_to_list(file) else: QMessageBox.critical(self, "Main window", "File " + file[0] + " has an unknown format!") self.data_table.initialize(self.data_sets)
def visualize(file): vowels = 'aeiou' data = read_file(file).lower() results = {} # empty dict for v in vowels: size = data.count(v) results[v] = size x = list(results.keys()) h = list(results.values()) colors = ['red', 'blue', 'green', '#ff8800', '#9090ff'] plt.bar(x, h, color=colors) plt.savefig('images/vowels_bar.png', bbox_inches='tight')
def count_grouped_by_genre(file_name): """Returns a dictionary with genre names as keys and with the total number of that genre.""" preferences_dict = reader.read_file(file_name, simple_string_is_enough=False) try: genres_lst = list(set(preferences_dict["genres"])) except TypeError: return "You messed up the dict with your code. GJ" genre_count_dict = {} for genre_key in genres_lst: count_value = preferences_dict["genres"].count(genre_key) genre_count_dict[genre_key] = count_value return genre_count_dict
def process(): edges = read_file('input') vertices = {v for edge in edges for v in edge} duration = {v: (60 + ord(v) - 64) for v in vertices} graph = {vertice: [] for vertice in vertices} positions = [] for pre, pos in edges: graph[pre].append(pos) max_worker = 5 workers = [None for i in range(0, max_worker)] tick = 0 while True: print(tick, workers) # Update worker new_workers = [] # import pdb; pdb.set_trace() for worker in workers: if worker is None: new_workers.append(None) else: if duration[worker] == 1: graph.pop(worker, None) vertices.remove(worker) new_workers.append(None) else: duration[worker] -= 1 new_workers.append(worker) workers = new_workers # select next task next_vertices = list( filter(lambda x: x not in workers, filter_vertices(vertices, graph))) new_workers = [] #if tick == 7: # import pdb; pdb.set_trace() for idx, worker in enumerate(workers): if worker is None and next_vertices: new_workers.append(next_vertices.pop(0)) else: new_workers.append(worker) workers = new_workers #import pdb; pdb.set_trace() #print(tick) if not graph: break tick += 1 print(tick)
def generate_chains( fname, key_size ): chains = {} text = reader.read_file( fname ) words = text.split() i = 0 while i < len(words) - key_size: key = ' '.join( words[i : i+key_size] ) value = words[i + key_size] if key in chains: chains[ key ].append( value ) else: new_list = [] new_list.append( value ) chains[ key ] = new_list i+= 1 return chains
def main(): ''' Takes in the command line arguments, selects a sorter, and then sorts the URLs. After they've been sorted, the URLs are written out to a file the user has selected (else the default file). ''' (parser, opts, args) = controller() if not opts.filename or not opts.algorithm or not opts.output: parser.print_help() sys.exit(1) try: algorithm = int(opts.algorithm) except: print("ERROR: invalid sorting algorithm, using default") algorithm = 1 filename = opts.filename output = opts.output if algorithm < 1 or algorithm > 8: parser.print_help() sys.exit(1) try: f = open(filename, 'r') unsorted = reader.read_file(f) except IOError as e: handle_io_exception(filename, e) try: ''' Grab the algorithm from the list of imported modules. Why this order? Because we can! ''' alg_list = [selectionsort, heapsort, mergesort, radixsort, BinarySorter, HeapSorter, InsertionSorter, MergeSorter,] sorter = alg_list[algorithm - 1] sorted_list = sorter.sort(unsorted) out = open(output, 'w') for url in sorted_list: out.write(url + "\n") except IOError as e: handle_io_exception(output, e)
def readText(f,knum): t = reader.read_file(f) if gutenReformat: t = gutenreform(t) #print t t = t.replace("\n"," ") t = t.replace("\t","") lstCut = customsplit(t," ",knum) lst = customsplit(t," ",1) for i in range(len(lstCut) - 1): if not(lstCut[i] in dic): dic[lstCut[i]] = []#If key doesn't exist, make a new one dic[lstCut[i]].append(lst[i + knum]) return dic
def readText(f, knum): t = reader.read_file(f) if gutenReformat: t = gutenreform(t) #print t t = t.replace("\n", " ") t = t.replace("\t", "") lstCut = customsplit(t, " ", knum) lst = customsplit(t, " ", 1) for i in range(len(lstCut) - 1): if not (lstCut[i] in dic): dic[lstCut[i]] = [] #If key doesn't exist, make a new one dic[lstCut[i]].append(lst[i + knum]) return dic
def __init__(self, robot, com_port, indicator, tcp, points_filepath, axis='x'): """ ==========FIRST OF ALL USE IT========== @robot - Robot object or ip to robot @axis - indicator direction, one of string values: x, y, -x, -y @com_port - Serial object or com number @indicator - default indicator value """ if isinstance(robot, urx.Robot): self.ROBOT = robot else: self.ROBOT = urx.Robot(robot) if isinstance(com_port, serial.Serial): self.COM_PORT = com_port elif com_port is None: pass else: self.COM_PORT = serial.Serial(com_port, baudrate=9600, bytesize=serial.EIGHTBITS, stopbits=serial.STOPBITS_ONE, parity=serial.PARITY_NONE) self.FILE = rd.read_file(points_filepath) if axis.startswith('-'): self.AXIS_DIRECTION = -1 self.TOOL_AXIS = axis[1:] else: self.AXIS_DIRECTION = 1 self.TOOL_AXIS = axis self.ROBOT.set_tcp(tcp) # default indicator value self.INDICATOR_ORIGIN = indicator
def main(): ''' Takes in a list of URL's in a file and outputs the validity of the URL, the canonicalized URL, the uniqueness of the URL and the canonicalized URL ''' (parser, opts, args) = controller() if not opts.filename: parser.print_help() sys.exit(1) filename = opts.filename try: f = open(filename, 'r') raw_url_list = reader.read_file(f) except IOError as e: handle_io_exception(filename, e) unique_raw_urls = set() unique_canonicalized_urls = set() is_raw_valid = False is_raw_unique = False is_canonical_unique = False canonicalized_url = "" for raw_url in raw_url_list: print("Source: " + raw_url) is_raw_valid = url_validator.is_valid(raw_url) print("Valid: " + str(is_raw_valid)) canonicalized_url = url_normalize.url_normalize(raw_url) print("Canonical: " + canonicalized_url) is_raw_unique = raw_url not in unique_raw_urls if is_raw_unique: unique_raw_urls.add(raw_url) print("Source unique: " + str(is_raw_unique)) is_canonical_unique = canonicalized_url not in unique_canonicalized_urls if is_canonical_unique: unique_canonicalized_urls.add(canonicalized_url) print("Canonicalized URL unique: " + str(is_canonical_unique))
def __init__(self, categories, filelist): # A list of (tokenized sentence, class) tuples self.data = list() # A dictionary where the keys are the classes and the values, the # list of indexes in 'data' that belong to that class self.classDict = dict() for c in categories: self.classDict[c] = list() dataIndex = 0 self.ids = dict() for f in filelist: # Posicion of sentence in file fileIndex = 0 filename = f[5:-4] for (s, c) in reader.read_file(f, categories): self.data.append((s, c)) self.classDict[c].append(dataIndex) dataIndex += 1 self.ids[tuple(s)] = filename + '_' + str(fileIndex) fileIndex += 1
def calculate(arguments): filenames = [ os.path.join(arguments.input_path, filename) for filename in os.listdir(arguments.input_path) if filename.endswith(arguments.ext) ] medians = [] for filename in filenames: depth_map = read_file(filename) median = np.nanmedian(depth_map) medians.append(median) medians = np.array(medians) # Check and create output directory. if not os.path.exists(arguments.output_path): os.makedirs(arguments.output_path) with open(os.path.join(arguments.output_path, "median.txt"), "w") as file: file.write("{:.6f}".format(np.median(medians))) print("{:.6f}".format(np.median(medians)))
def when_was_top_sold_fps(file_name): """Returns corresponding value from dictionayr's release dates named list with the highest selling FPS game. The error handling is redundant. But just in case... """ game_infos_dict = reader.read_file(file_name, simple_string_is_enough=False) top_sell = 0.0 game_index = 0 for index, value in enumerate(game_infos_dict["genres"]): if value == "First-person shooter": try: game_sold = float(game_infos_dict["copies sold lst"][index]) except ValueError: return "You did something with my primary error handling, didn't you?" if game_sold > top_sell: top_sell = game_sold game_index = index try: year_of_top_sold_FPS = int(game_infos_dict["release dates"][game_index]) except ValueError: return "Use my primary error handling!" return year_of_top_sold_FPS
def test_doc(sess): # _, _, test_corpus = reader.read_corpus(index='relu', pick_train=False, pick_valid=False, pick_test=True) # _, test_corpus, _ = reader.read_corpus(index='relu', pick_train=False, pick_valid=True, pick_test=False) test_corpus = reader.read_file('data/corpus/check/test_klb') model = em_doc( max_seq_size=120, glossary_size=FLAGS.glossary_size, embedding_size=FLAGS.embedding_size, hidden_size=FLAGS.hidden_size, attn_lenth=FLAGS.attn_lenth, learning_rate=0.01 ) model.buildTrainGraph() init = tf.global_variables_initializer() # sess.run(init, feed_dict={model.pretrained_wv: pretrained_wv}) sess.run(init) saver = tf.train.Saver(tf.trainable_variables()) if not restore_from_checkpoint(sess, saver, FLAGS.ckpt_dir): return sum_loss = 0 sum_acc_t = 0 test_logits = [] test_labels = [] i = 0 print("Test initialized") start = time.time() for test_title_input, test_title_lenth, test_text_inputs, test_text_lenths, test_label in get_piece(test_corpus): feed_dict = { model.title_input: test_title_input, model.title_lenth: test_title_lenth, model.text_inputs: test_text_inputs, model.text_lenths: test_text_lenths, model.label: test_label } loss, t_acc, logit, label = sess.run([model.loss, model.train_accuracy, model._logits, model._labels], feed_dict=feed_dict) i += 1 print(i, logit, label) sum_loss += loss sum_acc_t += t_acc print(sum_acc_t, sum_acc_t[0] / i) test_logits.append(logit) test_labels.append(label) def f_value(): # 真正例 TP = 0 # 假正例 FP = 0 # 假反例 FN = 0 # 真反例 TN = 0 # We pay more attention on negative samples. for i in range(len(test_corpus)): if test_labels[i] == 0 and test_logits[i] == 0: TP += 1 elif test_labels[i] == 0 and test_logits[i] == 1: FN += 1 elif test_labels[i] == 1 and test_logits[i] == 0: FP += 1 elif test_labels[i] == 1 and test_logits[i] == 1: TN += 1 P = TP / (TP + FP + 0.0001) R = TP / (TP + FN + 0.0001) F = 2 * P * R / (P + R) P_ = TN / (TN + FN + 0.0001) R_ = TN / (TN + FP + 0.0001) F_ = 2 * P_ * R_ / (P_ + R_) print("About negative samples:") print(" precision rate: {:.4f}".format(P)) print(" recall rate: {:.4f}".format(R)) print(" f-value: {:.4f}".format(F)) print("About positive samples:") print(" precision rate: {:.4f}".format(P_)) print(" recall rate: {:.4f}".format(R_)) print(" f-value: {:.4f}".format(F_)) end = time.time() print("Average loss;".format(sum_loss[0] / len(test_corpus)), "time: {:.4f} sec;".format(end - start), "accuracy rate: {:.4f}".format(sum_acc_t[0] / len(test_corpus))) f_value()
# -*- coding: utf-8 -*- import reader config = reader.read_file("config.json") arguments = {}
def analyze(src): program = ' '.join(reader.read_file(src)) lex = reader.open_file('caspal/lex')[1] print(program) print(lex.analyze(program))
def seeComments(): array_comment = reader.get_list(reader.read_file("data/comments.csv")) array_name = reader.get_list(reader.read_file("data/usernames.csv")) return render_template("comments.html", array_comment = array_comment, array_name = array_name)
import reader tokens = reader.read_file("caspalc/tokens") parser = reader.read_json("parse_table.json") productions = parser["productions"] table = parser["table"] def check(tokens): stack = ["$", "<S>"] i = 0 current_word = get_token_info(tokens[i]) while stack != ["$"]: if i >= len(tokens) and stack != ["$"]: print("ERROR: Unexpected end of file.") return False print(stack, tokens[i]) if stack[-1] == current_word: i += 1 if i < len(tokens): current_word = get_token_info(tokens[i]) del stack[-1] else: top = stack[-1] subs = productions[table[top][current_word]] if subs == ["ERR"]: print("ERROR: SYNTAX ERROR NEAR " + current_word) return False
</body> </html> """ def print_tree(tree, node, board, max_depth, depth, current, path): s = [] s.extend(print_board(tree.n, tree.d, board, "".join(current), node, depth)) if node.children and path: childnode = node.children[path[0]] board.append(path[0]) s.extend(print_tree( tree, childnode, board, max_depth, depth + 1, current + [str(path[0]) + "/"], path[1:])) board.pop() return s tree = reader.read_file("solution.txt") reasonmap = reason.read_reason() boardvalues = read_boardvalues() app = Flask(__name__) reason = reason.read_reason() @app.route("/game/<path:subpath>") def root(subpath): return render(subpath) @app.route("/") def rootempty(): return render("") def render(subpath): path = [int(x) for x in subpath.split("/") if x] s = []
def analyze(src): program = ' '.join(reader.read_file(src)) lex = reader.open_file('caspalc/lex')[1] tokens = lex.analyze(program) reader.write_file('caspalc/tokens', tokens)
action='store_true', help='Optional: Do NOT anonymize the target IP address ' '/ network in the fingerprint') return parser.parse_args() if __name__ == '__main__': print_logo() args = parse_arguments() if args.debug: LOGGER.setLevel('DEBUG') filetype = determine_filetype(args.files) # Read the file(s) into a dataframe data: pd.DataFrame = pd.concat([ read_file(f, filetype=filetype, nr_processes=args.n) for f in args.files ]) attack = Attack(data, filetype) # Construct an Attack object with the DDoS data target = args.target or infer_target( attack) # Infer the attack target if not passed as an argument attack.filter_data_on_target( target_network=target) # Keep only the traffic sent to the target attack_vectors = extract_attack_vectors( attack) # Extract the attack vectors from the attack summary = compute_summary( attack_vectors ) # Compute summary statistics of the attack (e.g. average bps / Bpp / pps) # Generate fingeperint fingerprint = Fingerprint(target=target,
def main(): tree = reader.read_file("solution.txt") print(top_html()) print_tree(tree, tree.root, [], 5, 0) print(bottom_html())
def sort_abc(file_name): """Return the titles dict list in order. I have used the updated bubble sort from Python SI1""" game_infos_dict = reader.read_file(file_name, simple_string_is_enough=False) return bubble_sort(game_infos_dict["titles"])
mult_association(Hi, 'Yoyo', 'crud') mult_association(Hi, 'Yo', 'Blah') mult_association(Hi, 'Yo', 'Blah', False) nonExistant = mult_association({}, 'Oh no ', 'ERROR INCOMING') print Hi Yo = mult_association('Yo', 'Hi', 'Yo') print Yo print nonExistant cupid = {} append_dict(cupid, Hi, Yo, nonExistant) print cupid rename_dict_items(cupid, 0, 'Hi') print cupid Blah = {} WoahBro = {'HiThere': 'Mate'} mult_association(WoahBro, 'HiThere', 'Cuppid') print WoahBro append_dict(Blah, 'Hi', Hi, 'Yo', Yo, 'nonExistant', nonExistant) import reader import functionsPlus test_source = functionsPlus.except_parse(reader.read_file('HANC Data')) print dictify(test_source) print dictify('Name: HANC\nSomething: This\nOther Thing: That') dictName = {'Hi': 'There', 'Hit': ['Her'], 'Hitting': ['Her', 'There']} print dictName remove_list_values(dictName) print dictName