FLamparski

Populations.py

May 15th, 2013
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.22 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import argparse
  4. from pickle import load, dump
  5.  
  6. class PopulationInfo:
  7.     def __init__ (self, size, avgAge, femRatio):
  8.         self.AVG_DEATH_AGE = 89.4
  9.         self.size = size
  10.         self.avgAge = avgAge
  11.         self.femRatio = femRatio
  12.        
  13. def create(args):
  14.     print("Creating a population of {}, average age of {} years, {}% of which is female.".format(args.size, args.avg_age, (args.fraction_female * 100)))
  15.     new_pop = PopulationInfo(args.size, args.avg_age, args.fraction_female)
  16.     print("Saving to {}...".format(args.filename))
  17.     with open(args.filename, "wb") as f:
  18.         dump(new_pop, f)
  19.     print("Done.")
  20.  
  21.  
  22. def analyse(args):
  23.     '''Prints out a paragraph in English that describes a population based on the
  24.    PopulationInfo provided by pickle from file located at args.filename.
  25.    
  26.    Result on screen will be in the form of:
  27.        This is a population of size NUMBER. The population is LIFESTAGE, and
  28.        there is a SEX_BALANCE.
  29.        
  30.    Yeah, it's like Mad Libs.
  31.    '''
  32.     print("Loading a population from file {}".format(args.filename))
  33.     pop = None
  34.     with open(args.filename, "rb") as f:
  35.         pop = load(f)
  36.    
  37.     description = "This is a population of size {}. The population is ".format(pop.size)
  38.    
  39.     if(pop.avgAge <= (pop.AVG_DEATH_AGE / 3)):
  40.         description += "young"
  41.     elif(pop.avgAge <= (pop.AVG_DEATH_AGE * (2/3))):
  42.         description += "middle-aged"
  43.     elif(pop.avgAge <= pop.AVG_DEATH_AGE):
  44.         description += "old"
  45.     else:
  46.         description += "extremely old"
  47.    
  48.     description += ", and there is a "
  49.    
  50.     if(pop.femRatio <= 0.25):
  51.         description += "significant dominance of males."
  52.     elif(pop.femRatio <= 0.45):
  53.         description += "dominance of males."
  54.     elif(pop.femRatio < 0.5):
  55.         description += "slight dominance of males."
  56.     elif(pop.femRatio == 0.5):
  57.         description += "balance of the sexes."
  58.     elif(pop.femRatio > 0.5 and pop.femRatio <= 0.55):
  59.         description += "slight dominance of females."
  60.     elif(pop.femRatio <= 0.65):
  61.         description += "dominance of females."
  62.     else:
  63.         description += "significant dominance of females."
  64.    
  65.     print(description)
  66.  
  67. if __name__ == "__main__":
  68.     cmparse = argparse.ArgumentParser(description="Generate natural-language statements from population information structures.")
  69.     actions = cmparse.add_subparsers()
  70.    
  71.     parser_create = actions.add_parser("create", help="Create a new PopulationInfo")
  72.     parser_create.add_argument("size", type=int, help="Size of the new population")
  73.     parser_create.add_argument("avg_age", type=float, help="Average age of the new population")
  74.     parser_create.add_argument("fraction_female", type=float, help="Fraction of the population that is female")
  75.     parser_create.add_argument("filename", help="Pickled PopulationInfo to use")
  76.     parser_create.set_defaults(func=create)
  77.    
  78.     parser_analyse = actions.add_parser("analyse", help="Analyse an existing PopulationInfo")
  79.     parser_analyse.add_argument("filename", help="Pickled PopulationInfo to use")
  80.     parser_analyse.set_defaults(func=analyse)
  81.    
  82.     args = cmparse.parse_args()
  83.     args.func(args)
Advertisement
Add Comment
Please, Sign In to add comment