In [1]:
%%html
<style>
.text_cell_render * {
font-family: OfficinaSansCTT;
}
.reveal code {
font-family: OfficinaSansCTT;
}
.text_cell_render h3 {
font-family: OfficinaSansCTT;
}
.reveal section img {
max-height: 500px;
margin-left: auto;
margin-right: auto;
}
</style>
In [3]:
import yaml
import random
with open("answers.yaml", "r") as conf:
config = yaml.load(conf)
def get_answer(message):
lower_msg = message.lower()
for key in config['answers']:
if key in lower_msg:
return random.choice(config['answers'][key])
In [ ]:
In [10]:
import random
import threading
import time
class SleepThread(threading.Thread):
def __init__(self, num):
super().__init__()
self.num = num
def run(self):
time.sleep(self.num)
print(self.num)
a = [random.randint(0, 10) for _ in range(10)]
threads = [SleepThread(i) for i in a]
for t in threads:
t.start()
In [11]:
def sleep_print(num):
time.sleep(num)
print(num)
a = [random.randint(0, 10) for _ in range(10)]
threads = [
threading.Thread(target=sleep_print, args=(i,))
for i in a
]
for t in threads:
t.start()
In [12]:
import concurrent.futures as cf
def hold_my_beer(num):
time.sleep(num)
return num
a = [random.randint(0, 10) for _ in range(10)]
with cf.ThreadPoolExecutor(max_workers=len(a)) as pool:
for future in cf.as_completed([
pool.submit(hold_my_beer, i) for i in a
]):
print(future.result())
In [ ]:
import asyncio
asyncio.Queue() # асинхронная очередь
asyncio.sleep(10) # асинхронный "сон"
asyncio.create_subprocess_exec() # асинхронный subprocess
asyncio.Lock() # асинхронный мьютекс
asyncio.ensure_future() # ручное добавление корутины в event loop
asyncio.gather() # дождаться окончания работы списка корутин
In [ ]:
In [13]:
import asyncio
async def hello(name):
return "Hello, {}!".format(name)
In [14]:
hello("Vasya")
Out[14]:
In [15]:
await hello("Vasya")
In [16]:
import asyncio
async def hello(name):
return "Hello, {}!".format(name)
async def call_vasya():
greeting = await hello("Vasya")
return greeting
loop = asyncio.get_event_loop()
print(loop.run_until_complete(call_vasya()))
In [ ]:
In [ ]:
import asyncio
import random
async def hold(num):
await asyncio.sleep(num)
return num
a = [random.randint(0, 10) for _ in range(10)]
~$ pip install django
~$ django-admin startproject mysite
~$ python manage.py runserver
~$ python manage.py startapp hello
In [ ]:
# hello/views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello!")
In [ ]:
# hello/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
In [ ]:
# urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^hello/', include('hello.urls')),
url(r'^admin/', admin.site.urls),
]
In [ ]:
# Добавляем в settings.py строчки
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static")
]
# а также похожую строчку в TEMPLATES["DIRS"]:
os.path.join(BASE_DIR, "templates")
In [ ]:
# а в urls.py делаем так:
from django.conf import settings
from django.conf.urls import include, url
from django.conf.urls.static import static
from django.contrib import admin
urlpatterns = [
url(r'^hello/', include('hello.urls')),
url(r'^admin/', admin.site.urls),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
In [ ]:
# hello/views.py
from django.http import HttpResponse
from django.shortcuts import render
def index(request):
return render(request, 'index.html', {})
In [ ]:
# my_application.py
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello, World!"
~$ FLASK_APP=my_application.py flask run
In [ ]: