Loading people/people-cli.py +38 −27 Original line number Diff line number Diff line from builtins import int import os import sys import argparse import json import string from people.Human import Human from people.ListOfHumans import ListOfHumans import argparse import sys def initialize_people_from_fake_values() -> ListOfHumans: def initialize_people_from_fake_values(args) -> ListOfHumans: """ initialize a list of humans in the people container :param people: ListOfHumans: existing object (empty or not) :param args: no used :return: people with added items """ people = ListOfHumans(Human("Joe", 10)) Loading @@ -20,27 +16,42 @@ def initialize_people_from_fake_values() -> ListOfHumans: return people if __name__ == "__main__": # Create the parser my_parser = argparse.ArgumentParser(description="Manage people in a list") my_parser.add_argument( "-i", metavar="file", required=False, type=str, help="the file containing people (name:age)", ) def initialize_people_from_file(args) -> ListOfHumans: """ initialize a list of humans in the people container :param people: ListOfHumans: existing object (empty or not) :return: people with added items """ people = ListOfHumans(input_file=args.input_file) return people # Execute parse_args() args = my_parser.parse_args() if args.i: def main(): parser = argparse.ArgumentParser() subparsers = parser.add_subparsers() add_subcommand_init_people(subparsers) add_subcommand_add_people_from_file(subparsers) my_people = ListOfHumans(input_file=args.i) else: # initialize people with fake values my_people = initialize_people_from_fake_values() if len(sys.argv) == 1: parser.print_help() sys.exit(1) args = parser.parse_args() my_people = args.func(args) print(my_people) print("average age = {age} ".format(age=my_people.get_average_age())) def add_subcommand_init_people(subparsers): parser = subparsers.add_parser('init', help='initialize people in list') parser.set_defaults(func=initialize_people_from_fake_values) def add_subcommand_add_people_from_file(subparsers): parser = subparsers.add_parser('file', help='add people in list from json file') parser.set_defaults(func=initialize_people_from_file) parser.add_argument('-i', '--input_file', type=str, default='data/people.son', help='input file name') if __name__ == '__main__': main() tests/__init__.py 0 → 100644 +0 −0 Empty file added. Loading
people/people-cli.py +38 −27 Original line number Diff line number Diff line from builtins import int import os import sys import argparse import json import string from people.Human import Human from people.ListOfHumans import ListOfHumans import argparse import sys def initialize_people_from_fake_values() -> ListOfHumans: def initialize_people_from_fake_values(args) -> ListOfHumans: """ initialize a list of humans in the people container :param people: ListOfHumans: existing object (empty or not) :param args: no used :return: people with added items """ people = ListOfHumans(Human("Joe", 10)) Loading @@ -20,27 +16,42 @@ def initialize_people_from_fake_values() -> ListOfHumans: return people if __name__ == "__main__": # Create the parser my_parser = argparse.ArgumentParser(description="Manage people in a list") my_parser.add_argument( "-i", metavar="file", required=False, type=str, help="the file containing people (name:age)", ) def initialize_people_from_file(args) -> ListOfHumans: """ initialize a list of humans in the people container :param people: ListOfHumans: existing object (empty or not) :return: people with added items """ people = ListOfHumans(input_file=args.input_file) return people # Execute parse_args() args = my_parser.parse_args() if args.i: def main(): parser = argparse.ArgumentParser() subparsers = parser.add_subparsers() add_subcommand_init_people(subparsers) add_subcommand_add_people_from_file(subparsers) my_people = ListOfHumans(input_file=args.i) else: # initialize people with fake values my_people = initialize_people_from_fake_values() if len(sys.argv) == 1: parser.print_help() sys.exit(1) args = parser.parse_args() my_people = args.func(args) print(my_people) print("average age = {age} ".format(age=my_people.get_average_age())) def add_subcommand_init_people(subparsers): parser = subparsers.add_parser('init', help='initialize people in list') parser.set_defaults(func=initialize_people_from_fake_values) def add_subcommand_add_people_from_file(subparsers): parser = subparsers.add_parser('file', help='add people in list from json file') parser.set_defaults(func=initialize_people_from_file) parser.add_argument('-i', '--input_file', type=str, default='data/people.son', help='input file name') if __name__ == '__main__': main()