HW7 with py, testpy, and two txt

This commit is contained in:
2023-06-09 17:04:23 -07:00
parent e75cba4117
commit 582e2bcfe1
4 changed files with 263 additions and 0 deletions
+77
View File
@@ -0,0 +1,77 @@
########## Some commands in pdb ##########
# help: Display a list of available pdb commands or get help on a specific command.
# break or b: Set a breakpoint at a specific line or function.
# continue or c: Continue execution until the next breakpoint or the program finishes.
# step or s: Step into the next line of code, diving into function calls.
# next or n: Step over the next line of code, without diving into function calls.
# return or r: Continue execution until the current function returns.
# list or l: Show the current line of code and surrounding lines.
# print or p: Evaluate and print the value of an expression or variable.
# watch: Set a watchpoint on an expression or variable. The debugger will break when the value changes.
# quit or q: Quit the debugger and stop execution.
########## There is a main() function at the end ##########
# 2. Provide an example script where setting a breakpoint on a particular line
# Then using either step over or step into will yield the same result.
class Same_StepOverStepInto_Result:
def calculation2(self, a, b):
result = a + b
product = self.multiply(a, b) # Step into this method call
breakpoint() # Set a breakpoint here
print(f"The sum is: {result}")
print(f"The product is: {product}")
def multiply(self, x, y):
return x * y
# calculator2 = Same_StepOverStepInto_Result()
# calculator2.calculation2(6, 7)
# 3. Here is another example which will provide different results from 'step' and 'next' commands
class Diff_StepOverStepInto_Result:
def multiply(self, a, b):
return a * b
def calculation3(self, a, b):
result = a + b
breakpoint() # Set a breakpoint here
product = self.multiply(a, b)
print(f"The sum is: {result}")
print(f"The product is: {product}")
# calculator3 = Diff_StepOverStepInto_Result()
# calculator3.calculation3(6, 7)
# Choose wich Class to run
def main():
choice = input("Enter 2 to run Q2 class for same result, 3 to run Q3 class for different result: ")
if choice == '2':
print('Here is the sample task for Question 2:')
calculator2 = Same_StepOverStepInto_Result()
calculator2.calculation2(6, 7)
elif choice == '3':
print('Here is the sample task for Question 3:')
calculator3 = Diff_StepOverStepInto_Result()
calculator3.calculation3(6, 7)
else:
print("Please only choose between 2 and 3")
if __name__ == "__main__":
main()
+79
View File
@@ -0,0 +1,79 @@
class Vector_1:
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, other):
if isinstance(other, Vector_1):
# Add the corresponding coordinates of both vectors
new_x = self.x + other.x
new_y = self.y + other.y
return Vector_1(new_x, new_y)
else:
raise TypeError("Unsupported operand type: +")
def __str__(self):
return f"Vector({self.x}, {self.y})"
# # Create two Vector instances
# vector1 = Vector_1(2, 3)
# vector2 = Vector_1(4, 5)
# # Add two Vector instances using the + operator
# result1 = vector1 + vector2
# # Print the result
# print(result1)
class Vector_2:
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, other):
if isinstance(other, Vector_2):
# Add the corresponding coordinates of both vectors
new_x = self.x + other.x
new_y = self.y + other.y
return Vector_2(new_x, new_y)
else:
raise TypeError("Unsupported operand type: +")
# def __str__(self):
# return f"Vector({self.x}, {self.y})"
# # Create two Vector instances
# vector3 = Vector_2(2, 3)
# vector4 = Vector_2(4, 5)
# # Add two Vector instances using the + operator
# result2 = vector3 + vector4
# # Print the result
# print(result2)
def main():
# Create two Vector instances
vector1 = Vector_1(2, 3)
vector2 = Vector_1(4, 5)
# Add two Vector instances using the + operator
result1 = vector1 + vector2
# Print the result
print("This is printout of the Class using __str__() method:")
print(result1)
# Create two Vector instances
vector3 = Vector_2(2, 3)
vector4 = Vector_2(4, 5)
# Add two Vector instances using the + operator
result2 = vector3 + vector4
# Print the result
print("This is printout of the Class without using __str__() method:")
print(result2)
if __name__ == "__main__":
main()
+56
View File
@@ -0,0 +1,56 @@
{\rtf1\ansi\ansicpg1252\cocoartf2580
\cocoatextscaling0\cocoaplatform0{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
{\*\expandedcolortbl;;}
\margl1440\margr1440\vieww11520\viewh8400\viewkind0
\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\partightenfactor0
\f0\fs24 \cf0 scott@MacBook-Pro-6 Scott_Submissions % python3 HW7.py\
Enter 2 to run Q2 class for same result, 3 to run Q3 class for different result: 1\
Please only choose between 2 and 3\
scott@MacBook-Pro-6 Scott_Submissions % python3 HW7.py\
Enter 2 to run Q2 class for same result, 3 to run Q3 class for different result: 2\
Here is the sample task for Question 2:\
> /Users/scott/Desktop/GTSP/python-bootcamp/Scott_Submissions/HW7.py(27)calculation2()\
-> print(f"The sum is: \{result\}")\
(Pdb) step\
The sum is: 13\
> /Users/scott/Desktop/GTSP/python-bootcamp/Scott_Submissions/HW7.py(28)calculation2()\
-> print(f"The product is: \{product\}")\
(Pdb) c\
The product is: 42\
scott@MacBook-Pro-6 Scott_Submissions % python3 HW7.py\
Enter 2 to run Q2 class for same result, 3 to run Q3 class for different result: 2\
Here is the sample task for Question 2:\
> /Users/scott/Desktop/GTSP/python-bootcamp/Scott_Submissions/HW7.py(27)calculation2()\
-> print(f"The sum is: \{result\}")\
(Pdb) next\
The sum is: 13\
> /Users/scott/Desktop/GTSP/python-bootcamp/Scott_Submissions/HW7.py(28)calculation2()\
-> print(f"The product is: \{product\}")\
(Pdb) c\
The product is: 42\
scott@MacBook-Pro-6 Scott_Submissions % python3 HW7.py\
Enter 2 to run Q2 class for same result, 3 to run Q3 class for different result: 3\
Here is the sample task for Question 3:\
> /Users/scott/Desktop/GTSP/python-bootcamp/Scott_Submissions/HW7.py(50)calculation3()\
-> product = self.multiply(a, b)\
(Pdb) step\
--Call--\
> /Users/scott/Desktop/GTSP/python-bootcamp/Scott_Submissions/HW7.py(42)multiply()\
-> def multiply(self, a, b):\
(Pdb) c\
The sum is: 13\
The product is: 42\
scott@MacBook-Pro-6 Scott_Submissions % python3 HW7.py\
Enter 2 to run Q2 class for same result, 3 to run Q3 class for different result: 3\
Here is the sample task for Question 3:\
> /Users/scott/Desktop/GTSP/python-bootcamp/Scott_Submissions/HW7.py(50)calculation3()\
-> product = self.multiply(a, b)\
(Pdb) next\
> /Users/scott/Desktop/GTSP/python-bootcamp/Scott_Submissions/HW7.py(52)calculation3()\
-> print(f"The sum is: \{result\}")\
(Pdb) c\
The sum is: 13\
The product is: 42\
scott@MacBook-Pro-6 Scott_Submissions % }
@@ -0,0 +1,51 @@
{\rtf1\ansi\ansicpg1252\cocoartf2580
\cocoatextscaling0\cocoaplatform0{\fonttbl\f0\fnil\fcharset0 LucidaGrande-Bold;\f1\fnil\fcharset0 LucidaGrande;}
{\colortbl;\red255\green255\blue255;\red26\green26\blue26;\red255\green255\blue255;}
{\*\expandedcolortbl;;\cssrgb\c13333\c13333\c13333;\cssrgb\c100000\c100000\c100000;}
\margl1440\margr1440\vieww11520\viewh8400\viewkind0
\deftab720
\pard\pardeftab720\parhyphenfactor20\partightenfactor0
\f0\b\fs32 \cf2 \cb3 \expnd0\expndtw0\kerning0
CPython implementation detail:
\f1\b0 CPython currently uses a reference-counting scheme with (optional) delayed detection of cyclically linked garbage, which collects most objects as soon as they become unreachable, but is not guaranteed to collect garbage containing circular references.\
\
What is a r\cf2 eference-counting scheme?\
It refers to a memory management technique where each object in a programming language implementation, such as CPython, keeps track of the number of references pointing to it. \
Whenever a reference is created to an object, its reference count is increased, and when a reference is deleted or goes out of scope, the reference count is decreased. \
\
What is a reference?\
In the context of memory management, a "reference" refers to an identifier or value that allows access to an object in a programming language. \
It is a way to refer to or point to the memory location where an object is stored.\
\
What is the advantage of reference-counting scheme?\
The advantage of the reference-counting scheme is that it allows objects to be deallocated immediately when they are no longer needed. \
It ensures efficient memory management because objects can be freed as soon as their reference count reaches zero, without the need for more complex garbage collection algorithms.\
\
What is a garbage?\
In the context of memory management, "garbage" refers to objects that are no longer reachable or used by the program but still occupy memory. \
This typically occurs when objects are created dynamically during program execution but are not properly deallocated or no longer accessible. \
Garbage collection is the process of identifying and reclaiming memory occupied by such unused objects, making it available for reuse. \
The reference-counting scheme used by CPython helps in automatically deallocating most objects as soon as they become unreachable. \
However, in the case of circular references, where objects refer to each other in a loop, additional mechanisms are required to detect and collect such cyclically linked garbage. It\'92s called "delayed detection of cyclically linked garbage" to collect and deallocate objects that are involved in cyclic references.\
\
\
\
What is a __blabla__(): method?\
In Python, the following are special attributes (also known as dunder attributes) that can be accessed on objects or functions.\
These special attributes provide additional information or access to specific properties of objects in Python. \
\
What is the advantages of the dunder method?\
Overall, dunder attributes provide flexibility, customization, and enhanced functionality to Python objects and classes. \
They contribute to the expressive nature of the language and enable powerful features like operator overloading and introspection.\
\
Convention: Dunder attributes follow a specific naming convention, starting and ending with double underscores (__). This convention makes them easily recognizable and distinguishes them from regular attributes.\
Customization: Dunder attributes allow you to customize and define the behavior of objects and classes. By implementing specific dunder methods, you can change how objects behave in certain situations, such as arithmetic operations, string representation, comparisons, and more.\
Operator Overloading: Dunder attributes enable operator overloading, allowing objects to participate in built-in operations like addition (+), subtraction (-), equality (==), and more. This makes it possible to define meaningful operations for custom classes.\
Introspection: Dunder attributes provide a way to introspect and access information about objects and classes. For example, name and doc allow you to retrieve the name and documentation of an object. This introspection capability is useful for debugging, documentation generation, and code analysis.\
Integration with Language Features: Dunder attributes seamlessly integrate with various language features, such as iteration (with iter), context managers (with enter and exit), attribute access (with getattr, setattr), and more. This allows you to leverage the full power of the Python language in your classes.\
Code Readability: By following the conventions for dunder attributes, your code becomes more readable and understandable by other Python developers. They can easily recognize and understand the purpose of these special attributes.\
\
\
\
## The attached file HW7_test.py provides an example on how dunder method works. Don\'92t forget to check it out.}