timmy bu gisti düzenledi 10 months ago. Düzenlemeye git
Değişiklik yok
timmy bu gisti düzenledi 10 months ago. Düzenlemeye git
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 bu gisti düzenledi 10 months ago. Düzenlemeye git
Değişiklik yok
timmy bu gisti düzenledi 10 months ago. Düzenlemeye git
Değişiklik yok
timmy bu gisti düzenledi 10 months ago. Düzenlemeye git
Değişiklik yok
timmy bu gisti düzenledi 2 years ago. Düzenlemeye git
1 file changed, 31 insertions
import_platform_and_funcy_decorators_needmac_needlinux.py(dosya oluşturuldu)
| @@ -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() # 呼叫函式 | |