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/bec_lukasz/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.
142 changes: 142 additions & 0 deletions students/bec_lukasz/lesson_13_objects_and_classes/circle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import copy
import math


class Point:
"""Represents a point in 2-D space."""


class Rectangle:
"""Represents a rectangle.

attributes: width, height, corner.
"""


class Circle:
"""Represents a circle.

Attributes: center, radius
"""


def print_point(p):
print('(%g, %g)' % (p.x, p.y))


def find_center(rect):
p = Point()
p.x = rect.corner.x + rect.width / 2
p.y = rect.corner.y + rect.height / 2
return p


def distance_between_points(first, second):
dx = first.x - second.x
dy = first.y - second.y
distance = math.sqrt(dx**2 + dy**2)
return distance


def grow_rectangle(rect, width_arg, height_arg):
rect.width += width_arg
rect.height += height_arg


def point_in_circle(point, circle):
"""Checks whether a point lies inside a circle (or on the boundary).

point: Point object
circle: Circle object
"""
d = distance_between_points(point, circle.center)
print(d)
return d <= circle.radius


def rect_in_circle(rect, circle):
"""Checks whether the corners of a rect fall in/on a circle.

rect: Rectangle object
circle: Circle object
"""
p = copy.copy(rect.corner)
print_point(p)
if not point_in_circle(p, circle):
return False

p.x += rect.width
print_point(p)
if not point_in_circle(p, circle):
return False

p.y -= rect.height
print_point(p)
if not point_in_circle(p, circle):
return False

p.x -= rect.width
print_point(p)
if not point_in_circle(p, circle):
return False

return True


def rect_circle_overlap(rect, circle):
"""Checks whether any corners of a rect fall in/on a circle.

rect: Rectangle object
circle: Circle object
"""
p = copy.copy(rect.corner)
print_point(p)
if point_in_circle(p, circle):
return True

p.x += rect.width
print_point(p)
if point_in_circle(p, circle):
return True

p.y -= rect.height
print_point(p)
if point_in_circle(p, circle):
return True

p.x -= rect.width
print_point(p)
if point_in_circle(p, circle):
return True

return False


def main():
box = Rectangle()
box.width = 100.0
box.height = 200.0
box.corner = Point()
box.corner.x = 50.0
box.corner.y = 50.0

print(box.corner.x)
print(box.corner.y)

circle = Circle
circle.center = Point()
circle.center.x = 150.0
circle.center.y = 100.0
circle.radius = 75.0

print(circle.center.x)
print(circle.center.y)
print(circle.radius)

print(point_in_circle(box.corner, circle))
print(rect_in_circle(box, circle))
print(rect_circle_overlap(box, circle))


if __name__ == '__main__':
main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Point:
def __init__(self, x, y):
self.x = x
self.y = y


def object_inspector(obj, attributes):
dictionary = {}
for attribute in attributes:
value = getattr(obj, attribute)
dictionary[attribute] = value
return dictionary


def main():
point = Point(10, 20)
print(object_inspector(point, ['x', 'y']))


if __name__ == '__main__':
main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Point:
def __init__(self, x, y):
self.x = x
self.y = y


class Sth:
def __init__(self, v, x, y, z):
self.v = v
self.x = x
self.y = y
self.z = z


def object_inspector2(obj):
attributes = dir(obj)
dictionary = {}
for i in attributes:
if isinstance(getattr(obj, i), (int, float, str)):
value = getattr(obj, i)
dictionary[i] = value
return dictionary


def main():
point = Point(10, 20)
sth = Sth(point, 10, 20.0, 'trzydziesci')
print(object_inspector2(sth))


if __name__ == '__main__':
main()
27 changes: 27 additions & 0 deletions students/bec_lukasz/lesson_13_objects_and_classes/shallow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Sth:
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z


def shallow(object_one, object_two, attributes):
for attr in attributes:

if getattr(object_one, attr) != getattr(object_two, attr):
print('Not equal')
else:
print('Equal')


def main():
first = Sth(10, 10.0, 'first')
second = Sth(10, 10.0, 'first')
third = Sth(10, 10.0, 'third')
attrs = ['x', 'y', 'z']
shallow(first, second, attrs)
shallow(first, third, attrs)


if __name__ == '__main__':
main()