Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions 1_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import re
from collections import Counter


def find_spammer(li, how_many_spamers=2):
'''найти спамера/ов'''
# print(li)
ip_list = (s[0] for s in li)
di_res = Counter(ip_list)
print(di_res.most_common(how_many_spamers))


def get_pars_logs():
'''обработчик лог файла'''
result_li = []
with open('nginx_logs.txt', 'r') as file:
for line in file:
pat = r'^\d+.\d+.\d+.\d+'
pat_call = r'GET \s*\S*'
try:
ip = re.search(pattern=pat, string=line)[0]
call = re.search(pat_call, string=line)[0]
get, load = call.split(' ')
tup = ip, get, load
result_li.append(tup)
except TypeError:
pass
line = file.readline()
find_spammer(result_li)


get_pars_logs()
23 changes: 23 additions & 0 deletions 3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

with open('users.csv') as file:
names = file.readlines()

with open('hobby.csv') as file:
hobby = file.read().split(',')
len_names = len(names)
len_hobby = len(hobby)
while len_hobby < len_names:
hobby.append('None')
len_hobby = len(hobby)
if len_names < len_hobby:
exit(1)

new_di = {}
for i, name in enumerate(names):
new_di[name.replace('\n', '')] = hobby[i].replace('\n', '')
print(new_di)

with open('result.txt', 'w') as file:
for k, v in new_di.items():
line = f'{k} {v}\n'
file.write(line)
37 changes: 37 additions & 0 deletions 4_5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from sys import argv


def run():
with open(path_users_file) as users:
gen_usr = (user.replace('\n', '') for user in users)

with open(path_hobby_file) as hobbys:
gen_hobby = (hobby.replace('\n', '') for hobby in hobbys)

res_str = ''
with open(path_result, 'w') as res:
while True:
try:
res_str = f'{gen_usr.__next__()}: {gen_hobby.__next__()}\n'
res.write(res_str)
except StopIteration:
break


if len(argv) == 1:
path_users_file = 'users.csv'
path_hobby_file = 'hobby.csv'
path_result = 'users_hobby.txt'
run()
elif len(argv) != 4:
print('''set names:
1) users_names
2) hobbys
3) result_file ''')
elif len(argv) == 4:
path_users_file = argv[1]
path_hobby_file = argv[2]
path_result = argv[3]
run()
else:
print('Not correct input')
19 changes: 19 additions & 0 deletions 5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from sys import argv
import csv

path_db = 'bakery.csv'


def print_db():
'''show all db'''


def add_in_db(inner):
'''add in db'''
with open(path_db, 'a') as file:
csv.writer(file, inner)

if len(argv) == 1:
print_db()
elif len(argv) ==2:
add_in_db(argv[1])
38 changes: 38 additions & 0 deletions 6_add_sale.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from sys import argv

path_db = 'bakery.csv'


def print_db():
'''show all db'''
with open(path_db, 'r') as file:
db = file.read()
print(db, end='')


def add_in_db(inner):
'''add in db'''

with open(path_db, 'a') as file:
inner = inner.replace('\n', '')
file.write(f'{inner}\n\r')


def print_period(start, stop):
bool_start = False
with open(path_db, 'r') as file:
for num, line in enumerate(file.readlines()):
if bool_start:
print(line, end='')
if str(num) == start:
bool_start = True
elif stop == str(num):
break


if len(argv) == 1:
print_db()
elif len(argv) ==2:
add_in_db(argv[1])
elif len(argv) ==3:
print_period(argv[1], argv[2])
37 changes: 37 additions & 0 deletions 6_show_sale.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from sys import argv

path_db = 'bakery.csv'


def print_db():
'''show all db'''
with open(path_db, 'r') as file:
line = file.readline()
while line:
print(line, end='')
line = file.readline()


def print_period(start, stop=None):
bool_start = False
count = 1
with open(path_db, 'r') as file:
line = file.readline()
while line:
if str(count) == start:
bool_start = True
elif stop == str(count):
print(line, end='')
break
if bool_start:
print(line, end='')
line = file.readline()
count += 1


if len(argv) == 1:
print_db()
elif len(argv) == 2:
print_period(argv[1])
elif len(argv) == 3:
print_period(argv[1], argv[2])
23 changes: 23 additions & 0 deletions 7_refactor_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from sys import argv

path_db = 'bakery.csv'


def set_new(num, value):
count = 1
with open(path_db, 'r+') as file:
line = file.readline()
while line:
if str(count) == num:
pos = file.tell()
file.seek(pos)
file.write(value)
break
line = file.readline()
count += 1
else:
print(f'file not have position {num}')


if len(argv) == 3:
set_new(argv[1], argv[2])
4 changes: 4 additions & 0 deletions bakery.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
5978,5
8914,3
7879,1
1573,7
2 changes: 2 additions & 0 deletions hobby.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
скалолазание,охота
горные лыжи,
2 changes: 2 additions & 0 deletions new_hobby.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
скалолазание,охота
горные лыжи,
Loading