项目作者: MatteoGuadrini

项目描述 :
reports is a python library that allows you to create complex report from various sources
高级语言: Python
项目地址: git://github.com/MatteoGuadrini/reports.git
创建时间: 2021-02-01T16:28:54Z
项目社区:https://github.com/MatteoGuadrini/reports

开源协议:GNU General Public License v3.0

下载


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:

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

Install package

To install package, follow these instructions:

  1. $ pip install pyreports #from pypi
  2. $ git clone https://github.com/MatteoGuadrini/pyreports.git #from official repo
  3. $ cd pyreports
  4. $ 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

  1. import pyreports
  2. # Select source: this is a DatabaseManager object
  3. mydb = pyreports.manager('mysql', host='mysql1.local', database='login_users', user='dba', password='dba0000')
  4. # Get data
  5. mydb.execute('SELECT * FROM site_login')
  6. site_login = mydb.fetchall()
  7. # Filter data
  8. error_login = pyreports.Executor(site_login)
  9. error_login.filter([400, 401, 403, 404, 500])
  10. # Save report: this is a FileManager object
  11. output = pyreports.manager('csv', '/home/report/error_login.csv')
  12. 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

  1. import pyreports
  2. # Select source: this is a DatabaseManager object
  3. mydb = pyreports.manager('mysql', host='mysql1.local', database='login_users', user='dba', password='dba0000')
  4. # Select another source: this is a FileManager object
  5. mylog = pyreports.manager('file', '/var/log/httpd/error.log')
  6. # Get data
  7. mydb.execute('SELECT * FROM site_login')
  8. site_login = mydb.fetchall()
  9. error_log = mylog.read()
  10. # Filter database
  11. error_login = pyreports.Executor(site_login)
  12. error_login.filter([400, 401, 403, 404, 500])
  13. users_in_error = set(error_login.select_column('users'))
  14. # Prepare log
  15. myreport = dict()
  16. log_user_error = pyreports.Executor(error_log)
  17. log_user_error.filter(list(users_in_error))
  18. for line in log_user_error:
  19. for user in users_in_error:
  20. myreport.setdefault(user, [])
  21. myreport[user].append(line)
  22. # Save report: this is a FileManager object
  23. output = pyreports.manager('json', '/home/report/error_login.json')
  24. output.write(myreport)

Report object

  1. import pyreports
  2. # Select source: this is a DatabaseManager object
  3. mydb = pyreports.manager('mysql', host='mysql1.local', database='login_users', user='dba', password='dba0000')
  4. output = pyreports.manager('xlsx', '/home/report/error_login.xlsx', mode='w')
  5. # Get data
  6. mydb.execute('SELECT * FROM site_login')
  7. site_login = mydb.fetchall()
  8. # Create report data
  9. report = pyreports.Report(site_login, title='Site login failed', filters=[400, 401, 403, 404, 500], output=output)
  10. # Filter data
  11. report.exec()
  12. # Save data on file
  13. report.export()

ReportBook collection object

  1. import pyreports
  2. # Select source: this is a DatabaseManager object
  3. mydb = pyreports.manager('mysql', host='mysql1.local', database='login_users', user='dba', password='dba0000')
  4. # Get data
  5. mydb.execute('SELECT * FROM site_login')
  6. site_login = mydb.fetchall()
  7. # Create report data
  8. report_failed = pyreports.Report(site_login, title='Site login failed', filters=[400, 401, 403, 404, 500])
  9. report_success = pyreports.Report(site_login, title='Site login success', filters=[200, 201, 202, 'OK'])
  10. # Filter data
  11. report_failed.exec()
  12. report_success.exec()
  13. # Create my ReportBook object
  14. my_report = pyreports.ReportBook([report_failed, report_success])
  15. # Save data on Excel file, with two worksheet ('Site login failed' and 'Site login success')
  16. 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.

  1. import pyreports
  2. # Select source: this is a DatabaseManager object
  3. mydb = pyreports.manager('mysql', host='mysql1.local', database='login_users', user='dba', password='dba0000')
  4. # Get data
  5. mydb.execute('SELECT * FROM site_login')
  6. site_login = mydb.fetchall()
  7. # Most common error
  8. most_common_error_code = pyreports.most_common(site_login, 'code') # args: Dataset, column name
  9. print(most_common_error_code) # 200
  10. # Percentage of error 404
  11. percentage_error_404 = pyreports.percentage(site_login, 404) # args: Dataset, filter
  12. print(percentage_error_404) # 16.088264794 (percent)
  13. # Count every error code
  14. count_error_code = pyreports.counter(site_login, 'code') # args: Dataset, column name
  15. print(count_error_code) # Counter({200: 4032, 201: 42, 202: 1, 400: 40, 401: 38, 403: 27, 404: 802, 500: 3})

Command line

  1. $ cat car.yml
  2. reports:
  3. - report:
  4. title: 'Red ford machine'
  5. input:
  6. manager: 'mysql'
  7. source:
  8. # Connection parameters of my mysql database
  9. host: 'mysql1.local'
  10. database: 'cars'
  11. user: 'admin'
  12. password: 'dba0000'
  13. params:
  14. query: 'SELECT * FROM cars WHERE brand = %s AND color = %s'
  15. params: ['ford', 'red']
  16. # Filter km
  17. filters: [40000, 45000]
  18. output:
  19. manager: 'csv'
  20. filename: '/tmp/car_csv.csv'
  21. $ report car.yaml

Official docs

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

Open source

pyreports is an open source project. Any contribute, 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 answers, 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!