Changeset 52 for pycodebattler/trunk
- Timestamp:
- 12/18/10 01:39:00 (14 years ago)
- Location:
- pycodebattler/trunk/pycodebattler
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
pycodebattler/trunk/pycodebattler/score.py
r47 r52 4 4 import os 5 5 import sys 6 import zlib 6 7 import token 7 8 import parser … … 11 12 12 13 14 def _compseq(seq, level): 15 c = zlib.compressobj(level) 16 return ''.join([c.compress(chunk) for chunk in seq] + [c.flush()]) 17 18 19 def _maxstrlen(seq, iferr=0): 20 try: 21 return max(len(l) for l in seq) 22 except: 23 return iferr 24 25 13 26 class Scorer: 14 27 def __init__(self, lines): 28 self._code = ''.join(lines) 29 self._ccode = _compseq(lines, 9) 15 30 self._lines = lines 16 self._lnum = len(lines) 17 self._code = ''.join(lines) 18 self._bytes = len(self._code) 31 self._nlines = [l for l in lines 32 if l.strip() and not l.strip().startswith('#')] 19 33 20 self._nlines = [l for l in lines if not l.strip().startswith('#')] 21 self._nlnum = len(self._nlines) 22 self._nbytes = sum(len(l) for l in self._nlines) 34 self._astpl = parser.suite(self.code()).totuple() 35 self._token_cache = {} 23 36 24 self._st = parser.suite(self._code) 25 self._p8c = self._pep8count() 26 self._cpsum, self._cplen = self._complexity() 37 self.complexity() 38 self.style_errors() 27 39 28 def _complexity(self, item='all'): 40 def tupletree(self): 41 return self._astpl 42 43 def code(self): 44 return self._code 45 46 def lines(self): 47 return self._lines 48 49 def uncommented_lines(self): 50 return self._nlines 51 52 def bytes(self): 53 return len(self.code()) or 1 54 55 def uncommented_bytes(self): 56 return sum(len(l) for l in self.uncommented_lines()) or 1 57 58 def rows(self): 59 return len(self.lines()) or 1 60 61 def uncommented_rows(self): 62 return len(self.uncommented_lines()) or 1 63 64 def compressibility(self, level=6): 65 return float(len(self._ccode)) / self.bytes() 66 67 def token_bytes(self, types): 68 def _bytes(stp, bytes=0): 69 if not stp: 70 return bytes 71 if stp[0] in types: 72 bytes += len(stp[1]) 73 else: 74 for p in stp[1:]: 75 bytes = _bytes(p, bytes) 76 return bytes 77 78 types = tuple(types) 79 80 try: 81 ret = self._token_cache[types] 82 except KeyError: 83 ret = self._token_cache[types] = _bytes(self.tupletree()) 84 85 return ret 86 87 def complexity(self, item='all', iferr=(256, 1)): 29 88 try: 30 89 stat = cc.measure_complexity(self._code, item) 31 90 ccpp = cc.PrettyPrinter(None, True) 32 91 cplx = zip(*ccpp.flatten_stats(stat))[-1] 33 return sum(cplx) + 1, len(cplx) + 1 92 self._ccsum = sum(cplx) or 1 93 self._cclen = len(cplx) or 1 34 94 except: 35 return 256, 1 95 self._ccsum, self._cclen = iferr 96 return self._ccsum, self._cclen 36 97 37 def _pep8count(self, name=''):98 def style_errors(self, name='', iferr=256): 38 99 pep8.process_options([name]) 39 100 p8ck = pep8.Checker(None) … … 44 105 tmpfp = sys.stdout 45 106 sys.stdout = open(os.devnull, 'w') 46 47 try: # nest for 2.4 or earlier 48 return p8ck.check_all() + 1 49 except: 50 return 256 107 self._estyle = p8ck.check_all() or 1 108 except: 109 self._estyle = iferr 51 110 finally: 52 111 sys.stdout = tmpfp 112 return self._estyle 53 113 54 def _parsebytes(self, types): 55 def litbytes(stp, bytes=0): 56 if not stp: 57 return bytes 58 if stp[0] in types: 59 bytes += len(stp[1]) 60 else: 61 for p in stp[1:]: 62 bytes = litbytes(p, bytes) 63 return bytes 64 return litbytes(self._st.totuple()) 114 def complex_score(self, coefficient=32): 115 maxlen = _maxstrlen(self.lines(), 256) 116 score = self._cclen * coefficient 117 score /= (maxlen / (coefficient * 2)) or 1 118 score /= (self._ccsum / ((coefficient / 3) or 1)) or 1 119 score *= self.compressibility() 120 return int(score) or 1 65 121 66 @staticmethod 67 def _strlen_max(seq, iferr=0): 68 try: 69 return max(len(l) for l in seq) 70 except: 71 return iferr 122 def style_score(self, coefficient=64): 123 data_bytes = self.token_bytes([token.NUMBER, token.STRING, 124 token.NAME, token.COLON]) 125 score = self.uncommented_bytes() - data_bytes 126 score /= (self.uncommented_rows() / coefficient) or 1 127 score /= self._estyle 128 score *= (coefficient / 12) or 1 129 score *= self.compressibility() 130 return int(score) or 1 72 131 73 def pep8_score(self): 74 bytes = self._nbytes - self._parsebytes( 75 [token.NUMBER, token.STRING, token.NAME, token.COLON]) 76 return (bytes / ((self._nlnum / 256) + 1) / self._p8c) + 1 132 def lines_score(self, coefficient=4): 133 maxlen = _maxstrlen(self.lines(), 256) 134 data_bytes = self.token_bytes([token.NUMBER, token.STRING, 135 token.NAME, token.COLON]) 136 score = self.uncommented_bytes() - data_bytes 137 score /= (maxlen / self.uncommented_rows()) or 1 138 score /= self.uncommented_rows() 139 score *= coefficient 140 score *= self.compressibility() 141 return int(score) or 1 77 142 78 def complex_score(self): 79 bmax = self._strlen_max(self._lines, 256) 80 return (32 * self._cplen / ((bmax / 79) + 1) / \ 81 ((self._cpsum / 12) + 1)) + 1 143 def const_score(self, coefficient=2): 144 const_bytes = self.token_bytes([token.NUMBER, token.STRING]) or 1 145 score = self.uncommented_bytes() / const_bytes 146 score *= coefficient 147 score *= self.compressibility() 148 return int(score) or 1 82 149 83 def lines_score(self): 84 bmax = self._strlen_max(self._lines, 256) 85 bytes = self._nbytes - self._parsebytes( 86 [token.NUMBER, token.STRING, token.NAME, token.COLON]) 87 return (bytes / ((bmax / (self._nlnum + 1)) + 1) / \ 88 (self._nlnum + 1)) + 1 150 def colon_score(self, coefficient=1): 151 colon_bytes = self.token_bytes([token.COLON]) or 1 152 score = self.uncommented_bytes() 153 score /= (score / colon_bytes) or 1 154 score *= coefficient 155 score *= self.compressibility() 156 return int(score) or 1 89 157 90 def const_score(self): 91 cbytes = self._parsebytes([token.NUMBER, token.STRING]) + 1 92 return (self._nbytes + 1) // cbytes 93 94 def colon_score(self): 95 bytes = self._nbytes + 1 96 cbytes = self._parsebytes([token.COLON]) + 1 97 return (bytes // ((bytes // cbytes) + 1)) + 1 98 99 def name_score(self): 100 bytes = self._nbytes + 1 101 nbytes = self._parsebytes([token.NAME]) + 1 102 return bytes // ((nbytes / 16) + 1) 158 def name_score(self, coefficient=16): 159 name_bytes = self.token_bytes([token.NAME]) or 1 160 score = self.uncommented_bytes() 161 score /= (name_bytes / coefficient) or 1 162 score *= self.compressibility() 163 return int(score) or 1 -
pycodebattler/trunk/pycodebattler/warrior.py
r47 r52 59 59 self._name = name 60 60 self._skill_list = {} 61 self._max_hitpoint = self._hitpoint = scorer. pep8_score()61 self._max_hitpoint = self._hitpoint = scorer.style_score() 62 62 self._max_skillpoint = self._skillpoint = scorer.name_score() 63 63 self._strength = scorer.complex_score()
Note: See TracChangeset
for help on using the changeset viewer.