| Conditions | 16 | 
| Total Lines | 57 | 
| Code Lines | 44 | 
| 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:
Complex classes like ige.IMarshal.IMarshal.__encodeV11() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | # | ||
| 71 | #try: | ||
| 72 | # from ICompressScheme import compress | ||
| 73 | #except ImportError: | ||
| 74 | compress = {} | ||
| 75 | |||
| 76 | decompress = {} | ||
| 77 | |||
| 78 | for key, value in compress.items(): | ||
| 79 | decompress[str(value)] = key | ||
| 80 | |||
| 81 | # statistics | ||
| 82 | import cPickle as pickle | ||
| 83 | |||
| 84 | class Stats: | ||
| 85 | def __init__(self): | ||
| 86 |         self.data = {} | ||
| 87 | self.total = 0 | ||
| 88 | self.hits = 0 | ||
| 89 | self.totalBytes = 0 | ||
| 90 | self.savedBytes = 0 | ||
| 91 | self.encBytes = 0 | ||
| 92 | self.zipBytes = 0 | ||
| 93 | |||
| 94 | # load stats TODO remove profiling code | ||
| 95 | # TODO change dir according to config file | ||
| 96 | try: | ||
| 97 |     fh = open('var/marshal.stats.data', 'rb') | ||
| 98 | stats = pickle.load(fh) | ||
| 99 | fh.close() | ||
| 100 | except IOError, e: | ||
| 101 | stats = Stats() | ||
| 102 | except EOFError, e: | ||
| 103 | stats = Stats() | ||
| 104 | |||
| 105 | def saveStats(directory): | ||
| 106 | print 'Saving IMarshal statistics' | ||
| 107 | # stats | ||
| 108 | fh = open(os.path.join(directory, 'marshal.stats.data'), 'wb') | ||
|  | |||
| 109 | pickle.dump(stats, fh, 1) | ||
| 110 | fh.close() | ||
| 111 | # various data | ||
| 112 | keys = [] | ||
| 113 | for key in stats.data.keys(): | ||
| 114 | keys.append((len(key) * stats.data[key],key)) | ||
| 115 | keys.sort() | ||
| 116 | keys.reverse() | ||
| 117 | fstats = open(os.path.join(directory, 'marshal.stats'), 'w') | ||
| 118 | fscheme = open(os.path.join(directory, 'marshal.cscheme'), 'w') | ||
| 119 | fpysrc = open(os.path.join(directory, 'marshal.cscheme.py'), 'w') | ||
| 120 |     print >> fpysrc, 'compress = {' | ||
| 121 | print >> fstats, '# Summary' | ||
| 122 | print >> fstats, '# Total strings:', stats.total | ||
| 123 | print >> fstats, '# Compressed strings:', stats.hits | ||
| 124 | print >> fstats, '# Uncompressed strings:', stats.total - stats.hits | ||
| 125 | print >> fstats, '# Ratio:', stats.hits / stats.total * 100L, '%' | ||
| 126 | print >> fstats, '# Uncompressed size:', stats.totalBytes | ||
| 127 | print >> fstats, '# Compressed size:', stats.totalBytes - stats.savedBytes | ||
| 128 | print >> fstats, '# Saved bytes:', stats.savedBytes | ||
| 184 |