index.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. }
  12. PRINT_DENSE = True
  13. def region_in_string(string):
  14. """Look for region in string"""
  15. for region in REGIONS:
  16. if region in string:
  17. return True
  18. return False
  19. def calculate_index():
  20. """Calculate index bases on file"""
  21. with open('index.txt', 'r') as file:
  22. line_nr = 0
  23. last_percentage = 11
  24. for line in file:
  25. line_nr += 1
  26. if region_in_string(line):
  27. percentage = 11 - 10 / 998 * line_nr
  28. diff_percentage = last_percentage - percentage
  29. last_percentage = percentage
  30. region = line.strip().split('\t')
  31. REGIONS[region[0].strip()] = percentage
  32. if not PRINT_DENSE:
  33. print('%40s %2s %6.2f %% %6.2f %%' % (
  34. region[0],
  35. region[1],
  36. percentage,
  37. diff_percentage
  38. ))
  39. if PRINT_DENSE:
  40. index = 1
  41. length = len(REGIONS)
  42. for region in REGIONS:
  43. if index == length:
  44. print('%.2f' % REGIONS[region])
  45. else:
  46. print('%.2f,' % REGIONS[region], end='')
  47. index += 1
  48. if __name__ == '__main__':
  49. calculate_index()