reading-notes

401 Class 2 Reading Notes

Summary: This class is about Testing and Modules

2 / 2

Testing and modules are both essential components of software development. Testing allows developers to ensure that their code works as expected and can catch potential bugs or issues before they reach the end-users. Modules, on the other hand, help to break down a software system into smaller, more manageable pieces, making it easier to develop, test, and maintain.

By using modules, developers can reuse code and build upon existing functionality, ultimately resulting in more efficient and scalable software development. Overall, testing and modules are critical components of modern software development, and neglecting either can lead to significant issues down the line.

Qp:What are the key principles of Test-Driven Development (TDD) in Python, and how do they contribute to the overall quality of code?

A:Test-Driven Development (TDD) is a software development approach that emphasizes writing automated tests before writing the actual code. The key principles of TDD in Python are as follows:

Write a test: Before writing any code, the developer writes a test to define what the code should do. The test should be specific, measurable, and independent.

Run the test: The developer runs the test to ensure that it fails. This step ensures that the test is actually testing something and that it’s not passing due to coincidence.

Write the code: The developer writes code to make the test pass. The code should be simple and straightforward, and it should only address the specific requirement of the test.

Run the test again: The developer runs the test again to ensure that the code they just wrote actually passes the test.

Refactor: Once the test has passed, the developer refactors the code to make it more efficient, more readable, and more maintainable.

These principles contribute to the overall quality of code in a number of ways:

Better code coverage: By writing tests before code, TDD ensures that all aspects of the code are tested, resulting in better code coverage.

Reduced errors: By catching errors early, TDD helps to reduce the number of errors in the final code. When errors are caught early, they are easier and less expensive to fix.

Improved design: TDD promotes good design principles, such as modularity, simplicity, and testability. This leads to code that is easier to understand, maintain, and extend.

Faster development: Although TDD may initially seem like a slower process, it can actually speed up development in the long run by reducing the time spent debugging and maintaining code.

Overall, TDD can help developers to produce high-quality, well-designed code that is less prone to errors and easier to maintain over time.

### “TRWRR”

Qp:Explain the purpose of the if name == ‘main’: statement in Python scripts. What are some use cases for including this conditional in your code?

A:The if name == ‘main’: statement is a Python conditional statement that is used to execute a certain block of code only if the Python script is executed as the main program.

In Python, every module has a built-in attribute called name. This attribute holds the name of the module. When a Python script is executed, Python sets the name attribute of the module to ‘main’ if it is the main program being run, or to the name of the module if it is being imported as a module by another program.

Therefore, the if name == ‘main’: statement is typically used at the end of a Python script to separate the code that should only be executed when the script is run as the main program from the code that should be executed when the script is imported as a module.

Here are some use cases for including this conditional in your code:

Script execution: If you want to write a Python script that can be run from the command line or by clicking on it, you can put your main script logic inside the if name == ‘main’: statement. This will ensure that the script runs only when it is executed as the main program, and not when it is imported as a module.

Testing: You can write test cases for your code and put them inside the if name == ‘main’: statement. This way, the tests will only be run when the script is executed as the main program, and not when it is imported as a module.

Modularization: You can write a script with functions and classes that can be imported as a module into other scripts. In this case, you would typically put the functions and classes outside the if name == ‘main’: statement, and put any code that should be executed when the script is run as the main program inside the statement.

Performance optimization: If you have a large script that takes a long time to run, you can split the script into multiple modules and use the if name == ‘main’: statement to only execute the parts of the script that are necessary for a particular run. This can help to improve the overall performance of your script.

Qp: Provide another example of importing module:

A:Sure, here’s another example of importing a module in Python:

Suppose you have a file named math_utils.py which contains some math utility functions:

python Copy code

math_utils.py

def add(a, b): return a + b

def multiply(a, b): return a * b And you have another file named main.py which imports the math_utils module and uses its functions:

python Copy code

main.py

import math_utils

result = math_utils.add(2, 3) print(result)

result = math_utils.multiply(4, 5) print(result) When you run main.py, Python will execute the code in math_utils.py once, setting the name attribute of the module to ‘math_utils’, and then execute the code in main.py, which imports the math_utils module and uses its functions.

When math_utils is imported, its code is executed, which defines the add() and multiply() functions. Since the name attribute is set to ‘math_utils’, the code inside the if name == ‘main’: block is not executed.

When main.py is run, Python executes its code, which imports the math_utils module using the import statement. This makes the functions defined in math_utils available for use in main.py. In main.py, the add() function is called with arguments 2 and 3, and the result 5 is printed. Then the multiply() function is called with arguments 4 and 5, and the result 20 is printed.

Qp: Describe the concept of recursion in Python.

A: Recursion is a programming technique in which a function calls itself to solve a problem by breaking it down into smaller sub-problems. In Python, a recursive function is a function that calls itself during its execution.

A recursive function generally consists of two parts: the base case and the recursive case. The base case is a condition that tells the function when to stop calling itself and return a result. The recursive case is the part of the function that calls itself with a smaller version of the problem.

Here is an example of a simple recursive function in Python that calculates the factorial of a given number:

arduino Copy code def factorial(n): if n == 0: return 1 else: return n * factorial(n-1) In this function, the base case is when n == 0. When n is 0, the function returns 1. Otherwise, the function calls itself with n-1 and multiplies the result by n.

* It’s important to note that recursive functions can be very powerful, but they can also be very memory-intensive. Each time a recursive function calls itself, it creates a new stack frame, which can quickly add up and cause a stack overflow error. Therefore, it’s important to ensure that a recursive function has a base case that will eventually be met, and that the number of recursive calls is reasonable for the size of the problem being solved.

Qp: What is the difference between Python modules and packages? Explain how to create, import, and use them in your Python programs.

A: In Python, a module is a single file containing Python definitions, statements, and functions, while a package is a collection of modules that are organized in a hierarchical manner to form a single Python application or library.

To create a module in Python, you can simply write your code in a file with a .py extension. For example, if you create a file called my_module.py and include a function called my_function, you can use it in another Python program like this:

java Copy code from my_module import my_function

result = my_function() To create a package, you need to create a directory with a special file called init.py. This file can be empty or contain initialization code that runs when the package is imported. You can then create additional modules within this directory to organize your code. For example, if you create a directory called my_package, with an init.py file and a my_module.py file containing a function called my_function, you can use it in another Python program like this:

java Copy code from my_package.my_module import my_function

result = my_function() Note that you can also import a module or package with an alias, like this:

javascript Copy code import my_module as mm from my_package import my_module as mm This can be useful when you have multiple modules or packages with similar names.

Once you have imported a module or package, you can use its functions and classes in your code. For example, if you import a module called my_module that contains a function called my_function, you can call it like this:

java Copy code import my_module

result = my_module.my_function() Similarly, if you import a package called my_package that contains a module called my_module with a function called my_function, you can call it like this:

java Copy code import my_package.my_module

result = my_package.my_module.my_function()

In summary, modules and packages are both used to organize code in Python. Modules are individual files that contain code, while packages are collections of modules organized in a hierarchical manner. To use them in your Python programs, you can import them using the import statement and then use their functions and classes in your code.

Python Strings

https://developers.google.com/edu/python/strings

Qp: Condense the following into Key points:

A:

Sources:

ChatGpt

https://code.likeagirl.io/in-tests-we-trust-tdd-with-python-af69f47e6932

https://developers.google.com/edu/python/lists

https://realpython.com/python-modules-packages/#python-modules-overview

https://docs.pytest.org/en/latest/

https://www.guru99.com/pytest-tutorial.html#3

Things I want to know more about:

Pytest.