-
Notifications
You must be signed in to change notification settings - Fork 72
lesson_13 #697
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
lesson_13 #697
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| ### Lesson 13 - Objects and classes | ||
| #### introduction | ||
| - [Think Python: How to Think Like a Computer Scientist / Chapter 15](http://greenteapress.com/thinkpython2/html/thinkpython2016.html) | ||
| - [Python dir() | ||
| function](https://www.programiz.com/python-programming/methods/built-in/dir) | ||
| - [Python id() function](https://www.programiz.com/python-programming/methods/built-in/id) | ||
| - [Python isinstance() function](https://www.programiz.com/python-programming/methods/built-in/isinstance) | ||
| - [How To Construct Classes and Define Objects in Python 3](https://www.digitalocean.com/community/tutorials/how-to-construct-classes-and-define-objects-in-python-3) (supplementary materials) | ||
| - [Understanding Class and Instance Variables in Python 3](https://www.digitalocean.com/community/tutorials/understanding-class-and-instance-variables-in-python-3) (supplementary materials) | ||
| #### practice projects | ||
| 1. [Think Python: How to Think Like a Computer Scientist / Chapter 15 / Exercise 1 ](http://greenteapress.com/thinkpython2/html/thinkpython2016.html) | ||
| 1. Object inspector 1 - write a function that for a given object and list of attribute names returns dictionary with names and values of object's attributes. | ||
| 1. Object inspector 2 - write a function that for a given object returns dictionary with names and values of all object's attributes that are instances of string, integer or float. | ||
| 1. Selective shallow compare - write a function that for given 2 objects and list of attribute names checks if objects' attributes are equal. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| import math | ||
|
|
||
|
|
||
| class Point: | ||
| def __init__(self, x=0, y=0): | ||
| """ Create a new point at the origin """ | ||
| self.x = x | ||
| self.y = y | ||
|
|
||
|
|
||
| class Circle: | ||
| def __init__(self, center, radius): | ||
| self.center = center | ||
| self.radius = radius | ||
|
|
||
|
|
||
| class Rectangle: | ||
| def __init__(self, topLeft, topRight, bottomLeft, bottomRight): | ||
| self.tL = topLeft | ||
| self.tR = topRight | ||
| self.bL = bottomLeft | ||
| self.bR = bottomRight | ||
|
|
||
| def point_in_circle(point, circle): | ||
| distance_between_points = math.sqrt((point.x - circle.center.x)** 2 + | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. E225 missing whitespace around operator |
||
| (point.y - circle.center.y)**2) | ||
| return distance_between_points <= circle.radius | ||
|
|
||
|
|
||
| def point_in_circle_bounduary(point, circle): | ||
| distance_between_points = math.sqrt((point.x - circle.center.x)** 2 + | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. E225 missing whitespace around operator |
||
| (point.y - circle.center.y)**2) | ||
| return distance_between_points == circle.radius | ||
|
|
||
|
|
||
| def rect_circle(rectangle, circle): | ||
| check_tL = point_in_circle_bounduary(rectangle.tL, circle) | ||
| check_tR = point_in_circle_bounduary(rectangle.tR, circle) | ||
| check_bL = point_in_circle_bounduary(rectangle.bL, circle) | ||
| check_bR = point_in_circle_bounduary(rectangle.bL, circle) | ||
| return check_tL and check_tR and check_bL and check_bR | ||
|
|
||
|
|
||
| def rect_circle_overlap(rectangle, circle): | ||
| check_tL = point_in_circle(rectangle.tL, circle) | ||
| check_tR = point_in_circle(rectangle.tR, circle) | ||
| check_bL = point_in_circle(rectangle.bL, circle) | ||
| check_bR = point_in_circle(rectangle.bL, circle) | ||
| return check_tL and check_tR and check_bL and check_bR | ||
|
|
||
|
|
||
| def main(): | ||
| point1 = Point(370,550) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. E231 missing whitespace after ',' |
||
| circle_center = Point(150, 100) | ||
| my_circle = Circle(circle_center, 75) | ||
| print('Is point(%s, %s) in the circle(center=(%s, %s), radious=%s)?' | ||
| % (point1.x, point1.y, circle_center.x, circle_center.y, | ||
| my_circle.radius)) | ||
| print(point_in_circle(point1, my_circle)) | ||
|
|
||
| # rectangle in circle | ||
| # rectangle1 = Rectangle(Point(1,10),Point(6,10), Point(1,5), Point(6,5)) | ||
| rectangles_points = (Point(-2, 2), Point(2, 2), Point(-2, -2), Point(2, -2)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. E501 line too long (80 > 79 characters) |
||
| rectangle2 = Rectangle(*rectangles_points) | ||
| circle_center = Point(0, 0) | ||
| my_circle2 = Circle(circle_center, math.sqrt(8)) | ||
| print('Is rectangle:((%s,%s),(%s,%s),(%s,%s),(%s,%s)) in the circle' | ||
| '(center=(%s, %s),radius=%s) board?' | ||
| % (rectangle2.tL.x, rectangle2.tL.y, rectangle2.tR.x, rectangle2.tR.y, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. E501 line too long (80 > 79 characters) |
||
| rectangle2.bL.x, rectangle2.bL.y, rectangle2.bL.x, rectangle2.bL.y, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. E501 line too long (80 > 79 characters) |
||
| circle_center.x, circle_center.y, my_circle2.radius)) | ||
| print(rect_circle(rectangle2, my_circle2)) | ||
|
|
||
| # rectangle in circle overlap | ||
| # rectangle1 = Rectangle(Point(1,10),Point(6,10), Point(1,5), Point(6,5)) | ||
| rectangles_points = (Point(1, 10), Point(6, 10), Point(1, 5), Point(6, 5)) | ||
| rectangle2 = Rectangle(*rectangles_points) | ||
| circle_center = Point(2, 3) | ||
| my_circle2 = Circle(circle_center, 20) | ||
| print('Is rectangle:((%s,%s),(%s,%s),(%s,%s),(%s,%s)) in the circle' | ||
| '(center=(%s, %s),radius=%s)?' | ||
| % (rectangle2.tL.x, rectangle2.tL.y, rectangle2.tR.x, rectangle2.tR.y, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. E501 line too long (80 > 79 characters) |
||
| rectangle2.bL.x, rectangle2.bL.y, rectangle2.bL.x, rectangle2.bL.y, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. E501 line too long (80 > 79 characters) |
||
| circle_center.x, circle_center.y, my_circle.radius)) | ||
| print(rect_circle_overlap (rectangle2, my_circle2)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. E211 whitespace before '(' |
||
|
|
||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. E303 too many blank lines (3) |
||
| main() | ||
|
|
||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. W391 blank line at end of file |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| class Point: | ||
| def __init__(self, x=0, y=0): | ||
| """ Create a new point at the origin """ | ||
| self.x = x | ||
| self.y = y | ||
|
|
||
|
|
||
| def object_inspector1(instance, keys): | ||
| instance_all = instance.__dict__ | ||
| selected_keys = {key:instance_all[key] for key in keys} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. E231 missing whitespace after ':' |
||
| return(selected_keys) | ||
|
|
||
|
|
||
| def main(): | ||
| ainspector = Point() | ||
| ainspector.g = 7 | ||
| ainspector.x = 12 | ||
| ainspector.b = [77, 88] | ||
| ainspector.c = 4 | ||
| ainspector.y = {'c':77, 'g':88} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. E231 missing whitespace after ':' |
||
| ainspector.s = "sssss" | ||
| ainspector.p = 4.564744 | ||
| ainspector.q = 5 | ||
| ainspector.m = 4.564744 | ||
|
|
||
| a = ['b', 'x', 'y', 'p', 's'] | ||
| print(object_inspector1(ainspector, a)) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. W292 no newline at end of file |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| class Point: | ||
| def __init__(self, x=0, y=0): | ||
| """ Create a new point at the origin """ | ||
| self.x = x | ||
| self.y = y | ||
|
|
||
|
|
||
| def object_inspector2(instance, keys): | ||
| instance_all = instance.__dict__ | ||
| selected_keys = {key: instance_all[key] for key in keys | ||
| if (isinstance(instance_all[key], int) or | ||
| isinstance(instance_all[key], str) or | ||
| isinstance(instance_all[key], float))} | ||
| return(selected_keys) | ||
|
|
||
|
|
||
| def main(): | ||
| ainspector = Point() | ||
| ainspector.g = 7 | ||
| ainspector.x = 12 | ||
| ainspector.b = [77, 88] | ||
| ainspector.c = 4 | ||
| ainspector.y = {'c':77, 'g':88} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. E231 missing whitespace after ':' |
||
| ainspector.s = "sssss" | ||
| ainspector.p = 4.564744 | ||
| ainspector.q = 5 | ||
| ainspector.m = 4.564744 | ||
|
|
||
| a = ['b', 'x', 'y', 'p', 's'] | ||
| print(object_inspector2(ainspector, a)) | ||
|
|
||
| if __name__ == "__main__": | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. E305 expected 2 blank lines after class or function definition, found 1 |
||
| main() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. W292 no newline at end of file |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| class Point: | ||
| def __init__(self, x=0, y=0): | ||
| """ Create a new point at the origin """ | ||
| self.x = x | ||
| self.y = y | ||
|
|
||
|
|
||
| def compere_objects(instance1, instance2, keys): | ||
| instance1_all = instance1.__dict__ | ||
| instance2_all = instance2.__dict__ | ||
| compare_result = {key: (instance1_all[key] == instance2_all[key]) | ||
| for key in keys} | ||
| return(compare_result) | ||
|
|
||
|
|
||
| def main(): | ||
| ainspector = Point() | ||
| ainspector.g = 7 | ||
| ainspector.x = 12 | ||
| ainspector.b = [77, 88] | ||
| ainspector.c = 4 | ||
| ainspector.y = {'c':77, 'g':88} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. E231 missing whitespace after ':' |
||
| ainspector.s = "sssss" | ||
| ainspector.p = 4.564744 | ||
| ainspector.q = 5 | ||
| ainspector.m = 4.564744 | ||
|
|
||
| binspector = Point() | ||
| binspector.g = 7 | ||
| binspector.x = 5 | ||
| binspector.b = [22, 88] | ||
| binspector.c = 4 | ||
| binspector.y = {'c':77, 'g':88} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. E231 missing whitespace after ':' |
||
| binspector.s = "ssassss" | ||
| binspector.p = 4.564744 | ||
| binspector.q = 5 | ||
| ainspector.m = 4.564744 | ||
|
|
||
| attributes = ['b', 'x', 'y', 'p', 's'] | ||
| print(compere_objects(ainspector, binspector, attributes)) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. W292 no newline at end of file |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E302 expected 2 blank lines, found 1