Skip to content

Commit

Permalink
Merge pull request #1 from levops-cloud/master
Browse files Browse the repository at this point in the history
pull req
  • Loading branch information
costyaDev authored Nov 30, 2020
2 parents 30cefd7 + 3cd6e96 commit d6ddf5e
Show file tree
Hide file tree
Showing 25 changed files with 273,471 additions and 104 deletions.
6 changes: 6 additions & 0 deletions OOP/dog.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ def __init__(self, name, age):
self.name = name
self.age = age

def speak(self, sound):
return f"{self.name} says {sound}"


# Instantiate the Dog object
philo = Dog("Philo", 5)
Expand All @@ -27,3 +30,6 @@ def __init__(self, name, age):
# Is Philo a mammal?
if philo.species == "mammal":
print(f"{philo.name} is a {philo.species}!")

print(philo.speak("Woof Woof"))
print(mikey.speak("Waf Waf"))
40 changes: 0 additions & 40 deletions OOP/dog_inheritance.py

This file was deleted.

27 changes: 0 additions & 27 deletions OOP/dog_sol.py

This file was deleted.

25 changes: 25 additions & 0 deletions OOP/encapsulation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Using OOP in Python, we can restrict access to methods and variables.
# This prevents data from direct modification which is called encapsulation.
# In Python, we denote private attributes using underscore as the prefix i.e single _ or double __.

class Computer:

def __init__(self):
self.__maxprice = 900

def sell(self):
print("Selling Price: {}".format(self.__maxprice))

def setMaxPrice(self, price):
self.__maxprice = price

c = Computer()
c.sell()

# change the price
c.__maxprice = 1000
c.sell()

# using setter function
c.setMaxPrice(1000)
c.sell()
30 changes: 30 additions & 0 deletions OOP/inheritance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# parent class
class Bird:

def __init__(self):
print("Bird is ready")

def whoisThis(self):
print("Bird")

def swim(self):
print("Swim faster")

# child class
class Penguin(Bird):

def __init__(self):
# call super() function
super().__init__()
print("Penguin is ready")

def whoisThis(self):
print("Penguin")

def run(self):
print("Run faster")

peggy = Penguin()
peggy.whoisThis()
peggy.swim()
peggy.run()
27 changes: 27 additions & 0 deletions OOP/polymorphism.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Parrot:

def fly(self):
print("Parrot can fly")

def swim(self):
print("Parrot can't swim")

class Penguin:

def fly(self):
print("Penguin can't fly")

def swim(self):
print("Penguin can swim")

# common interface
def flying_test(bird):
bird.fly()

#instantiate objects
blu = Parrot()
peggy = Penguin()

# passing the object
flying_test(blu)
flying_test(peggy)
16 changes: 16 additions & 0 deletions closures/closure.func.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
def print_msg(msg):
# This is the outer enclosing function

def printer():
# This is the nested function
print(msg)

return printer # returns the nested function


# Now let's try calling this function.
# Output: Hello
another = print_msg("Hello")
another()


20 changes: 20 additions & 0 deletions closures/closure.insted.class.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
def make_multiplier_of(n):
def multiplier(x):
return x * n
return multiplier


# Multiplier of 3
times3 = make_multiplier_of(3)

# Multiplier of 5
times5 = make_multiplier_of(5)

# Output: 27
print(times3(9))

# Output: 15
print(times5(3))

# Output: 30
print(times5(times3(2)))
14 changes: 14 additions & 0 deletions closures/nested.function.non-local.var.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
def print_msg(msg):
# This is the outer enclosing function

def printer():
# This is the nested function
print(msg)

printer()

# We execute the function
# Output: Hello
print_msg("Hello")


39 changes: 39 additions & 0 deletions decorators/syntax.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# This is not a valid Python module - Don't run it.

# ONE DECORATOR
def func(arg1, arg2, ...):
pass

func = decorator(func)

# is equivalent to the following:

@decorator
def func(arg1, arg2, ...):
pass


# TWO DECORATORS
def func(arg1, arg2, ...):
pass

func = deco1(deco2(func))

# is equivalent to the following:

@deco1
@deco2
def func(arg1, arg2, ...):
pass

# DECORATOR WITH ARGUMENTS
def func(arg1, arg2, ...):
pass

func = decoarg(arg_a, arg_b)(func)

# is equivalent to the following:

@decoarg(arg_a, arg_b)
def func(arg1, arg2, ...):
pass
21 changes: 21 additions & 0 deletions decorators/time.measure.deco2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from time import sleep, time
from functools import wraps


def measure(func):
@wraps(func)
def wrapper(*args, **kwargs):
t = time()
func(*args, **kwargs)
print(func.__name__, 'took:', time() - t)
return wrapper


@measure
def f(sleep_time=0.1):
"""I'm a cat. I love to sleep! """
sleep(sleep_time)


f(sleep_time=0.3) # f took: 0.3010902404785156
print(f.__name__, ':', f.__doc__) # f : I'm a cat. I love to sleep!
16 changes: 16 additions & 0 deletions decorators/time.measure.dry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# whole timing mechanism has been encapsulated into
# a function so we don't repeat code.

from time import sleep, time

def f():
sleep(.3)

def g():
sleep(.5)

def measure(func):
t = time()
func()

print(func.__name__, 'took:', time() - t)
14 changes: 14 additions & 0 deletions decorators/time.measure.start.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from time import sleep, time

def f():
sleep(.3)

def g():
sleep(.5)

t = time()
f()
print('f took: ', time() - t) # f took: 0.3003859519958496
t = time()
g()
print('g took:', time() - t) # g took: 0.5005719661712646
17 changes: 17 additions & 0 deletions decorators/time.measute.deco1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from time import sleep, time

def f(sleep_time=0.1):
sleep(sleep_time)

def measure(func):
def wrapper(*args, **kwargs):
t = time()
func(*args, **kwargs)
print(func.__name__, 'took:', time() - t)
return wrapper

f = measure(f) # decoration point

f(0.2) # f took: 0.2002875804901123
f(sleep_time=0.3) # f took: 0.3003721237182617
print(f.__name__) # wrapper <- ouch!
29 changes: 29 additions & 0 deletions decorators/two.decorators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from time import sleep, time
from functools import wraps

def measure(func):
@wraps(func)
def wrapper(*args, **kwargs):
t = time()
result = func(*args, **kwargs)
print(func.__name__, 'took:', time() - t)
return result
return wrapper

def max_result(func):
@wraps(func)
def wrapper(*args, **kwargs):
result = func(*args, **kwargs)
if result > 100:
print('Result is too big ({0}). Max allowed is 100.'
.format(result))
return result
return wrapper

@measure
@max_result
def cube(n):
return n ** 3

print(cube(2))
print(cube(5))
Loading

0 comments on commit d6ddf5e

Please sign in to comment.