afwf Utility Tools#
afwf
自带了一些开发 Workflow 时常用到的工具. 和 Alfred 官放的 Python 包不同的是, 官放倾向于自己从 0 只用标准库造一个轮子, 而 afwf
倾向于使用成熟的第三方库.
Cache#
一个基于 diskcache 的磁盘缓存工具. 详细使用说明可以参考单元测试:
# -*- coding: utf-8 -*-
from pathlib import Path
from afwf.opt.cache.impl import TypedCache
dir_cache = Path(__file__).absolute().parent.joinpath(".cache")
cache = TypedCache(str(dir_cache))
class Dog:
def __init__(self):
self.bark_count = 0
@cache.typed_memoize(expire=10)
def bark(self, say: str):
_ = say
self.bark_count += 1
def test():
cache.clear()
dog = Dog()
dog.bark("woof")
dog.bark("woof")
dog.bark("woof")
dog.bark("ruff")
dog.bark("ruff")
dog.bark("ruff")
dog.bark("ruff")
dog.bark("ruff")
assert dog.bark_count == 2
if __name__ == "__main__":
from afwf.tests import run_cov_test
run_cov_test(__file__, "afwf.opt.cache", preview=False)
Fuzzy Item#
一个基于 fuzzywuzzy 的模糊搜索工具, 用于对 items 进行模糊搜索, 排序. 详细使用说明可以参考单元测试:
# -*- coding: utf-8 -*-
from afwf.opt.fuzzy_item.impl import Item, FuzzyItemMatcher
class TestFuzzyItemMatcher:
def test(self):
items = [
Item(title="1").set_fuzzy_match_name("apple and banana and cherry"),
Item(title="2").set_fuzzy_match_name("alice and bob and charlie"),
]
fuzzy = FuzzyItemMatcher.from_items(items)
res = fuzzy.match(name="alice bob")
assert len(res) == 1
assert res[0].title == "2"
res = fuzzy.match(name="this is invalid", threshold=95)
assert len(res) == 0
if __name__ == "__main__":
from afwf.tests import run_cov_test
run_cov_test(__file__, "afwf.opt.fuzzy_item.impl", preview=False)