Skip to content
This repository has been archived by the owner on Nov 29, 2023. It is now read-only.

Commit

Permalink
feat: 433 Creates a Dict from a given json List (#437)
Browse files Browse the repository at this point in the history
* 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
5 people committed Apr 21, 2022
1 parent 15f530c commit f6fc3fa
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 0 deletions.
3 changes: 3 additions & 0 deletions package-parser/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ out/

# VSCode Settings
.vscode/

# IntelliJ/Pycharm settings
.idea/
Empty file.
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.
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")
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_"
]

0 comments on commit f6fc3fa

Please sign in to comment.