Advertisement
Ahmed_Negm

Untitled

Sep 26th, 2023
711
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.37 KB | None | 0 0
  1. '''
  2. Import statements:
  3.    1. Import the built-in json python package
  4.    2. From employee.py, import the details function and the employee_name, age, title variables
  5. '''
  6.  
  7. ### WRITE IMPORT STATEMENTS HERE
  8. import json
  9. from employee import details, employee_name, age, title
  10.  
  11. def create_dict(name, age, title):
  12.     """ Creates a dictionary that stores an employee's information
  13.  
  14.    [IMPLEMENT ME]
  15.        1. Return a dictionary that maps "first_name" to name, "age" to age, and "title" to title
  16.  
  17.    Args:
  18.        name: Name of employee
  19.        age: Age of employee
  20.        title: Title of employee
  21.  
  22.    Returns:
  23.        dict - A dictionary that maps "first_name", "age", and "title" to the
  24.               name, age, and title arguments, respectively. Make sure that
  25.               the values are typecasted correctly (name - string, age - int,
  26.               title - string)
  27.    """
  28.     ### WRITE SOLUTION HERE
  29.    
  30.     d = {"first_name": name, "age": int(age), "title": title}
  31.     return d
  32.     raise NotImplementedError()
  33.  
  34. def write_json_to_file(json_obj, output_file):
  35.     """ Write json string to file
  36.  
  37.    [IMPLEMENT ME]
  38.        1. Open a new file defined by output_file
  39.        2. Write json_obj to the new file
  40.  
  41.    Args:
  42.        json_obj: json string containing employee information
  43.        output_file: the file the json is being written to
  44.    """
  45.     ### WRITE SOLUTION HERE
  46.     newfile = open(output_file, "w")
  47.     newfile.write(json_obj)
  48.     newfile.close()
  49.     return newfile
  50.     raise NotImplementedError()
  51.  
  52. def main():
  53.     # Print the contents of details() -- This should print the details of an employee
  54.     details()
  55.  
  56.     # Create employee dictionary
  57.     employee_dict = create_dict(employee_name, age, title)
  58.     print("employee_dict: " + str(employee_dict))
  59.  
  60.     '''
  61.    Use a function called dumps from the json module to convert employee_dict
  62.    into a json string and store it in a variable called json_object.
  63.    '''
  64.     ### WRITE YOUR CODE BY MODIFYING THE LINE BELOW
  65.     # In the line below replace the None keyword with your code.
  66.     # The format should look like: variable = json.dumps(dict)
  67.     json_object = json.dumps(employee_dict)
  68.     print("json_object: " + str(json_object))
  69.  
  70.     # Write out the json object to file
  71.     write_json_to_file(json_object, "employee.json")
  72.  
  73. if __name__ == "__main__":
  74.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement