コード例 #1
0
if __name__ == '__main__':

    data_file_path = 'data/route-costs-10000000.txt'
    print("This should take roughly a minute...")

    # =============== Uncomment to benchmark building the trie =============
    #
    # stopwatch.mark("Starting to build trie")
    # trie = build_cost_trie(data_file_path)
    #
    # stopwatch.mark("Starting to pickle")
    # pickle.dump(trie, open('trie.pickle', 'wb'))
    #
    # ======================================================================

    stopwatch.mark("Unpickling Trie:")
    trie = pickle.load(open('trie.pickle', 'rb'))

    stopwatch.mark("Reading 10,000 phone numbers from file:")
    phone_numbers = open("data/phone-numbers-10000.txt").read().splitlines()

    stopwatch.mark(f"Getting costs for {len(phone_numbers)} phone numbers:")
    find_call_cost(phone_numbers, trie)

    # ===== Uncomment to benchmark writing the costs to results file =======
    #
    # with open('data/results-3.txt', 'w') as out:
    #     numbers = (number for number in open(
    #         'data/phone-numbers-10000.txt').read().splitlines())
    #     for number in numbers:
    #         cost = trie.search(number)
コード例 #2
0
    Params:
    costs_dict: dictionary containing number prefix, cost data

    input_file_path: path to input file with valid phone numbers (string)
    # Format (+1913123434)

    output_file_path: path to to file where outputted phonenumber, cost
    data will be written
    """

    with open(input_file_path) as f:
        test_numbers = f.read().splitlines()

    with open(output_file_path, 'w') as out:
        for number in test_numbers:
            cost = search_number(costs_dict, number)
            out.write(f'{number},{cost}\n')


if __name__ == '__main__':
    from stopwatch import StopWatch
    watch = StopWatch()

    watch.mark("Compiling 10,000,000 route costs into dictionary:")
    costs_dict = get_cost_dict("data/route-costs-10000000.txt")

    watch.mark("Getting costs for 1,000 numbers and storing in results file:")
    get_all_costs(costs_dict, 'data/phone-numbers-1000.txt',
                  'data/results-2.txt')
    watch.end()