diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b823c27 --- /dev/null +++ b/.gitignore @@ -0,0 +1,71 @@ +# Python cache files +__pycache__/ +*.py[cod] +*$py.class + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg + +# PyInstaller +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +.hypothesis/ +.pytest_cache/ + +# Flask instance folder +instance/ + +# Environment variables +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# IDE +.vscode/ +.idea/ +*.swp +*.swo +*~ + +# OS +.DS_Store +Thumbs.db + +# Project specific +uploads/ +*.pdf +*.xlsx +*.xls \ No newline at end of file diff --git a/part1.py b/part1.py index c6ef0e3..7febb1d 100644 --- a/part1.py +++ b/part1.py @@ -41,21 +41,30 @@ def report_file(): doch = [] for sh in range(len(sheets)): letters = read_numeric_values_from_excel(request.json['filePath'], sheets[sh]['name'], sheets[sh]['columnLetters']) - sum = 0 + total = 0 print(letters) - numbers = list(filter(lambda x: int(x) != None, letters)) + numbers = [] + for x in letters: + try: + num = float(x) # Use float to handle both int and decimal numbers + numbers.append(num) + except (ValueError, TypeError): + continue # Skip non-numeric values print('numbers', numbers) - if sheets[sh]['active'] == 'sum': - for i in numbers: - sum += i - else: - index = 0 - for i in numbers: - sum += i - index += 1 - sum /= index - doch.append({"sheetName": sheets[sh]['name'], "active": sheets[sh]['active'], "answer": sum}) - report_pdf_file(doch) + if sheets[sh]['active'] == 'sum': + for i in numbers: + total += i + else: + index = 0 + for i in numbers: + total += i + index += 1 + if index > 0: + total /= index + else: + total = 0 # Handle case with no numeric values + doch.append({"sheetName": sheets[sh]['name'], "active": sheets[sh]['active'], "answer": total}) + report_pdf_file(doch) return doch def report_pdf_file(doch): diff --git a/part2.py b/part2.py index 0c47781..12b3b20 100644 --- a/part2.py +++ b/part2.py @@ -27,21 +27,24 @@ def upload_file(): } return obj1 -def sum_excel_values(file_path): - wb = load_workbook(file_path) - total_sum = 0 - for sheet in wb.sheetnames: - ws = wb[sheet] - for row in ws.iter_rows(values_only=True): - for cell in row: - if cell is not None: - total_sum += int(cell) +def sum_excel_values(file_path): + wb = load_workbook(file_path) + total_sum = 0 + for sheet in wb.sheetnames: + ws = wb[sheet] + for row in ws.iter_rows(values_only=True): + for cell in row: + if cell is not None: + try: + total_sum += float(cell) # Use float to handle both int and decimal numbers + except (ValueError, TypeError): + continue # Skip non-numeric values return total_sum -@app.route('/field_sum', methods=['POST']) -def sum_of_values_field_in_excel(): - file_path = request.json['filePath'] - sum = sum_excel_values(file_path) - return {'sum': sum} +@app.route('/field_sum', methods=['POST']) +def sum_of_values_field_in_excel(): + file_path = request.json['filePath'] + total = sum_excel_values(file_path) + return {'sum': total} def plot_excel_sheets_sum(file_path): wb = load_workbook(file_path) @@ -50,10 +53,13 @@ def plot_excel_sheets_sum(file_path): for sheet in wb.sheetnames: ws = wb[sheet] sheet_sum = 0 - for row in ws.iter_rows(values_only=True): - for cell in row: - if cell is not None: - sheet_sum += int(cell) + for row in ws.iter_rows(values_only=True): + for cell in row: + if cell is not None: + try: + sheet_sum += float(cell) # Use float to handle both int and decimal numbers + except (ValueError, TypeError): + continue # Skip non-numeric values sums.append(sheet_sum) sheet_names.append(sheet) plt.bar(sheet_names, sums) @@ -69,25 +75,31 @@ def plot_excel(): plot_excel_sheets_sum(file_path) return {"successfully": 100} -def avarage_excel_values(file_path): - wb = load_workbook(file_path) - total_sum = [] - for sheet in wb.sheetnames: - ws = wb[sheet] - sum = 0 - for row in ws.iter_rows(values_only=True): - for cell in row: - if cell is not None: - sum += int(cell) - total_sum.append(sum) - sum = 0 - for i in total_sum: - sum += i - return sum / len(total_sum) -@app.route('/average', methods=['POST']) -def average_of_sheets(): - file_path = request.json['filePath'] - avg = avarage_excel_values(file_path) +def average_excel_values(file_path): + wb = load_workbook(file_path) + total_sum = [] + for sheet in wb.sheetnames: + ws = wb[sheet] + sheet_total = 0 + for row in ws.iter_rows(values_only=True): + for cell in row: + if cell is not None: + try: + sheet_total += float(cell) # Use float to handle both int and decimal numbers + except (ValueError, TypeError): + continue # Skip non-numeric values + total_sum.append(sheet_total) + overall_total = 0 + for i in total_sum: + overall_total += i + if len(total_sum) > 0: + return overall_total / len(total_sum) + else: + return 0 # Handle case with no sheets +@app.route('/average', methods=['POST']) +def average_of_sheets(): + file_path = request.json['filePath'] + avg = average_excel_values(file_path) return {'average': avg} @@ -101,7 +113,7 @@ def doch_pdf_total(): print(names) sums = plot_excel_sheets_sum(file_path)['sums'] print(sums) - avg = avarage_excel_values(file_path) + avg = average_excel_values(file_path) print(avg) obj_pdf = { 'file_name': file_name,