| Conditions | 5 |
| Total Lines | 64 |
| Code Lines | 45 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | # -*- coding: utf-8 -*- |
||
| 46 | def get_reports_xml(gmp, from_date, to_date): |
||
| 47 | report_filter = "rows=-1 and created>{0} and created<{1}".format( |
||
| 48 | from_date.isoformat(), to_date.isoformat() |
||
| 49 | ) |
||
| 50 | |||
| 51 | return gmp.get_reports(filter=report_filter) |
||
| 52 | |||
| 53 | |||
| 54 | def print_result_sums(reports_xml, from_date, to_date): |
||
| 55 | sum_high = reports_xml.xpath( |
||
| 56 | 'sum(report/report/result_count/hole/full/text())' |
||
| 57 | ) |
||
| 58 | sum_medium = reports_xml.xpath( |
||
| 59 | 'sum(report/report/result_count/warning/full/text())' |
||
| 60 | ) |
||
| 61 | sum_low = reports_xml.xpath( |
||
| 62 | 'sum(report/report/result_count/info/full/text())' |
||
| 63 | ) |
||
| 64 | |||
| 65 | print( |
||
| 66 | 'Summary of results from {3} to {4}\nHigh: {0}\nMedium: {1}\nLow: ' |
||
| 67 | '{2}\n\n'.format( |
||
| 68 | int(sum_high), |
||
| 69 | int(sum_medium), |
||
| 70 | int(sum_low), |
||
| 71 | from_date.isoformat(), |
||
| 72 | to_date.isoformat(), |
||
| 73 | ) |
||
| 74 | ) |
||
| 75 | |||
| 76 | |||
| 77 | def print_result_tables(gmp, reports_xml): |
||
| 78 | |||
| 79 | report_list = reports_xml.xpath('report') |
||
| 80 | |||
| 81 | for report in report_list: |
||
| 82 | report_id = report.xpath('report/@id')[0] |
||
| 83 | name = report.xpath('name/text()')[0] |
||
| 84 | |||
| 85 | res = gmp.get_report(report_id) |
||
| 86 | |||
| 87 | print('\nReport: {0}'.format(report_id)) |
||
| 88 | |||
| 89 | table_data = [['Hostname', 'IP', 'Bericht', 'high', 'medium', 'low']] |
||
| 90 | |||
| 91 | for host in res.xpath('report/report/host'): |
||
| 92 | hostname = host.xpath( |
||
| 93 | 'detail/name[text()="hostname"]/../' 'value/text()' |
||
| 94 | ) |
||
| 95 | if len(hostname) > 0: |
||
| 96 | hostname = str(hostname[0]) |
||
| 97 | else: |
||
| 98 | hostname = "" |
||
| 99 | |||
| 100 | ip = host.xpath('ip/text()')[0] |
||
| 101 | high = host.xpath('result_count/hole/page/text()')[0] |
||
| 102 | medium = host.xpath('result_count/warning/page/text()')[0] |
||
| 103 | low = host.xpath('result_count/info/page/text()')[0] |
||
| 104 | |||
| 105 | table_data.append([hostname, ip, name, high, medium, low]) |
||
| 106 | host.clear() |
||
| 107 | del host |
||
| 108 | |||
| 109 | table = AsciiTable(table_data) |
||
| 110 | print(table.table + '\n') |
||
| 142 |