Merge pull request 'degtyarev_mikhail_lab_2 is ready' (#98) from degtyarev_mikhail_lab_2 into main
Reviewed-on: http://student.git.athene.tech/Alexey/DAS_2023_1/pulls/98
This commit is contained in:
commit
5341271f9e
BIN
degtyarev_mikhail_lab_2/1.png
Normal file
BIN
degtyarev_mikhail_lab_2/1.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 8.2 KiB |
BIN
degtyarev_mikhail_lab_2/2.png
Normal file
BIN
degtyarev_mikhail_lab_2/2.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 28 KiB |
BIN
degtyarev_mikhail_lab_2/3.png
Normal file
BIN
degtyarev_mikhail_lab_2/3.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 4.7 KiB |
66
degtyarev_mikhail_lab_2/README.md
Normal file
66
degtyarev_mikhail_lab_2/README.md
Normal file
@ -0,0 +1,66 @@
|
||||
# Лабораторная 2
|
||||
## Вариант 9
|
||||
|
||||
## Задание
|
||||
Разработать два приложения такие, что результат первого является исходными данными для второго.
|
||||
|
||||
Описание задач двух программ:
|
||||
- Берёт из каталога /var/data случайный файл и перекладывает его в /var/result/data.txt.
|
||||
- Ищет наибольшее число из файла /var/data/data.txt и сохраняет его вторую степень в /var/result/result.txt.
|
||||
|
||||
## Описание Программы
|
||||
|
||||
### Как запустить?
|
||||
|
||||
```docker compose up -d```
|
||||
|
||||
### Созданные файлы:
|
||||
|
||||
**Dockerfile**
|
||||
|
||||
Они идентичны
|
||||
|
||||
```
|
||||
FROM python:3.11
|
||||
WORKDIR /app
|
||||
COPY . .
|
||||
CMD ["python", "worker_1.py"]
|
||||
```
|
||||
**docker-compose**
|
||||
|
||||
```
|
||||
version: "3"
|
||||
services:
|
||||
worker1:
|
||||
build:
|
||||
context: ./worker1
|
||||
dockerfile: Dockerfile
|
||||
volumes:
|
||||
- ./data:/var/data
|
||||
- ./result:/var/result
|
||||
|
||||
worker2:
|
||||
depends_on:
|
||||
- worker1
|
||||
build:
|
||||
context: ./worker2
|
||||
dockerfile: Dockerfile
|
||||
volumes:
|
||||
- ./result:/var/data
|
||||
- ./result:/var/result
|
||||
|
||||
```
|
||||
|
||||
**Исходные файлы**
|
||||
|
||||
Файлы содержат абсолютно рандомные числа:
|
||||
|
||||
![](1.png)
|
||||
|
||||
|
||||
### Результаты
|
||||
![](2.png)
|
||||
|
||||
![](3.png)
|
||||
|
||||
Ссылка на видео: https://vk.com/video/@id151119348?z=video151119348_456239199%2Fln-K8jegCHykzxSWCSO4A%2Fpl_151119348_-2
|
3
degtyarev_mikhail_lab_2/data/1.txt
Normal file
3
degtyarev_mikhail_lab_2/data/1.txt
Normal file
@ -0,0 +1,3 @@
|
||||
163
|
||||
446
|
||||
769
|
4
degtyarev_mikhail_lab_2/data/2.txt
Normal file
4
degtyarev_mikhail_lab_2/data/2.txt
Normal file
@ -0,0 +1,4 @@
|
||||
234
|
||||
125
|
||||
664
|
||||
889
|
4
degtyarev_mikhail_lab_2/data/3.txt
Normal file
4
degtyarev_mikhail_lab_2/data/3.txt
Normal file
@ -0,0 +1,4 @@
|
||||
1111
|
||||
522
|
||||
333
|
||||
531
|
19
degtyarev_mikhail_lab_2/docker-compose.yml
Normal file
19
degtyarev_mikhail_lab_2/docker-compose.yml
Normal file
@ -0,0 +1,19 @@
|
||||
version: "3"
|
||||
services:
|
||||
worker1:
|
||||
build:
|
||||
context: ./worker1
|
||||
dockerfile: Dockerfile
|
||||
volumes:
|
||||
- ./data:/var/data
|
||||
- ./result:/var/result
|
||||
|
||||
worker2:
|
||||
depends_on:
|
||||
- worker1
|
||||
build:
|
||||
context: ./worker2
|
||||
dockerfile: Dockerfile
|
||||
volumes:
|
||||
- ./result:/var/data
|
||||
- ./result:/var/result
|
4
degtyarev_mikhail_lab_2/result/data.txt
Normal file
4
degtyarev_mikhail_lab_2/result/data.txt
Normal file
@ -0,0 +1,4 @@
|
||||
1111
|
||||
522
|
||||
333
|
||||
531
|
1
degtyarev_mikhail_lab_2/result/result.txt
Normal file
1
degtyarev_mikhail_lab_2/result/result.txt
Normal file
@ -0,0 +1 @@
|
||||
1234321
|
4
degtyarev_mikhail_lab_2/worker1/Dockerfile
Normal file
4
degtyarev_mikhail_lab_2/worker1/Dockerfile
Normal file
@ -0,0 +1,4 @@
|
||||
FROM python:3.11
|
||||
WORKDIR /app
|
||||
COPY . .
|
||||
CMD ["python", "worker_1.py"]
|
33
degtyarev_mikhail_lab_2/worker1/worker_1.py
Normal file
33
degtyarev_mikhail_lab_2/worker1/worker_1.py
Normal file
@ -0,0 +1,33 @@
|
||||
import os
|
||||
import shutil
|
||||
import random
|
||||
import logging
|
||||
|
||||
def find_random_file(directory):
|
||||
files = os.listdir(directory)
|
||||
if not files:
|
||||
return None
|
||||
|
||||
random_file = random.choice(files)
|
||||
return random_file
|
||||
|
||||
def main():
|
||||
data_dir = '/var/data'
|
||||
result_file = '/var/result/data.txt'
|
||||
|
||||
try:
|
||||
random_file = find_random_file(data_dir)
|
||||
|
||||
if random_file:
|
||||
src_path = os.path.join(data_dir, random_file)
|
||||
shutil.copy2(src_path, result_file)
|
||||
logging.info(f'Successfully copied {src_path} to {result_file}')
|
||||
else:
|
||||
logging.warning('No files to copy.')
|
||||
|
||||
except Exception as e:
|
||||
logging.error(f'An error occurred: {str(e)}')
|
||||
|
||||
if __name__ == "__main__":
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
main()
|
4
degtyarev_mikhail_lab_2/worker2/Dockerfile
Normal file
4
degtyarev_mikhail_lab_2/worker2/Dockerfile
Normal file
@ -0,0 +1,4 @@
|
||||
FROM python:3.11
|
||||
WORKDIR /app
|
||||
COPY . .
|
||||
CMD ["python", "worker_2.py"]
|
30
degtyarev_mikhail_lab_2/worker2/worker_2.py
Normal file
30
degtyarev_mikhail_lab_2/worker2/worker_2.py
Normal file
@ -0,0 +1,30 @@
|
||||
def find_max_and_save_square(file_path):
|
||||
try:
|
||||
with open(file_path, 'r') as file:
|
||||
numbers = [int(line.strip()) for line in file.readlines()]
|
||||
|
||||
if numbers:
|
||||
max_number = max(numbers)
|
||||
square = max_number ** 2
|
||||
|
||||
with open("/var/result/result.txt", 'w') as output_file:
|
||||
output_file.write(str(square))
|
||||
|
||||
print(f"Наибольшее число: {max_number}, его вторая степень: {max_number}^2 = {square}")
|
||||
return square
|
||||
else:
|
||||
return None
|
||||
|
||||
except Exception as e:
|
||||
return None
|
||||
|
||||
def main():
|
||||
input_file_path = "/var/data/data.txt"
|
||||
|
||||
result = find_max_and_save_square(input_file_path)
|
||||
|
||||
if result is None:
|
||||
print("No result to save.")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Loading…
Reference in New Issue
Block a user