This repository has been archived by the owner on Nov 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 433 Creates a Dict from a given json List (#437)
* Creates a Dict from a given json List * Changed wrong Type Str to str * style: apply automatic fixes of linters * fix: pytest failed on expected exception * fix: file location is required * style: apply automatic fixes of linters * fix: assumed wrong working dir * removed changes in .idea and added it to gitignore of package-parser * revert changes in .idea folder * improve code style Co-authored-by: jofaul <jofaul@users.noreply.github.com> Co-authored-by: Aclrian <32142988+Aclrian@users.noreply.github.com> Co-authored-by: Aclrian <Aclrian@users.noreply.github.com> Co-authored-by: GideonKoenig <44088734+GideonKoenig@users.noreply.github.com>
- Loading branch information
1 parent
15f530c
commit f6fc3fa
Showing
6 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,3 +30,6 @@ out/ | |
|
||
# VSCode Settings | ||
.vscode/ | ||
|
||
# IntelliJ/Pycharm settings | ||
.idea/ |
Empty file.
44 changes: 44 additions & 0 deletions
44
package-parser/package_parser/commands/generate_annotations/_generate_unused_annotations.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import json | ||
import re | ||
from typing import Dict, List, Tuple | ||
|
||
|
||
def generate_unused_annotations(in_file_path: str): | ||
""" | ||
Returns a Dict of unused functions or classes | ||
:param in_file_path: JSON file that contains a list of unused functions or classes | ||
""" | ||
|
||
with open(in_file_path, "r", encoding="UTF-8") as in_file: | ||
data = json.load(in_file) | ||
|
||
unuseds: Dict[str, Dict[str, str]] = {} | ||
for name in data: | ||
formatted_name = format_name(name) | ||
unuseds[formatted_name] = {"target": formatted_name} | ||
|
||
return unuseds | ||
|
||
|
||
def format_name(name: str): | ||
if name is None: | ||
return None | ||
|
||
parts = re.split("\\.", name) | ||
newname = "sklearn/" + parts[0] | ||
|
||
if len(parts) == 1: | ||
return newname | ||
|
||
slash = False | ||
for part in parts[1:-1]: | ||
if not slash and re.match("^_{0,2}[A-Z]", part): | ||
slash = True | ||
if slash: | ||
newname += "/" + part | ||
else: | ||
newname += "." + part | ||
|
||
newname += "/" + parts[-1] | ||
return newname |
Empty file.
67 changes: 67 additions & 0 deletions
67
package-parser/tests/commands/generate_annotations/test_generate_unused_annotations.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import pytest | ||
from package_parser.commands.generate_annotations._generate_unused_annotations import ( | ||
format_name, | ||
generate_unused_annotations, | ||
) | ||
|
||
EXPECTED_VALUE = { | ||
"sklearn/sklearn.base/_BaseEstimator/__setstate__": { | ||
"target": "sklearn/sklearn.base/_BaseEstimator/__setstate__" | ||
}, | ||
"sklearn/sklearn.base/is_regressor": { | ||
"target": "sklearn/sklearn.base/is_regressor" | ||
}, | ||
"sklearn/sklearn.cluster._agglomerative/linkage_tree": { | ||
"target": "sklearn/sklearn.cluster._agglomerative/linkage_tree" | ||
}, | ||
"sklearn/sklearn.cluster._kmeans/MiniBatchKMeans/init_size_": { | ||
"target": "sklearn/sklearn.cluster._kmeans/MiniBatchKMeans/init_size_" | ||
}, | ||
} | ||
|
||
|
||
def test_format_underscores(): | ||
assert ( | ||
format_name("sklearn.cluster._kmeans._MiniBatchKMeans.random_state_") | ||
== "sklearn/sklearn.cluster._kmeans/_MiniBatchKMeans/random_state_" | ||
) | ||
|
||
|
||
def test_format_uppercase(): | ||
assert ( | ||
format_name("sklearn.cluster._kmeans.MiniBatchKMeans.random_state_") | ||
== "sklearn/sklearn.cluster._kmeans/MiniBatchKMeans/random_state_" | ||
) | ||
|
||
|
||
def test_format_normal(): | ||
assert ( | ||
format_name("sklearn.cluster._mean_shift.get_bin_seeds") | ||
== "sklearn/sklearn.cluster._mean_shift/get_bin_seeds" | ||
) | ||
|
||
|
||
def test_format_one_part(): | ||
assert format_name("test") == "sklearn/test" | ||
|
||
|
||
def test_format_none(): | ||
assert format_name(None) is None | ||
|
||
|
||
def test_format_empty(): | ||
assert format_name("") == "sklearn/" | ||
|
||
|
||
def test_generate(): | ||
assert ( | ||
generate_unused_annotations( | ||
"tests/commands/generate_annotations/unused_functions_list.json" | ||
) | ||
== EXPECTED_VALUE | ||
) | ||
|
||
|
||
def test_generate_bad_path(): | ||
with pytest.raises(FileNotFoundError): | ||
generate_unused_annotations("aaaaaaaaaaaAAAAAAAAAAAA") |
6 changes: 6 additions & 0 deletions
6
package-parser/tests/commands/generate_annotations/unused_functions_list.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[ | ||
"sklearn.base._BaseEstimator.__setstate__", | ||
"sklearn.base.is_regressor", | ||
"sklearn.cluster._agglomerative.linkage_tree", | ||
"sklearn.cluster._kmeans.MiniBatchKMeans.init_size_" | ||
] |