| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 | import osimport stringimport pathlibclass aleatory_file_name:    """    That create new random file name, with given size and given extension.    File name is returned as a string. Creating new instance of that class    does not return an instance, but only string result.    """    def __new__(cls, extension: str = "", size: int = 32) -> str:        """        That create new random file name from system random number         generator, extension and size. Resulted file name have given size,        given extension but name is fully random. Name could contain small        letters, big letters and digits.        Parameters        ----------        extension : str (default: "")            Extension of the file.                size : int (default: 32)            Lenght of the file name.        Returns        -------        str             Random name of the file.        """        name_lenght = size               if len(extension) > 0:            name_lenght -= 1 + len(extension)        name = cls.__get_random_name(name_lenght)        return cls.__get_file_name(name, extension)    @classmethod    def path(cls, *args, **kwargs) -> pathlib.Path:        """        That function work same as initializer, but return pathlib.Path,        instread of string.        Parameters        ----------        *args, **kwargs            Same as __new__ parameters.        Returns        -------        pathlib.Path            Path instread of string.        """        return pathlib.Path(cls(*args, **kwargs))    @classmethod    def __get_chars(cls) -> str:        """        It return full aphabet with small and big letters. To letters it         add also digits.        Returns        -------        str            Full alphabet with digits, big and small letters.        """        return str(string.ascii_letters + string.digits)    @classmethod    def __get_random_name(cls, size: int) -> str:        """        That generate random name (random string wich letters from alphabet        generated in __get_chars) with size passed by intiger parameter.        Parameters        ----------        size : int            Lenght of random name.        Returns        -------        str            Random name with given lenght.        """                chars = cls.__get_chars()        result = str()        for count in range(size):            random = os.urandom(1)[0]            random = random % len(chars)            result = result + chars[random]        return result    @classmethod    def __get_file_name(cls, name: str, extension: str) -> str:        """        That create file name from name and extension. When extension is        blank, then it return file name without extension. When extension         is passed then return file name as "name.extension".        Parameters        ----------        name : str            Name of the file.        extensions : str            Extension of the file.        Returns        -------        str            Full file name from name and extension.        """        if len(extension) > 0:            return name + "." + extension                return name
 |