chore: cleanup ruff flake8-simplify linter rules (#8286)

Co-authored-by: -LAN- <laipz8200@outlook.com>
This commit is contained in:
Bowen Liang
2024-09-12 12:55:45 +08:00
committed by GitHub
parent 0bb7569d46
commit 0f14873255
34 changed files with 108 additions and 136 deletions

View File

@@ -201,9 +201,7 @@ class ListWorksheetRecordsTool(BuiltinTool):
elif value.startswith('[{"organizeId"'):
value = json.loads(value)
value = "".join([item["organizeName"] for item in value])
elif value.startswith('[{"file_id"'):
value = ""
elif value == "[]":
elif value.startswith('[{"file_id"') or value == "[]":
value = ""
elif hasattr(value, "accountId"):
value = value["fullname"]

View File

@@ -35,7 +35,7 @@ class NovitaAiModelQueryTool(BuiltinTool):
models_data=[],
headers=headers,
params=params,
recursive=False if result_type == "first sd_name" or result_type == "first name sd_name pair" else True,
recursive=not (result_type == "first sd_name" or result_type == "first name sd_name pair"),
)
result_str = ""

View File

@@ -39,7 +39,7 @@ class QRCodeGeneratorTool(BuiltinTool):
# get error_correction
error_correction = tool_parameters.get("error_correction", "")
if error_correction not in self.error_correction_levels.keys():
if error_correction not in self.error_correction_levels:
return self.create_text_message("Invalid parameter error_correction")
try:

View File

@@ -44,36 +44,36 @@ class SearchAPI:
@staticmethod
def _process_response(res: dict, type: str) -> str:
"""Process response from SearchAPI."""
if "error" in res.keys():
if "error" in res:
raise ValueError(f"Got error from SearchApi: {res['error']}")
toret = ""
if type == "text":
if "answer_box" in res.keys() and "answer" in res["answer_box"].keys():
if "answer_box" in res and "answer" in res["answer_box"]:
toret += res["answer_box"]["answer"] + "\n"
if "answer_box" in res.keys() and "snippet" in res["answer_box"].keys():
if "answer_box" in res and "snippet" in res["answer_box"]:
toret += res["answer_box"]["snippet"] + "\n"
if "knowledge_graph" in res.keys() and "description" in res["knowledge_graph"].keys():
if "knowledge_graph" in res and "description" in res["knowledge_graph"]:
toret += res["knowledge_graph"]["description"] + "\n"
if "organic_results" in res.keys() and "snippet" in res["organic_results"][0].keys():
if "organic_results" in res and "snippet" in res["organic_results"][0]:
for item in res["organic_results"]:
toret += "content: " + item["snippet"] + "\n" + "link: " + item["link"] + "\n"
if toret == "":
toret = "No good search result found"
elif type == "link":
if "answer_box" in res.keys() and "organic_result" in res["answer_box"].keys():
if "title" in res["answer_box"]["organic_result"].keys():
if "answer_box" in res and "organic_result" in res["answer_box"]:
if "title" in res["answer_box"]["organic_result"]:
toret = f"[{res['answer_box']['organic_result']['title']}]({res['answer_box']['organic_result']['link']})\n"
elif "organic_results" in res.keys() and "link" in res["organic_results"][0].keys():
elif "organic_results" in res and "link" in res["organic_results"][0]:
toret = ""
for item in res["organic_results"]:
toret += f"[{item['title']}]({item['link']})\n"
elif "related_questions" in res.keys() and "link" in res["related_questions"][0].keys():
elif "related_questions" in res and "link" in res["related_questions"][0]:
toret = ""
for item in res["related_questions"]:
toret += f"[{item['title']}]({item['link']})\n"
elif "related_searches" in res.keys() and "link" in res["related_searches"][0].keys():
elif "related_searches" in res and "link" in res["related_searches"][0]:
toret = ""
for item in res["related_searches"]:
toret += f"[{item['title']}]({item['link']})\n"

View File

@@ -44,12 +44,12 @@ class SearchAPI:
@staticmethod
def _process_response(res: dict, type: str) -> str:
"""Process response from SearchAPI."""
if "error" in res.keys():
if "error" in res:
raise ValueError(f"Got error from SearchApi: {res['error']}")
toret = ""
if type == "text":
if "jobs" in res.keys() and "title" in res["jobs"][0].keys():
if "jobs" in res and "title" in res["jobs"][0]:
for item in res["jobs"]:
toret += (
"title: "
@@ -65,7 +65,7 @@ class SearchAPI:
toret = "No good search result found"
elif type == "link":
if "jobs" in res.keys() and "apply_link" in res["jobs"][0].keys():
if "jobs" in res and "apply_link" in res["jobs"][0]:
for item in res["jobs"]:
toret += f"[{item['title']} - {item['company_name']}]({item['apply_link']})\n"
else:

View File

@@ -44,25 +44,25 @@ class SearchAPI:
@staticmethod
def _process_response(res: dict, type: str) -> str:
"""Process response from SearchAPI."""
if "error" in res.keys():
if "error" in res:
raise ValueError(f"Got error from SearchApi: {res['error']}")
toret = ""
if type == "text":
if "organic_results" in res.keys() and "snippet" in res["organic_results"][0].keys():
if "organic_results" in res and "snippet" in res["organic_results"][0]:
for item in res["organic_results"]:
toret += "content: " + item["snippet"] + "\n" + "link: " + item["link"] + "\n"
if "top_stories" in res.keys() and "title" in res["top_stories"][0].keys():
if "top_stories" in res and "title" in res["top_stories"][0]:
for item in res["top_stories"]:
toret += "title: " + item["title"] + "\n" + "link: " + item["link"] + "\n"
if toret == "":
toret = "No good search result found"
elif type == "link":
if "organic_results" in res.keys() and "title" in res["organic_results"][0].keys():
if "organic_results" in res and "title" in res["organic_results"][0]:
for item in res["organic_results"]:
toret += f"[{item['title']}]({item['link']})\n"
elif "top_stories" in res.keys() and "title" in res["top_stories"][0].keys():
elif "top_stories" in res and "title" in res["top_stories"][0]:
for item in res["top_stories"]:
toret += f"[{item['title']}]({item['link']})\n"
else:

View File

@@ -44,11 +44,11 @@ class SearchAPI:
@staticmethod
def _process_response(res: dict) -> str:
"""Process response from SearchAPI."""
if "error" in res.keys():
if "error" in res:
raise ValueError(f"Got error from SearchApi: {res['error']}")
toret = ""
if "transcripts" in res.keys() and "text" in res["transcripts"][0].keys():
if "transcripts" in res and "text" in res["transcripts"][0]:
for item in res["transcripts"]:
toret += item["text"] + " "
if toret == "":

View File

@@ -35,7 +35,7 @@ class StableDiffusionTool(BuiltinTool, BaseStabilityAuthorization):
if model in ["sd3", "sd3-turbo"]:
payload["model"] = tool_parameters.get("model")
if not model == "sd3-turbo":
if model != "sd3-turbo":
payload["negative_prompt"] = tool_parameters.get("negative_prompt", "")
response = post(

View File

@@ -206,10 +206,9 @@ class StableDiffusionTool(BuiltinTool):
# Convert image to RGB and save as PNG
try:
with Image.open(io.BytesIO(image_binary)) as image:
with io.BytesIO() as buffer:
image.convert("RGB").save(buffer, format="PNG")
image_binary = buffer.getvalue()
with Image.open(io.BytesIO(image_binary)) as image, io.BytesIO() as buffer:
image.convert("RGB").save(buffer, format="PNG")
image_binary = buffer.getvalue()
except Exception as e:
return self.create_text_message(f"Failed to process the image: {str(e)}")

View File

@@ -27,7 +27,7 @@ class WikipediaAPIWrapper:
self.doc_content_chars_max = doc_content_chars_max
def run(self, query: str, lang: str = "") -> str:
if lang in wikipedia.languages().keys():
if lang in wikipedia.languages():
self.lang = lang
wikipedia.set_lang(self.lang)