Package: pyreports
pyreportspyreportsis 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 packageTo test the package, follow these instructions:$gitclonehttps://github.com/MatteoGuadrini/pyreports.git$cdpyreports$python-munittestdiscovertestsInstall packageTo install package, follow these instructions:$pipinstallpyreports#from pypi$gitclonehttps://github.com/MatteoGuadrini/pyreports.git#from official repo$cdpyreports$pythonsetup.pyinstallWhy choose this library?pyreportswants 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 workpyreportsuses thetabliblibrary to organize the data intoDatasetobject.Simple reportI take the data from a database table, filter the data I need and save it in a csv fileimportpyreports# Select source: this is a DatabaseManager objectmydb=pyreports.manager('mysql',host='mysql1.local',database='login_users',user='dba',password='dba0000')# Get datamydb.execute('SELECT * FROM site_login')site_login=mydb.fetchall()# Filter dataerror_login=pyreports.Executor(site_login)error_login.filter([400,401,403,404,500])# Save report: this is a FileManager objectoutput=pyreports.manager('csv','/home/report/error_login.csv')output.write(error_login.get_data())Combine sourceI take the data from a database table, and a log file, and save the report in json formatimportpyreports# Select source: this is a DatabaseManager objectmydb=pyreports.manager('mysql',host='mysql1.local',database='login_users',user='dba',password='dba0000')# Select another source: this is a FileManager objectmylog=pyreports.manager('file','/var/log/httpd/error.log')# Get datamydb.execute('SELECT * FROM site_login')site_login=mydb.fetchall()error_log=mylog.read()# Filter databaseerror_login=pyreports.Executor(site_login)error_login.filter([400,401,403,404,500])users_in_error=set(error_login.select_column('users'))# Prepare logmyreport=dict()log_user_error=pyreports.Executor(error_log)log_user_error.filter(list(users_in_error))forlineinlog_user_error:foruserinusers_in_error:myreport.setdefault(user,[])myreport[user].append(line)# Save report: this is a FileManager objectoutput=pyreports.manager('json','/home/report/error_login.json')output.write(myreport)Report objectimportpyreports# Select source: this is a DatabaseManager objectmydb=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 datamydb.execute('SELECT * FROM site_login')site_login=mydb.fetchall()# Create report datareport=pyreports.Report(site_login,title='Site login failed',filters=[400,401,403,404,500],output=output)# Filter datareport.exec()# Save data on filereport.export()ReportBook collection objectimportpyreports# Select source: this is a DatabaseManager objectmydb=pyreports.manager('mysql',host='mysql1.local',database='login_users',user='dba',password='dba0000')# Get datamydb.execute('SELECT * FROM site_login')site_login=mydb.fetchall()# Create report datareport_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 datareport_failed.exec()report_success.exec()# Create my ReportBook objectmy_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 datasetThis library includes many tools for handling data received from databases and files.
Here are some practical examples of data manipulation.importpyreports# Select source: this is a DatabaseManager objectmydb=pyreports.manager('mysql',host='mysql1.local',database='login_users',user='dba',password='dba0000')# Get datamydb.execute('SELECT * FROM site_login')site_login=mydb.fetchall()# Most common errormost_common_error_code=pyreports.most_common(site_login,'code')# args: Dataset, column nameprint(most_common_error_code)# 200# Percentage of error 404percentage_error_404=pyreports.percentage(site_login,404)# args: Dataset, filterprint(percentage_error_404)# 16.088264794 (percent)# Count every error codecount_error_code=pyreports.counter(site_login,'code')# args: Dataset, column nameprint(count_error_code)# Counter({200: 4032, 201: 42, 202: 1, 400: 40, 401: 38, 403: 27, 404: 802, 500: 3})Command line$catcar.ymlreports:- report:title: 'Red ford machine'input:manager: 'mysql'source:#Connectionparametersofmymysqldatabasehost: 'mysql1.local'database: 'cars'user: 'admin'password: 'dba0000'params:query: 'SELECT * FROM cars WHERE brand = %s AND color = %s'params: ['ford', 'red']#Filterkmfilters: [40000, 45000]output:manager: 'csv'filename: '/tmp/car_csv.csv'$reportcar.yamlOfficial docsIn the following links there is theofficial documentation, for the use and development of the library.Managers:docExecutor:docReport:docdata tools:docexamples:docAPI:io,coreCLI:cliOpen sourcepyreportsis an open source project. Any contribute, It's welcome.A great thanks.For donations, press thisFor meForTelethonThe 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.Adopt the futureAcknowledgmentsThanks to Mark Lutz for writing theLearning PythonandProgramming Pythonbooks that make up my python foundation.Thanks to Kenneth Reitz and Tanya Schlusser for writing theThe Hitchhiker’s Guide to Pythonbooks.Thanks to Dane Hillard for writing thePractices of the Python Probooks.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!