feat: Added several demo programs

This commit is contained in:
xpk
2025-11-28 08:29:28 +08:00
parent 9d044c20c9
commit 3baa1996c2
4 changed files with 104 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
downloaded_files
+42
View File
@@ -0,0 +1,42 @@
#!/usr/bin/env python3
r"""
Documentation
License: This program is released under the MIT License
"""
# Imports
from enum import Enum
class Color(Enum):
RED = '#FF0000'
GREEN = '#00FF00'
BLUE = '#0000FF'
# function which uses the Enum
def paint_wall(color: Color) -> None:
match color:
case Color.RED:
print("Red wall, are you serious?")
case Color.GREEN:
print("Green wall, very foresty.")
case Color.BLUE:
print("Blue wall, I like it.")
case _:
print("Other colors are not preferred.")
# Main function
def main() -> None:
# Check Enum name and value
print(f"Enum: {Color.RED} Name: {Color.RED.name} Value: {Color.RED.value}")
# function that uses Enum
paint_wall(Color.RED)
# print all Enum values
print([x.value for x in Color])
# check if value is a member of the Enum, kind of pointless as IDE will report the problem ahead
# print(Color.BLACK in Color)
# Call main function
if __name__ == '__main__':
main()
+18
View File
@@ -0,0 +1,18 @@
#!/usr/bin/env python3
import numpy as np
celsius_temps = np.array([70, 75, 80])
fahrenheit_temps = (celsius_temps * 9/5) + 32
print(fahrenheit_temps) # [158. 167. 176.]
revenues = np.array([1000, 1500, 800, 2000, 1200])
costs = np.array([600, 900, 500, 1100, 700])
tax_rates = np.array([0.15, 0.18, 0.12, 0.20, 0.16])
gross_profits = revenues - costs
net_profits = gross_profits * (1 - tax_rates)
print(net_profits) # [340. 492. 264. 720. 420.]
+43
View File
@@ -0,0 +1,43 @@
#!/usr/bin/env python3
from seleniumbase import SB
from seleniumbase.common.exceptions import WebDriverException
import re
def main():
try:
with SB(uc=True, headless=True) as sb:
"""
UC mode is designed to make browser automation appear human and thus evade detection by anti-bot systems.
CDP mode uses the Chrome DevTools Protocol to allow more direct and lower-level control over the browser.
don't need this # sb.activate_cdp_mode(url)
"""
sb.open("https://www.openssh.org/goals.html")
# page source
print(sb.get_page_source())
print("-" * 80)
# enumerate all elements... causes TimeoutError
# all_elements = sb.find_elements("xpath", "/html/body/*")
# print(f"Total elements found: {len(all_elements)}")
# for elem in all_elements:
# print(elem.tag_name)
# print("-" * 80)
# specific html element and element id
print(sb.get_text("/html/body/h2[@id='OpenBSD']"))
print("-" * 80)
# similar to the above, but line breaks are differently presented
print(sb.get_element("xpath", "//h2[@id='OpenBSD']").get_attribute('innerText'))
print("-" * 80)
except WebDriverException as e:
print(f"Failed to get page: {e}")
except Exception as e:
print(e)
if __name__ == '__main__':
main()