index.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. """
  2. Calculate percentage level of index for regions
  3. inside the Verenigde Nederlanden.
  4. """
  5. REGIONS = {
  6. 'Northern Netherlands': None,
  7. 'Eastern Netherlands': None,
  8. 'Southern Netherlands': None,
  9. 'Western Netherlands': None,
  10. 'Amsterdam': None,
  11. 'Luxembourg': None,
  12. }
  13. PRINT_DENSE = True
  14. def region_in_string(string):
  15. """Look for region in string"""
  16. for region in REGIONS:
  17. if region in string:
  18. return True
  19. return False
  20. def calculate_index():
  21. """Calculate index bases on file"""
  22. with open('index.txt', 'r') as file:
  23. line_nr = 0
  24. last_percentage = 11
  25. for line in file:
  26. line_nr += 1
  27. if region_in_string(line):
  28. percentage = 11 - 10 / 998 * (line_nr - 3)
  29. diff_percentage = last_percentage - percentage
  30. last_percentage = percentage
  31. region = line.strip().split('\t')
  32. REGIONS[region[0].strip()] = percentage
  33. if not PRINT_DENSE:
  34. print('%40s %2s %6.2f %% %6.2f %%' % (
  35. region[0],
  36. region[1],
  37. percentage,
  38. diff_percentage
  39. ))
  40. if PRINT_DENSE:
  41. index = 1
  42. length = len(REGIONS)
  43. for region in REGIONS:
  44. if index == length:
  45. print('%.2f' % REGIONS[region])
  46. else:
  47. print('%.2f,' % REGIONS[region], end='')
  48. index += 1
  49. if __name__ == '__main__':
  50. calculate_index()