aleatory_file_name.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import os
  2. import string
  3. import pathlib
  4. class aleatory_file_name:
  5. """
  6. That create new random file name, with given size and given extension.
  7. File name is returned as a string. Creating new instance of that class
  8. does not return an instance, but only string result.
  9. """
  10. def __new__(cls, extension: str = "", size: int = 32) -> str:
  11. """
  12. That create new random file name from system random number
  13. generator, extension and size. Resulted file name have given size,
  14. given extension but name is fully random. Name could contain small
  15. letters, big letters and digits.
  16. Parameters
  17. ----------
  18. extension : str (default: "")
  19. Extension of the file.
  20. size : int (default: 32)
  21. Lenght of the file name.
  22. Returns
  23. -------
  24. str
  25. Random name of the file.
  26. """
  27. name_lenght = size
  28. if len(extension) > 0:
  29. name_lenght -= 1 + len(extension)
  30. name = cls.__get_random_name(name_lenght)
  31. return cls.__get_file_name(name, extension)
  32. @classmethod
  33. def path(cls, *args, **kwargs) -> pathlib.Path:
  34. """
  35. That function work same as initializer, but return pathlib.Path,
  36. instread of string.
  37. Parameters
  38. ----------
  39. *args, **kwargs
  40. Same as __new__ parameters.
  41. Returns
  42. -------
  43. pathlib.Path
  44. Path instread of string.
  45. """
  46. return pathlib.Path(cls(*args, **kwargs))
  47. @classmethod
  48. def __get_chars(cls) -> str:
  49. """
  50. It return full aphabet with small and big letters. To letters it
  51. add also digits.
  52. Returns
  53. -------
  54. str
  55. Full alphabet with digits, big and small letters.
  56. """
  57. return str(string.ascii_letters + string.digits)
  58. @classmethod
  59. def __get_random_name(cls, size: int) -> str:
  60. """
  61. That generate random name (random string wich letters from alphabet
  62. generated in __get_chars) with size passed by intiger parameter.
  63. Parameters
  64. ----------
  65. size : int
  66. Lenght of random name.
  67. Returns
  68. -------
  69. str
  70. Random name with given lenght.
  71. """
  72. chars = cls.__get_chars()
  73. result = str()
  74. for count in range(size):
  75. random = os.urandom(1)[0]
  76. random = random % len(chars)
  77. result = result + chars[random]
  78. return result
  79. @classmethod
  80. def __get_file_name(cls, name: str, extension: str) -> str:
  81. """
  82. That create file name from name and extension. When extension is
  83. blank, then it return file name without extension. When extension
  84. is passed then return file name as "name.extension".
  85. Parameters
  86. ----------
  87. name : str
  88. Name of the file.
  89. extensions : str
  90. Extension of the file.
  91. Returns
  92. -------
  93. str
  94. Full file name from name and extension.
  95. """
  96. if len(extension) > 0:
  97. return name + "." + extension
  98. return name