Compare commits
39 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1bf654f44d | |||
| 600f297e9f | |||
| 87372e3d08 | |||
| 228334dc4f | |||
| 5fadc70eaf | |||
| 8fd12709f7 | |||
| 07e4689bfe | |||
| 24d34e3a4f | |||
| b824c2f055 | |||
| b75906f2ba | |||
| 68d2b6eb67 | |||
| 87358e636c | |||
| 2ec3637ee5 | |||
| 38b26ffe9f | |||
| 94c33bec87 | |||
| f9aedd930a | |||
| 50a2a71a03 | |||
| 955662b4ef | |||
| 723e2940c7 | |||
| 61d24e3547 | |||
| e5f6a2b113 | |||
| d322c3b377 | |||
| aa0bfac172 | |||
| 6d00d64714 | |||
| 33dd9bf6e8 | |||
| a7dbcbd931 | |||
| 87984722b9 | |||
| c62c937189 | |||
| 903b94b281 | |||
| fe990504bd | |||
| be86ac639a | |||
| ef3ba77469 | |||
| 37bc70ee93 | |||
| 03df0f50a4 | |||
| eca1711b28 | |||
| a0cbacf209 | |||
| 48121d64c7 | |||
| 45ab84dda2 | |||
| c404fd251c |
+46
@@ -0,0 +1,46 @@
|
|||||||
|
import json
|
||||||
|
import os
|
||||||
|
|
||||||
|
def analyze_log(filepath: str) -> dict:
|
||||||
|
if not os.path.exists(filepath):
|
||||||
|
return {}
|
||||||
|
|
||||||
|
total = 0
|
||||||
|
by_level = {}
|
||||||
|
by_user = {}
|
||||||
|
last_error = None
|
||||||
|
|
||||||
|
try:
|
||||||
|
with open(filepath, 'r', encoding="utf-8") as f:
|
||||||
|
first_line = f.readline()
|
||||||
|
if not first_line.strip():
|
||||||
|
return {}
|
||||||
|
|
||||||
|
f.seek(0)
|
||||||
|
for line in f:
|
||||||
|
line = line.strip()
|
||||||
|
if not line:
|
||||||
|
continue
|
||||||
|
try:
|
||||||
|
log_entry = json.loads(line)
|
||||||
|
level = log_entry["level"]
|
||||||
|
user = log_entry["user"]
|
||||||
|
message = log_entry["message"]
|
||||||
|
except (json.JSONDecodeError, TypeError, KeyError):
|
||||||
|
continue
|
||||||
|
|
||||||
|
total += 1
|
||||||
|
by_level[level] = by_level.get(level, 0) + 1
|
||||||
|
by_user[user] = by_user.get(user, 0) + 1
|
||||||
|
|
||||||
|
if level == "ERROR":
|
||||||
|
last_error = message
|
||||||
|
except OSError:
|
||||||
|
return {}
|
||||||
|
|
||||||
|
return {
|
||||||
|
"total": total,
|
||||||
|
"by_level": by_level,
|
||||||
|
"by_user": by_user,
|
||||||
|
"last_error": last_error
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
{"timestamp":"2026-10-01 10:23:45","level":"INFO","message":"用户登录成功","user":"老三"}
|
||||||
|
{"timestamp":"2026-10-02 10:24:01","level":"ERROR","message":"数据库连接失败","user":"老四"}
|
||||||
|
{"timestamp":"2026-10-03 10:25:12","level":"INFO","message":"用户登出","user":"老八"}
|
||||||
|
{"timestamp":"2026-10-04 10:26:30","level":"ERROR","message":"超时","user":"老六"}
|
||||||
|
{"timestamp":"2026-10-05 10:27:00","level":"INFO","message":"任务完成","user":"老王"}
|
||||||
+55
@@ -0,0 +1,55 @@
|
|||||||
|
import json
|
||||||
|
|
||||||
|
class UserManager:
|
||||||
|
def __init__(self):
|
||||||
|
self.users = []
|
||||||
|
self.next_id = 1
|
||||||
|
|
||||||
|
def add_user(self, name: str, age: int) -> dict:
|
||||||
|
user = {
|
||||||
|
"id": self.next_id,
|
||||||
|
"name": name,
|
||||||
|
"age": age
|
||||||
|
}
|
||||||
|
self.users.append(user)
|
||||||
|
self.next_id += 1
|
||||||
|
return user
|
||||||
|
|
||||||
|
def get_user(self, user_id: int):
|
||||||
|
for u in self.users:
|
||||||
|
if u["id"] == user_id:
|
||||||
|
return u
|
||||||
|
return None
|
||||||
|
|
||||||
|
def update_age(self, user_id: int, new_age: int) -> bool:
|
||||||
|
user = self.get_user(user_id)
|
||||||
|
if user is None:
|
||||||
|
return False
|
||||||
|
user["age"] = new_age
|
||||||
|
return True
|
||||||
|
|
||||||
|
def remove_user(self, user_id: int) -> bool:
|
||||||
|
for idx, u in enumerate(self.users):
|
||||||
|
if u["id"] == user_id:
|
||||||
|
self.users.pop(idx)
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
def list_users(self):
|
||||||
|
return self.users.copy()
|
||||||
|
|
||||||
|
def save_to_json(self, file_path: str):
|
||||||
|
with open(file_path, "w", encoding="utf-8") as f:
|
||||||
|
json.dump(self.users, f, ensure_ascii=False, indent=2)
|
||||||
|
|
||||||
|
def load_from_json(self, file_path: str):
|
||||||
|
with open(file_path, "r", encoding="utf-8") as f:
|
||||||
|
loaded_users = json.load(f)
|
||||||
|
self.users = loaded_users
|
||||||
|
if self.users:
|
||||||
|
max_id = max(u["id"] for u in self.users)
|
||||||
|
self.next_id = max_id + 1
|
||||||
|
else:
|
||||||
|
self.next_id = 1
|
||||||
|
|
||||||
|
|
||||||
+17
-3
@@ -60,7 +60,7 @@
|
|||||||
4. 依次执行以下代码,输出是什么?
|
4. 依次执行以下代码,输出是什么?
|
||||||
|
|
||||||
```python
|
```python
|
||||||
s = " hello "
|
s = " hello "
|
||||||
print(len(s))
|
print(len(s))
|
||||||
print(len(s.strip()))
|
print(len(s.strip()))
|
||||||
```
|
```
|
||||||
@@ -163,7 +163,7 @@
|
|||||||
|
|
||||||
### 答案
|
### 答案
|
||||||
|
|
||||||
(在此填写,格式:`1. A 2. B 3. C 4. D 5. A 6. B 7. C 8. D 9. A 10. B`)
|
(在此填写,格式:`1. B 2.B 3. B 4. B 5. A 6. B 7. B 8. B 9. B 10. B )
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -182,7 +182,8 @@ import copy
|
|||||||
c = copy.deepcopy(a)
|
c = copy.deepcopy(a)
|
||||||
```
|
```
|
||||||
|
|
||||||
(在此作答)
|
(在此作答)b是a的浅层copy,而c则是a的深层copy,c相对而言更独立(不受a的影响),所以最后b是[[1, 2, 99], [3, 4]]
|
||||||
|
而c没变
|
||||||
|
|
||||||
### 第 2 题:字典与列表的综合应用
|
### 第 2 题:字典与列表的综合应用
|
||||||
|
|
||||||
@@ -203,6 +204,9 @@ logs = [
|
|||||||
3. 解释为什么第 2 问不能直接用 `len(logs)` 得到结果,需要什么遍历结构?
|
3. 解释为什么第 2 问不能直接用 `len(logs)` 得到结果,需要什么遍历结构?
|
||||||
|
|
||||||
(在此作答)
|
(在此作答)
|
||||||
|
1.[log for log in logs if log["level"] == "ERROR"]
|
||||||
|
2.{user: sum(1 for log in logs if log["user"] == user) for user in set(log["user"] for log in logs)}
|
||||||
|
3.因为len(logs)是在计算元素数量(5),应该先for遍历提取user的值(先创建空字典),没则计
|
||||||
|
|
||||||
### 第 3 题:异常处理设计
|
### 第 3 题:异常处理设计
|
||||||
|
|
||||||
@@ -218,3 +222,13 @@ Day_10 中你写过 `safe_int(s)` 函数:能转就返回整数,不能转就
|
|||||||
请写出函数代码,并说明:为什么这里用 `try/except` 比先用 `if` 判断再计算更好?
|
请写出函数代码,并说明:为什么这里用 `try/except` 比先用 `if` 判断再计算更好?
|
||||||
|
|
||||||
(在此作答)
|
(在此作答)
|
||||||
|
def safe_divide(a, b):
|
||||||
|
try:
|
||||||
|
num_a = float(a)
|
||||||
|
num_b = float(b)
|
||||||
|
result = num_a / num_b
|
||||||
|
return result
|
||||||
|
except (ValueError, ZeroDivisionError):
|
||||||
|
return None
|
||||||
|
因为if干的活try/except也可以干,不用if可以让代码更简洁
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user