Proofreading avancé.

This commit is contained in:
Julien Palard 2021-05-31 13:46:02 +02:00
parent 32ad841e45
commit f8334d4499
Signed by: mdk
GPG Key ID: 0EFC1AC1006886F8
9 changed files with 3165 additions and 506 deletions

View File

@ -223,7 +223,7 @@ Notes:
## contextlib
- `@suppress`
- `with suppress:`
- `@contextmanager`

View File

@ -29,14 +29,23 @@ Notes:
Jamais `sudo`, toujours dans un `venv`.
## pip install -e .
## pyproject.toml + setup.cfg
Notes:
En attendant l'implémentation de la PEP 621.
Probablement celui qu'on utilise le plus souvent.
## pip install .
## Packager
- Source dist
- Binary dist
```bash
pip install build
python -m build
```
### Publier
```bash
pip instqll twine
twine upload dist/*
```

224
avancé/6-coro.md Normal file
View File

@ -0,0 +1,224 @@
# async / await
Une coroutine est une fonction dont l'exécution peut être suspendue.
## Callback Hell
```
function pong_handler(client)
{
client.on('data', function (data)
{
client.on('data_written', function ()
{
client.close()
});
client.write(data)
client.flush()
});
}
```
## Avec des coroutines
```python
async def pong_handler():
client.write(await client.read())
await client.flush()
client.close()
```
## Les coroutines
- generator-based coroutines
- native coroutines
## Generator-based coroutines
```python
@types.coroutine
def get_then_print(url):
...
```
## Native coroutines
```python
async def get_then_print(url):
...
```
## Coroutines
Une `coroutine`, renvoie un objet `coroutine` :
```
>>> async def tum():
... print("tum")
...
>>> tum()
<coroutine object tum at 0x7fa294538468>
```
## Coroutines
```
>>> async def tum():
... print("tum")
...
>>> a_coroutine_object = tum()
>>> a_coroutine_object.send(None)
tum
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
```
Notes:
qu'on peut manipuler.
As you can see, calling `tum()` did not execute the `print("tum")`,
but calling `.send(None)` did (see PEP 342).
L'appel de .send est fait par la main loop (asyncio.run).
## Récupérer un résultat
Le résultat d'une coroutine est stocké dans l'exception `StopIteration`.
Notes:
Dans l'attribut `value`.
## await
```
async def two():
return 2
async def four():
return await two() + await two()
coro = four()
coro.send(None)
```
Notes:
Ça donne `StopIteration: 4`, de manière complètement synchrone.
## Suspendre une coroutine.
Ce n'est pas possible dans une coroutine.
Notes:
Bon, à part `await asyncio.sleep(0)`, ou toute attente vers un
awaitable qui se suspend sans rien faire.
## Future-like object
Un `future-like object` est un object implémentant `__await__`, qui a
le droit de `yield`. L'expression du yield traversera toute la stack
d'`await` jusqu'au `send(None)`.
## Awaitables
Les [awaitables](https://www.python.org/dev/peps/pep-0492/#await-expression)
sont des objets pouvant être « attendus » via un `await`.
Notes:
Typiquement `coroutine` ou un objet implémentant `__await__`.
## Gérer ses coroutines
```python
async def two():
return 2
async def four():
return await two() + await two()
def coro_manager(coro):
try:
coro.send(None)
except StopIteration as stop:
return stop.value
print(coro_manager(four()))
```
## Gérer ses coroutines
```python
class Awaitable:
def __await__(self):
yield
async def wont_terminate_here():
await Awaitable()
print("Terminated")
return 42
print(coro_manager(wont_terminate_here()))
```
## Gérer ses coroutines
```
def frenetic_coro_manager(coro):
try:
while True:
coro.send(None)
except StopIteration as stop:
return stop.value
```
## Gérer ses coroutines
```python
def frenetic_coros_manager(*coros):
coros = list(coros)
while coros:
coro = random.choice(coros)
try:
coro.send(None)
except StopIteration as stop:
coros.remove(coro)
```
## Gérer ses coroutines
```python
async def tum():
while True:
await Awaitable()
print("Tum")
async def pak():
while True:
await Awaitable()
print("Pak")
frenetic_coro_manager(tum(), pak())
```

Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 108 KiB

2
avancé/static/fifix.css Normal file
View File

@ -0,0 +1,2 @@
.reveal section img { border: none; }
.reveal pre { width: 100%; font-size: .9em;}

160
avancé/static/github.css Normal file
View File

@ -0,0 +1,160 @@
a.sourceLine { display: inline-block; line-height: 1.25; }
a.sourceLine { pointer-events: none; color: inherit; text-decoration: inherit; }
a.sourceLine:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode { white-space: pre; position: relative; }
div.sourceCode {
margin: 1em 0;
color: #333;
background: #f8f8f8;
box-shadow: 0px 5px 15px rgba(0, 0, 0, 0.15);
}
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
code.sourceCode { white-space: pre-wrap; }
a.sourceLine { text-indent: -1em; padding-left: 1em; }
}
pre.numberSource a.sourceLine
{ position: relative; left: -4em; }
pre.numberSource a.sourceLine::before
{ content: attr(title);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; pointer-events: all; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
a.sourceLine::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #333; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #998; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #008080; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #008080; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #333; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #008080; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #d14; } /* String */
code span.va { color: #008080; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
.hljs {
display: block;
overflow-x: auto;
padding: 0.5em;
color: #333;
background: #f8f8f8;
}
.hljs-comment,
.hljs-quote {
color: #998;
font-style: italic;
}
.hljs-keyword,
.hljs-selector-tag,
.hljs-subst {
color: #333;
font-weight: bold;
}
.hljs-number,
.hljs-literal,
.hljs-variable,
.hljs-template-variable,
.hljs-tag .hljs-attr {
color: #008080;
}
.hljs-string,
.hljs-doctag {
color: #d14;
}
.hljs-title,
.hljs-section,
.hljs-selector-id {
color: #900;
font-weight: bold;
}
.hljs-subst {
font-weight: normal;
}
.hljs-type,
.hljs-class .hljs-title {
color: #458;
font-weight: bold;
}
.hljs-tag,
.hljs-name,
.hljs-attribute {
color: #000080;
font-weight: normal;
}
.hljs-regexp,
.hljs-link {
color: #009926;
}
.hljs-symbol,
.hljs-bullet {
color: #990073;
}
.hljs-built_in,
.hljs-builtin-name {
color: #0086b3;
}
.hljs-meta {
color: #999;
font-weight: bold;
}
.hljs-deletion {
background: #fdd;
}
.hljs-addition {
background: #dfd;
}
.hljs-emphasis {
font-style: italic;
}
.hljs-strong {
font-weight: bold;
}

File diff suppressed because one or more lines are too long