diff --git a/students/dychowicz_monika/lesson_13_objects_and_classes/README.md b/students/dychowicz_monika/lesson_13_objects_and_classes/README.md new file mode 100644 index 000000000..07a701571 --- /dev/null +++ b/students/dychowicz_monika/lesson_13_objects_and_classes/README.md @@ -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. \ No newline at end of file diff --git a/students/dychowicz_monika/lesson_13_objects_and_classes/chapter_exercise1.py b/students/dychowicz_monika/lesson_13_objects_and_classes/chapter_exercise1.py new file mode 100644 index 000000000..96463b678 --- /dev/null +++ b/students/dychowicz_monika/lesson_13_objects_and_classes/chapter_exercise1.py @@ -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 + + (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 + + (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) + 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)) + 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, + rectangle2.bL.x, rectangle2.bL.y, rectangle2.bL.x, rectangle2.bL.y, + 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, + rectangle2.bL.x, rectangle2.bL.y, rectangle2.bL.x, rectangle2.bL.y, + circle_center.x, circle_center.y, my_circle.radius)) + print(rect_circle_overlap (rectangle2, my_circle2)) + + + +if __name__ == "__main__": + main() + + diff --git a/students/dychowicz_monika/lesson_13_objects_and_classes/object_inspector1.py b/students/dychowicz_monika/lesson_13_objects_and_classes/object_inspector1.py new file mode 100644 index 000000000..ca7645d0c --- /dev/null +++ b/students/dychowicz_monika/lesson_13_objects_and_classes/object_inspector1.py @@ -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} + 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} + 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() \ No newline at end of file diff --git a/students/dychowicz_monika/lesson_13_objects_and_classes/object_inspector2.py b/students/dychowicz_monika/lesson_13_objects_and_classes/object_inspector2.py new file mode 100644 index 000000000..7705e98d6 --- /dev/null +++ b/students/dychowicz_monika/lesson_13_objects_and_classes/object_inspector2.py @@ -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} + 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__": + main() \ No newline at end of file diff --git a/students/dychowicz_monika/lesson_13_objects_and_classes/selective_shallow_compare.py b/students/dychowicz_monika/lesson_13_objects_and_classes/selective_shallow_compare.py new file mode 100644 index 000000000..12cce2972 --- /dev/null +++ b/students/dychowicz_monika/lesson_13_objects_and_classes/selective_shallow_compare.py @@ -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} + 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} + 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() \ No newline at end of file diff --git a/students/dychowicz_monika/lesson_14_decorators/README.md b/students/dychowicz_monika/lesson_14_decorators/README.md new file mode 100644 index 000000000..341540ab0 --- /dev/null +++ b/students/dychowicz_monika/lesson_14_decorators/README.md @@ -0,0 +1,3 @@ +### Lesson 14 - Decorators + more OOP +#### introduction +#### practice projects \ No newline at end of file diff --git a/students/dychowicz_monika/lesson_14_decorators/__pycache__/authorization.cpython-36.pyc b/students/dychowicz_monika/lesson_14_decorators/__pycache__/authorization.cpython-36.pyc new file mode 100644 index 000000000..8c554f97c Binary files /dev/null and b/students/dychowicz_monika/lesson_14_decorators/__pycache__/authorization.cpython-36.pyc differ diff --git a/students/dychowicz_monika/lesson_14_decorators/__pycache__/logger_wrapper.cpython-36.pyc b/students/dychowicz_monika/lesson_14_decorators/__pycache__/logger_wrapper.cpython-36.pyc new file mode 100644 index 000000000..6067b12ec Binary files /dev/null and b/students/dychowicz_monika/lesson_14_decorators/__pycache__/logger_wrapper.cpython-36.pyc differ diff --git a/students/dychowicz_monika/lesson_14_decorators/__pycache__/restrictions.cpython-36.pyc b/students/dychowicz_monika/lesson_14_decorators/__pycache__/restrictions.cpython-36.pyc new file mode 100644 index 000000000..4c3fb6206 Binary files /dev/null and b/students/dychowicz_monika/lesson_14_decorators/__pycache__/restrictions.cpython-36.pyc differ diff --git a/students/dychowicz_monika/lesson_14_decorators/__pycache__/sort.cpython-36.pyc b/students/dychowicz_monika/lesson_14_decorators/__pycache__/sort.cpython-36.pyc new file mode 100644 index 000000000..516ab7e64 Binary files /dev/null and b/students/dychowicz_monika/lesson_14_decorators/__pycache__/sort.cpython-36.pyc differ diff --git a/students/dychowicz_monika/lesson_14_decorators/authorization.py b/students/dychowicz_monika/lesson_14_decorators/authorization.py new file mode 100644 index 000000000..baf09bd8e --- /dev/null +++ b/students/dychowicz_monika/lesson_14_decorators/authorization.py @@ -0,0 +1,14 @@ +access_count = -1 + + +def has_access(): + ''' + function calls some authorization services + returns True if access is granted and False if not + access is granted every 3rd time + ''' + global access_count + access_count += 1 + if access_count >= 3: + access_count = 0 + return access_count == 0 \ No newline at end of file diff --git a/students/dychowicz_monika/lesson_14_decorators/checkrestrictions.py b/students/dychowicz_monika/lesson_14_decorators/checkrestrictions.py new file mode 100644 index 000000000..fc2d206f2 --- /dev/null +++ b/students/dychowicz_monika/lesson_14_decorators/checkrestrictions.py @@ -0,0 +1,17 @@ +from restrictions import AuthorizationError +import restrictions + + + +def test_restricted_print_on_access_granted(monkeypatch): + monkeypatch.setattr('authorization.has_access', lambda: True) + try: + restrictions.restricted_print() + except Exception: + pytest.fail('Unexpected exception') + + +def test_restricted_print_on_access_denied(monkeypatch): + monkeypatch.setattr('authorization.has_access', lambda: False) + with pytest.raises(AuthorizationError): + restrictions.restricted_print() \ No newline at end of file diff --git a/students/dychowicz_monika/lesson_14_decorators/checksort.py b/students/dychowicz_monika/lesson_14_decorators/checksort.py new file mode 100644 index 000000000..c2eae4ddd --- /dev/null +++ b/students/dychowicz_monika/lesson_14_decorators/checksort.py @@ -0,0 +1,11 @@ +import sort + + +test_data = [ + ((1, 4, -2, 2), [-2, 1, 2, 4]), + ((-1, 111, 1, 0), [-1, 0, 1, 111]) +] + + +def test_to_list_on_test_data(input, output): + assert sort.to_list(*input) == output \ No newline at end of file diff --git a/students/dychowicz_monika/lesson_14_decorators/logger.py b/students/dychowicz_monika/lesson_14_decorators/logger.py new file mode 100644 index 000000000..3fae4afb3 --- /dev/null +++ b/students/dychowicz_monika/lesson_14_decorators/logger.py @@ -0,0 +1,40 @@ +import logger_wrapper + + +def logged(func): + def function_wrapper(*args, **kwargs): + return logger_wrapper.logger_wrapper(func, *args, **kwargs) + return function_wrapper + + +@logged +def test_method_1(): + pass + + +@logged +def test_method_2(): + return 'foo' + + +@logged +def test_method_3(arg1, arg2): + return arg1 + arg2 + + +@logged +def test_method_4(*args): + return [x for x in args] + + +@logged +def test_method_5(*args, **kwargs): + return None + + +if __name__ == '__main__': + test_method_1() + test_method_2() + test_method_3(1, 2) + test_method_4(1, 4, 5, -3, 'aa') + test_method_5(1, 4, 5, -3, 'aa', a=1, b=45, c='buz') \ No newline at end of file diff --git a/students/dychowicz_monika/lesson_14_decorators/logger_wrapper.py b/students/dychowicz_monika/lesson_14_decorators/logger_wrapper.py new file mode 100644 index 000000000..848d4c80b --- /dev/null +++ b/students/dychowicz_monika/lesson_14_decorators/logger_wrapper.py @@ -0,0 +1,19 @@ +def _get_arguments_string(*args, **kwargs): + return ', '.join( + ['{0!r}'.format(i) for i in args] + + ['{0}={1!r}'.format(k, v) for k, v in kwargs.items()] + ) + + +def logger_wrapper(foo, *args, **kwargs): + print('--log: {0}({1})' + .format(foo.__name__, _get_arguments_string(*args, **kwargs))) + foo(*args, **kwargs) + + +if __name__ == '__main__': + def buz(a, b, c, d, e): + print('In buz() function') + + buz(1, 2, 3, d=12, e=11) + logger_wrapper(buz, 1, 2, 3, d=12, e=11) \ No newline at end of file diff --git a/students/dychowicz_monika/lesson_14_decorators/restrictions.py b/students/dychowicz_monika/lesson_14_decorators/restrictions.py new file mode 100644 index 000000000..8b504d1ad --- /dev/null +++ b/students/dychowicz_monika/lesson_14_decorators/restrictions.py @@ -0,0 +1,33 @@ +import authorization + + +class AuthorizationError(Exception): + pass + + +def access_required(func): + def function_wrapper(*args, **kwargs): + if not authorization.has_access(): + raise AuthorizationError + return func(*args, **kwargs) + return function_wrapper + + +@access_required +def restricted_print(*args, **kwargs): + print(*args, **kwargs) + + +if __name__ == '__main__': + data = [ + '1 - visible', + '2 - invisible', + '3 - invisible', + '4 - visible' + ] + + for item in data: + try: + restricted_print(item) + except AuthorizationError: + pass \ No newline at end of file diff --git a/students/dychowicz_monika/lesson_14_decorators/sort.py b/students/dychowicz_monika/lesson_14_decorators/sort.py new file mode 100644 index 000000000..b95338508 --- /dev/null +++ b/students/dychowicz_monika/lesson_14_decorators/sort.py @@ -0,0 +1,14 @@ + +def sort(func): + def function_wrapper(*args): + return sorted(func(*args)) + return function_wrapper + + + +def to_list(*args): + return [x for x in args] + + +if __name__ == '__main__': + print(to_list(1, 11, 7, -22)) \ No newline at end of file