Skip to content

Instantly share code, notes, and snippets.

>>> phone_numbers = { 'Jack': '070-02222748', 'Pete': '010-2488634', 'Eric': '06-10101010' }
>>> list(phone_numbers)
['Jack', 'Pete', 'Eric']
>>> sorted(phone_numbers)
['Eric', 'Jack', 'Pete']
>>> 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
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
@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
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):
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
@eriky
eriky / multiproc.py
Last active February 6, 2021 02:10
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
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")
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
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,