这是应该如何使用importlib.resources吗?

时间:2019-03-10 15:27:56

标签: python python-3.x

应该使用importlib.resources吗?

from importlib.resources import open_text
from typing import Dict

DESKTOP_AGENTS = open_text('he.data', 'user_agents.txt').readlines()


# just a function using DESKTOP_AGENTS, its functionality doesn't matter
def random_headers() -> Dict[str, str]:
    """Returns headers dict for use when making requests to web servers,
    in order to simulate a browser more realistically.
    The user agent is random."""
    return {
        'User-Agent': choice(DESKTOP_AGENTS).strip(),
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    }

具有该功能的文件为src/he/http_tools.py

完整的程序包结构在这里:https://github.com/Laurentiu-Andronache/he,但这是摘要版本:

/src/he/__init__.py
/src/he/http_tools.py
/src/he/data/__init__.py
/src/he/data/user_agents.txt

该库可pip安装。

setup.py中,我有:

    packages=find_packages(where="src"),
    package_dir={"": "src"},
    package_data={'he': ['data/*.txt']},

0 个答案:

没有答案