chore: apply flake8-comprehensions Ruff rules to improve collection comprehensions (#5652)

Co-authored-by: -LAN- <laipz8200@outlook.com>
This commit is contained in:
Bowen Liang
2024-06-27 11:21:31 +08:00
committed by GitHub
parent 2e718b85e9
commit dcb72e0067
58 changed files with 123 additions and 136 deletions

View File

@@ -105,15 +105,15 @@ class BingSearchTool(BuiltinTool):
def validate_credentials(self, credentials: dict[str, Any], tool_parameters: dict[str, Any]) -> None:
key = credentials.get('subscription_key', None)
key = credentials.get('subscription_key')
if not key:
raise Exception('subscription_key is required')
server_url = credentials.get('server_url', None)
server_url = credentials.get('server_url')
if not server_url:
server_url = self.url
query = tool_parameters.get('query', None)
query = tool_parameters.get('query')
if not query:
raise Exception('query is required')
@@ -170,7 +170,7 @@ class BingSearchTool(BuiltinTool):
if not server_url:
server_url = self.url
query = tool_parameters.get('query', None)
query = tool_parameters.get('query')
if not query:
raise Exception('query is required')

View File

@@ -16,12 +16,12 @@ class BarChartTool(BuiltinTool):
data = data.split(';')
# if all data is int, convert to int
if all([i.isdigit() for i in data]):
if all(i.isdigit() for i in data):
data = [int(i) for i in data]
else:
data = [float(i) for i in data]
axis = tool_parameters.get('x_axis', None) or None
axis = tool_parameters.get('x_axis') or None
if axis:
axis = axis.split(';')
if len(axis) != len(data):

View File

@@ -17,14 +17,14 @@ class LinearChartTool(BuiltinTool):
return self.create_text_message('Please input data')
data = data.split(';')
axis = tool_parameters.get('x_axis', None) or None
axis = tool_parameters.get('x_axis') or None
if axis:
axis = axis.split(';')
if len(axis) != len(data):
axis = None
# if all data is int, convert to int
if all([i.isdigit() for i in data]):
if all(i.isdigit() for i in data):
data = [int(i) for i in data]
else:
data = [float(i) for i in data]

View File

@@ -16,10 +16,10 @@ class PieChartTool(BuiltinTool):
if not data:
return self.create_text_message('Please input data')
data = data.split(';')
categories = tool_parameters.get('categories', None) or None
categories = tool_parameters.get('categories') or None
# if all data is int, convert to int
if all([i.isdigit() for i in data]):
if all(i.isdigit() for i in data):
data = [int(i) for i in data]
else:
data = [float(i) for i in data]

View File

@@ -37,10 +37,10 @@ class GaodeRepositoriesTool(BuiltinTool):
apikey=self.runtime.credentials.get('api_key')))
weatherInfo_data = weatherInfo_response.json()
if weatherInfo_response.status_code == 200 and weatherInfo_data.get('info') == 'OK':
contents = list()
contents = []
if len(weatherInfo_data.get('forecasts')) > 0:
for item in weatherInfo_data['forecasts'][0]['casts']:
content = dict()
content = {}
content['date'] = item.get('date')
content['week'] = item.get('week')
content['dayweather'] = item.get('dayweather')

View File

@@ -39,10 +39,10 @@ class GihubRepositoriesTool(BuiltinTool):
f"q={quote(query)}&sort=stars&per_page={top_n}&order=desc")
response_data = response.json()
if response.status_code == 200 and isinstance(response_data.get('items'), list):
contents = list()
contents = []
if len(response_data.get('items')) > 0:
for item in response_data.get('items'):
content = dict()
content = {}
updated_at_object = datetime.strptime(item['updated_at'], "%Y-%m-%dT%H:%M:%SZ")
content['owner'] = item['owner']['login']
content['name'] = item['name']

View File

@@ -26,11 +26,11 @@ class JinaReaderTool(BuiltinTool):
if 'api_key' in self.runtime.credentials and self.runtime.credentials.get('api_key'):
headers['Authorization'] = "Bearer " + self.runtime.credentials.get('api_key')
target_selector = tool_parameters.get('target_selector', None)
target_selector = tool_parameters.get('target_selector')
if target_selector is not None and target_selector != '':
headers['X-Target-Selector'] = target_selector
wait_for_selector = tool_parameters.get('wait_for_selector', None)
wait_for_selector = tool_parameters.get('wait_for_selector')
if wait_for_selector is not None and wait_for_selector != '':
headers['X-Wait-For-Selector'] = wait_for_selector
@@ -43,7 +43,7 @@ class JinaReaderTool(BuiltinTool):
if tool_parameters.get('gather_all_images_at_the_end', False):
headers['X-With-Images-Summary'] = 'true'
proxy_server = tool_parameters.get('proxy_server', None)
proxy_server = tool_parameters.get('proxy_server')
if proxy_server is not None and proxy_server != '':
headers['X-Proxy-Url'] = proxy_server

View File

@@ -33,7 +33,7 @@ class JinaSearchTool(BuiltinTool):
if tool_parameters.get('gather_all_images_at_the_end', False):
headers['X-With-Images-Summary'] = 'true'
proxy_server = tool_parameters.get('proxy_server', None)
proxy_server = tool_parameters.get('proxy_server')
if proxy_server is not None and proxy_server != '':
headers['X-Proxy-Url'] = proxy_server

View File

@@ -94,7 +94,7 @@ class GoogleTool(BuiltinTool):
google_domain = tool_parameters.get("google_domain", "google.com")
gl = tool_parameters.get("gl", "us")
hl = tool_parameters.get("hl", "en")
location = tool_parameters.get("location", None)
location = tool_parameters.get("location")
api_key = self.runtime.credentials['searchapi_api_key']
result = SearchAPI(api_key).run(query, result_type=result_type, num=num, google_domain=google_domain, gl=gl, hl=hl, location=location)

View File

@@ -72,11 +72,11 @@ class GoogleJobsTool(BuiltinTool):
"""
query = tool_parameters['query']
result_type = tool_parameters['result_type']
is_remote = tool_parameters.get("is_remote", None)
is_remote = tool_parameters.get("is_remote")
google_domain = tool_parameters.get("google_domain", "google.com")
gl = tool_parameters.get("gl", "us")
hl = tool_parameters.get("hl", "en")
location = tool_parameters.get("location", None)
location = tool_parameters.get("location")
ltype = 1 if is_remote else None

View File

@@ -82,7 +82,7 @@ class GoogleNewsTool(BuiltinTool):
google_domain = tool_parameters.get("google_domain", "google.com")
gl = tool_parameters.get("gl", "us")
hl = tool_parameters.get("hl", "en")
location = tool_parameters.get("location", None)
location = tool_parameters.get("location")
api_key = self.runtime.credentials['searchapi_api_key']
result = SearchAPI(api_key).run(query, result_type=result_type, num=num, google_domain=google_domain, gl=gl, hl=hl, location=location)

View File

@@ -107,7 +107,7 @@ class SearXNGSearchTool(BuiltinTool):
if not host:
raise Exception('SearXNG api is required')
query = tool_parameters.get('query', None)
query = tool_parameters.get('query')
if not query:
return self.create_text_message('Please input query')

View File

@@ -43,7 +43,7 @@ class GetMarkdownTool(BuiltinTool):
Invoke the SerplyApi tool.
"""
url = tool_parameters["url"]
location = tool_parameters.get("location", None)
location = tool_parameters.get("location")
api_key = self.runtime.credentials["serply_api_key"]
result = SerplyApi(api_key).run(url, location=location)

View File

@@ -55,7 +55,7 @@ class SerplyApi:
f"Employer: {job['employer']}",
f"Location: {job['location']}",
f"Link: {job['link']}",
f"""Highest: {", ".join([h for h in job["highlights"]])}""",
f"""Highest: {", ".join(list(job["highlights"]))}""",
"---",
])
)
@@ -78,7 +78,7 @@ class JobSearchTool(BuiltinTool):
query = tool_parameters["query"]
gl = tool_parameters.get("gl", "us")
hl = tool_parameters.get("hl", "en")
location = tool_parameters.get("location", None)
location = tool_parameters.get("location")
api_key = self.runtime.credentials["serply_api_key"]
result = SerplyApi(api_key).run(query, gl=gl, hl=hl, location=location)

View File

@@ -80,7 +80,7 @@ class NewsSearchTool(BuiltinTool):
query = tool_parameters["query"]
gl = tool_parameters.get("gl", "us")
hl = tool_parameters.get("hl", "en")
location = tool_parameters.get("location", None)
location = tool_parameters.get("location")
api_key = self.runtime.credentials["serply_api_key"]
result = SerplyApi(api_key).run(query, gl=gl, hl=hl, location=location)

View File

@@ -83,7 +83,7 @@ class ScholarSearchTool(BuiltinTool):
query = tool_parameters["query"]
gl = tool_parameters.get("gl", "us")
hl = tool_parameters.get("hl", "en")
location = tool_parameters.get("location", None)
location = tool_parameters.get("location")
api_key = self.runtime.credentials["serply_api_key"]
result = SerplyApi(api_key).run(query, gl=gl, hl=hl, location=location)

View File

@@ -38,7 +38,7 @@ class BuiltinToolProviderController(ToolProviderController):
super().__init__(**{
'identity': provider_yaml['identity'],
'credentials_schema': provider_yaml['credentials_for_provider'] if 'credentials_for_provider' in provider_yaml else None,
'credentials_schema': provider_yaml.get('credentials_for_provider', None),
})
def _get_builtin_tools(self) -> list[Tool]: