Module 1: Python Coding Practices and Packaging Concepts

Spread the love

INTRODUCTION – Python Coding Practices and Packaging Concepts

In this module, you’ll stumble upon all the differences that can be found in web applications and APIs, covering the general aspects that make them both unique and different from one another. Additionally, you will get to familiarize yourself with the application development lifecycle, giving you a fair idea of what happens from requirement gathering to project maintenance. It will get you on the track of understanding how some of the major phases have got different implementations and replicas.

In this module, we will introduce you to Python coding standards, specifically PEP8. This is an essential coding-style guideline for writing clean and consistent Python code. You will also learn about static code analysis, which is a technique to ensure that your code meets certain standards and conforms to practices.

Also, you will gain practical knowledge through developing and executing unit tests, which are essential in guaranteeing that your code is functional. This module also goes through the sections that let you generate, verify, and run python packages to complete your arsenal for efficient software development practices.

Learning Outcomes

  • Articulate each of the phases in the application development lifecycle
  • Distinguish between web applications and APIs
    Organize your application’s code into multiple files for clarity
  • Understand and follow Python coding conventions PEP8
  • Use static code analysis to verify code quality
  • Create and execute unit tests to verify that the code works
  • Create, verify, and execute Python packages for modular development

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! In a Python package, the modules should be included using import statements in the init.py file. This file allows organizing and managing the whole structure of a package and makes accessing different modules simpler after the import of the package itself.

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

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

Correct! As a preliminary condition, unittest recognizes unit test functions only with the prefix test.

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

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

Correct! Preferably, method names should be written in lowercase with words separated by the underscore symbol, according to PEP8 standards.

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! Each package needs to have all its modules referenced inside the __init__.py. Otherwise, the package won’t be 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! Web applications and application programming interfaces connect two different parts of a system. Web applications usually link the frontend and backend while APIs connect the different software components or applications within themselves.

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! Yes, web apps commonly 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! Exactly! Integration testing focuses on ensuring that different components or systems within an application work together as expected after being integrated. While unit tests check individual components (e.g., functions or methods), integration tests verify that these components can interact smoothly when combined.

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! Linters search for various programming constructs and objects that are incorrect in coding conventions with performance issues. Pylint is one of them. It is a static code analysis tool that detects bug and style errors in Python. Checks your code against PEP8 guidelines and states your compliance with those standards.

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! Constants should be referred to using only uppercase words with a linking underscore in-between according to PEP8 conventions. Hence, the name of the constant should rightly be 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! A business requirement is effectively defined by this statement. A business requirement identifies the high-level objective or need the business is trying to fulfill through the app or project without going into specifics on how the actual development would happen. Its focus is on the value or problem that the solution is intended to address.

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! Unit testing classes are an excellent way to make it easier to run all the unit tests via 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 assertEqual() function’s values are incorrect. The assertEqual() function asserts that the two given values are equal.

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 has to be called using the file, and the file will import 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! This structure is {package_name}.{module_name}.{function_name}(parameters) for testing your package. It allows you to call that function from a module in your package and pass necessary parameters to it for testing.

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 together with an __init__.py file, which imports all modules in the package. This means that all modules can be imported and used as though they are one single package.

CONCLUSION – Python Coding Practices and Packaging Concepts

To summarize, this module provides a very good grounding in web application and API development. You will learn how to cover all parts in a comprehensive discussion of the application development lifecycle-from requirement gathering to station project maintenance.

By adhering to the PEP8 style guide and doing your static code analysis, you would be expected to conform your code to high standards. Additionally, the practice of unit tests and Python package management will improve one’s development skill to take on advanced programming tasks.

Leave a Comment