| Conditions | 3 |
| Total Lines | 52 |
| 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: utf8 |
||
| 37 | def parse_request(request): |
||
| 38 | """ |
||
| 39 | >>> request = b'''<?xml version="1.0" encoding="UTF-8"?> |
||
| 40 | ... <request protocol="3.0" |
||
| 41 | ... version="1.3.23.0" |
||
| 42 | ... ismachine="0" |
||
| 43 | ... sessionid="{5FAD27D4-6BFA-4daa-A1B3-5A1F821FEE0F}" |
||
| 44 | ... userid="{D0BBD725-742D-44ae-8D46-0231E881D58E}" |
||
| 45 | ... installsource="scheduler" |
||
| 46 | ... testsource="ossdev" |
||
| 47 | ... requestid="{C8F6EDF3-B623-4ee6-B2DA-1D08A0B4C665}"> |
||
| 48 | ... <os platform="win" version="6.1" sp="" arch="x64"/> |
||
| 49 | ... <app appid="{430FD4D0-B729-4F61-AA34-91526481799D}" version="1.2.23.0" nextversion="" lang="en" brand="GGLS" |
||
| 50 | ... client="someclientid" installage="39"> |
||
| 51 | ... <updatecheck/> |
||
| 52 | ... <ping r="1"/> |
||
| 53 | ... </app> |
||
| 54 | ... <app appid="{D0AB2EBC-931B-4013-9FEB-C9C4C2225C8C}" version="2.2.2.0" nextversion="" lang="en" brand="GGLS" |
||
| 55 | ... client="" installage="6"> |
||
| 56 | ... <updatecheck/> |
||
| 57 | ... <ping r="1"/> |
||
| 58 | ... </app> |
||
| 59 | ... </request>''' |
||
| 60 | >>> request_obj = parse_request(request) |
||
| 61 | >>> request_obj.get('version') |
||
| 62 | '1.3.23.0' |
||
| 63 | >>> request_obj.os.get('platform') |
||
| 64 | 'win' |
||
| 65 | >>> request_obj.app.get('appid') |
||
| 66 | '{430FD4D0-B729-4F61-AA34-91526481799D}' |
||
| 67 | >>> request_obj.app.find('updatecheck') |
||
| 68 | '' |
||
| 69 | >>> request_obj.keys() |
||
| 70 | ['protocol', 'version', 'ismachine', 'sessionid', 'userid', 'installsource', 'testsource', 'requestid'] |
||
| 71 | >>> request_obj.values() |
||
| 72 | ['3.0', '1.3.23.0', '0', '{5FAD27D4-6BFA-4daa-A1B3-5A1F821FEE0F}', '{D0BBD725-742D-44ae-8D46-0231E881D58E}', 'scheduler', 'ossdev', '{C8F6EDF3-B623-4ee6-B2DA-1D08A0B4C665}'] |
||
| 73 | >>> request_obj.tag |
||
| 74 | 'request' |
||
| 75 | >>> for app in request_obj.find('app'): |
||
| 76 | ... app.get('appid') |
||
| 77 | ... |
||
| 78 | '{430FD4D0-B729-4F61-AA34-91526481799D}' |
||
| 79 | '{D0AB2EBC-931B-4013-9FEB-C9C4C2225C8C}' |
||
| 80 | """ |
||
| 81 | |||
| 82 | obj = objectify.fromstring(request, parser) |
||
| 83 | |||
| 84 | # Check if this is coming from update_engine, which handles machines not applications |
||
| 85 | if obj.get('userid') == None and obj.app.get('machineid') != None: |
||
| 86 | obj.set('userid', obj.app.get('machineid')) |
||
| 87 | |||
| 88 | return obj |
||
| 89 | |||
| 93 |