コード例 #1
0
from charfinder import UnicodeNameIndex

TEMPLATE_NAME = 'http_charfinder.html'
CONTENT_TYPE = 'text/html; charset=UTF-8'
SAMPLE_WORDS = ('bismillah chess cat circled Malayalam digit'
                ' Roman face Ethiopic black mark symbol dot'
                ' operator Braille hexagram').split()

ROW_TPL = '<tr><td>{code_str}</td><th>{char}</th><td>{name}</td></tr>'
LINK_TPL = '<a href="/?query={0}" title="find &quot;{0}&quot;">{0}</a>'
LINKS_HTML = ', '.join(LINK_TPL.format(word) for word in
                       sorted(SAMPLE_WORDS, key=str.upper))


index = UnicodeNameIndex()
with open(TEMPLATE_NAME) as tpl:
    template = tpl.read()
template = template.replace('{links}', LINKS_HTML)

# BEGIN HTTP_CHARFINDER_HOME
def home(request):  # <1>
    query = request.GET.get('query', '').strip()  # <2>
    print('Query: {!r}'.format(query))  # <3>
    if query:  # <4>
        descriptions = list(index.find_descriptions(query))
        res = '\n'.join(ROW_TPL.format(**descr._asdict())
                        for descr in descriptions)
        msg = index.status(query, len(descriptions))
    else:
        descriptions = []
コード例 #2
0
ファイル: test_charfinder.py プロジェクト: ZoroOP/C-and-CPP
def test_index():
    sample_index = UnicodeNameIndex(sample_chars)
    assert len(sample_index) == 9
コード例 #3
0
ファイル: test_charfinder.py プロジェクト: ZoroOP/C-and-CPP
def sample_index():
    return UnicodeNameIndex(sample_chars)
コード例 #4
0
def load_index():
    import time
    global index
    index = UnicodeNameIndex()
    time.sleep(2)
コード例 #5
0
ファイル: test_charfinder.py プロジェクト: ZoroOP/C-and-CPP
def full_index():
    return UnicodeNameIndex()
コード例 #6
0
def test_find_word_match_2():
    result = UnicodeNameIndex().find_chars('sign')
    assert result.items[:2] == ['#', '$']
コード例 #7
0
def test_find_description_match_2():
    result = UnicodeNameIndex().find_descriptions('sign', 1, 3)
    result = list(result)
    assert result[0].char == '$'
    assert result[1].char == '%'
コード例 #8
0
def test_find_word_match_1():
    result = UnicodeNameIndex().find_chars('chess black')
    assert result.count == 6
コード例 #9
0
def test_find_no_word_match():
    result = UnicodeNameIndex().find_chars('kdjshfkjsd')
    assert result.count == 0
コード例 #10
0
def test_index():
    index = UnicodeNameIndex().index
    assert len(index) > 10000
コード例 #11
0
ファイル: jingu.py プロジェクト: kibitzing/FluentPython
    DESCRIPTION:
        implementation and comments of code 18-14
        quite not sure with TCP server usage
"""

import sys
import asyncio
# should import charfinder from https://github.com/fluentpython/example-code/blob/master/18-asyncio/charfinder/charfinder.py

from charfinder import UnicodeNameIndex  # UnicodeNameIndex: 문자명 인덱스 생성, 쿼리 메서드 제공

CRLF = b'\r\n'
PROMPT = b'?> '

index = UnicodeNameIndex(
)  # 객체 생성할 떄 기존 charfinder_index.pickle이 있으면 사용, 없으면 인덱스 빌드 -> 수초 소


@asyncio.coroutine
def handle_queries(reader, writer):  # 서버에 전달할 코루틴
    while True:  # 제어문자 올때 까지 무한 루프
        writer.write(PROMPT)  # not coroutine
        yield from writer.drain()  # is coroutine

        data = yield from reader.readline()  # is coroutine

        try:
            query = data.decode().strip()
        except UnicodeDecodeError:
            query = '\x00'  # null, 간편함을 위한 트릭
コード例 #12
0
import asyncio
import sys

from charfinder import UnicodeNameIndex  # 用于构建名称索引,提供查询方法

CRLF = b'\r\n'
PROMPT = b'?> '

index = UnicodeNameIndex(
)  # 实例化 UnicodeNameIndex 类时,会使用 charfinder_index.pickle 文件(如果存在),或者构建这个文件,因此第一次运行时要等几秒钟服务器才会启动


async def handle_queries(
    reader, writer
):  # 这个协程要传给 asyncio.start_server 函数,接收的两个参数是 asyncio.StreamReader 对象和 asyncio.StreamWriter 对象
    while True:  # 循环处理会话,直到从客户端接收到控制字符后退出
        writer.write(PROMPT)  # StreamWriter.write 方法不是协程,只是普通函数;这行代码发送 ?> 提示符
        await writer.drain()  # StreamWriter.drain 方法刷新 writer 缓冲,是协程
        data = await reader.readline(
        )  # StreamReader.readline 方法是协程,返回一个 bytes 对象
        try:
            query = data.decode().strip()  # 默认编码 utf8
        except UnicodeDecodeError:
            query = '\x00'
        client = writer.get_extra_info('peername')  # 返回与套接字连接的远程地址
        print('Received from {}: {!r}'.format(client, query))  # 在服务器控制台中记录查询
        if query:
            if ord(query[:1]) < 32:
                break  # 如果收到控制字符或空字符,退出循环
            lines = list(
                index.find_description_strs(query)