Here’s an example of a Python class that generates a registration code with an expiration date based on a given name:
import datetime
import random
import string
class RegistrationCodeGenerator:
def __init__(self, name):
self.name = name
def generate_code(self):
# Generate a random code consisting of uppercase letters and digits
code = ''.join(random.choices(string.ascii_uppercase + string.digits, k=8))
# Calculate the expiration date as 30 days from the current date
expiration_date = datetime.datetime.now() + datetime.timedelta(days=30)
# Format the expiration date as a string
expiration_date_str = expiration_date.strftime("%Y-%m-%d %H:%M:%S")
# Combine the code and expiration date with the name to create the registration code
registration_code = f"{self.name}_{code}_{expiration_date_str}"
return registration_code
# Example usage
name = "John Doe"
generator = RegistrationCodeGenerator(name)
registration_code = generator.generate_code()
print(registration_code)
In this example, the RegistrationCodeGenerator
class takes a name as an input during initialization. The generate_code
method generates a random code consisting of uppercase letters and digits, calculates the expiration date as 30 days