8000 Add files via upload · NUNGEN/python-scriptsFORK@5168018 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5168018

Browse files
authored
Add files via upload
1 parent 5efdb3d commit 5168018

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed

scripts/06_execution_time02.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"""
2+
ExecutionTime
3+
4+
This class is used for timing execution of code.
5+
6+
For example:
7+
8+
timer = ExecutionTime()
9+
print 'Hello world!'
10+
print 'Finished in {} seconds.'.format(timer.duration())
11+
12+
"""
13+
14+
15+
import time
16+
import random
17+
#
18+
#
19+
# class ExecutionTime:
20+
# def __init__(self):
21+
# self.start_time = time.time()
22+
#
23+
# def duration(self):
24+
# return time.time() - self.start_time
25+
#
26+
#
27+
# # ---- run code ---- #
28+
#
29+
#
30+
# timer = ExecutionTime()
31+
# sample_list = list()
32+
# my_list = [random.randint(1, 888898) for num in
33+
# range(1, 1000000) if num % 2 == 0]
34+
# print('Finished in {} seconds.'.format(timer.duration()))
35+
36+
37+
class ExecutionTime:
38+
def __init__(self):
39+
self.start_time = time.time()
40+
def duration(self):
41+
return time.time() - self.start_time
42+
43+
timer = ExecutionTime()
44+
my_list = []
45+
for num in range(1, 1000000):
46+
if num % 2 == 0:
47+
my_list.append(random.randint(1, 888898))
48+
print("Finished in {:.2f} seconds".format(timer.duration()))
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# import csv
2+
# import sys
3+
# import json
4+
#
5+
# """
6+
# Example usage:
7+
#
8+
# $ python 33_country_code.py 33_sample_csv.csv 33_country_codes.json
9+
# """
10+
#
11+
#
12+
# def get_data(csv_file, json_file):
13+
# countryCodes = []
14+
# countryNames = []
15+
# continentNames = []
16+
# with open(csv_file, 'rt') as file_one:
17+
# reader = csv.reader(file_one)
18+
# with open(json_file) as file_two:
19+
# json_data = json.load(file_two)
20+
# all_countries = json_data["country"]
21+
# for csv_row in reader:
22+
# for json_row in all_countries:
23+
# if csv_row[0] == json_row["countryCode"]:
24+
# countryCodes.append(json_row["countryCode"])
25+
# countryNames.append(json_row["countryName"])
26+
# continentNames.append(json_row["continentName"])
27+
#
28+
# return [
29+
# countryCodes,
30+
# countryNames,
31+
# continentNames
32+
# ]
33+
#
34+
#
35+
# def write_data(array_of_arrays):
36+
# with open('data.csv', 'wt') as csv_out:
37+
# writer = csv.writer(csv_out)
38+
# rows = zip(
39+
# array_of_arrays[0],
40+
# array_of_arrays[1],
41+
# array_of_arrays[2]
42+
# )
43+
# for row in rows:
44+
# writer.writerow(row)
45+
#
46+
#
47+
# if __name__ == '__main__':
48+
# csv_file_name = sys.argv[1]
49+
# json_file_name = sys.argv[2]
50+
# data = get_data(csv_file_name, json_file_name)
51+
# write_data(data)
52+
53+
import json
54+
import csv
55+
56+
def getData():
57+
with open("33_country_codes.json", "r") as file:
58+
data = json.load(file) # Correcting json.load to actually load the data
59+
countryCodes = []
60+
countries = []
61+
continents = []
62+
63+
for country in data["country"]:
64+
countryCodes.append(country["countryCode"])
65+
countries.append(country["countryName"])
66+
continents.append(country["continentName"])
67+
68+
return countryCodes, countries, continents
69+
70+
71+
def saveCSV(countryCodes, countries, continents):
72+
with open("MyCSV.csv", "w", newline="") as file:
73+
writer = csv.writer(file)
74+
writer.writerow(["country code", "country name", "continent name"])
75+
76+
for i in range(len(countries)):
77+
writer.writerow([countryCodes[i], countries[i], continents[i]])
78+
79+
80+
# Get country data
81+
countryCodes, countries, continents = getData()
82+
83+
# Save data to CSV
84+
saveCSV(countryCodes, countries, continents)

0 commit comments

Comments
 (0)
0