| Conditions | 6 |
| Total Lines | 62 |
| Code Lines | 26 |
| 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 | #! /usr/bin/env python |
||
| 57 | def getShortDescr(function): |
||
| 58 | """ |
||
| 59 | Uses the given function name and attemps to get a short description |
||
| 60 | from the php manual. Returns a string with "Nothing found" if nothing |
||
| 61 | was found, or a pretty string with the information requested. |
||
| 62 | """ |
||
| 63 | |||
| 64 | # If the function description is cached, return it |
||
| 65 | cached = cacheLookup(function) |
||
| 66 | if(cached is not None): |
||
| 67 | return cached |
||
| 68 | |||
| 69 | # Replace '_' with '-' |
||
| 70 | function = function.replace('_', '-')
|
||
| 71 | |||
| 72 | # Complete URL to the manual page (if it exists) |
||
| 73 | url = BASE_URL+function+ENDING_URL |
||
| 74 | |||
| 75 | # Try to fetch the site. If a incorrect function name is |
||
| 76 | # used, this will fail and print an error code. |
||
| 77 | siteData = None |
||
| 78 | try: |
||
| 79 | #print('Start to read') DEBUG
|
||
| 80 | siteData = urllib2.urlopen(url) |
||
| 81 | #print('Done reading.') DEBUG
|
||
| 82 | except urllib2.HTTPError, e: |
||
| 83 | print(e.code) |
||
| 84 | except urllib2.URLError, e: |
||
| 85 | print(e.args) |
||
| 86 | |||
| 87 | # This is the default value that will be returned if nothing is found. |
||
| 88 | result = 'Found nothing.' |
||
| 89 | |||
| 90 | # Actually parse and find the text |
||
| 91 | if siteData is not None: |
||
| 92 | # Use SoupStrainer to only parse what I need |
||
| 93 | tagsWithClass = SoupStrainer('p',{'class': 'refpurpose'})
|
||
| 94 | |||
| 95 | #print('Done creating SoupStrainer.') DEBUG
|
||
| 96 | |||
| 97 | # Create the soup object, using the SoupStrainer. |
||
| 98 | # This is what takes the most time (hence the .txt-file cache) |
||
| 99 | soup = BeautifulSoup(siteData, "lxml", parse_only=tagsWithClass) |
||
| 100 | |||
| 101 | #print('Done creating BeautifulSoup.') DEBUG
|
||
| 102 | |||
| 103 | # Get the specific tag I need |
||
| 104 | shortDescrPtag = soup.find("p", { "class" : "refpurpose" })
|
||
| 105 | |||
| 106 | #print('Done finding tag.') DEBUG
|
||
| 107 | try: |
||
| 108 | # Put the text without html tags in my fancy string |
||
| 109 | result = 'PHP-manualen: ' + shortDescrPtag.get_text() + ' - ' + url |
||
| 110 | result = result.replace('\n', '')
|
||
| 111 | result = result.encode('utf-8')
|
||
| 112 | # Cache the result (i.e. save it to the cache txt-file) |
||
| 113 | saveToCache(result) |
||
| 114 | except: |
||
| 115 | result = 'Found nothing.' |
||
| 116 | |||
| 117 | # Return the result |
||
| 118 | return result |
||
| 119 | |||
| 122 |