#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ # @Version : Python 3.11.4 # @Software : Sublime Text 4 # @Author : StudentCWZ # @Email : StudentCWZ@outlook.com # @Date : 2023/11/7 15:47 # @File : redis.py # @Description : """ class RedisUri: """ A class to generate a Redis connection URI. """ def __init__(self, host='localhost', port=6379, db=0, username=None, password=None): """ Initializes a new instance of the RedisUri class. :param host: The hostname of the Redis server. Default is 'localhost'. :type host: str :param port: The port number to connect to the Redis server. Default is 6379. :type port: int :param db: The database number to connect to on the Redis server. Default is 0. :type db: int :param username: The username for authentication to the Redis server. Default is None. :type username: str :param password: The password for authentication to the Redis server. Default is None. :type password: str """ self.host = host self.port = port self.db = db self.username = username self.password = password def create(self) -> str: """ Creates and returns the Redis connection URI. :return: The Redis connection URI. :rtype: str """ if self.username and self.password: return f'redis://{self.username}:{self.password}@{self.host}:{self.port}/{self.db}' else: return f'redis://{self.host}:{self.port}/{self.db}' def __repr__(self): """ Returns a formal string representation of the RedisUri object. :return: The formal string representation of the RedisUri object. :rtype: str """ return (f'RedisUri(host={self.host}, port={self.port}, db={self.db}, username={self.username}, ' f'password={self.password})') def __str__(self): """ Returns a string representation of the RedisUri object. :return: The string representation of the RedisUri object. :rtype: str """ return self.create()