Advertisement
bsimper

Age Study

Jan 19th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.11 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. import datetime
  4. import time
  5. import string
  6.  
  7. # https://en.wikipedia.org/wiki/List_of_members_of_the_Quorum_of_the_Twelve_Apostles_(LDS_Church)
  8. # https://en.wikipedia.org/wiki/List_of_Justices_of_the_Supreme_Court_of_the_United_States
  9.  
  10. ##########################################################
  11. def man_age(a_man, a_date):
  12.     # date of birth
  13.     a_birth = datetime.datetime.strptime(a_man['birth'], '%Y-%m-%d')
  14.     a_age = a_date - a_birth
  15.     age_year, age_day = divmod(a_age.days, 365.25)
  16.     return int(age_year)
  17.    
  18.    
  19. ##########################################################
  20. def man_age_x(a_man, a_date):
  21.     # date of birth
  22.     a_birth = datetime.datetime.strptime(a_man['birth'], '%Y-%m-%d')
  23.     a_age = a_date - a_birth
  24.     age_year, age_day = divmod(a_age.days, 365.25)
  25.  
  26.     # round the age to two decimal places
  27.     final_age = age_year + (int(age_day*100/365.25)/100)
  28.    
  29.     return final_age
  30.    
  31.    
  32. ##########################################################
  33. def currently_serving_man(a_man, a_date):
  34.     # acquire the date they started as an apostle
  35.     a_start = datetime.datetime.strptime(a_man['start'], '%Y-%m-%d')
  36.    
  37.     # acquire the date they stopped being an apostle
  38.     if a_man['end'] == "-":
  39.         # apostle is still serving
  40.         a_end = datetime.datetime.now()        
  41.     else:
  42.         # apostle is deceased or no longer serving
  43.         a_end = datetime.datetime.strptime(a_man['end'], '%Y-%m-%d')
  44.    
  45.     # Is the current date on or between those dates
  46.     if (a_date >= a_start) and (a_date <= a_end):
  47.         return True
  48.     else:
  49.         return False
  50.  
  51. ##########################################################
  52. def load_person_list (personfile, person_list):
  53.     p = open(personfile, "r")
  54.     for line in p:
  55.         field = line.split(',')
  56.         man = {
  57.                "name"  : field[0],
  58.                "birth" : field[1],
  59.                "start" : field[2],
  60.                "end"   : field[3]
  61.         }    
  62.         '''
  63.        print('Name: ', field[0], end='')
  64.        print(', Birth: ', field[1], end='')
  65.        print(', Start: ', field[2], end='')
  66.        print(', End: ', field[3])
  67.        '''
  68.         person_list.append(man)
  69.     p.close
  70.    
  71. #########################################################################
  72. # main()
  73.  
  74. # File containing a list of people, birthdays, and service dates
  75. persontextfile="apostle1.txt"
  76. #persontextfile="supreme.txt"
  77.  
  78. # List that will contain a list of people, birthdays, and service dates
  79. person_array=[]
  80.  
  81. load_person_list (persontextfile, person_array)
  82.  
  83. for y in range(1835, 2017):
  84.     # build a date for the week after April and October biannual general conference
  85.     apr_gc = datetime.datetime.strptime(str(y) + '-04-15', '%Y-%m-%d')
  86.     oct_gc = datetime.datetime.strptime(str(y) + '-10-15', '%Y-%m-%d')
  87.  
  88.     apr_man_total = 0
  89.     apr_age_total = 0
  90.     oct_man_total = 0
  91.     oct_age_total = 0
  92.    
  93.     for man in person_array:
  94.  
  95.         if currently_serving_man(man, apr_gc):
  96.             apr_man_total += 1
  97.             apr_age_total += man_age(man, apr_gc)
  98.  
  99.         if currently_serving_man(man, oct_gc):
  100.             oct_man_total += 1
  101.             oct_age_total += man_age(man, oct_gc)
  102.    
  103.     if apr_man_total > 0:
  104.         apr_average_age = round(apr_age_total / apr_man_total, 1)
  105.         print('Apr %s : %s' % (str(y), str(apr_average_age)))
  106.        
  107.     if oct_man_total > 0:
  108.         oct_average_age = round(oct_age_total / oct_man_total, 1)
  109.         print('Oct %s : %s' % (str(y), str(oct_average_age)))
  110.  
  111. # Initialize counts of men
  112. total_man_start = 0
  113. total_age_start = 0
  114. total_man_end = 0
  115. total_age_end = 0
  116.  
  117. # Initial identity of the youngest man at start.
  118. youngest_start = ''
  119. youngest_start_age = 1000
  120.  
  121. # Initial identity of the youngest man at end.
  122. youngest_end = ''
  123. youngest_end_age = 1000
  124.  
  125. # Initial identity of the oldest man at start.
  126. oldest_start = ''
  127. oldest_start_age = 0
  128.  
  129. # Initial identity of the ldest man at end.
  130. oldest_end = ''
  131. oldest_end_age = 0
  132.  
  133. for man in person_array:
  134.  
  135.     date_at_start = datetime.datetime.strptime(man['start'], '%Y-%m-%d')
  136.  
  137.     # allow for checking only later dates for whatever reason
  138.     earliest_date = '2000-01-01'
  139.     earliest = datetime.datetime.strptime(earliest_date, '%Y-%m-%d')
  140.  
  141.     if date_at_start >= earliest:
  142.         print(man['name'])
  143.         age_at_start = man_age_x(man, date_at_start)
  144.  
  145.         if man['end'] == "-":
  146.             # Still alive
  147.             age_at_end = 0
  148.             #print('%s, -----, %s' % (str(age_at_start), man['name']))
  149.         else:
  150.             # Deceased
  151.             date_at_end = datetime.datetime.strptime(man['end'], '%Y-%m-%d')
  152.             age_at_end = man_age_x(man, date_at_end)
  153.             #print('%s, %s, %s' % (str(age_at_start), str(age_at_end), man['name']))
  154.        
  155.         # Count the number of men
  156.         total_man_start += 1
  157.  
  158.         # Start date of current man
  159.         start_date = datetime.datetime.strptime(man['start'], '%Y-%m-%d')
  160.         # Age of current man at start
  161.         start_age = man_age_x(man, start_date)
  162.         # Sum those ages
  163.         total_age_start += start_age
  164.  
  165.         # Check if the current man is older or younger
  166.         # than the oldest or youngest at start
  167.         if start_age < youngest_start_age:
  168.             youngest_start_age = start_age
  169.             youngest_start = man['name']
  170.  
  171.         if start_age > oldest_start_age:
  172.             oldest_start_age = start_age
  173.             oldest_start = man['name']
  174.  
  175.         if man['end'] != "-":
  176.             total_man_end += 1
  177.             end_date = datetime.datetime.strptime(man['end'], '%Y-%m-%d')
  178.             end_age = man_age_x(man, end_date)
  179.             total_age_end += end_age
  180.  
  181.             # Check if the current man is older or younger
  182.             # than the oldest or youngest at end
  183.             if end_age < youngest_end_age:
  184.                 youngest_end_age = end_age
  185.                 youngest_end = man['name']
  186.  
  187.             if end_age > oldest_end_age:
  188.                 oldest_end_age = end_age
  189.                 oldest_end = man['name']
  190.     # Done with comparisons, start calculating and reporting
  191.  
  192. print('Stats of people with start dates after', earliest_date)
  193.  
  194. if total_man_start > 0:
  195.     avg_start = int(100 * total_age_start / total_man_start)/100
  196. else:
  197.     avg_start = 'none'
  198.  
  199. if total_man_end > 0:
  200.     avg_end = int(100 * total_age_end / total_man_end)/100
  201. else:
  202.     avg_end = 'none'
  203.  
  204. print ('total people          : %s' % (str(total_man_start)))
  205. print ('average age at start  : %s' % (str(avg_start)))
  206. print ('average age at end    : %s' % (str(avg_end)))
  207.  
  208. print ('')
  209. print ('youngest at start     : %s' % youngest_start)
  210. print ('age                   : %s' % str(youngest_start_age))
  211. print ('')
  212. print ('youngest at end       : %s' % youngest_end)
  213. print ('age                   : %s' % str(youngest_end_age))
  214. print ('')
  215. print ('oldest at start       : %s' % oldest_start)
  216. print ('age                   : %s' % str(oldest_start_age))
  217. print ('')
  218. print ('oldest at end         : %s' % oldest_end)
  219. print ('age                   : %s' % str(oldest_end_age))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement