Чистый неподкупный рабочий код 2-й лабораторной

This commit is contained in:
the 2024-10-04 14:33:05 +04:00
parent 8a96320fd5
commit 1213b5db3c
8 changed files with 110 additions and 0 deletions

2
bogdanov_dmitry_lab_2/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
data/
result/

View File

@ -0,0 +1,7 @@
FROM python:latest
WORKDIR /app
COPY app.py /app/
CMD ["python", "app.py"]

View File

@ -0,0 +1,22 @@
import os
# Variant 2
def solve(dir_files, dir_result, filename_result):
filenames = os.listdir(dir_files)
result = ''
for filename in filenames:
filepath = os.path.join(dir_files, filename)
file = open(filepath, "r")
result += f"{file.readline()}"
file.close()
if not os.path.exists(dir_result):
os.makedirs(dir_result)
if os.listdir(dir_result):
return
result_file = open(os.path.join(dir_result, filename_result), "w")
result_file.write(result)
result_file.close()
if __name__ == "__main__":
solve('/var/data', '/var/result', 'data.txt')

View File

@ -0,0 +1,7 @@
FROM python:latest
WORKDIR /app
COPY app.py /app/
CMD ["python", "app.py"]

View File

@ -0,0 +1,15 @@
import os
# Variant 1
def solve(dir_input, dir_result, filename_result):
file_input = open(os.path.join(dir_input, 'data.txt'))
inputs = [int(line) for line in file_input.readlines()]
if inputs:
result = max(inputs) ** 2
file_result = open(os.path.join(dir_result, filename_result), "w")
file_result.write(str(result))
file_result.close()
if __name__ == "__main__":
solve("/var/result", '/var/result', 'result.txt')

View File

@ -0,0 +1,7 @@
FROM python:latest
WORKDIR /app
COPY generator.py /app/
CMD ["python", "generate_files.py"]

View File

@ -0,0 +1,26 @@
import os
import random as rnd
import string
def generate_filename(l):
return ''.join(rnd.choices(string.ascii_lowercase + string.digits, k=l)) + '.txt'
def generate_files(dir, num_files, num_lines):
if not os.path.exists(dir):
os.makedirs(dir)
if os.listdir(dir):
return
for i in range(num_files):
filename = generate_filename(20)
filepath = os.path.join(dir, filename)
file = open(filepath, "w")
for j in range(num_lines):
file.write(f"{rnd.randint(-1000, 1000)}\n")
file.close()
if __name__ == "__main__":
generate_files('/var/data', 50, 50)

View File

@ -0,0 +1,24 @@
services:
generator:
build:
context: ./app-generator
volumes:
- ./data:/var/data
entrypoint: python generator.py
app1:
build:
context: ./app-1
volumes:
- ./data:/var/data
- ./result:/var/result
depends_on:
- generator
app2:
build:
context: ./app-2
volumes:
- ./result:/var/result
depends_on:
- app1