用于分析XBRL文档的库
python-xbrl的Python项目详细描述
python xbrl是用于分析的库 xbrl 提供基本模型对象和序列化输出的文档 对象星期四 marshmallow用于 呈现为标准格式,如json或http api
安装
安装python xbrl最简单的方法是使用pip
pip install python-xbrl
或者从github安装最新的开发版本(或者用 release vergitsion like @v1.1.0)
pip install git+https://github.com/greedo/python-xbrl.git@master
或
git clone https://github.com/greedo/python-xbrl.git
或
将python xbrl目录复制到python路径中。拉链 here
确保sys.path正确。
要求
- python>;=2.6或>;=3.3
python xbrl依赖于beautifulsoup4 它位于python xml解析器lxml的顶部。它还需要 marshmallow用于序列化对象。 有关详细信息,请参见requirements.txt
对于pypi支持,建议对lxml使用https://github.com/amauryfa/lxml,因为这是一个使用cffi而不是python capi的fork。
初始化
要开始使用库,首先导入XBRLParser
from xbrl import XBRLParser, GAAP, GAAPSerializer
简单的分析工作流
首先将传入的xrbl文件解析为一个新的XBRLbasic对象
xbrl_parser = XBRLParser() xbrl = xbrl_parser.parse(file("sam-20131228.xml"))
然后您可以使用不同的解析器分析文档
gaap_obj = xbrl_parser.parseGAAP(xbrl, doc_date="20131228", context="current", ignore_errors=0)
现在我们有一个GAAPmodel对象,它包含gaap解析的元素 从文件中。
此模型对象支持以下几种不同的功能:
- context支持当前、年份和即时上下文。如果可用,您还可以从文档日期起按天数获取上一季度的信息。例如:90、180等
- 错误处理。0引发所有分析错误的异常并停止分析,1抑制所有分析错误并继续分析,2记录所有分析错误并继续分析
可以将gaap模型对象序列化为序列化对象。 可用于转换为标准格式,如json或http api。
serializer = GAAPSerializer() result = serializer.dump(gaap_obj)
您也可以只查看序列化对象中的数据
print result.data
您可以将各种解析器应用于基对象XBRLParser以获取 不同于文件中的GAAP数据。除此之外 您还可以在 结果分析的数据对象。
正在提取DEI数据
dei_obj = xbrl_parser.parseDEI(xbrl) serializer = DEISerializer() result = serializer.dump(dei_obj)
提取自定义数据
custom_obj = xbrl_parser.parseCustom(xbrl) print custom_obj()
测试
要运行单元测试,您需要pytest
pip install pytest
一旦你有了它,cd进入这个repo的根目录并
py.test --tb=line -vs
错误
如果遇到任何错误,请打开github issue
贡献
- 检查打开的问题或打开一个新的问题,开始围绕功能想法或错误的讨论。
- 如果您对某个问题或您的更改感到不舒服或不确定,请随时发送电子邮件至@greedo,他会很乐意通过电子邮件、skype、远程配对或您喜欢的任何方式帮助您。
- 在github上分叉the repository,开始对master分支(或其分支)进行更改。
- 编写一个测试,显示错误已修复或功能按预期工作。
- 发送一个pull请求并对维护程序进行bug操作,直到它被合并并发布。:)确保将自己添加到AUTHORS。
许可证
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the license.