Última atividade 10 months ago

這段 Python 程式碼利用 funcy 模組的裝飾器來 檢查目前作業系統,從而限制某些函式僅在特定平台上執行。具體來說,need_mac、need_linux、need_windows 與 need_unix 分別用來檢查是否在 macOS、Linux、Windows 或 Unix(macOS 或 Linux)系統上執行;函式 foo() 被 @need_unix 裝飾,僅允許在 Unix 系統上執行,而函式 bar() 則僅允許在 Windows 系統上執行。主程式透過 try-except 捕捉例外,若目前作業系統不符合要求,則輸出相應錯誤訊息。這樣的設計有助於確保平台專屬功能在正確的環境中執行,避免跨平台錯誤。

timmy revisou este gist 10 months ago. Ir para a revisão

Sem alterações

timmy revisou este gist 10 months ago. Ir para a revisão

1 file changed, 37 insertions, 17 deletions

import_platform_and_funcy_decorators_needmac_needlinux.py

@@ -1,31 +1,51 @@
1 - import platform # 匯入 platform 模組
2 -
3 - from funcy import decorator # 匯入 funcy 模組中的 decorator 裝飾器
1 + import platform
2 + from funcy import decorator
4 3
5 4 @decorator
6 - def needmac(call):
5 + def need_mac(call):
7 6 """檢查系統是否為 macOS,若不是則拋出異常。"""
8 7 if platform.system() != "Darwin":
9 - raise Exception(
10 - "The system is not macOS. " "This functionality only supported in macOS"
11 - )
12 -
8 + raise Exception("This functionality is only supported in macOS")
13 9 return call()
14 10
15 11 @decorator
16 - def needlinux(call):
12 + def need_linux(call):
17 13 """檢查系統是否為 Linux,若不是則拋出異常。"""
18 14 if platform.system() != "Linux":
19 - raise Exception(
20 - "The system is not Linux. " "This functionality only supported in Linux"
21 - )
15 + raise Exception("This functionality is only supported in Linux")
16 + return call()
22 17
18 + @decorator
19 + def need_windows(call):
20 + """檢查系統是否為 Windows,若不是則拋出異常。"""
21 + if platform.system() != "Windows":
22 + raise Exception("This functionality is only supported in Windows")
23 23 return call()
24 24
25 - @needmac # 裝飾器:需要 macOS 系統
26 - @needlinux # 裝飾器:需要 Linux 系統
25 + @decorator
26 + def need_unix(call):
27 + """檢查系統是否為 macOS 或 Linux,若不是則拋出異常。"""
28 + if platform.system() not in ["Darwin", "Linux"]:
29 + raise Exception("This functionality is only supported in macOS or Linux")
30 + return call()
31 +
32 + @need_unix
27 33 def foo():
28 - """一個測試函式,若系統為 macOS 或 Linux,則印出 'Hello, world!'。"""
29 - print("Hello, world!")
34 + """測試函式,僅在 macOS 或 Linux 上執行。"""
35 + print("Hello, world! (Running on macOS or Linux)")
36 +
37 + @need_windows
38 + def bar():
39 + """測試函式,僅在 Windows 上執行。"""
40 + print("Hello, world! (Running on Windows)")
41 +
42 + if __name__ == "__main__":
43 + try:
44 + foo() # 只有 macOS 和 Linux 可以執行
45 + except Exception as e:
46 + print(f"Error in foo(): {e}")
30 47
31 - foo() # 呼叫函式
48 + try:
49 + bar() # 只有 Windows 可以執行
50 + except Exception as e:
51 + print(f"Error in bar(): {e}")

timmy revisou este gist 10 months ago. Ir para a revisão

Sem alterações

timmy revisou este gist 10 months ago. Ir para a revisão

Sem alterações

timmy revisou este gist 10 months ago. Ir para a revisão

Sem alterações

timmy revisou este gist 2 years ago. Ir para a revisão

1 file changed, 31 insertions

import_platform_and_funcy_decorators_needmac_needlinux.py(arquivo criado)

@@ -0,0 +1,31 @@
1 + import platform # 匯入 platform 模組
2 +
3 + from funcy import decorator # 匯入 funcy 模組中的 decorator 裝飾器
4 +
5 + @decorator
6 + def needmac(call):
7 + """檢查系統是否為 macOS,若不是則拋出異常。"""
8 + if platform.system() != "Darwin":
9 + raise Exception(
10 + "The system is not macOS. " "This functionality only supported in macOS"
11 + )
12 +
13 + return call()
14 +
15 + @decorator
16 + def needlinux(call):
17 + """檢查系統是否為 Linux,若不是則拋出異常。"""
18 + if platform.system() != "Linux":
19 + raise Exception(
20 + "The system is not Linux. " "This functionality only supported in Linux"
21 + )
22 +
23 + return call()
24 +
25 + @needmac # 裝飾器:需要 macOS 系統
26 + @needlinux # 裝飾器:需要 Linux 系統
27 + def foo():
28 + """一個測試函式,若系統為 macOS 或 Linux,則印出 'Hello, world!'。"""
29 + print("Hello, world!")
30 +
31 + foo() # 呼叫函式
Próximo Anterior