import_platform_and_funcy_decorators_needmac_needlinux.py
· 916 B · Python
Исходник
import platform # 匯入 platform 模組
from funcy import decorator # 匯入 funcy 模組中的 decorator 裝飾器
@decorator
def needmac(call):
"""檢查系統是否為 macOS,若不是則拋出異常。"""
if platform.system() != "Darwin":
raise Exception(
"The system is not macOS. " "This functionality only supported in macOS"
)
return call()
@decorator
def needlinux(call):
"""檢查系統是否為 Linux,若不是則拋出異常。"""
if platform.system() != "Linux":
raise Exception(
"The system is not Linux. " "This functionality only supported in Linux"
)
return call()
@needmac # 裝飾器:需要 macOS 系統
@needlinux # 裝飾器:需要 Linux 系統
def foo():
"""一個測試函式,若系統為 macOS 或 Linux,則印出 'Hello, world!'。"""
print("Hello, world!")
foo() # 呼叫函式
| 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() # 呼叫函式 |
| 32 |