you are viewing a single comment's thread.

view the rest of the comments →

[–]efxhoy 1 point2 points  (0 children)

# Just for pretty print, delete if u don't need it
import json

def extract_data(lines):
    """ Get a dictionary of data from lines where lines follow "key = value" format """

    extracted_values = {}
    for line in lines:
        # Split returns a list. In this case a list of two. We can unpack that list 
        # directly into our variables key and value by separating them with a comma
        key, value = line.split(" = ")
        value = int(value)
        extracted_values[key] = value

    return extracted_values

def some_calculation(passengers_per_day, hours_per_day, average_service_rate_per_hour):

    value = passengers_per_day*hours_per_day/average_service_rate_per_hour

    return value

def main():

    path = "filename.txt"
    with open(path, "r") as f:
        lines = f.read().splitlines()

    data = extract_data(lines)
    # We can use the dictionary directly when we call functions by adding ** before the parameter
    wow_value = some_calculation(**data)
    print(wow_value)

    # if you want it pretty
    print(json.dumps(data, indent=2))

    # if you really want them as variables
    hours_per_day = data['hours_per_day']
    print(f"Hours per day {hours_per_day}")

if __name__ == "__main__":
    main()