Skip to main content

一个可以保存Python脚本任何中间结果的工具

Project description

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支持两种种保存方式:

  1. DatabaseBackend,即变量保存到数据库(默认)
  2. FileBackend,即变量以json形式保存到文件中

DatabaseBackend

注意:需要先初始化数据库。例如:

backend = reporter.DatabaseBackend('sqlite:////path/to/reporter.db')
backend.create_table()
  1. 在程序内部指定数据库连接
report = reporter.Report(
    backend=reporter.DatabaseBackend('sqlite:////path/to/reporter.db')
)
  1. 通过环境变量指定数据库连接
export REPORTER_DB_URL = 'sqlite:////path/to/reporter.db'
report = reporter.Report()
  1. 使用默认数据库连接(即保存SQLite文件到当前目录)
report = reporter.Report()

FileBackend

  1. 在程序内部指定保存地址
report = reporter.Report(backend=reporter.FileBackend('/path/to/reporter'))

那么报告以文件保存到/path/to/reporter/文件夹下,文件名为reporter-report-xxx.json

  1. 通过环境变量指定保存地址
export REPORTER_DIR=/path/to/reporter
report = reporter.Report(backend=reporter.FileBackend())

那么报告以文件保存到/path/to/reporter/文件夹下,文件名为reporter-report-xxx.json

  1. 使用默认目录(即当前目录)
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())

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python-reporter-1.0.2.tar.gz (5.4 kB view details)

Uploaded Source

Built Distributions

python_reporter-1.0.2-py3.7.egg (8.3 kB view details)

Uploaded Source

python_reporter-1.0.2-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file python-reporter-1.0.2.tar.gz.

File metadata

  • Download URL: python-reporter-1.0.2.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for python-reporter-1.0.2.tar.gz
Algorithm Hash digest
SHA256 c94e82ffaeefba32f754a85f11d21fbd6da380f25f8385cc3c905ee7dda88d37
MD5 0edb5e824cb674ae6cbe8a783d2a4918
BLAKE2b-256 42f78c8e8d0d31b8889dfe2bd8ee0619e3ac544733a188d9199845784b6a2ae1

See more details on using hashes here.

File details

Details for the file python_reporter-1.0.2-py3.7.egg.

File metadata

  • Download URL: python_reporter-1.0.2-py3.7.egg
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for python_reporter-1.0.2-py3.7.egg
Algorithm Hash digest
SHA256 abde5119b64e20d96983f2d3bca00ec5a49a80cf04fd69945ea20100ba4343d7
MD5 1a120ba1de2e30ef3f84b8ec09a0775d
BLAKE2b-256 92a1bb36b6f5e3ec3ea15e60f5d5c0d9e0ba6913e02b4ec36270d201bf2a364f

See more details on using hashes here.

File details

Details for the file python_reporter-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: python_reporter-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for python_reporter-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0fd696d7a42b7a48a82eb6dc7d18c7a58eb756ec07ef7be2a7344942cb367707
MD5 4f02132f1a1716edb4962d23cf8a0645
BLAKE2b-256 e7aa9c6826e6b34c1f080155f02f7b3abd355fd005052e5f82af684db4dd3400

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page