将项目目录中的非python文件作为python namedtuple对象导入。
module-resources的Python项目详细描述
python模块资源
将项目目录中的非python文件作为python namedtuple对象导入。
如果您曾经在node中工作过,那么您可能熟悉一种语言特性,它允许您将json文件作为导入的模块拉入。
import { dataStuff } from 'myProjectResources/jsonFiles' dataStuff.contents === "A rich javascript object"
使用module_resources
,您可以在python中实现类似的功能。
frommy_project_resources.json_filesimportdata_stuffdata_stuff.contents=='A python namedtuple instance'
开始
要在您自己的项目中使用它,请使用pip安装。
pip install module-resources
# supports yaml files too
pip install module-resources[yaml]
在项目中创建所需可导入资源文件所在的模块位置。我会用this project as an example。
mkdir module_resources/examples/json/
touch module_resources/examples/json/__init__.py
你的模块是^{
frommodule_resourcesimportJsonModuleResource__all__=JsonModuleResource(__name__,__file__).intercept_imports()
从那里,将要作为python对象导入的资源移到该目录中。
tree ./module_resources/examples/json/
module_resources/examples/json/
├── __init__.py
└── logging_config.json
0 directories, 2 files
现在您可以导入一些logger对象的配置json作为python namedtuple。它有一些有趣的特性,可以很好地说明这个工具的一些注意事项。
> python
Python 3.7.2 (default, Jan 14 2019, 16:30:40)
[Clang 10.0.0 (clang-1000.10.44.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>frommodule_resources.examples.jsonimportlogging_config>>>logging_config.formatters.simplejson(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
您可以使用点属性表示法引用任何有效的namedtuple属性。
>>>logger.formatters.simple.format'%(asctime)s - %(name)s - %(levelname)s - %(message)s'
yaml配置文件也可以工作,with the ^{
>>>frommodule_resources.examples.yamlimportlogging_configasyaml_logging_config>>>yaml_logging_config.formattersyaml(simple=yaml(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
若要将此对象转换为字典,请将其传递给dict()
。
>>>importlogging.config>>>logging.config.dictConfig(dict(yaml_logging_config))>>>logging.getLogger('test').info('testing')
2019-07-23 11:43:57,296 - test - INFO - testing
注意事项
有一些注意事项需要注意。
有效与无效的namedtuple字段名
>>>frommodule_resources.examples.jsonimportlogging_config>>>logging_config.loggers.__main__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'ImportableFallbackDict' object has no attribute '__main__'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'ImportableFallbackDict' object has no attribute '__main__'
在namedtuple字段中,__main__
不是合法名称。它被公开为字典类型,ImportableFallbackDict
。
>>>logging_config.loggers['__main__'].level'INFO'
但是,它确实将有效的属性名作为namedtuples存储在该字典中。
仅导出文件的一部分
可以从资源的顶级命名空间导出。
>>>frommodule_resources.examples.json.logging_cofigimportformatters>>>formatters.simple.format'INFO'
不能从json或yaml文件导出任意深度的内容。仅支持一个级别的深度。
输入模块资源提示
使用这个工具会让pylint不高兴。
frommodule_resources.examples.jsonimportlogging_configimportlogging.configlogging.config.dictConfig(dict(logger))
上面的代码将引起Pylint的投诉。
pylint: Unable to import 'module_resources.example.json.logging_config' ("import-error") [E0401]
这个工具利用python语言的高度动态特性来完成它的工作,因此这些属性和类型在运行时才可用。如果要在使用此工具时保持较高的lint分数,则需要在代码库中包含这些对象的异常。
开发
开发时假设使用linux或mac环境,运行python version 3。
先决条件
make preqres
这个脚本将有助于确保您拥有针对本地项目开发的必要工具。
安装
make virtualenv
. .venv/bin/activate
安装依赖项后运行测试。
运行测试
make tests
以及编码风格测试
make lint
make mypy
make bandit
展开
针对主分支打开拉取请求。travis ci将在所有测试通过后发布预览版本。您可以从test.pypi.org安装此分支生成的预览版本。
pip install -i https://test.pypi.org/simple/ module-resources==0.0.${TRAVIS_BUILD_NUMBER}
请注意,如果从fork打开一个pull请求,则此步骤不会运行。
若要发布新的正式版本,请标记提交并将其推送到主分支。
git checkout master
git pull origin master
# examples of preparing a new tag for release
make tag-patch # also accepts: tag-minor, tag-major
git push origin --tags
请注意,只能从主分支创建和推送标记。在拉取请求中找到的标记不会起任何作用。
贡献
小请求可以直接作为请求提交。较大的变更应首先作为问题提交,并在开始提案工作之前进行规划。
请阅读CONTRIBUTING.md了解有关行为准则和提交请求的过程的详细信息。
版本控制
此项目使用SemVer进行版本控制。有关可用的版本,请参见tags on this repository。
作者
请参阅参与此项目的contributors列表。
此列表可以用make contributors
更新。
许可证
这个项目is licensed under the MIT License。
致谢
- @purplebooth用于the README.md和the CONTRIBUTING.md模板文件。