From c9305763eb3bdb6a80465cc2461a38df0a2cd7db Mon Sep 17 00:00:00 2001 From: Yaroslav Havryliv Date: Mon, 5 Sep 2022 12:06:10 +0300 Subject: [PATCH] explanation added to functions --- README.md | 141 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 137 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 45c524d5..7ba120ff 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,9 @@ # Linter formatter -- Read [the guideline](https://github.com/mate-academy/py-task-guideline/blob/main/README.md) before start +Read [the guideline](https://github.com/mate-academy/py-task-guideline/blob/main/README.md) before starting. The `flake8` linter gives the following error report when checking a directory with source code files: + ```python errors = { "./test_source_code_2.py": [], @@ -99,18 +100,150 @@ errors = { ], } ``` + Here `errors` is a dictionary, where the keys are the path to the file, and the value of this key is a list of errors, where each is a dictionary. But we store the execution results in a slightly different format. Write 3 functions to convert the report into the format we need: -1. `format_linter_error` - formats a single error; -2. `format_single_linter_file` - formats all errors for a particular file; -3. `format_linter_report` - formats all errors for all report files. +1. `format_linter_error` - formats a single error: + +```python +error = { + "code": "E501", + "filename": "./source_code_2.py", + "line_number": 18, + "column_number": 80, + "text": "line too long (99 > 79 characters)", + "physical_line": ' return f"I like to filter, rounding, doubling, ' + "store and decorate numbers: {', '.join(items)}!\"", +} + +print(format_linter_error(error=error)) +# The output will be: +{ + "line": 18, + "column": 80, + "message": "line too long (99 > 79 characters)", + "name": "E501", + "source": "flake8" +} +``` + +2. `format_single_linter_file` - formats all errors for a particular file and adds the `path` key — path to the file, +and the `status` key — "failed" if there are errors, "passed" if there are no errors: + +```python +errors = [ + { + "code": "E501", + "filename": "./source_code_2.py", + "line_number": 18, + "column_number": 80, + "text": "line too long (99 > 79 characters)", + "physical_line": ' return f"I like to filter, rounding, doubling, ' + "store and decorate numbers: {', '.join(items)}!\"", + }, + { + "code": "W292", + "filename": "./source_code_2.py", + "line_number": 18, + "column_number": 100, + "text": "no newline at end of file", + "physical_line": ' return f"I like to filter, rounding, doubling, ' + "store and decorate numbers: {', '.join(items)}!\"", + }, +] + +print(format_single_linter_file(file_path="./source_code_2.py", errors=errors)) +# The output will be: +{ + "errors": + [ + { + "line": 18, + "column": 80, + "message": "line too long (99 > 79 characters)", + "name": "E501", + "source": "flake8" + }, + { + "line": 18, + "column": 100, + "message": "no newline at end of file", + "name": "W292", + "source": "flake8" + } + ], + "path": "./source_code_2.py", + "status": "failed" +} +``` + +3. `format_linter_report` - formats all errors for all report files: + +```python +report_file = { + "./test_source_code_2.py": [], + "./source_code_2.py": + [ + { + "code": "E501", + "filename": "./source_code_2.py", + "line_number": 18, + "column_number": 80, + "text": "line too long (99 > 79 characters)", + "physical_line": ' return f"I like to filter, rounding, doubling, ' + "store and decorate numbers: {', '.join(items)}!\"", + }, + { + "code": "W292", + "filename": "./source_code_2.py", + "line_number": 18, + "column_number": 100, + "text": "no newline at end of file", + "physical_line": ' return f"I like to filter, rounding, doubling, ' + "store and decorate numbers: {', '.join(items)}!\"", + }, + ] +} + +print(format_linter_report(linter_report=report_file)) +# The output will be: +[ + { + "errors": [], + "path": "./test_source_code_2.py", + "status": "passed" + }, + { + "errors": + [ + { + "line": 18, + "column": 80, + "message": "line too long (99 > 79 characters)", + "name": "E501", + "source": "flake8" + }, + { + "line": 18, + "column": 100, + "message": "no newline at end of file", + "name": "W292", + "source": "flake8" + } + ], + "path": "./source_code_2.py", + "status": "failed" + } +] +``` All functions must contain only the `return` keyword. Required storage format: + ```python errors = [ {"errors": [], "path": "./test_source_code_2.py", "status": "passed"},