├── tests ├── __init__.py └── test_main.py ├── app └── main.py ├── .gitignore ├── apples.csv ├── oranges.csv ├── requirements.txt ├── grapes.csv ├── bananas.csv ├── .flake8 ├── .github └── workflows │ └── test.yml └── README.md /tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /app/main.py: -------------------------------------------------------------------------------- 1 | # write your code here 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | .vscode/ 3 | *.iml 4 | .env 5 | .DS_Store 6 | venv/ 7 | .pytest_cache/ 8 | **__pycache__/ 9 | -------------------------------------------------------------------------------- /apples.csv: -------------------------------------------------------------------------------- 1 | supply,30 2 | buy,10 3 | buy,13 4 | supply,17 5 | buy,10 6 | supply,5 7 | supply,80 8 | buy,39 9 | supply,56 10 | buy,32 11 | buy,11 12 | -------------------------------------------------------------------------------- /oranges.csv: -------------------------------------------------------------------------------- 1 | buy,19 2 | supply,33 3 | buy,37 4 | supply,170 5 | buy,80 6 | supply,44 7 | supply,31 8 | buy,3 9 | supply,17 10 | buy,6 11 | buy,9 12 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | flake8==5.0.4 2 | flake8-annotations==2.9.1 3 | flake8-quotes==3.3.1 4 | flake8-variables-names==0.0.5 5 | pep8-naming==0.13.2 6 | pytest==7.1.3 7 | -------------------------------------------------------------------------------- /grapes.csv: -------------------------------------------------------------------------------- 1 | supply,78 2 | buy,9 3 | buy,17 4 | buy,22 5 | supply,27 6 | buy,80 7 | supply,51 8 | supply,82 9 | buy,49 10 | supply,102 11 | supply,12 12 | buy,29 13 | buy,146 14 | -------------------------------------------------------------------------------- /bananas.csv: -------------------------------------------------------------------------------- 1 | buy,10 2 | supply,300 3 | buy,130 4 | buy,12 5 | supply,20 6 | buy,1 7 | supply,50 8 | supply,12 9 | supply,8 10 | buy,41 11 | supply,10 12 | buy,12 13 | buy,10 14 | buy,77 15 | supply,91 16 | -------------------------------------------------------------------------------- /.flake8: -------------------------------------------------------------------------------- 1 | [flake8] 2 | inline-quotes = " 3 | ignore = E203, E266, W503, ANN002, ANN003, ANN101, ANN102, ANN401, N807, N818 4 | max-line-length = 79 5 | max-complexity = 18 6 | select = B,C,E,F,W,T4,B9,ANN,Q0,N8,VNE 7 | exclude = venv, tests 8 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: Test 2 | 3 | on: [push, pull_request_target] 4 | 5 | jobs: 6 | test: 7 | runs-on: ubuntu-latest 8 | 9 | steps: 10 | - name: Checkout repo 11 | uses: actions/checkout@v2 12 | with: 13 | ref: ${{github.event.pull_request.head.ref}} 14 | repository: ${{github.event.pull_request.head.repo.full_name}} 15 | 16 | - name: Set Up Python 3.10 17 | uses: actions/setup-python@v2 18 | with: 19 | python-version: "3.10" 20 | 21 | - name: Install pytest and flake8 22 | run: | 23 | python -m pip install --upgrade pip 24 | pip install -r requirements.txt 25 | 26 | - name: Run flake8 27 | run: flake8 app/ 28 | - name: Run tests 29 | timeout-minutes: 5 30 | run: pytest tests/ 31 | - uses: mate-academy/auto-approve-action@v2 32 | if: ${{ github.event.pull_request && success() }} 33 | with: 34 | github-token: ${{ github.token }} 35 | - uses: mate-academy/auto-reject-action@v2 36 | if: ${{ github.event.pull_request && failure() }} 37 | with: 38 | github-token: ${{ github.token }} 39 | -------------------------------------------------------------------------------- /tests/test_main.py: -------------------------------------------------------------------------------- 1 | from __future__ import annotations 2 | import os 3 | from types import TracebackType 4 | from typing import Optional, Type 5 | 6 | import pytest 7 | 8 | from app.main import create_report 9 | 10 | 11 | class CleanUpFile: 12 | def __init__(self, filename: str) -> None: 13 | self.filename = filename 14 | 15 | def __enter__(self) -> CleanUpFile: 16 | return self 17 | 18 | def __exit__( 19 | self, 20 | exc_type: Optional[Type[BaseException]], 21 | exc_val: Optional[BaseException], 22 | exc_tb: Optional[TracebackType], 23 | ) -> None: 24 | if os.path.exists(self.filename): 25 | os.remove(self.filename) 26 | 27 | 28 | @pytest.mark.parametrize( 29 | "data_file_name,report_file_name,expected_report", 30 | [ 31 | ( 32 | "apples.csv", 33 | "apples_report.csv", 34 | "supply,188\nbuy,115\nresult,73\n", 35 | ), 36 | ( 37 | "bananas.csv", 38 | "bananas_report.csv", 39 | "supply,491\nbuy,293\nresult,198\n", 40 | ), 41 | ( 42 | "grapes.csv", 43 | "grapes_report.csv", 44 | "supply,352\nbuy,352\nresult,0\n", 45 | ), 46 | ( 47 | "oranges.csv", 48 | "oranges_report.csv", 49 | "supply,295\nbuy,154\nresult,141\n", 50 | ), 51 | ], 52 | ) 53 | def test_create_report( 54 | data_file_name: str, report_file_name: str, expected_report: str 55 | ) -> None: 56 | create_report(data_file_name, report_file_name) 57 | 58 | with CleanUpFile(report_file_name): 59 | with open(report_file_name, "r") as report_file: 60 | assert report_file.read() == expected_report 61 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Work with files 2 | 3 | Implement function `create_report` 4 | which will make a report using data from the market after a working day. 5 | 6 | - Read [the guideline](https://github.com/mate-academy/py-task-guideline/blob/main/README.md) before start 7 | 8 | This method has two parameters: 9 | - `data_file_name: str` - you should read data from this file 10 | - `report_file_name: str` - you should write the result to this file 11 | 12 | Input file has `.csv` format. CSV is a simple file format used to store tabular data. 13 | This type of file is very popular for storing information. So we will start working on it. 14 | CSV stands for "comma-separated values". Its data fields are most often separated or delimited by a comma. 15 | 16 | For example, let's say you had a spreadsheet containing the following data: 17 | 18 | | operation type | amount | 19 | | :------------: | :-------:| 20 | | supply | 30 | 21 | | buy | 10 | 22 | | buy | 13 | 23 | | supply | 17 | 24 | | buy | 10 | 25 | 26 | The above data could be represented in a CSV-formatted file as follows: 27 | ```csv 28 | supply,30 29 | buy,10 30 | buy,13 31 | supply,17 32 | buy,10 33 | ``` 34 | 35 | __Your task is to read all data from the input csv file, 36 | create a report and write it to the new file (the name of this file is the second parameter of the method).__ 37 | 38 | Example of the report: 39 | ```csv 40 | supply,47 41 | buy,33 42 | result,14 43 | ``` 44 | 45 | Explanation: 46 | - `supply = 30 + 17 = 47` 47 | - `buy = 10 + 13 + 10 = 33` 48 | - `result = supply - buy = 47 - 33 = 14` 49 | 50 | Note: 51 | 52 | The last line of the file may be empty and should not be processed 53 | 54 | Please use terminal here to run script/tests, to avoid file path errors 55 | --------------------------------------------------------------------------------