Skip to content

Commit

Permalink
Merge pull request mate-academy#164 from mate-academy/readme_changed
Browse files Browse the repository at this point in the history
explanation added to functions
  • Loading branch information
Y-Havryliv authored Sep 6, 2022
2 parents 621ff59 + c930576 commit 7c0e80a
Showing 1 changed file with 137 additions and 4 deletions.
141 changes: 137 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -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": [],
Expand Down Expand Up @@ -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"},
Expand Down

0 comments on commit 7c0e80a

Please sign in to comment.