Skip to content

Commit 063a540

Browse files
committed
ch22: new README.md and minor edits
1 parent eee989c commit 063a540

File tree

5 files changed

+51
-12
lines changed

5 files changed

+51
-12
lines changed

22-async/mojifinder/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Mojifinder: Unicode character search examples
2+
3+
Examples from _Fluent Python, Second Edition_—Chapter 22, _Asynchronous Programming_.
4+
5+
## How to run `web_mojifinder.py`
6+
7+
`web_mojifinder.py` is a Web application built with _[FastAPI](https://fastapi.tiangolo.com/)_.
8+
To run it, first install _FastAPI_ and an ASGI server.
9+
The application was tested with _[Uvicorn](https://www.uvicorn.org/)_.
10+
11+
```
12+
$ pip install fastapi uvicorn
13+
```
14+
15+
Now you can use `uvicorn` to run the app.
16+
17+
```
18+
$ uvicorn web_mojifinder:app
19+
```
20+
21+
Finally, visit http://127.0.0.1:8000/ with your browser to see the search form.
22+
23+
24+
## Directory contents
25+
26+
These files can be run as scripts directly from the command line:
27+
28+
- `charindex.py`: libray used by the Mojifinder examples. Also works as CLI search script.
29+
- `tcp_mojifinder.py`: TCP/IP Unicode search server. Depends only on the Python 3.9 standard library. Use a telnet application as client.
30+
- `web_mojifinder_bottle.py`: Unicode Web service. Depends on `bottle.py` and `static/form.html`. Use an HTTP browser as client.
31+
32+
This program requires an ASGI server to run it:
33+
34+
- `web_mojifinder.py`: Unicode Web service. Depends on _[FastAPI](https://fastapi.tiangolo.com/)_ and `static/form.html`.
35+
36+
Support files:
37+
38+
- `bottle.py`: local copy of the single-file _[Bottle](https://bottlepy.org/)_ Web framework.
39+
- `requirements.txt`: list of dependencies for `web_mojifinder.py`.
40+
- `static/form.html`: HTML form used by the `web_*` examples.
41+
- `README.md`: this file!

22-async/mojifinder/bottle.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from __future__ import with_statement
1717

1818
__author__ = 'Marcel Hellkamp'
19-
__version__ = '0.12.18'
19+
__version__ = '0.12.19'
2020
__license__ = 'MIT'
2121

2222
# The gevent server adapter needs to patch some modules before they are imported
@@ -440,7 +440,7 @@ def match(self, environ):
440440
nocheck = set(methods)
441441
for method in set(self.static) - nocheck:
442442
if path in self.static[method]:
443-
allowed.add(verb)
443+
allowed.add(method)
444444
for method in set(self.dyna_regexes) - allowed - nocheck:
445445
for combined, rules in self.dyna_regexes[method]:
446446
match = combined(path)
@@ -2585,7 +2585,7 @@ def parse_range_header(header, maxlen=0):
25852585

25862586
def _parse_qsl(qs):
25872587
r = []
2588-
for pair in qs.replace(';','&').split('&'):
2588+
for pair in qs.split('&'):
25892589
if not pair: continue
25902590
nv = pair.split('=', 1)
25912591
if len(nv) != 2: nv.append('')
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
click==7.1.2
22
fastapi==0.63.0
33
h11==0.12.0
4-
pydantic==1.7.3
4+
pydantic==1.8.1
55
starlette==0.13.6
6+
typing-extensions==3.7.4.3
67
uvicorn==0.13.4

22-async/mojifinder/web_mojifinder.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import pathlib
1+
from pathlib import Path
22
from unicodedata import name
33

44
from fastapi import FastAPI
@@ -18,9 +18,8 @@ class CharName(BaseModel): # <2>
1818

1919
def init(app): # <3>
2020
app.state.index = InvertedIndex()
21-
static = pathlib.Path(__file__).parent.absolute() / 'static' # <4>
22-
with open(static / 'form.html') as fp:
23-
app.state.form = fp.read()
21+
static = Path(__file__).parent.absolute() / 'static' # <4>
22+
app.state.form = (static / 'form.html').read_text()
2423

2524
init(app) # <5>
2625

@@ -33,4 +32,4 @@ async def search(q: str): # <7>
3332
def form(): # <9>
3433
return app.state.form
3534

36-
# no main funcion # <10>
35+
# no main funcion # <10>

22-async/mojifinder/web_mojifinder_bottle.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
@route('/')
1313
def form():
14-
return static_file('form.html', root = 'static/')
14+
return static_file('form.html', root='static/')
1515

1616

1717
@route('/search')
@@ -28,10 +28,8 @@ def search():
2828
def main(port):
2929
global index
3030
index = InvertedIndex()
31-
host = 'localhost'
3231
run(host='localhost', port=port, debug=True)
3332

3433

3534
if __name__ == '__main__':
3635
main(8000)
37-

0 commit comments

Comments
 (0)