RandomValues.py 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. from baangt.base.Faker import Faker
  2. import random
  3. import datetime
  4. import logging
  5. logger = logging.getLogger("pyC")
  6. class RandomValues:
  7. def __init__(self):
  8. self.fake = Faker().faker
  9. def retrieveRandomValue(self, RandomizationType="string", mini=None, maxi=None, format=None):
  10. '''
  11. updates attribute then calls another function to generate random value and return it to the caller.
  12. :param RandomizationType:
  13. :param mini:
  14. :param maxi:
  15. :param format:
  16. :return:
  17. '''
  18. self.RandomizationType = RandomizationType
  19. self.min = mini
  20. self.max = maxi
  21. self.format = format
  22. return self.generateValue()
  23. def generateValue(self):
  24. '''
  25. Generates random value as per input type and other parameters
  26. :return:
  27. '''
  28. if self.RandomizationType.lower() == "string":
  29. self.min = self.min or 3 # If value is none than change it to 3. We can do this in parameter but as we have
  30. self.max = self.max or 10 # multiple types and we need different default value for each of them, this is used.
  31. return self.fake.pystr(self.min, self.max) # used faker module of baangt to generate string name
  32. elif self.RandomizationType.lower() == "name":
  33. return self.fake.name() # used faker module of baangt to generate fake name
  34. elif self.RandomizationType.lower() == "int":
  35. self.min = self.min or 0
  36. self.max = self.max or 1000000
  37. return random.randint(self.min, self.max)
  38. elif self.RandomizationType.lower() == "float":
  39. self.min = self.min or 0
  40. self.max = self.max or 1000
  41. flt = random.uniform(self.min, self.max)
  42. return flt
  43. elif self.RandomizationType.lower() == "date":
  44. self.format = self.format or "%d/%m/%Y"
  45. if self.min:
  46. try:
  47. self.min = datetime.datetime.strptime(self.min, self.format).timestamp()
  48. except Exception as ex:
  49. logger.info(f"Minimum date's structure or format is incorrect - {str(ex)}")
  50. self.min = 86400
  51. else:
  52. self.min = 86400
  53. if self.max:
  54. try:
  55. self.max = datetime.datetime.strptime(self.max, self.format).timestamp()
  56. except Exception as ex:
  57. logger.info(f"Maximum date's structure or format is incorrect - {str(ex)}")
  58. self.max = datetime.datetime.now().timestamp()
  59. else:
  60. self.max = datetime.datetime.now().timestamp()
  61. return datetime.datetime.fromtimestamp(random.randint(self.min, self.max)).strftime(self.format)
  62. elif self.RandomizationType.lower() == "time":
  63. self.format = self.format or "%H:%M:%S"
  64. base = datetime.datetime(2000, 1, 1)
  65. if self.min:
  66. try:
  67. time = datetime.datetime.strptime(self.min, self.format)
  68. mini = base.replace(hour=time.hour, minute=time.minute, second=time.second).timestamp()
  69. except Exception as ex:
  70. logger.info(f"Minimum time's structure or format is incorrect - {str(ex)}")
  71. mini = base.replace(hour=0, minute=0, second=0).timestamp()
  72. else:
  73. mini = base.replace(hour=0, minute=0, second=0).timestamp()
  74. if self.max:
  75. try:
  76. time = datetime.datetime.strptime(self.max, self.format)
  77. maxi = base.replace(hour=time.hour, minute=time.minute, second=time.second).timestamp()
  78. except Exception as ex:
  79. logger.info(f"Maximum time's structure or format is incorrect - {str(ex)}")
  80. maxi = base.replace(hour=11, minute=59, second=59).timestamp()
  81. else:
  82. maxi = base.replace(hour=11, minute=59, second=59).timestamp()
  83. return datetime.datetime.fromtimestamp(random.uniform(mini, maxi)).strftime(self.format)
  84. else: # if type is not valid this statement will be executed
  85. raise BaseException(
  86. f"Incorrect type {self.RandomizationType}. Please use string, name, int, float, date, time.")