In [1]:
from datetime import datetime
def get_seconds():
"""Return current seconds"""
return datetime.now().second
get_seconds()
Out[1]:
In [2]:
help(get_seconds)
In [3]:
get_seconds.__doc__
Out[3]:
In [4]:
get_seconds.__name__
Out[4]:
In [ ]:
In [5]:
def split_tags(tag_string):
tag_list = []
for tag in tag_string.split(','):
tag_list.append(tag.strip())
return tag_list
split_tags('functions, classes, OOP, inheritance, methods')
Out[5]:
In [ ]:
In [6]:
split_tags()
In [ ]:
In [7]:
def add(x: int, y: int) -> int:
return x + y
print(add(10, 11))
print(add('still ', 'works'))
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [11]:
def extender(source_list, extend_list):
source_list.extend(extend_list)
values = [1, 2, 3]
extender(values, [4, 5, 6])
print(values)
In [12]:
def replacer(source_tuple, replace_with):
source_tuple = replace_with
user_info = ('Guido', '31/01')
replacer(user_info, ('Larry', '27/09'))
print(user_info)
In [ ]:
# mutable vs immutable
In [13]:
def say(greeting, name):
print('{} {}!'.format(greeting, name))
say('Hello', 'Kitty')
say(name='Kitty', greeting='Hello')
In [15]:
result = 0
def increment():
result = 1
return result
result = increment()
In [ ]:
# global & nonlocal
In [17]:
def greeting(name='it\'s me...'):
print('Hello, {}'.format(name))
greeting('Kitty')
print(greeting.__defaults__)
In [18]:
def append_one(iterable=[]):
iterable.append(1)
return iterable
print(append_one([1]))
In [19]:
print(append_one())
print(append_one())
In [20]:
print(append_one.__defaults__)
In [21]:
def function(iterable=None):
if iterable is None:
iterable = []
def function(iterable=None):
iterable = iterable or []
In [26]:
def printer(*args):
print(type(args))
for argument in args:
print(argument)
printer(1, 2, 3, 4, 5)
In [27]:
name_list = ['John', 'Bill', 'Amy', 'Jane']
printer(*name_list)
In [29]:
def printer(**kwargs):
print(type(kwargs))
print(kwargs.keys())
printer(a=10, b=11)
In [30]:
payload = {
'user_id': 117,
'feedback': {
'subject': 'Registration fields',
'message': 'There is no country for old men'
}
}
printer(user_id=117, feedback={
'subject':. ...
})
In [31]:
def caller(func, params):
return func(*params)
def printer(name, origin):
print('I\'m {} of {}!'.format(name, origin))
caller(printer, ['Moana', 'Motunui'])
In [52]:
caller(lambda x: x ** 3, [10])
Out[52]:
In [ ]:
In [32]:
def get_multiplier():
def inner(a, b):
return a * b
return inner
multiplier = get_multiplier()
multiplier(10, 11)
Out[32]:
In [33]:
print(multiplier.__name__)
In [39]:
def get_multiplier(number):
def inner(a):
return a * number
return inner
ten = get_multiplier(10)
two = get_multiplier(2)
two(2), ten(9)
Out[39]:
In [43]:
def squarify(a):
return a ** 2
list(map(squarify, [1, 2, 3, 4]))
Out[43]:
In [ ]:
squared_list = []
for number in range(5):
squared_list.append(squarify(number))
print(squared_list)
In [45]:
def is_positive(a):
return a > 0
list(filter(is_positive, [-3, 4, 5, -10]))
Out[45]:
In [ ]:
positive_list = []
for number in range(-2, 3):
if is_positive(number):
positive_list.append(number)
print(positive_list)
In [47]:
list(map(lambda x: x ** 2, range(5)))
Out[47]:
In [46]:
l = lambda x: x ** 2
Out[46]:
In [48]:
list(filter(lambda x: x > 0, range(-2, 3)))
Out[48]:
In [55]:
def stringify(numbers):
return list(map(str, numbers))
stringify([1, 2, 3, 4])
Out[55]:
In [56]:
from functools import reduce
def multiply(a, b):
return a * b
reduce(multiply, [1, 2, 3, 4, 5])
Out[56]:
In [57]:
reduce(lambda x, y: x * y, range(1, 5))
Out[57]:
In [58]:
from functools import partial
def greeter(person, greeting):
return '{}, {}!'.format(greeting, person)
hier = partial(greeter, greeting='Hi')
helloer = partial(greeter, greeting='Hello')
print(hier('brother'))
print(helloer('sir'))
In [59]:
square_list = []
for number in range(10):
square_list.append(number ** 2)
print(square_list)
In [60]:
square_list = [number ** 2 for number in range(10)]
print(square_list)
In [61]:
even_list = []
for number in range(10):
if number % 2 == 0:
even_list.append(number)
print(even_list)
In [63]:
even_list = [num for num in range(10) if num % 2 == 0]
print(even_list)
In [64]:
square_map = {number: number ** 2 for number in range(5)}
print(square_map)
In [65]:
reminders_set = {num % 10 for num in range(100)}
print(reminders_set)
In [66]:
print(type(number ** 2 for number in range(5)))
In [69]:
num_list = range(7)
squared_list = [x ** 2 for x in num_list]
squared_list = squared_list[:6]
list(zip(num_list, squared_list))
Out[69]:
In [70]:
def decorator(func):
return func
@decorator
def decorated():
print('Hello!')
In [ ]:
decorated = decorator(decorated)
In [ ]:
In [71]:
def decorator(func):
def new_func():
pass
return new_func
@decorator
def decorated():
print('Hello!')
decorated()
In [72]:
print(decorated.__name__)
In [ ]:
def f(func):
pass
def g():
pass
g = f(g)
In [ ]:
In [ ]:
In [ ]:
In [96]:
from functools import wraps
def logger(func):
@wraps(func)
def inner(*args, **kwargs):
result = func(*args, **kwargs)
with open('log.txt', 'w') as f:
f.write(str(result))
return result
return inner
@logger
def summator(numbers):
return sum(numbers)
@logger
def multiply(a, b):
return a * b
In [97]:
multiply.__name__
Out[97]:
In [ ]:
def logger(filename):
def decorator(func):
def inner(*args):
result = func(*args)
with open(filename, 'w') as f:
f.write(str(result))
return result
return inner
return decorator
@logger('new_log.txt')
def summator(numbers):
return sum(numbers)
# summator = logger('new_log.txt')(summator)
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [98]:
def first_decorator(func):
def wrapped():
print('Inside first_decorator product')
return func()
return wrapped
def second_decorator(func):
def wrapped():
print('Inside second_decorator product')
return func()
return wrapped
In [99]:
@first_decorator
@second_decorator
def decorated():
print('Finally called...')
# decorated = first_decorator(second_decorator(decorated))
decorated()
In [ ]:
In [101]:
def bold(func):
def wrapped():
return "<b>" + func() + "</b>"
return wrapped
def italic(func):
def wrapped():
return "<i>" + func() + "</i>"
return wrapped
@bold
@italic
def hello():
return "hello world"
# hello = bold(italic(hello))
print(hello())
In [106]:
isinstance([], list)
Out[106]:
In [ ]:
In [108]:
class DeadSimple:
pass
obj = DeadSimple()
In [ ]:
In [110]:
class User:
def __init__(self, name, email):
self.name = name
self.email = email
bob = User('bob', 'bob@mail.ru')
alice = User('alice', 'alice@mail.ru')
Out[110]:
In [111]:
In [115]:
class Singleton:
obj = None
def __new__(cls, *args, **kwargs):
if cls.obj is None:
cls.obj = object.__new__(cls, *args, **kwargs)
return cls.obj
s = Singleton()
print(s)
new_s = Singleton()
print(new_s)
print(Singleton.obj)
In [ ]:
In [116]:
class ToBeDeleted:
def __del__(self, *args, **kwargs):
print('I\'m gone')
obj = ToBeDeleted()
del obj
In [119]:
class News:
importance = 25
...
class Blog:
importance = 17
def __init__(self, name, moderator):
self.name = name
self.moderator = moderator
lala = Blog('Lalala', 'alice@mail.ru')
lala.importance
Out[119]:
In [ ]:
In [123]:
# lala.importance = 18
# Blog.importance == lala.importance
print(lala.__dict__)
In [ ]:
news.importance
In [124]:
getattr(news, 'name')
Out[124]:
In [ ]:
setattr(news, 'name', 'News!')
news.name
In [ ]:
In [125]:
class Storage:
def __init__(self, amount):
self.__amount = amount
def _calculate():
pass
storage = Storage(100)
storage.__amount
In [126]:
storage.__dict__
Out[126]:
In [127]:
storage._Storage__amount # gotcha!
Out[127]:
In [135]:
class User:
dbtable = 'Users.User'
def __init__(self, name):
self.name = name
self.url = self.get_url()
@staticmethod
def get_url(self):
return '/user/'
alice = User('alice')
bob = User('bob')
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
@staticmethod
def get_url():
return '/user/'
@property
def url(self):
return '/user/'
In [147]:
class Publication:
def __init__(self, title, content):
self.title = title
self.content = content
def render(self):
return '{} — {}'.format(self.title, self.content)
class Post(Publication):
def __init__(self, blog, *args, **kwargs):
self.blog = blog
super().__init__(*args, **kwargs)
post = Post('News', 'Post title', 'Post content')
advert = Advert('Sell', 'Advert title', 'Advert content')
In [155]:
print(post)
In [ ]:
post.render()
In [149]:
isinstance(post, Post)
Out[149]:
In [148]:
isinstance(post, Publication)
Out[148]:
In [150]:
isinstance(post, object)
Out[150]:
In [156]:
isinstance(Publication, object)
Out[156]:
In [157]:
issubclass(Publication, object)
Out[157]:
In [ ]:
In [158]:
class Base:
pass
class A(Base):
pass
class B(Base):
pass
A.mro()
Out[158]:
In [ ]:
In [161]:
class C(B, A):
pass
C.__mro__
Out[161]:
In [ ]:
In [162]:
c = C()
isinstance(c, A)
Out[162]:
In [163]:
isinstance(A(), C)
Out[163]:
In [ ]:
In [ ]:
class JSONable:
def to_json(self):
return json.dumps(self.content)
class BlogEntry(Publication, JSONable):
pass
class Advert(Publication)
In [ ]:
class User(JSONable):
pass
In [ ]:
In [ ]:
user = User()
user.to_json()
In [ ]:
In [ ]:
In [136]:
def average(num_list):
return sum(num_list) / len(num_list)
In [ ]:
In [137]:
average([])
In [ ]:
In [138]:
try:
average([])
except ZeroDivisionError:
print('Error occurred')
In [ ]:
In [139]:
try:
average([])
except ZeroDivisionError as e:
print(e)
In [ ]:
In [ ]:
try:
average([])
except Exception:
print('Avoid this')
In [ ]:
In [144]:
try:
average([])
except ZeroDivisionError as e:
raise e
else:
print('It\'s OK!')
finally:
print('Hello there')
In [ ]:
In [145]:
try:
average([])
except (ValueError, TypeError, ZeroDivisionError):
pass
In [ ]:
In [ ]:
try:
average([])
except ValueError:
print('Value!')
except ZeroDivisionError:
print('Zero!')
In [ ]:
In [ ]:
class ParkException(Exception):
pass
In [ ]:
In [ ]:
raise ValueError('Wrong value')
In [ ]:
In [146]:
try:
1 / 0
except ZeroDivisionError as e:
raise ValueError from e
In [ ]:
def func(a):
assert isinstance(a, int)
func('1')
In [ ]:
assert True, 'I\'m surprised'
In [ ]:
assert False, 'What did you expect?'
In [ ]:
In [ ]:
import random
assert random.randint(1, 5) == 3
print('The End')