department.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. """
  2. Calculate percentage level of index for regions
  3. inside the Verenigde Nederlanden.
  4. """
  5. import sys
  6. import math
  7. import re
  8. from datetime import timedelta, datetime
  9. import dateutil.parser
  10. # Config
  11. PLAYER = False
  12. DATE = True
  13. TOTAL = False
  14. def calculate_buildings(filename='department.txt'):
  15. """Count working in departments"""
  16. total = 0
  17. players = {}
  18. days = {}
  19. with open(filename, 'r') as file:
  20. for line in file:
  21. try:
  22. date_str = re.search(r'\s\d\d.*', line).group(0)
  23. date_str = re.sub(r'\s(\d\d|\d\d\d)\s', '', date_str)
  24. date = dateutil.parser.parse(date_str)
  25. if date.hour >= 20:
  26. date += timedelta(days=1)
  27. line = re.sub(r'\s\d\d.*', '', line)
  28. line = re.sub(r'\[.*\]', '', line)
  29. line = line.strip()
  30. count = re.search(r'\+\d+', line).group(0)
  31. count = count.replace('+', '')
  32. count = int(count)
  33. total += count
  34. if PLAYER:
  35. player = re.sub(r'\s\(.*', '', line)
  36. if player in players:
  37. players[player] += count
  38. else:
  39. players[player] = count
  40. if DATE or TOTAL:
  41. date_format = date.strftime("%Y-%m-%d")
  42. if date_format in days:
  43. days[date_format] += count
  44. else:
  45. days[date_format] = count
  46. except Exception as exception:
  47. print('%s %s' % (line, exception))
  48. last_date = datetime.now()
  49. for date in sorted(days, reverse=True):
  50. date = dateutil.parser.parse(date)
  51. difference = last_date - date
  52. if difference.days > 1:
  53. for i in range(1, difference.days):
  54. new_date = date + timedelta(days=i)
  55. new_date_formatted = new_date.strftime("%Y-%m-%d")
  56. days[new_date_formatted] = 0
  57. last_date = date
  58. if PLAYER:
  59. print_players(players)
  60. if DATE:
  61. print_days(days)
  62. if TOTAL:
  63. print_total(days)
  64. def print_players(players):
  65. """Print players and reward"""
  66. total_reward = 0
  67. print('punten,beloning,naam')
  68. for player in sorted(players, key=players.get, reverse=True):
  69. worked_times = math.floor(players[player] / 10)
  70. reward = worked_times * 2000000000
  71. if worked_times >= 6:
  72. reward += 10000000000
  73. total_reward += reward
  74. print('%3s,$ %5s,%s' % (players[player], bucks(reward), player))
  75. print(total_reward)
  76. def print_days(days):
  77. """Print date and points"""
  78. for date in sorted(days, reverse=True):
  79. print('%s,%3s' % (date, days[date]))
  80. def print_total(days):
  81. """Print total points in department"""
  82. total = 0
  83. for date in sorted(days):
  84. total += days[date]
  85. last_day = dateutil.parser.parse(date) - timedelta(weeks=2)
  86. last_day_format = last_day.strftime("%Y-%m-%d")
  87. if last_day_format in days:
  88. total -= days[last_day_format]
  89. print('%s,%3s' % (date, total))
  90. def bucks(money):
  91. """Format money"""
  92. str_format = '{:,}'.format(money)
  93. new_str = ''
  94. for i in range(len(str_format), 0, -4):
  95. if str_format[i-4:i] == ',000':
  96. new_str = 'k' + new_str
  97. else:
  98. new_str = str_format[:i] + new_str
  99. break
  100. new_str = new_str.replace('kkkk', 't')
  101. new_str = new_str.replace(',', '.')
  102. return new_str
  103. if __name__ == '__main__':
  104. if len(sys.argv) < 2:
  105. calculate_buildings()
  106. else:
  107. calculate_buildings(sys.argv[1])