Commit 737395f1 authored by Dom31's avatar Dom31
Browse files

tests cli

parent 75dbfde8
Loading
Loading
Loading
Loading
Loading

data/people.json

0 → 100644
+12 −0
Original line number Diff line number Diff line
{
  "people": [
    {
      "name": "Joe",
      "age": "40"
    },
    {
      "name": "John",
      "age": "50"
    }
  ]
}
 No newline at end of file
+9 −4
Original line number Diff line number Diff line
import string

class Human:
    """
Module Human
    class defining an human: name, age,...
    """
class Human:
    def __init__(self, name: string, age: int):
        """
        initialize a human
        :param name: string: human name
        :param age: int: human age
        """
        self.age = age
        self.name = name

    def change_age(self, age: int):
        """
        change the age
        :param age:
        :return:
        :param age: new (int) age
        :return: None
        """
        self.age = age
+36 −16
Original line number Diff line number Diff line
from src.people.Human import Human
import sys
import os
import string
import json
from people.Human import Human


class ListOfHumans:
@@ -6,14 +10,20 @@ class ListOfHumans:
    manage a list of humans
    """

    def __init__(self, human: Human = None):

    def __init__(self, human: Human = None, input_file: string = None):
        """
        initialize the list of humans, possibly with a human given as parameter
        :param human: Human: human object
        """
        self.separator = ","
        self.humans = []
        if human:
            assert isinstance(human, Human)
            self.humans.append(human)

        if input_file:
            self.initialize_people_from_input_file (input_file)

    def __str__(self):
        return self.get_names_and_ages()

@@ -32,12 +42,34 @@ class ListOfHumans:
    def add_human(self, human: Human):
        """
        add a human in the list
        :param human:
        :param human: new human to be added in the list
        :return: None
        """
        assert isinstance(human, Human)
        self.humans.append(human)

    def initialize_people_from_input_file(self, input_file: string):
        """
        read a Json file and fill the people container
        :param input_file: string: path/name of a json file
        :param people: ListOfHumans: existing object (empty or not)
        :return: people with added items
        """

        if os.path.isfile(input_file):
            with open(input_file) as json_file:
                data = json.load(json_file)
                for p in data["people"]:
                    self.add_human(Human(p["name"], int(p["age"])))
        else:
            print(
                "The file {filename} specified does not exist".format(
                    filename=input_file
                )
            )
            sys.exit()


    def get_average_age(self) -> float:
        """
        return the average of the age of all the humans
@@ -49,15 +81,3 @@ class ListOfHumans:
            total_age += human.age

        return total_age / len(self.humans)

    def __calculate_mean(a: int, b: int) -> float:
        """
        calculate a+b
        :return:
        :param a:
        :param b:
        :return:
        """
        assert isinstance(a, int)
        assert isinstance(b, int)
        return (a + b) / 2

people/main.py

deleted100644 → 0
+0 −69
Original line number Diff line number Diff line
from builtins import int
import os
import sys
import argparse
import json
import tkinter as tk

from src.people.Human import Human
from src.people.ListOfHumans import ListOfHumans
from src.people.GUI import menu


def init_people() -> ListOfHumans:
    people = ListOfHumans(Human("Joe", 10))
    # print(people)
    people.add_human(Human("Toto", 47))
    # print(people)
    return people


if __name__ == "__main__":

    # init people container
    my_people = ListOfHumans()
    # 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",
    )

    my_parser.add_argument(
        "-g", "--use_gui", action="store_true", help="use GUI instead of command line"
    )

    # Execute parse_args()
    args = my_parser.parse_args()

    if args.use_gui:
        root = tk.Tk()
        app = menu.MyApplication(root)
        app.run()
    else:
        # init people container
        my_people = ListOfHumans()
        if args.i:
            input_file = args.i
            if os.path.isfile(input_file):

                with open(input_file) as json_file:
                    data = json.load(json_file)
                    for p in data["people"]:
                        my_people.add_human(Human(p["name"], int(p["age"])))
            else:
                print(
                    "The file {filename} specified does not exist".format(
                        filename=input_file
                    )
                )
                sys.exit()
        else:
            # initialize people with fake values
            my_people = init_people()

        print(my_people)
        print("average age = {age} ".format(age=my_people.get_average_age()))

people/people-cli.py

0 → 100644
+46 −0
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


def initialize_people_from_fake_values() -> 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(Human("Joe", 10))
    people.add_human(Human("Toto", 47))
    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)",
    )

    # Execute parse_args()
    args = my_parser.parse_args()

    if args.i:

        my_people = ListOfHumans(input_file=args.i)
    else:
        # initialize people with fake values
        my_people = initialize_people_from_fake_values()

    print(my_people)
    print("average age = {age} ".format(age=my_people.get_average_age()))
Loading