-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
72 lines (61 loc) · 2.93 KB
/
Copy pathmain.py
File metadata and controls
72 lines (61 loc) · 2.93 KB
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
# -*- coding: utf-8 -*-
import os
def read_reciept(name):
# Создаем словарь из файла
with open(name, mode='r', encoding='utf-8') as f:
result = {}
for line in f:
name = line.strip()
quatity = int(f.readline().strip())
lines = []
for item in range(quatity):
data = f.readline().strip().split(' | ')
lines.append({'ingredient_name': data[0],
'quantity': int(data[1]),
'measure': data[2]})
result[name] = lines
f.readline()
return result
def rec_reciept(book, name='reciept.txt'):
# создаем файл из словаря
f = open(name, mode='w', encoding='utf-8')
for dish, recipe in book.items():
f.write(dish + '\n')
f.write(str(len(recipe)) + '\n')
for ingridients in recipe:
f.write(' | '.join(map(str, ingridients.values())) + '\n')
f.write('\n')
f.close()
def get_shop_list_by_dishes(dishes, person_count):
# Создаем список покупок на ужин
result = {}
for i in dishes:
if i in cook_book:
for reciept in cook_book[i]:
if reciept['ingredient_name'] in result:
result[reciept['ingredient_name']]['quantity'] += reciept['quantity'] * person_count
else:
result[reciept['ingredient_name']] = {'measure': reciept['measure'],
'quantity': reciept['quantity'] * person_count}
return result
def add_files(lst_files):
# Задание 3
work_zone = []
for i in lst_files:
with open(os.path.join(text_direct, i), mode='r', encoding='utf-8') as f:
work_zone.append((i, len(f.readlines())))
work_zone.sort(key=lambda x: x[1])
f = open('result.txt', 'w', encoding='utf-8')
for name, lenght in work_zone:
with open(os.path.join(text_direct, name), mode='r', encoding='utf-8') as work_file:
print(name + '\n' + str(lenght), file=f)
for line in work_file:
print(line.rstrip(), file=f)
f.close()
reciept_direct = os.path.join(os.getcwd(), 'reciepts') # Адрес папки для рецептов
text_direct = os.path.join(os.getcwd(), 'texts') # Адрес папки для текстов
lst_files = os.listdir(text_direct) # Список файлов для склейки в 1
# add_files(lst_files) # Вызов функции для склейки файлов
# cook_book = read_reciept(os.path.join(reciept_direct, 'rec2')) # создание словаря из текста в файле
# print(get_shop_list_by_dishes(['Омлет', 'Омлет'], 1)) # сколько продуктов купить
# rec_reciept(cook_book, 'rec2') # на всякий пожарный, создание файла из словаря