Skip to content

Commit

Permalink
chore: Remove redundant clean_requirements function and update packag…
Browse files Browse the repository at this point in the history
…e_utils
  • Loading branch information
safoinme committed Jul 29, 2024
1 parent 1909303 commit 031cd89
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
DatabricksEntrypointConfiguration,
)
from zenml.integrations.databricks.utils.databricks_utils import (
clean_requirements,
convert_step_to_task,
)
from zenml.io import fileio
Expand All @@ -53,6 +52,7 @@
from zenml.orchestrators.wheeled_orchestrator import WheeledOrchestrator
from zenml.stack import StackValidator
from zenml.utils import io_utils
from zenml.utils.package_utils import clean_requirements
from zenml.utils.pipeline_docker_image_builder import (
PipelineDockerImageBuilder,
)
Expand Down
23 changes: 0 additions & 23 deletions src/zenml/integrations/databricks/utils/databricks_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,26 +85,3 @@ def sanitize_labels(labels: Dict[str, str]) -> None:
labels[key] = re.sub(r"[^0-9a-zA-Z-_\.]+", "_", value)[:63].strip(
"-_."
)


def clean_requirements(requirements: List[str]) -> List[str]:
"""Clean requirements list.
Args:
requirements: List of requirements.
Returns:
Cleaned list of requirements
"""
cleaned = {}
for req in requirements:
package = (
req.split(">=")[0]
.split("==")[0]
.split("<")[0]
.split("[")[0]
.strip()
)
if package not in cleaned or ("=" in req or ">" in req or "<" in req):
cleaned[package] = req
return sorted(cleaned.values())
25 changes: 25 additions & 0 deletions src/zenml/utils/package_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# permissions and limitations under the License.
"""Utility functions for the package."""

from typing import List

import requests
from packaging import version

Expand Down Expand Up @@ -48,3 +50,26 @@ def is_latest_zenml_version() -> bool:
return False
else:
return True


def clean_requirements(requirements: List[str]) -> List[str]:
"""Clean requirements list from redundant requirements.
Args:
requirements: List of requirements.
Returns:
Cleaned list of requirements
"""
cleaned = {}
for req in requirements:
package = (
req.split(">=")[0]
.split("==")[0]
.split("<")[0]
.split("[")[0]
.strip()
)
if package not in cleaned or ("=" in req or ">" in req or "<" in req):
cleaned[package] = req
return sorted(cleaned.values())
53 changes: 53 additions & 0 deletions tests/unit/utils/test_package_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Copyright (c) ZenML GmbH 2022. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
# or implied. See the License for the specific language governing
# permissions and limitations under the License.
import pytest

from zenml.utils.package_utils import clean_requirements


@pytest.mark.parametrize("input_reqs, expected_output", [
(
["package1==1.0.0", "package2>=2.0.0", "package3<3.0.0"],
["package1==1.0.0", "package2>=2.0.0", "package3<3.0.0"]
),
(
["package1==1.0.0", "package1==2.0.0", "package2>=2.0.0"],
["package1==2.0.0", "package2>=2.0.0"]
),
(
["package1[extra]==1.0.0", "package2[test,dev]>=2.0.0"],
["package1[extra]==1.0.0", "package2[test,dev]>=2.0.0"]
),
(
["package1", "package2==2.0.0", "package1>=1.5.0", "package3<3.0.0"],
["package1>=1.5.0", "package2==2.0.0", "package3<3.0.0"]
),
(
[],
[]
),
])
def test_clean_requirements(input_reqs, expected_output):
"""Test clean_requirements function."""
assert clean_requirements(input_reqs) == expected_output

def test_clean_requirements_type_error():
"""Test clean_requirements function with wrong input type."""
with pytest.raises(TypeError):
clean_requirements("not a list")

def test_clean_requirements_value_error():
"""Test clean_requirements function with wrong input value."""
with pytest.raises(ValueError):
clean_requirements([1, 2, 3]) # List of non-string elements

0 comments on commit 031cd89

Please sign in to comment.