Designed for a data scientist
115 |116 | Simplifying the life of those who collect and process data 117 |
118 |104 | pyreports wants to be a library that simplifies the collection of data from multiple sources such as databases, 105 | files and directory servers (through LDAP), the processing of them through built-in and customized functions, 106 | and the saving in various formats (or, by inserting the data in a database). 107 |
108 |116 | Simplifying the life of those who collect and process data 117 |
118 |It saves time, without worrying about creating connections or managing files manually.
127 |Each row of code is PEP 8 compliant
136 |Install pyreports is easy with pip.
168 |
171 | $ pip install pyreports
172 |
173 | I take the data from a database table, filter the data I need and save it in a csv file.
179 | 180 |
183 |
184 | import pyreports
185 |
186 | # Select source: this is a DatabaseManager object
187 | mydb = pyreports.manager('mysql', host='mysql1.local', database='login_users', user='dba', password='dba0000')
188 |
189 | # Get data
190 | mydb.execute('SELECT * FROM site_login')
191 | site_login = mydb.fetchall()
192 |
193 | # Filter data
194 | error_login = pyreports.Executor(site_login)
195 | error_login.filter([400, 401, 403, 404, 500])
196 |
197 | # Save report: this is a FileManager object
198 | output = pyreports.manager('csv', '/home/report/error_login.csv')
199 | output.write(error_login.get_data())
200 |
201 | In this example we will analyze and capture parts of a web server log. 207 | For each error code present in the log, we will create a report that will be inserted in a book, 208 | where each sheet will contain the details of the error code. In the last sheet, 209 | there will be an element counter for every single error present in the report.
210 |
212 | import pyreports
213 | import tablib
214 |
215 | # Get apache log data: this is a FileManager object
216 | apache_log = pyreports.manager('log', '/var/log/httpd/error.log')
217 | # Read log based on regexp
218 | data_log = apache_log.read('([(\d\.)]+) - - \[(.*?)\] "(.*?)" (\d+) - "(.*?)" "(.*?)"',
219 | headers=['ip', 'date', 'operation', 'url', 'code', 'client'])
220 |
221 | # Create a collection of Report objects
222 | all_apache_error = pyreports.ReportBook(title='Apache error on my site')
223 |
224 | # Create a Report object based on error code
225 | all_error = set(data_log['code'])
226 | for code in all_error:
227 | all_apache_error.add(pyreports.Report(data_log, filters=[code], title=f'Error {code}'))
228 |
229 | # Count all error code
230 | counter = pyreports.counter(data_log, 'code')
231 | # Append new Report on ReportBook with error code counters
232 | error_counter = tablib.Dataset(counter.values(), headers=counter)
233 | all_apache_error.add(pyreports.Report(error_counter))
234 |
235 | # Save ReportBook on Excel
236 | all_apache_error.export('/home/report/apache_log_error_code.xlsx')
237 |
238 | pyreports has a command line interface which takes a configuration file in YAML format as an argument.
244 |
246 | $ cat car.yml
247 | reports:
248 | - report:
249 | title: 'Red ford machine'
250 | input:
251 | manager: 'mysql'
252 | source:
253 | # Connection parameters of my mysql database
254 | host: 'mysql1.local'
255 | database: 'cars'
256 | user: 'admin'
257 | password: 'dba0000'
258 | params:
259 | query: 'SELECT * FROM cars WHERE brand = %s AND color = %s'
260 | params: ['ford', 'red']
261 | # Filter km
262 | filters: [40000, 45000]
263 | output:
264 | manager: 'csv'
265 | filename: '/tmp/car_csv.csv'
266 |
267 | $ report car.yaml
268 |
269 | The complete documentation is much more comprehensive and can be found by clicking the button below.
275 |276 | More on ReadTheDocs 277 |
278 |pyreports is free and open source.
291 |The license is GPLv3 and you can consult it directly here: Github License
292 |Donate: PayPal
293 |If you are interested in improvements or have innovation proposals, or simply want to help improve the library, you can fork the project from here! Thank you for your support!
297 |I hope you find this python library useful.
Feel free to get in touch if you have any questions or suggestions.