日本郵政的 CSV 資料

看到「Parsing the Infamous Japanese Postal CSV」這篇在抱怨日本郵政提供的 CSV 資料超級糟糕的問題覺得頗「有趣」的,在 Hacker News 上也有人「同病相憐」XDDD:「Parsing the Infamous Japanese Postal CSV (dampfkraft.com)」。

文章作者是「posuto」這個套件的維護人,這個套件讓開發者可以很方便的在 Python 下從郵遞區號推出對應的地點資訊:

import posuto as 〒

? = 〒.get('〒105-0011')

print(?)
# "東京都港区芝公園"
print(?.prefecture)
# "東京都"
print(?.kana)
# "トウキョウトミナトクシバコウエン"
print(?.romaji)
# "Tokyo To, Minato Ku, Shibakoen"
print(?.note)
# None

作者建立的資料是從「読み仮名データの促音・拗音を小書きで表記するもの(zip形式)」這邊取得並且分析,然後這篇文章就是在描述這些 CSV 檔的資料裡面有超級多奇怪的例外,用機器讀取超哭爸的 XDDD (好像不怎麼意外?)

不過最有趣的應該還是他提到的這個:

Oh, and if you need a Win3.1 or DOS program to copy the data onto an IBM H floppy disk, just check the bottom of JP Post's page - they've got you covered.

看起來是頁面下方的這塊:

這就真的太哭爸了啊 XDDD

Python 3.9.0 以及 3.10.0a1

Python 3.9.0 出了:「Python 3.9.0 is now available, and you can already test 3.10.0a1!」。

下一個版本是 3.10 而非 Python 4:

OK, boring! Where is Python 4?

Not so fast! The next release after 3.9 will be 3.10. It will be an incremental improvement over 3.9, just as 3.9 was over 3.8, and so on.

在「What’s New In Python 3.9」裡面的「Optimizations」這段有測一些基本操作的效能,可以看到 Python 3.9 其實比 Python 3.8 慢了一些?好像也沒有提到原因就是了...

Facebook 放出 Pysa,靜態分析 Python 程式碼的工具

Facebook 丟出來的靜態分析工具,可以拿來分析 Python 程式碼:「Pysa: An open source static analysis tool to detect and prevent security issues in Python code」,專案在「facebook/pyre-check」這邊可以取得。

不過軟體居然是用 OCaml 寫的啊,另外已經包好了,可以用 pip 直接裝 pyre-check

官方的說明裡面有提到要裝 watchman,不過這算是選擇性安裝,不裝 watchman 直接執行也可以用,只是會跳個訊息跟你說裝了可以遞增檢查:

To enable pyre incremental, you can install watchman: https://facebook.github.io/watchman/docs/install

最近在寫的專案都是用 Python,剛好可以拿來掃這些專案...

Django 3.1

看到「Django 3.1 Released」這邊的公告,比較完整的改變可以在「Django 3.1 release notes」這邊翻到。

這應該是第一次要把手上的專案跳 minor version 升級,看起來應該是還好,但天曉得會有什麼狀況... 看起來主要會是 sha1 要被換成 sha256 會有影響到。

另外看到這個:

SimpleTestCase now implements the debug() method to allow running a test without collecting the result and catching exceptions. This can be used to support running tests under a debugger.

看起來應該也蠻有用的,可以玩看看...

Python 上的 OCR

這個 OCR 專案是在 Python 包好,讓你很快就可以上手用:「Easy OCR」。

從結果的 screenshot 可以看到輸出的內容很簡單,就是座標與 OCR 出來的內容:

然後支援的語言很多:

We are currently supporting following 42 languages.

Afrikaans (af), Azerbaijani (az), Bosnian (bs), Simplified Chinese (ch_sim), Traditional Chinese (ch_tra), Czech (cs), Welsh (cy), Danish (da), German (de), English (en), Spanish (es), Estonian (et), French (fr), Irish (ga), Croatian (hr), Hungarian (hu), Indonesian (id), Icelandic (is), Italian (it), Japanese (ja), Korean (ko), Kurdish (ku), Latin (la), Lithuanian (lt), Latvian (lv), Maori (mi), Malay (ms), Maltese (mt), Dutch (nl), Norwegian (no), Polish (pl), Portuguese (pt),Romanian (ro), Slovak (sk) (need revisit), Slovenian (sl), Albanian (sq), Swedish (sv),Swahili (sw), Thai (th), Tagalog (tl), Turkish (tr), Uzbek (uz), Vietnamese (vi)

有些參數可以調整,但預設值似乎就跑得不錯了...

在瀏覽器內跑 Python

看到「A Python 3 implementation for client-side web programming」這個專案,在瀏覽器裡把 Python 程式碼用 <script type="text/plain"></script> 包起來,然後掛個 js 進去,就可以在瀏覽器裡面跑 Python 操作 DOM:

Brython is designed to replace Javascript as the scripting language for the Web. As such, it is a Python 3 implementation (you can take it for a test drive through a web console), adapted to the HTML5 environment, that is to say with an interface to the DOM objects and events.

另外在 Brython 3.8.9 performance compared to CPython 3.8.0 這頁也有跟 CPython 的比較 (看起來是用 Firefox 測的),其實速度看起來不慢?我猜是 JS 這邊的軍備競賽把整個引擎弄的超快 XDDD

讓 Python 輸出變豐富的 Rich

Hacker News 上看到的 Python 專案,讓 terminal 輸出變得更好看:「Rich is a Python library for rich text and beautiful formatting in the terminal.」。

看到當下吸引我的地方在於表格:

from rich.console import Console
from rich.table import Column, Table

console = Console()

table = Table(show_header=True, header_style="bold magenta")
table.add_column("Date", style="dim", width=12)
table.add_column("Title")
table.add_column("Production Budget", justify="right")
table.add_column("Box Office", justify="right")
table.add_row(
    "Dev 20, 2019", "Star Wars: The Rise of Skywalker", "$275,000,000", "$375,126,118"
)
table.add_row(
    "May 25, 2018",
    "[red]Solo[/red]: A Star Wars Story",
    "$275,000,000",
    "$393,151,347",
)
table.add_row(
    "Dec 15, 2017",
    "Star Wars Ep. VIII: The Last Jedi",
    "$262,000,000",
    "[bold]$1,332,539,889[/bold]",
)

console.print(table)

輸出長這樣:

另外還有不少功能也不錯,會讓畫面豐富不少。

用 Python 刻 GUI 的 guietta

Hacker News Daily 翻到的奇怪工具 guietta,可以在 Python 下用奇怪的方法刻 GUI,範例就很好解釋了調性:

from guietta import _, Gui, Quit

gui = Gui(
    
  [  'Enter numbers:', '__a__'  , '+' , '__b__',  ['Calculate'] ],
  [  'Result:  -->'  , 'result' ,  _  ,    _   ,       _        ],
  [  _               ,    _     ,  _  ,    _   ,      Quit      ]
)

with gui.Calculate:
    gui.result = float(gui.a) + float(gui.b)
    
gui.run()

然後 Ubuntu 下的輸出會是:

Hacker News 的討論串「Guietta – Python module to create simple GUIs (github.com)」這邊有人也介紹了其他 Python 下刻 GUI 的方式,翻了一下也還不少有趣的東西。

guietta 看起來拿來刻一些簡單的東西應該還算堪用,尤其是討論裡面有提到在教學授課時可以簡化不少 interface 的問題。

Trac 開發版 1.5.1 對 Python 3 的說明

Trac 開發版 1.5.1 的 Change Log 裡可以看到說明:

This will be the only release in the 1.5.x release line that supports Python 2.7. Future releases will support Python 3.5+.

先前在 mailing list 上有看到 Python 3 的計畫,不過好像是第一次在 changelog 裡面看到說明了...

Python 2.7.18,官方提供的最後一個 2.7 的版本

本來 Python 2 官方的計畫是支援到 2020/01/01,但最後一個版本 Python 2.7.18 一直到剛剛 2020/04/20 才出。對照前一個版本 2.7.17,是 2019/10/19 的時候出的,這應該算是一個「經典」的落幕:「Python 2.7.18, the last release of Python 2」。

對於得用 Python 2 才能跑的專案 (目前手上應該就是 Trac),如果 Python 3 的版本再不出,再過個一年應該只能用 pyenv 撐場了...