-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from levops-cloud/master
pull req
- Loading branch information
Showing
25 changed files
with
273,471 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
Oops, something went wrong.