Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import argparse
- from pickle import load, dump
- class PopulationInfo:
- def __init__ (self, size, avgAge, femRatio):
- self.AVG_DEATH_AGE = 89.4
- self.size = size
- self.avgAge = avgAge
- self.femRatio = femRatio
- def create(args):
- print("Creating a population of {}, average age of {} years, {}% of which is female.".format(args.size, args.avg_age, (args.fraction_female * 100)))
- new_pop = PopulationInfo(args.size, args.avg_age, args.fraction_female)
- print("Saving to {}...".format(args.filename))
- with open(args.filename, "wb") as f:
- dump(new_pop, f)
- print("Done.")
- def analyse(args):
- '''Prints out a paragraph in English that describes a population based on the
- PopulationInfo provided by pickle from file located at args.filename.
- Result on screen will be in the form of:
- This is a population of size NUMBER. The population is LIFESTAGE, and
- there is a SEX_BALANCE.
- Yeah, it's like Mad Libs.
- '''
- print("Loading a population from file {}".format(args.filename))
- pop = None
- with open(args.filename, "rb") as f:
- pop = load(f)
- description = "This is a population of size {}. The population is ".format(pop.size)
- if(pop.avgAge <= (pop.AVG_DEATH_AGE / 3)):
- description += "young"
- elif(pop.avgAge <= (pop.AVG_DEATH_AGE * (2/3))):
- description += "middle-aged"
- elif(pop.avgAge <= pop.AVG_DEATH_AGE):
- description += "old"
- else:
- description += "extremely old"
- description += ", and there is a "
- if(pop.femRatio <= 0.25):
- description += "significant dominance of males."
- elif(pop.femRatio <= 0.45):
- description += "dominance of males."
- elif(pop.femRatio < 0.5):
- description += "slight dominance of males."
- elif(pop.femRatio == 0.5):
- description += "balance of the sexes."
- elif(pop.femRatio > 0.5 and pop.femRatio <= 0.55):
- description += "slight dominance of females."
- elif(pop.femRatio <= 0.65):
- description += "dominance of females."
- else:
- description += "significant dominance of females."
- print(description)
- if __name__ == "__main__":
- cmparse = argparse.ArgumentParser(description="Generate natural-language statements from population information structures.")
- actions = cmparse.add_subparsers()
- parser_create = actions.add_parser("create", help="Create a new PopulationInfo")
- parser_create.add_argument("size", type=int, help="Size of the new population")
- parser_create.add_argument("avg_age", type=float, help="Average age of the new population")
- parser_create.add_argument("fraction_female", type=float, help="Fraction of the population that is female")
- parser_create.add_argument("filename", help="Pickled PopulationInfo to use")
- parser_create.set_defaults(func=create)
- parser_analyse = actions.add_parser("analyse", help="Analyse an existing PopulationInfo")
- parser_analyse.add_argument("filename", help="Pickled PopulationInfo to use")
- parser_analyse.set_defaults(func=analyse)
- args = cmparse.parse_args()
- args.func(args)
Advertisement
Add Comment
Please, Sign In to add comment