Skip to main content

pyreports

pyreports

Codacy Badge CircleCI

pyreports is a Python library that allows you to create complex reports from various sources such as databases, text files, ldap, etc. and perform processing, filters, counters, etc. and then export or write them in various formats or in databases.

Test package

To test the package, follow these instructions:

$ git clone https://github.com/MatteoGuadrini/pyreports.git
$ cd pyreports
$ python -m unittest discover tests

Install package

To install the package, follow these instructions:

$ pip install pyreports #from pypi

$ git clone https://github.com/MatteoGuadrini/pyreports.git #from official repo
$ cd pyreports
$ pip install .     # or python setup.py install

Why choose this library?

pyreports wants to be a library that simplifies the collection of data from multiple sources such as databases, files and directory servers (through LDAP), the processing of them through built-in and customized functions, and the saving in various formats (or, by inserting the data in a database).

How does it work?

pyreports uses the tablib library to organize the data into Dataset object.

Simple report

I take the data from a database table, filter the data I need and save it in a csv file

import pyreports

# Select source: this is a DatabaseManager object
mydb = pyreports.manager('mysql', host='mysql1.local', database='login_users', user='dba', password='dba0000')

# Get data
mydb.execute('SELECT * FROM site_login')
site_login = mydb.fetchall()

# Filter data
error_login = pyreports.Executor(site_login)
error_login.filter([400, 401, 403, 404, 500])

# Save report: this is a FileManager object
output = pyreports.manager('csv', '/home/report/error_login.csv')
output.write(error_login.get_data())

Combine source

I take the data from a database table, and a log file, and save the report in json format

import pyreports

# Select source: this is a DatabaseManager object
mydb = pyreports.manager('mysql', host='mysql1.local', database='login_users', user='dba', password='dba0000')
# Select another source: this is a FileManager object
mylog = pyreports.manager('file', '/var/log/httpd/error.log')

# Get data
mydb.execute('SELECT * FROM site_login')
site_login = mydb.fetchall()
error_log = mylog.read()

# Filter database
error_login = pyreports.Executor(site_login)
error_login.filter([400, 401, 403, 404, 500])
users_in_error = set(error_login.select_column('users'))

# Prepare log
myreport = dict()
log_user_error = pyreports.Executor(error_log)
log_user_error.filter(list(users_in_error))
for line in log_user_error:
    for user in users_in_error:
        myreport.setdefault(user, [])
        myreport[user].append(line)

# Save report: this is a FileManager object
output = pyreports.manager('json', '/home/report/error_login.json')
output.write(myreport)

Report object

import pyreports

# Select source: this is a DatabaseManager object
mydb = pyreports.manager('mysql', host='mysql1.local', database='login_users', user='dba', password='dba0000')
output = pyreports.manager('xlsx', '/home/report/error_login.xlsx', mode='w')

# Get data
mydb.execute('SELECT * FROM site_login')
site_login = mydb.fetchall()

# Create report data
report = pyreports.Report(site_login, title='Site login failed', filters=[400, 401, 403, 404, 500], output=output)
# Filter data
report.exec()
# Save data on file
report.export()

ReportBook collection object

import pyreports

# Select source: this is a DatabaseManager object
mydb = pyreports.manager('mysql', host='mysql1.local', database='login_users', user='dba', password='dba0000')

# Get data
mydb.execute('SELECT * FROM site_login')
site_login = mydb.fetchall()

# Create report data
report_failed = pyreports.Report(site_login, title='Site login failed', filters=[400, 401, 403, 404, 500])
report_success = pyreports.Report(site_login, title='Site login success', filters=[200, 201, 202, 'OK'])
# Filter data
report_failed.exec()
report_success.exec()
# Create my ReportBook object
my_report = pyreports.ReportBook([report_failed, report_success])
# Save data on Excel file, with two worksheet ('Site login failed' and 'Site login success')
my_report.export(output='/home/report/site_login.xlsx')

Tools for dataset

This library includes many tools for handling data received from databases and files. Here are some practical examples of data manipulation.

import pyreports

# Select source: this is a DatabaseManager object
mydb = pyreports.manager('mysql', host='mysql1.local', database='login_users', user='dba', password='dba0000')

# Get data
mydb.execute('SELECT * FROM site_login')
site_login = mydb.fetchall()

# Most common error
most_common_error_code = pyreports.most_common(site_login, 'code')  # args: Dataset, column name
print(most_common_error_code)   # 200

# Percentage of error 404
percentage_error_404 = pyreports.percentage(site_login, 404)    # args: Dataset, filter
print(percentage_error_404)   # 16.088264794 (percent)

# Count every error code
count_error_code = pyreports.counter(site_login, 'code')  # args: Dataset, column name
print(count_error_code)   # Counter({200: 4032, 201: 42, 202: 1, 400: 40, 401: 38, 403: 27, 404: 802, 500: 3})

Command line

$ cat car.yml
reports:
  - report:
    title: 'Red ford machine'
    input:
      manager: 'mysql'
      source:
      # Connection parameters of my mysql database
        host: 'mysql1.local'
        database: 'cars'
        user: 'admin'
        password: 'dba0000'
      params:
        query: 'SELECT * FROM cars WHERE brand = %s AND color = %s'
        params: ['ford', 'red']
    # Filter km
    filters: [40000, 45000]
    output:
      manager: 'csv'
      filename: '/tmp/car_csv.csv'

$ report car.yaml

Official docs

In the following links there is the official documentation, for the use and development of the library.

Next

  1. Add more NoSQL databases (MongoDB, Redis, etc.)
  2. Towards v2

Open source

pyreports is an open source project. Any contribution, It's welcome.

A great thanks.

For donations, press this

For me

paypal

For Telethon

The Telethon Foundation is a non-profit organization recognized by the Ministry of University and Scientific and Technological Research. They were born in 1990 to respond to the appeal of patients suffering from rare diseases. Come today, we are organized to dare to listen to them and answer, every day of the year.

Telethon

Adopt the future

Acknowledgments

Thanks to Mark Lutz for writing the Learning Python and Programming Python books that make up my python foundation.

Thanks to Kenneth Reitz and Tanya Schlusser for writing the The Hitchhiker’s Guide to Python books.

Thanks to Dane Hillard for writing the Practices of the Python Pro books.

Special thanks go to my wife, who understood the hours of absence for this development. Thanks to my children, for the daily inspiration they give me and to make me realize, that life must be simple.

Thanks Python!

Metadata

Release files for pyreports 1.8.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyreports 1.8.2
File Size Uploaded Provenance Actions
pyreports-1.8.2.tar.gz 85.8 kB Without provenance Details for pyreports-1.8.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pyreports 1.8.2
File Interpreter ABI Platform Actions
pyreports-1.8.2-py3-none-any.whl Python 3 none any Details for pyreports-1.8.2-py3-none-any.whl

Total release size: 120.2 kB

Release files / pyreports-1.8.2.tar.gz

Download URL pyreports-1.8.2.tar.gz
Size 85.8 kB
Tags Source
SHA-256 checksum
How to use checksums
73be3f3da4cc8612290e14a2e02e0bc8cbb89afc28d9d39bd80fce2686ab8073
BLAKE2b-256 checksum
How to use checksums
87c0f79689f97371c5617a501f1046e760514ed13424e6f1c3c3c5548c9e3507
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.4

Release files / pyreports-1.8.2-py3-none-any.whl

Download URL pyreports-1.8.2-py3-none-any.whl
Size 34.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9b350949754c19b71e715111bf12dcee84bfd276bdb01005a9d2e6dc0dfd544a
BLAKE2b-256 checksum
How to use checksums
5082176706175488175f9ece1c937c42ff4a853115b34b4cb1ddd3884302ca7d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.4

Security for pyreports 1.8.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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page