Random String Generator with System Info

  • Share this:

Code introduction


The function generates a random string, calculates the circumference of a circle given the radius, saves the data to a file, hashes the random string, gets the current time, and prints system information.


Technology Stack : random, math, string, os, sys, hashlib, json, datetime

Code Type : Function

Code Difficulty : Intermediate


                
                    
import random
import math
import string
import os
import sys
import hashlib
import json
import datetime

def generate_random_string(length=10):
    return ''.join(random.choices(string.ascii_letters + string.digits, k=length))

def calculate_circumference(radius):
    return 2 * math.pi * radius

def save_data_to_file(data, file_name):
    with open(file_name, 'w') as file:
        json.dump(data, file)

def hash_string(input_string):
    return hashlib.sha256(input_string.encode()).hexdigest()

def get_current_time():
    return datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')

def print_system_info():
    info = {
        'platform': sys.platform,
        'version': sys.version,
        'executable': sys.executable,
        'path': os.environ.get('PATH')
    }
    for key, value in info.items():
        print(f"{key}: {value}")

def xxx(arg1, arg2):
    random_string = generate_random_string(15)
    circumference = calculate_circumference(arg1)
    data_to_save = {
        'random_string': random_string,
        'circumference': circumference
    }
    save_data_to_file(data_to_save, 'output.json')
    hashed_string = hash_string(random_string)
    current_time = get_current_time()
    print_system_info()
    return hashed_string, current_time

# JSON output