You can download this code by clicking the button below.
This code is now available for download.
This code combination uses multiple Python built-in libraries to demonstrate Python's capabilities, including generating random strings, removing invalid characters from strings, saving data to a file, formatting time, calculating the area of a circle, running scripts, and getting system information.
Technology Stack : random, string, re, os, sys, math, time, datetime
Code Type : Code combination
Code Difficulty : Advanced
import random
import string
import re
import os
import sys
import math
import time
import datetime
def generate_random_string(length, chars=string.ascii_letters + string.digits):
return ''.join(random.choice(chars) for _ in range(length))
def remove_invalid_characters(text, valid_chars=string.ascii_letters + string.digits + " -_"):
return re.sub(f"[^{re.escape(valid_chars)}]", "", text)
def save_to_file(data, file_path):
with open(file_path, 'w') as file:
file.write(data)
def print_formatted_time(time_str):
print(time.strftime('%Y-%m-%d %H:%M:%S', time.strptime(time_str, '%Y-%m-%d %H:%M:%S')))
def calculate_circle_area(radius):
return math.pi * (radius ** 2)
def run_script(script_path):
if os.path.isfile(script_path):
with open(script_path, 'r') as script_file:
exec(script_file.read())
else:
print(f"Script file {script_path} not found.")
def get_system_info():
info = {
'platform': sys.platform,
'version': sys.version,
'path': os.environ['PATH'],
'uptime': datetime.timedelta(seconds=os.getloadavg()[0] // 1)
}
return info