python-reporter:一个可以保存Python脚本任何中间结果的工具。
在执行一个Python脚本时,如果需要的获取程序运行的一些中间结果,往往没有什么好的办法。例如,在xxx程序中以xxx计算中位数,
这个中位数只是一个中间变量。如果在其他地方需要这个中位数,一种常见的做法是把这个中位数打印出来。但是这种方法不适用于大量中间数据
的情况,python-reporter致力于解决这个问题。
安装
pip install python-reporter
使用
读取报告
import reporter
report_id = 'xxx'
report = reporter.Report(report_id)
report.foo # 第一种方法
report.get('foo') # 第二种方法
report.pop('foo') # 第三种方法
保存报告
import reporter
report = reporter.Report()
report << ('foo', 'bar') # 第一种方法
report + ('hello', 'world') # 第二种方法
report.add('foobar', 'foobar') # 第三种方法
report_id = report.save()
第一种、第二种方法,传入的必须是长度为2的元组或列表,第一个元素是变量名称、第二个元素是变量的值。 由于变量需要保存,所以变量必须是可序列化的。
保存方式
reporter支持两种种保存方式:
DatabaseBackend,即变量保存到数据库(默认)FileBackend,即变量以json形式保存到文件中
DatabaseBackend
注意:需要先初始化数据库。例如:
backend = reporter.DatabaseBackend('sqlite:////path/to/reporter.db')
backend.create_table()
- 在程序内部指定数据库连接
report = reporter.Report(
backend=reporter.DatabaseBackend('sqlite:////path/to/reporter.db')
)
- 通过环境变量指定数据库连接
export REPORTER_DB_URL = 'sqlite:////path/to/reporter.db'
report = reporter.Report()
- 使用默认数据库连接(即保存SQLite文件到当前目录)
report = reporter.Report()
FileBackend
- 在程序内部指定保存地址
report = reporter.Report(backend=reporter.FileBackend('/path/to/reporter'))
那么报告以文件保存到/path/to/reporter/文件夹下,文件名为reporter-report-xxx.json。
- 通过环境变量指定保存地址
export REPORTER_DIR=/path/to/reporter
report = reporter.Report(backend=reporter.FileBackend())
那么报告以文件保存到/path/to/reporter/文件夹下,文件名为reporter-report-xxx.json。
- 使用默认目录(即当前目录)
report = reporter.Report(backend=reporter.FileBackend())
那么报告以文件保存到当前目录下,文件名为reporter-report-xxx.json。
如何自定义保存方式
reporter默认支持FileBackend,如果需要以其他方式保存,可以自定义Backend,继承自reporter.Backend,且
实现load(report_id)、save(report_id, data)两个方法即可。
class MyBackend(reporter.Backend):
def load(self, report_id):
pass
def save(self, report_id, data):
pass
report = reporter.Report(backend=MyBackend())
Metadata
Release files for python-reporter 1.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| python-reporter-1.0.2.tar.gz | 5.4 kB | Without provenance | Details for python-reporter-1.0.2.tar.gz |
Built distributions (wheels)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| python_reporter-1.0.2-py3.7.egg | Legacy Egg format | - | - | Details for python_reporter-1.0.2-py3.7.egg |
| python_reporter-1.0.2-py3-none-any.whl | Python 3 | none | any | Details for python_reporter-1.0.2-py3-none-any.whl |
Total release size: 19.3 kB
Release files / python-reporter-1.0.2.tar.gz
| Download URL | python-reporter-1.0.2.tar.gz |
|---|---|
| Size | 5.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c94e82ffaeefba32f754a85f11d21fbd6da380f25f8385cc3c905ee7dda88d37
|
|
BLAKE2b-256 checksum How to use checksums |
42f78c8e8d0d31b8889dfe2bd8ee0619e3ac544733a188d9199845784b6a2ae1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.7.9
|
Release files / python_reporter-1.0.2-py3.7.egg
| Download URL | python_reporter-1.0.2-py3.7.egg |
|---|---|
| Size | 8.3 kB |
| Tags | Egg |
|
SHA-256 checksum How to use checksums |
abde5119b64e20d96983f2d3bca00ec5a49a80cf04fd69945ea20100ba4343d7
|
|
BLAKE2b-256 checksum How to use checksums |
92a1bb36b6f5e3ec3ea15e60f5d5c0d9e0ba6913e02b4ec36270d201bf2a364f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.7.9
|
Release files / python_reporter-1.0.2-py3-none-any.whl
| Download URL | python_reporter-1.0.2-py3-none-any.whl |
|---|---|
| Size | 5.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0fd696d7a42b7a48a82eb6dc7d18c7a58eb756ec07ef7be2a7344942cb367707
|
|
BLAKE2b-256 checksum How to use checksums |
e7aa9c6826e6b34c1f080155f02f7b3abd355fd005052e5f82af684db4dd3400
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.7.9
|
Security for python-reporter 1.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.