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

3 changes: 3 additions & 0 deletions students/dychowicz_monika/lesson_14_decorators/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Lesson 14 - Decorators + more OOP
#### introduction
#### practice projects
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
14 changes: 14 additions & 0 deletions students/dychowicz_monika/lesson_14_decorators/authorization.py
Original file line number Diff line number Diff line change
@@ -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
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,17 @@
from restrictions import AuthorizationError
import restrictions



def test_restricted_print_on_access_granted(monkeypatch):
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)

monkeypatch.setattr('authorization.has_access', lambda: True)
try:
restrictions.restricted_print()
except Exception:
pytest.fail('Unexpected exception')
Copy link
Contributor

Choose a reason for hiding this comment

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

F821 undefined name 'pytest'



def test_restricted_print_on_access_denied(monkeypatch):
monkeypatch.setattr('authorization.has_access', lambda: False)
with pytest.raises(AuthorizationError):
Copy link
Contributor

Choose a reason for hiding this comment

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

F821 undefined name 'pytest'

restrictions.restricted_print()
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

11 changes: 11 additions & 0 deletions students/dychowicz_monika/lesson_14_decorators/checksort.py
Original file line number Diff line number Diff line change
@@ -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
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

40 changes: 40 additions & 0 deletions students/dychowicz_monika/lesson_14_decorators/logger.py
Original file line number Diff line number Diff line change
@@ -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')
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

19 changes: 19 additions & 0 deletions students/dychowicz_monika/lesson_14_decorators/logger_wrapper.py
Original file line number Diff line number Diff line change
@@ -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)
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

33 changes: 33 additions & 0 deletions students/dychowicz_monika/lesson_14_decorators/restrictions.py
Original file line number Diff line number Diff line change
@@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

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

E999 SyntaxError: invalid syntax



if __name__ == '__main__':
data = [
'1 - visible',
'2 - invisible',
'3 - invisible',
'4 - visible'
]

for item in data:
try:
restricted_print(item)
except AuthorizationError:
pass
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

14 changes: 14 additions & 0 deletions students/dychowicz_monika/lesson_14_decorators/sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

def sort(func):
def function_wrapper(*args):
return sorted(func(*args))
return function_wrapper



def to_list(*args):
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)

return [x for x in args]


if __name__ == '__main__':
print(to_list(1, 11, 7, -22))
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