Skip to content
Merged
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
110 changes: 108 additions & 2 deletions Students/salim/notes/class_notes.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ def func(c, list=[]): # this is wrong because it dones't create a function le
return list.append(c)


############################## SESSION03 ##############################
############################## SESSION04 ##############################
"""
you almost never have to loop through sequences using range()
- zip() will zip lists together
Expand Down Expand Up @@ -742,7 +742,7 @@ def sort_key(item):



############################## SESSION04 ##############################
############################## SESSION05 ##############################

""" dealing with "ordered" / "sorted" dicts """

Expand Down Expand Up @@ -794,3 +794,109 @@ def func(*args, **kwargs): # you can recieve an undefined number of args

"""testing in python"""




############################## SESSION06 ##############################

"""Singletons should be tested using 'is'"""

"""Anonymous functions"""

lambda x, y: x + y

"""Functional Programming"""

"""
'self' means the new instance of the class that you just created
"""


############################## SESSION07 ##############################
"""
Personal Project:
- Make sure to use Pep 8
- Make sure to have unit tests
- Make sure to user version control
- Due the Friday after the last class (Due on December 12th)
- Send proposal by next week
"""

"""
What are subclasses for?
- Subclassing is not for specialization
- Subclassing is for reusing code
- Bear in mind that the subclass is in charge. This means keep in mind
that the subclass can change
"""

"""
Multiple Inheritance:
- You can create subclasses from multiple
"""

"""
New-Style Classes:
- when you subclass a class from "object", this is a new style class
- you should always inherit from object
"""

"""
super()
- allows you to call super classes when you are inheriting
"""

"""
properties:
- property
- setters
- deleters
"""

"""
Static Methods:
- a method that doesn't need self to be passed
- however, these are not very useful
"""

"""
Class Methods:
- a method that gets the class object, rather than an instance object, as
the first argument
"""

"""
Special Methods:
- all of the special methods are in the format __methodname__
"""


############################## SESSION08 ##############################

"""
Callable classes:
- a "callable" is anything that you can call like a function (i.e., a class
is a "callable")
- __call__ is the special method that you use to make your call callable

Writing your own sequence type:
- __len__
- __getitem__
- __setitem__
- __delitem__
- __contains__
"""

"""
Iterators:
- every iterator has an __iter__ method (e.g., list.__iter__())
- in order to make your Class an interator (i.e., so you can use it in a
loop), you need the following methods
- __iter__()
- next()
"""

"""
Generators:
- generators give you the iterator immediately
"""
Empty file.
2 changes: 1 addition & 1 deletion Students/salim/session04/kata_14.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def clean_string(s, lowercase = False, punctuation = False):
end_line = 0 # ending line of the file

num_of_words_to_print = 200
start_words = 'I did'
start_words = 'But we'

# read file to list
f = open(path_book)
Expand Down
9 changes: 9 additions & 0 deletions Students/salim/session05/email/Salim Sr.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

Copy link
Contributor

Choose a reason for hiding this comment

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

Note: you generally don't want to add generated files to git....

Dear Salim Sr,

Thank you very much for your generous donation of $101.00. We
appreciate your thoughtfullness and we will make sure your donation
goes to the right cause.

Kind Regards,
Donation Team
9 changes: 9 additions & 0 deletions Students/salim/session05/email/Zeina.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

Dear Zeina,

Thank you very much for your generous donation of $500.00. We
appreciate your thoughtfullness and we will make sure your donation
goes to the right cause.

Kind Regards,
Donation Team
9 changes: 9 additions & 0 deletions Students/salim/session05/email/joanna hamed.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

Dear Joanna Hamed,

Thank you very much for your generous donation of $100.00. We
appreciate your thoughtfullness and we will make sure your donation
goes to the right cause.

Kind Regards,
Donation Team
10 changes: 10 additions & 0 deletions Students/salim/session06/html_render.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/local/bin/python

class Element(object):

def __init__(self, content=None):
self.content = content
def append(self, new_content):
self.content += new_content
def render(self, file_out, ind=""):
pass
5 changes: 5 additions & 0 deletions Students/salim/session06/lambda_keyword_lab.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def incremental(n):
l = []
for i in range(n):
l.append(lambda x, e=i: x + e)
return l
45 changes: 45 additions & 0 deletions Students/salim/session07/circle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!usr/local/bin/python

from math import pi


class Circle(object):
"""Generic Circle class."""

def __init__(self, radius):
self.radius = radius

@property
def diameter(self):
return self.radius * 2.0

@diameter.setter
def diameter(self, value):
self.radius = value / 2.0

@property
def area(self):
return pi * self.radius ** 2

@classmethod
def from_diameter(cls, diameter):
return cls(diameter / 2.0)

def __str__(self):
return 'Circle with radius: {:.2f}'.format(self.radius)

def __repr__(self):
return 'Circle({})'.format(self.radius)

def __add__(self, other):
return Circle(self.radius + other.radius)

def __mul__(self, other):
return Circle(self.radius * other)

def __rmul__(self, other):
return Circle(self.radius * other)

def __cmp__(self, other):
result = float(self.radius) - float(other.radius)
return -1 if result < 0 else 1 if result > 0 else 0
102 changes: 102 additions & 0 deletions Students/salim/session07/circle_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#!usr/local/bin/python
from circle import Circle
from math import pi


def test_circle_class():
c = Circle(2)
assert isinstance(c, Circle)


def test_radius():
c = Circle(2.0)
assert c.radius == 2.0


def test_get_diameter():
c = Circle(2.5)
assert c.diameter == 5.0


def test_set_diameter():
c = Circle(4.3)
c.diameter = 3.0
assert c.radius == 1.5
assert c.diameter == 3.0


def test_area():
c = Circle(10)
assert c.area == pi * 10 ** 2


def test_set_area():
c = Circle(4)
try:
c.area = 10
except AttributeError as error:
assert error.message == "can't set attribute"
else:
assert False


def test_from_diameter():
c = Circle.from_diameter(10)
assert isinstance(c, Circle)
assert c.radius == 5.0


def test_print_circle():
c_int = Circle(3)
c_float = Circle(3.50)
assert str(c_int) == 'Circle with radius: 3.00'
assert str(c_float) == 'Circle with radius: 3.50'


def test_repr():
c = Circle(3)
assert repr(c) == 'Circle(3)'


def test_add():
a = Circle(10)
b = Circle(15)
assert isinstance(a + b, Circle)
assert (a + b).radius == Circle(25).radius


def test_multiply():
a = Circle(10)
c_mult = a * 3
assert isinstance(c_mult, Circle)
assert c_mult.radius == 30

c2_mult = 4 * a
assert isinstance(c2_mult, Circle)
assert c2_mult.radius == 40


def test_compare_circle():
a3 = Circle(3)
b3 = Circle(3)
c5 = Circle(5)
d10 = Circle(10)
e3f = Circle(3.0)
assert not a3 > b3
assert c5 > b3
assert not c5 < b3
assert a3 < d10
assert a3 == b3
assert not d10 == c5


def test_sort():
c_list = [Circle(6), Circle(7), Circle(15), Circle(1), Circle(6.5)]
c_list.sort()

sorted_list = [Circle(1), Circle(6), Circle(6.5), Circle(7), Circle(15)]
assert c_list[0].radius == sorted_list[0].radius
assert c_list[1].radius == sorted_list[1].radius
assert c_list[2].radius == sorted_list[2].radius
assert c_list[3].radius == sorted_list[3].radius
assert c_list[4].radius == sorted_list[4].radius
14 changes: 14 additions & 0 deletions Students/salim/session07/subclassing_notes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Animal(object):
"""Generic animal class"""

def __init__(self, name):
self.name = name

def walk(self):
print ('{} is Walking'.format(self.name))

class Dog(Animal):
"""Man's best friend"""

def bark(self):
print('Woof!')
42 changes: 42 additions & 0 deletions Students/salim/session08/generator_lab.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!usr/local/bin/python


def intsum():
args = [0, 1, 2, 3, 4, 5, 6, 7]
x = 0
for i in args:
yield x + i
x = x + i


def doubler():
args = range(1, 100)
x = 0
for i in args:
yield max([x * 2, 1])
x = max([x * 2, 1])


def fib():
l = [0, 0]
while True:
if sum(l) == 0:
yield 1
l.append(1)
else:
yield sum(l)
l.append(sum(l))
del l[0]


def prime():
num = 1
while True:
num += 1
prime = True
for i in xrange(2, num + 1):
if num % i == 0 and i != num:
prime = False
break
if prime:
yield num
Loading