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
>>> phone_numbers = { 'Jack': '070-02222748', 'Pete': '010-2488634', 'Eric': '06-10101010' } | |
>>> list(phone_numbers) | |
['Jack', 'Pete', 'Eric'] | |
>>> sorted(phone_numbers) | |
['Eric', 'Jack', 'Pete'] |
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
>>> phone_numbers.items() | |
dict_items([('Jack', '070-02222748'), ('Pete', '010-2488634'), ('Eric', '06-10101010')]) | |
>>> for name, phonenr in phone_numbers.items(): | |
... print(name, ":", phonenr) | |
... | |
Jack : 070-02222748 | |
Pete : 010-2488634 | |
Eric : 06-10101010 |
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
def print_argument(func): | |
def wrapper(the_number): | |
print("Argument for", func.__name__, "is", the_number) | |
return func(the_number) | |
return wrapper | |
@print_argument | |
def add_one(x): | |
return x + 1 |
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
@attrs | |
class Person(object): | |
name = attrib(default='John') | |
surname = attrib(default='Doe') | |
age = attrib(init=False) | |
p = Person() | |
print(p) | |
p = Person('Bill', 'Gates') | |
p.age = 60 |
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
import threading | |
import time | |
# An I/O intensive calculation. | |
# We simulate it with sleep. | |
def heavy(n, myid): | |
time.sleep(2) | |
print(myid, "is done") | |
def threaded(n): |
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
import time | |
import multiprocessing | |
# A CPU heavy calculation, just | |
# as an example. This can be | |
# anything you like | |
def heavy(n, myid): | |
for x in range(1, n): | |
for y in range(1, n): | |
x**y |
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
import time | |
import multiprocessing | |
# A CPU heavy calculation, just | |
# as an example. This can be | |
# anything you like | |
def heavy(n, myid): | |
for x in range(1, n): | |
for y in range(1, n): | |
x**y |
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
import time | |
# A CPU heavy calculation, just | |
# as an example. This can be | |
# anything you like | |
def heavy(n, myid): | |
for x in range(1, n): | |
for y in range(1, n): | |
x**y | |
print(myid, "is done") |
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
import threading | |
import time | |
# A CPU heavy calculation, just | |
# as an example. This can be | |
# anything you like | |
def heavy(n, myid): | |
for x in range(1, n): | |
for y in range(1, n): | |
x**y |
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
from concurrent.futures import ThreadPoolExecutor | |
from time import sleep | |
def return_after_5_secs(message): | |
sleep(5) | |
return message | |
pool = ThreadPoolExecutor(3) | |
future = pool.submit(return_after_5_secs, |
NewerOlder