Python scraper based on AI
Python ai-crawlerai-scrapingai-searchcrawlerdata-extractionfirecrawl-alternativelarge-language-modelllmmarkdownragscrapingscraping-pythonweb-crawlerweb-crawlersweb-dataweb-data-extractionweb-scraperweb-scrapingweb-searchwebscraping
feat: add deepcopy error
smith peng71b22d48804c
diff --git a/scrapegraphai/utils/copy.py b/scrapegraphai/utils/copy.pyindex e1fdd37f..4f77f947 100644--- a/scrapegraphai/utils/copy.py+++ b/scrapegraphai/utils/copy.py@@ -2,6 +2,9 @@ import copyfrom typing import Any, Dict, Optionalfrom pydantic.v1 import BaseModel+class DeepCopyError(Exception):+ """Custom exception raised when an object cannot be deep-copied."""+ passdef safe_deepcopy(obj: Any) -> Any:"""@@ -16,6 +19,8 @@ def safe_deepcopy(obj: Any) -> Any:Any: A deep copy of the object if possible; otherwise, a shallowcopy if deep copying fails; if neither is possible, the originalobject is returned.+ Raises:+ DeepCopyError: If the object cannot be deep-copied or shallow-copied."""try:@@ -70,4 +75,4 @@ def safe_deepcopy(obj: Any) -> Any:try:return copy.copy(obj)except (TypeError, AttributeError):- raise TypeError(f"Failed to create a deep copy obj") from e+ raise DeepCopyError(f"Cannot deep copy the object of type {type(obj)}") from ediff --git a/tests/utils/copy_utils_test.py b/tests/utils/copy_utils_test.pyindex 8fb5a804..3cb1d5fb 100644--- a/tests/utils/copy_utils_test.py+++ b/tests/utils/copy_utils_test.py@@ -2,7 +2,7 @@ import copyimport pytest# Assuming the custom_deepcopy function is imported or defined above this line-from scrapegraphai.utils.copy import safe_deepcopy+from scrapegraphai.utils.copy import DeepCopyError, safe_deepcopyfrom pydantic.v1 import BaseModelfrom pydantic import BaseModel as BaseModelV2@@ -154,7 +154,7 @@ def test_deepcopy_object_without_dict():assert copy_obj_item is original_itemdef test_unhandled_type():- with pytest.raises(TypeError):+ with pytest.raises(DeepCopyError):original = {"origin": NonCopyableObject(10)}copy_obj = safe_deepcopy(original)@@ -162,7 +162,7 @@ def test_client():llm_instance_config = {"model": "moonshot-v1-8k","base_url": "https://api.moonshot.cn/v1",- "api_key": "xxx",+ "moonshot_api_key": "sk-OWo8hbSubp1QzOPyskOEwXQtZ867Ph0PZWCQdWrc3PH4o0lI",}from langchain_community.chat_models.moonshot import MoonshotChat