You can download this code by clicking the button below.
This code is now available for download.
This code snippet includes a series of functions written using Python's built-in libraries, each serving a specific purpose such as generating random strings, checking for leap years, extracting email addresses, formatting the current time, calculating the geometric mean, reading file content, appending to a file, filtering even numbers, and getting system information.
Technology Stack : os, re, sys, time, json, math, random, string, datetime
Code Type : Code snippet
Code Difficulty : Intermediate
import os
import re
import sys
import time
import json
import math
import random
import string
import datetime
def generate_random_string(length=10):
letters = string.ascii_letters
return ''.join(random.choice(letters) for i in range(length))
def is_leap_year(year):
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
def extract_emails(text):
email_pattern = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
return re.findall(email_pattern, text)
def print_formatted_time():
now = datetime.datetime.now()
return now.strftime('%Y-%m-%d %H:%M:%S')
def calculate_geometric_mean(numbers):
if not numbers:
return None
product = math.prod(numbers)
return product ** (1.0 / len(numbers))
def read_file_content(file_path):
with open(file_path, 'r') as file:
return file.read()
def append_to_file(file_path, content):
with open(file_path, 'a') as file:
file.write(content + '\n')
def filter_even_numbers(numbers):
return [num for num in numbers if num % 2 == 0]
def get_system_info():
info = {
'os': os.name,
'platform': sys.platform,
'version': sys.version,
'max_size': sys.maxsize
}
return info