This commit is contained in:
Will McGugan 2020-03-15 15:46:03 +00:00
parent 82a11b9960
commit d968a7732f
2 changed files with 81 additions and 0 deletions

17
rich/cjk.py Normal file
View File

@ -0,0 +1,17 @@
from rich.console import Console
from rich.panel import Panel
console = Console(width=15)
# console.print(Panel("""这是对亚洲语言支持的测试。面对模棱两可的想法,拒绝猜测的诱惑。""", expand=False,))
# console.print(Panel("""Hello, World!""", width=8, expand=False))
console.print(Panel(""":pile_of_poo::vampire::thumbs_up: """ * 20))
print("x" * 15)
# console = Console(width=15)
# from rich.text import Text
# console.print("[u][b]Where[/b] there is a [i]Will[/i], there is a Way.[/u]")
# print("x" * 15)

View File

@ -0,0 +1,64 @@
from functools import partial
from typing import List
import os.path
from urllib.request import urlopen
from rich.progress import Progress
def download(url: str) -> str:
"""Copy data from a url to a local file."""
# This will break if the response doesn't contain content length
filename = url.rsplit("/")[-1]
if os.path.exists(filename):
print(f"{filename} exists")
return filename
progress = Progress()
task = progress.add_task(filename)
with progress:
response = urlopen(url)
progress.update(task, total=int(response.info()["Content-length"]))
with open(filename, "wb") as dest_file:
for data in iter(partial(response.read, 32768), b""):
dest_file.write(data)
progress.advance(task, len(data))
return filename
def get_data():
east_asian_filename = download(
"http://www.unicode.org/Public/UNIDATA/EastAsianWidth.txt"
)
download(
"http://www.unicode.org/Public/UNIDATA/extracted/DerivedGeneralCategory.txt"
)
print(parse_east_asian(east_asian_filename))
def parse_east_asian(filename: str) -> List[int]:
codepoints: List[int] = []
for line in open(filename, "rt"):
if line.startswith("#") or not line.strip():
continue
print(line)
first_field = line.split()[0]
if ";" not in first_field:
continue
codepoint_range, details = first_field.split(";", 1)
if ".." in codepoint_range:
start, end = codepoint_range.split("..")
codepoints.extend(range(int(start, 16), int(end, 16) + 1))
else:
codepoints.append(int(codepoint_range, 16))
return codepoints
def run():
get_data()
if __name__ == "__main__":
run()