| Conditions | 3 | 
| Total Lines | 57 | 
| Code Lines | 39 | 
| 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 print_reports(gmp, from_date, to_date):  | 
            ||
| 47 |     asset_filter = "rows=-1 and modified>{0} and modified<{1}".format( | 
            ||
| 48 | from_date.isoformat(), to_date.isoformat()  | 
            ||
| 49 | )  | 
            ||
| 50 | |||
| 51 | assets_xml = gmp.get_assets(  | 
            ||
| 52 | asset_type=gmp.types.AssetType.HOST, filter=asset_filter  | 
            ||
| 53 | )  | 
            ||
| 54 | |||
| 55 | sum_high = 0  | 
            ||
| 56 | sum_medium = 0  | 
            ||
| 57 | sum_low = 0  | 
            ||
| 58 | table_data = [['Hostname', 'IP', 'Bericht', 'high', 'medium', 'low']]  | 
            ||
| 59 | |||
| 60 |     for asset in assets_xml.xpath('asset'): | 
            ||
| 61 |         ip = asset.xpath('name/text()')[0] | 
            ||
| 62 | |||
| 63 | hostnames = asset.xpath(  | 
            ||
| 64 | 'identifiers/identifier/name[text()="hostname"]/../value/text()'  | 
            ||
| 65 | )  | 
            ||
| 66 | |||
| 67 | if len(hostnames) == 0:  | 
            ||
| 68 | continue  | 
            ||
| 69 | |||
| 70 | hostname = hostnames[0]  | 
            ||
| 71 | |||
| 72 | results = gmp.get_results(  | 
            ||
| 73 |             details=False, filter='host={0} and severity>0.0'.format(ip) | 
            ||
| 74 | )  | 
            ||
| 75 | |||
| 76 |         low = int(results.xpath('count(//result/threat[text()="Low"])')) | 
            ||
| 77 | sum_low += low  | 
            ||
| 78 | |||
| 79 |         medium = int(results.xpath('count(//result/threat[text()="Medium"])')) | 
            ||
| 80 | sum_medium += medium  | 
            ||
| 81 | |||
| 82 |         high = int(results.xpath('count(//result/threat[text()="High"])')) | 
            ||
| 83 | sum_high += high  | 
            ||
| 84 | |||
| 85 | best_os_cpe_report_id = asset.xpath(  | 
            ||
| 86 | 'host/detail/name[text()="best_os_cpe"]/../source/@id'  | 
            ||
| 87 | )[0]  | 
            ||
| 88 | |||
| 89 | table_data.append(  | 
            ||
| 90 | [hostname, ip, best_os_cpe_report_id, high, medium, low]  | 
            ||
| 91 | )  | 
            ||
| 92 | |||
| 93 | table = AsciiTable(table_data)  | 
            ||
| 94 | print(table.table + '\n')  | 
            ||
| 95 | print(  | 
            ||
| 96 |         'Summary of results from {3} to {4}\nHigh: {0}\nMedium: {1}' | 
            ||
| 97 |         '\nLow: {2}\n\n'.format( | 
            ||
| 98 | int(sum_high),  | 
            ||
| 99 | int(sum_medium),  | 
            ||
| 100 | int(sum_low),  | 
            ||
| 101 | from_date.isoformat(),  | 
            ||
| 102 | to_date.isoformat(),  | 
            ||
| 103 | )  | 
            ||
| 124 |