progress.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. """Utils progress `macop` module when verbose enable
  2. """
  3. import sys
  4. class Colors:
  5. """Macop color representation
  6. """
  7. ENDC = '\033[m'
  8. GREEN = '\033[32m'
  9. GREY = '\033[90m'
  10. def macop_text(algo, msg):
  11. """Display Macop message to user interface
  12. Args:
  13. algo: {Algorithm} -- current algorithm instance
  14. msg: {str} -- message to display
  15. """
  16. if algo._verbose:
  17. print(Colors.GREEN + 'M' + Colors.ENDC + Colors.GREY + 'acop' \
  18. + Colors.ENDC + Colors.GREEN + ' :: ' + Colors.ENDC \
  19. + Colors.GREY + msg + Colors.ENDC)
  20. def macop_line(algo):
  21. """Macop split line
  22. Args:
  23. algo: {Algorithm} -- current algorithm instance
  24. """
  25. if not algo._verbose:
  26. return
  27. line = ''
  28. for i in range(41):
  29. if i % 2 == 0:
  30. line += Colors.GREEN + '----' + Colors.ENDC
  31. else:
  32. line += Colors.GREY + '----' + Colors.ENDC
  33. print(line)
  34. def macop_progress(algo, evaluations, max):
  35. """Progress line of macop
  36. Args:
  37. algo: {Algorithm} -- current algorithm instance
  38. evaluations: {int} -- current number of evaluations
  39. max: {int} -- max number of expected evaluations
  40. """
  41. if not algo._verbose:
  42. return
  43. barWidth = 156
  44. progress = evaluations / float(max)
  45. output_str = Colors.GREEN + '[' + Colors.ENDC
  46. pos = int(barWidth * progress)
  47. for i in range(barWidth):
  48. if i < pos:
  49. output_str = output_str + Colors.GREY + '=' + Colors.ENDC
  50. elif i == pos:
  51. output_str = output_str + Colors.GREEN + '>' + Colors.ENDC
  52. else:
  53. output_str = output_str + Colors.GREY + ' ' + Colors.ENDC
  54. output_str = output_str + Colors.GREEN + '] ' + Colors.ENDC + str(
  55. int(progress * 100.0)) + "%\r"
  56. print(output_str)
  57. sys.stdout.write("\033[F")
  58. # go to line
  59. if progress >= 1.:
  60. print()
  61. macop_line(algo)