python_version_checker.py
· 1.2 KiB · Python
Bruto
import sys
class PythonVersionChecker:
def __init__(self, supported_versions):
self.supported_versions = supported_versions
def get_current_version(self):
"""取得當前的 Python 版本。"""
return sys.version_info[:2]
def is_supported(self):
"""檢查當前的 Python 版本是否在支援的版本列表中。"""
current_version = self.get_current_version()
return current_version in self.supported_versions
class StreamlitVersionChecker(PythonVersionChecker):
def __init__(self):
# 定義 Streamlit 支援的 Python 版本
supported_versions = [(3, 9), (3, 10), (3, 11), (3, 12), (3, 13)]
super().__init__(supported_versions)
def check_version(self):
"""檢查並輸出當前的 Python 版本是否受 Streamlit 支援。"""
current_version = self.get_current_version()
if self.is_supported():
print(f"當前的 Python 版本 {current_version[0]}.{current_version[1]} 受 Streamlit 支援。")
else:
print(f"當前的 Python 版本 {current_version[0]}.{current_version[1]} 不受 Streamlit 支援。")
# 使用範例
checker = StreamlitVersionChecker()
checker.check_version()
| 1 | import sys |
| 2 | |
| 3 | class PythonVersionChecker: |
| 4 | def __init__(self, supported_versions): |
| 5 | self.supported_versions = supported_versions |
| 6 | |
| 7 | def get_current_version(self): |
| 8 | """取得當前的 Python 版本。""" |
| 9 | return sys.version_info[:2] |
| 10 | |
| 11 | def is_supported(self): |
| 12 | """檢查當前的 Python 版本是否在支援的版本列表中。""" |
| 13 | current_version = self.get_current_version() |
| 14 | return current_version in self.supported_versions |
| 15 | |
| 16 | class StreamlitVersionChecker(PythonVersionChecker): |
| 17 | def __init__(self): |
| 18 | # 定義 Streamlit 支援的 Python 版本 |
| 19 | supported_versions = [(3, 9), (3, 10), (3, 11), (3, 12), (3, 13)] |
| 20 | super().__init__(supported_versions) |
| 21 | |
| 22 | def check_version(self): |
| 23 | """檢查並輸出當前的 Python 版本是否受 Streamlit 支援。""" |
| 24 | current_version = self.get_current_version() |
| 25 | if self.is_supported(): |
| 26 | print(f"當前的 Python 版本 {current_version[0]}.{current_version[1]} 受 Streamlit 支援。") |
| 27 | else: |
| 28 | print(f"當前的 Python 版本 {current_version[0]}.{current_version[1]} 不受 Streamlit 支援。") |
| 29 | |
| 30 | # 使用範例 |
| 31 | checker = StreamlitVersionChecker() |
| 32 | checker.check_version() |
| 33 | |
| 34 |