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 | """ Getting the Reports in the defined time period """ |
||
48 | |||
49 | report_filter = "rows=-1 created>{0} and created<{1}".format( |
||
50 | from_date.isoformat(), to_date.isoformat() |
||
51 | ) |
||
52 | |||
53 | return gmp.get_reports(filter=report_filter) |
||
54 | |||
55 | |||
56 | def print_result_sums(reports_xml, from_date, to_date): |
||
57 | print('Found {0} reports'.format(len(reports_xml.xpath('report')))) |
||
58 | |||
59 | sum_high = reports_xml.xpath( |
||
60 | 'sum(report/report/result_count/hole/full/text())' |
||
61 | ) |
||
62 | sum_medium = reports_xml.xpath( |
||
63 | 'sum(report/report/result_count/warning/full/text())' |
||
64 | ) |
||
65 | sum_low = reports_xml.xpath( |
||
66 | 'sum(report/report/result_count/info/full/text())' |
||
67 | ) |
||
68 | |||
69 | print( |
||
70 | 'Summary of results from {3} to {4}\nHigh: {0}\nMedium: {1}\nLow: ' |
||
71 | '{2}\n\n'.format( |
||
72 | int(sum_high), |
||
73 | int(sum_medium), |
||
74 | int(sum_low), |
||
75 | from_date.isoformat(), |
||
76 | to_date.isoformat(), |
||
77 | ) |
||
78 | ) |
||
79 | |||
80 | |||
81 | def print_result_tables(gmp, reports_xml): |
||
82 | report_list = reports_xml.xpath('report') |
||
83 | |||
84 | for report in report_list: |
||
85 | report_id = report.xpath('report/@id')[0] |
||
86 | name = report.xpath('name/text()')[0] |
||
87 | |||
88 | res = gmp.get_report(report_id) |
||
89 | |||
90 | print('\nReport: {0}'.format(report_id)) |
||
91 | |||
92 | table_data = [['Hostname', 'IP', 'Bericht', 'high', 'medium', 'low']] |
||
93 | |||
94 | for host in res.xpath('report/report/host'): |
||
95 | hostname = host.xpath( |
||
96 | 'detail/name[text()="hostname"]/../' 'value/text()' |
||
97 | ) |
||
98 | if len(hostname) > 0: |
||
99 | hostname = str(hostname[0]) |
||
100 | else: |
||
101 | hostname = "" |
||
102 | |||
103 | ip = host.xpath('ip/text()')[0] |
||
104 | high = host.xpath('result_count/hole/page/text()')[0] |
||
105 | medium = host.xpath('result_count/warning/page/text()')[0] |
||
106 | low = host.xpath('result_count/info/page/text()')[0] |
||
107 | |||
108 | table_data.append([hostname, ip, name, high, medium, low]) |
||
109 | host.clear() |
||
110 | del host |
||
138 |