COURSE 7 – DEVELOPING AI APPLICATIONS WITH PYTHON AND FLASK

Module 1: Python Coding Practices and Packaging Concepts

IBM AI DEVELOPER PROFESSIONAL CERTIFICATE

Complete Coursera Study Guide

Last updated:

INTRODUCTION – Python Coding Practices and Packaging Concepts

In this module, you will begin by exploring the fundamental differences between web applications and APIs. Following this, you will delve into the application development lifecycle, covering everything from gathering requirements to maintaining the project. You will become acquainted with the best coding practices as outlined in the Python Enhancement Proposal (PEP8) style guide.

Additionally, you will learn about static code analysis, a method used to ensure your code adheres to established coding standards. The module also includes instructions on how to create and execute unit tests. Finally, you will gain knowledge on how to create, verify, and run Python packages.

Learning Objectives

  • Describe the different phases of the application development lifecycle
  • Differentiate between web apps and Application Programming Interface (API)
  • Recognize the importance of writing application codes in multiple files
  • Outline the Python coding conventions
  • Perform static code analysis
  • Create and run unit tests
  • Create, verify, and execute Python packages

MODULE 1 PRACTICE QUIZ: PYTHON CODING PRACTICES AND PACKAGING CONCEPTS

1. Identify the file where you add code to reference the modules in the package? 

  • myproject.py
  • __main__.py
  • __init__.py (CORRECT)
  • __name__.py

Correct! You will need to add code to reference the modules in the package in the __init__.py file.

2. Which of the following is a best practice when creating function names to write test cases?

  • test (CORRECT)
  • _test
  • function.test
  • Function

Correct! The unittest library identifies only the functions with the prefix ‘test’ as the functions for conducting unit tests.

3. Which of the following is a valid method name based on PEP8 guidelines?

  • GetUserName()
  • get_user_name() (CORRECT)
  • GET_USER_NAME()
  • getUserName()

Correct! According to the PEP8 guidelines, the method name should preferably be lowercase, with underscores separating the words.

4. Which of the following is required to package a Python module?

  • import sys as the first line of code in the Python script
  • A folder with server.py
  • The methods in the Python module should be defined as public methods
  • The Python module should be packaged in a directory with __init__.py in the same directory (CORRECT)

Correct! Package requires referencing to all its modules in __inti__.py. Without this, the package is not callable.

5. Which of the following is TRUE about web apps and APIs? (Select two)

  • All APIs allow users to access apps without installing them on their local systems.
  • Web apps is a generic term that includes “online” or web-based and “offline” apps.
  • Both web apps and APIs link two system parts. (CORRECT)
  • All web apps are APIs, but not all APIs are web apps. (CORRECT)

Correct! Both web apps and APIs create links between two system parts.

Correct! All web apps are APIs, but not all APIs are web apps.

MODULE 1 GRADED QUIZ: PYTHON CODING PRACTICES AND PACKAGING CONCEPTS

1. Which of the following statements are true about web apps? (Select two)

  • Web apps help link offline apps.
  • All web apps are APIs. (CORRECT)
  • Web apps is a more generic term given to all forms of apps that create a link between any two parts of a system.
  • Web apps support CRUD actions. (CORRECT)

Correct! All web apps are APIs.

Correct! Web apps support CRUD actions.

2. In which testing phase do you verify that the application functions within the larger framework?

  • Integration testing (CORRECT)
  • Performance testing
  • User testing 
  • Unit testing

Correct! Integration tests verify that all involved programs continue functioning as expected after integration. Integration testing also verifies that the application functions within a larger framework.

3. Which of the following statements is TRUE about PyLint?

  • PyLint is a Python static code analysis tool. (CORRECT)
  • PyLint is a Python code compiler.
  • PyLint is a Python IDE.
  • PyLint is a Python-based package creation tool.

Correct! PyLint is a Python static code analysis tool. You can use the PyLint library to check the compliance of your Python code with PEP8 guidelines.

4. Which of the following is the right way to name constants based on the PEP8 guidelines?

  • DATE_OF_BIRTH (CORRECT)
  • Date_Of_Birth
  • date_of_birth
  • dateOfBirth

Correct! The right way to name constants based on the PEP8 guidelines is DATE_OF_BIRTH.

5. When gathering requirements for an app to manage events, the customer mentions that the project’s objective is to improve customer retention.

Which of the following requirements describes the above scenario?

  • Technical requirements
  • Constraints
  • User requirements
  • Business requirements (CORRECT)

Correct! The statement describes a business requirement. A business requirement provides a high-level description of the business need for the app or project.

6. Why is it a good practice to build a unit testing class?

  • To add one or more assertion methods 
  • To import the unittest library
  • To check if functions are returning the correct values
  • To call unit tests from a single class object (CORRECT)

Correct! It is a good practice to build a unit testing class as it allows you to call the unit tests from a single class object

7. For this question, review the partial module code and the unit test code:

9VZsz1ae8hZhS94V4NMFkfKzrOZGi95UCzQh1lK6bDqY7kpDTjb Cg2WIp5WCWjGWqE5Fp786u6ku2ZlU2VQIZTeLohcTXCcaokD8vjYyXEiX2HVoZelYaeQ7NpjNWZdjMEFDPSaqfl13A QLjUP2w

When the unit test is executed, the test fails. Which of the following code statement will you modify for the unit test to pass?

  • self. assertEqual (subract (6, 4), 3) (CORRECT)
  • self. assertEqual (add (6, 4), 10)
  • def test_subract(self)
  • from math import add

Correct! The values in the assertEqual () function are incorrect. The assertEqual () function checks if the provided values are the same.

8. Consider the following code in the file my_code.py a directory named mypackage with __init__.py.

def print_hello():

 print(“Hello”)

Which of the following two statements are the right ways to access the function in the method?

  • import my_code
  • print_hello ()
  • from mypackage import print_hello
  • print_hello ()
  • from mypackage import my_code
  • my_code.print_hello () (CORRECT)
  • from mypackage.my_code import print_hello
  • print_hello () (CORRECT)

Correct! The function needs to be called from the file and the file from the package.

Correct! The function must be called from the file, and the file must be called from the package.

9. Which of the following is the general structure for testing a package?

  • import {package_name}
  • run {package_name}. {module_name}. {function_name} (parameters)
  • {package_name}. {module_name}. {function_name} (parameters) (CORRECT)
  • {package_name} (parameters)

Correct! The general structure for testing your package is {package_name}. {module_name}. {function_name} (parameters)

10. Which of the following is the correct folder structure for the package “MyPackage”?

  • MyPackage -> module_1-> module_1.py MyPackage -> module_2-> module_2.py
  • MyPackage -> module_1.py, module_2.py, __init__.py (CORRECT)
  • MyPackage -> module_1.py, module_2.py
  • MyPackage -> module_1.py, module_2.py, init.py

Correct! The module files are stored in the package folder along with a __init__.py file, which imports all modules.

CONCLUSION – Python Coding Practices and Packaging Concepts

In conclusion, this module provides a comprehensive foundation in the development of web applications and APIs. You will gain a clear understanding of the entire application development lifecycle, from requirement gathering to project maintenance.

By adhering to the PEP8 style guide and utilizing static code analysis, you will ensure your code meets high standards. Additionally, mastering unit testing and Python package management will further enhance your development skills, preparing you for advanced programming challenges.