Skip to content

Commit 58c6c3f

Browse files
committed
refactor: Clean up and organize imports in Google ADK instrumentation files
- Reordered and grouped import statements for better readability in main.py and tools.py. - Updated import statements to use consistent quotation styles. - Removed unnecessary blank lines and improved formatting for clarity. - Enhanced the organization of code in internal modules to align with best practices. This refactoring improves code maintainability and readability across the Google ADK instrumentation files. Change-Id: I3e9723b4870ea0f062a044ea7d2c6c1e7bee39da Co-developed-by: Cursor <noreply@cursor.com>
1 parent b76aab9 commit 58c6c3f

File tree

16 files changed

+1526
-1096
lines changed

16 files changed

+1526
-1096
lines changed

instrumentation-loongsuite/loongsuite-instrumentation-google-adk/examples/main.py

Lines changed: 98 additions & 78 deletions
Large diffs are not rendered by default.

instrumentation-loongsuite/loongsuite-instrumentation-google-adk/examples/tools.py

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,27 @@
66

77
import math
88
import random
9-
import json
109
from datetime import datetime
11-
from typing import List, Dict, Any
10+
from typing import Any, Dict, List
11+
1212

1313
def get_current_time() -> str:
1414
"""
1515
获取当前时间
16-
16+
1717
Returns:
1818
当前时间的字符串表示
1919
"""
2020
return f"当前时间是: {datetime.now().strftime('%Y年%m月%d日 %H:%M:%S')}"
2121

22+
2223
def calculate_math(expression: str) -> str:
2324
"""
2425
数学计算工具函数
25-
26+
2627
Args:
2728
expression: 数学表达式字符串
28-
29+
2930
Returns:
3031
计算结果的字符串
3132
"""
@@ -34,37 +35,42 @@ def calculate_math(expression: str) -> str:
3435
allowed_names = {
3536
k: v for k, v in math.__dict__.items() if not k.startswith("__")
3637
}
37-
allowed_names.update({"abs": abs, "round": round, "pow": pow, "min": min, "max": max})
38-
38+
allowed_names.update(
39+
{"abs": abs, "round": round, "pow": pow, "min": min, "max": max}
40+
)
41+
3942
result = eval(expression, {"__builtins__": {}}, allowed_names)
4043
return f"🔢 计算结果:{expression} = {result}"
4144
except Exception as e:
4245
return f"❌ 计算错误:{str(e)}"
4346

47+
4448
def roll_dice(sides: int = 6) -> int:
4549
"""
4650
掷骰子工具函数
47-
51+
4852
Args:
4953
sides: 骰子面数,默认为6
50-
54+
5155
Returns:
5256
掷骰子的结果
5357
"""
5458
if sides < 2:
5559
sides = 6
5660
return random.randint(1, sides)
5761

62+
5863
def check_prime_numbers(numbers: List[int]) -> Dict[str, Any]:
5964
"""
6065
检查数字是否为质数
61-
66+
6267
Args:
6368
numbers: 要检查的数字列表
64-
69+
6570
Returns:
6671
包含检查结果的字典
6772
"""
73+
6874
def is_prime(n):
6975
if n < 2:
7076
return False
@@ -76,32 +82,33 @@ def is_prime(n):
7682
if n % i == 0:
7783
return False
7884
return True
79-
85+
8086
results = {}
8187
primes = []
8288
non_primes = []
83-
89+
8490
for num in numbers:
8591
if is_prime(num):
8692
primes.append(num)
8793
else:
8894
non_primes.append(num)
8995
results[str(num)] = is_prime(num)
90-
96+
9197
return {
9298
"results": results,
9399
"primes": primes,
94100
"non_primes": non_primes,
95-
"summary": f"在 {numbers} 中,质数有: {primes},非质数有: {non_primes}"
101+
"summary": f"在 {numbers} 中,质数有: {primes},非质数有: {non_primes}",
96102
}
97103

104+
98105
def get_weather_info(city: str) -> str:
99106
"""
100107
获取天气信息工具函数(模拟)
101-
108+
102109
Args:
103110
city: 城市名称
104-
111+
105112
Returns:
106113
天气信息字符串
107114
"""
@@ -111,19 +118,20 @@ def get_weather_info(city: str) -> str:
111118
"上海": "多云,温度 18°C,湿度 60%,东南风",
112119
"深圳": "小雨,温度 25°C,湿度 80%,南风",
113120
"杭州": "阴天,温度 20°C,湿度 55%,西北风",
114-
"广州": "晴朗,温度 28°C,湿度 65%,东风"
121+
"广州": "晴朗,温度 28°C,湿度 65%,东风",
115122
}
116-
123+
117124
weather = weather_data.get(city, f"{city}的天气信息暂时无法获取")
118125
return f"📍 {city}的天气:{weather}"
119126

127+
120128
def search_web(query: str) -> str:
121129
"""
122130
网络搜索工具函数(模拟)
123-
131+
124132
Args:
125133
query: 搜索查询
126-
134+
127135
Returns:
128136
搜索结果字符串
129137
"""
@@ -132,23 +140,24 @@ def search_web(query: str) -> str:
132140
"人工智能": "人工智能是计算机科学的一个分支,它企图了解智能的实质,并生产出一种新的能以人类智能相似的方式做出反应的智能机器。",
133141
"机器学习": "机器学习是人工智能的一个分支,是一门多领域交叉学科,涉及概率论、统计学、逼近论、凸分析、算法复杂度理论等多门学科。",
134142
"深度学习": "深度学习是机器学习的一个分支,它基于人工神经网络,利用多层非线性变换对数据进行特征提取和转换。",
135-
"自然语言处理": "自然语言处理是计算机科学领域与人工智能领域中的一个重要方向,它研究能实现人与计算机之间用自然语言进行有效通信的各种理论和方法。"
143+
"自然语言处理": "自然语言处理是计算机科学领域与人工智能领域中的一个重要方向,它研究能实现人与计算机之间用自然语言进行有效通信的各种理论和方法。",
136144
}
137-
145+
138146
for key, value in mock_results.items():
139147
if key in query:
140148
return value
141-
149+
142150
return f"🔍 关于'{query}'的搜索结果:这是模拟的搜索结果,实际应用中会连接真实的搜索引擎API。"
143151

152+
144153
def translate_text(text: str, target_language: str = "en") -> str:
145154
"""
146155
文本翻译工具函数(模拟)
147-
156+
148157
Args:
149158
text: 要翻译的文本
150159
target_language: 目标语言代码
151-
160+
152161
Returns:
153162
翻译结果字符串
154163
"""
@@ -158,14 +167,15 @@ def translate_text(text: str, target_language: str = "en") -> str:
158167
"谢谢": "Thank you",
159168
"再见": "Goodbye",
160169
"人工智能": "Artificial Intelligence",
161-
"机器学习": "Machine Learning"
170+
"机器学习": "Machine Learning",
162171
}
163-
172+
164173
if target_language.lower() == "en":
165174
return translations.get(text, f"Translated: {text}")
166175
else:
167176
return f"翻译到{target_language}{text}"
168177

178+
169179
# 导出所有工具函数
170180
__all__ = [
171181
"get_current_time",
@@ -174,5 +184,5 @@ def translate_text(text: str, target_language: str = "en") -> str:
174184
"check_prime_numbers",
175185
"get_weather_info",
176186
"search_web",
177-
"translate_text"
187+
"translate_text",
178188
]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
"""OpenTelemetry namespace package."""
2-
__path__ = __import__('pkgutil').extend_path(__path__, __name__)
32

3+
__path__ = __import__("pkgutil").extend_path(__path__, __name__)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
"""OpenTelemetry instrumentation namespace package."""
2-
__path__ = __import__('pkgutil').extend_path(__path__, __name__)
32

3+
__path__ = __import__("pkgutil").extend_path(__path__, __name__)

0 commit comments

Comments
 (0)