Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions students/dychowicz_monika/lesson_13_objects_and_classes/README.md
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):
Copy link
Contributor

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

distance_between_points = math.sqrt((point.x - circle.center.x)** 2 +
Copy link
Contributor

Choose a reason for hiding this comment

The 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 +
Copy link
Contributor

Choose a reason for hiding this comment

The 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)
Copy link
Contributor

Choose a reason for hiding this comment

The 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))
Copy link
Contributor

Choose a reason for hiding this comment

The 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,
Copy link
Contributor

Choose a reason for hiding this comment

The 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,
Copy link
Contributor

Choose a reason for hiding this comment

The 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,
Copy link
Contributor

Choose a reason for hiding this comment

The 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,
Copy link
Contributor

Choose a reason for hiding this comment

The 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))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

E211 whitespace before '('




if __name__ == "__main__":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

E303 too many blank lines (3)

main()


Copy link
Contributor

Choose a reason for hiding this comment

The 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}
Copy link
Contributor

Choose a reason for hiding this comment

The 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}
Copy link
Contributor

Choose a reason for hiding this comment

The 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()
Copy link
Contributor

Choose a reason for hiding this comment

The 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}
Copy link
Contributor

Choose a reason for hiding this comment

The 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__":
Copy link
Contributor

Choose a reason for hiding this comment

The 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()
Copy link
Contributor

Choose a reason for hiding this comment

The 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}
Copy link
Contributor

Choose a reason for hiding this comment

The 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}
Copy link
Contributor

Choose a reason for hiding this comment

The 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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W292 no newline at end of file