import time import csv import json count line in small file my_file tx

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import time
import csv
import json
# count line in small file
f = 'my_file.txt'
start_time_1 = time.time()
num_lines_1 = len(open(f).readlines())
print("#1--- %s seconds ---" % (time.time() - start_time_1))
print(f'Number of lines in {f}: {num_lines_1}')
# count line in large files - third variant is winner)
start_time_2 = time.time()
num_lines_2 = 0
for ind, line in enumerate(open(f)): num_lines_2 += 1
print("#2--- %s seconds ---" % (time.time() - start_time_2))
print(f'Number of lines in {f}: {num_lines_2}')
start_time_3 = time.time()
num_lines_3 = 0
for line in open(f): num_lines_3 += 1
print("#3--- %s seconds ---" % (time.time() - start_time_3))
print(f'Number of lines in {f}: {num_lines_3}')
# time command
# real or total or elapsed (wall clock time) is the time from start to finish of the call. It is the time from the moment you hit the Enter key until the moment the wget command is completed.
# user - The cumulative time spent by all the CPUs during the computation
# system or sys - The cumulative time spent by all the CPUs during system-related tasks such as memory allocation.
# Notice that sometimes user + sys might be greater than real, as multiple processors may work in parallel.
# exemples
# Tetianas-MacBook-Pro:~ tetiana.markova$ time ping google.com
#
# real 0m12.228s
# user 0m0.001s
# sys 0m0.007s
# Дано список пар (рядок, число) - написанти програму, яка по цьому списку сформує список де кожній парі відповідає частина списку де “рядок” повторюється “число” раз
# Вхідні дані
# [('aa', 2), ('c1', 1), ('d2', 4)]
# Очікуваний результат
# ['aa', 'aa', 'c1', 'd2', 'd2', 'd2', 'd2']
# Пояснення. (‘aa’, 2) - спершу іде 2 рази аа, (‘c1’, 1) потім іде 1 с1, (‘d2’, 4) потім 4ри d2
initial_data = [('aa', 2), ('c1', 1), ('d2', 4)]
result_data = []
for i in initial_data:
for some in range(0, i[1]):
result_data.append(i[0])
print(f'{result_data}')
# 1. There is a file with logs, get list of ding ids that appear in this file and write to txt file,
# 2. get list mac,ding_id and write into csv file
def get_parameters_from_logs(input_file, parameter_1, parameter_2, output_file_txt, output_file_csv):
csv_file = csv.reader(open(input_file, "r"), delimiter=" ")
file_parameters = open(output_file_txt, 'a')
double_parametres = open(output_file_csv, 'a')
double_parametres.write(f'{parameter_1}, {parameter_2}\n')
line_count = 0
for row in csv_file:
if line_count == 0: line_count += 1
else:
line_count += 1
parsed_row = (json.loads(row[0]))
if parameter_1 in parsed_row:
file_parameters.write(f'{parsed_row[parameter_1]}\n')
if parameter_2 in parsed_row:
double_parametres.write(f'{parsed_row[parameter_1]}, {parsed_row[parameter_2]}\n')
else: double_parametres.write(f'{parsed_row[parameter_1]}, null\n')
get_parameters_from_logs('athena_logs.csv', 'ding_id', 'doorbot_id', 'ding_ids.txt', 'ding_mac.csv')