import csv, math, json rows = {r['corpus']: r for r in csv.DictReader(open('metrics.csv'))} REF = 'satoshi_v0.1.0' def f(r, k): v = r.get(k, '') try: return float(v) except: return None # Binary checklist of Satoshi v0.1 habits: (label, feature, test) checks = [ ('Spaces (not tabs) for indentation', 'tab_indent_ratio', lambda v: v < 0.2), ('4-column indent unit', 'dominant_space_indent', lambda v: v == 4), ('Allman braces (opening { on own line)', 'allman_ratio', lambda v: v > 0.8), ('Never "} else" on one line', 'cuddled_else_per_kloc', lambda v: v < 1), ('Space between if/for/while and "("', 'kw_space_ratio', lambda v: v > 0.9), ('Pointer/ref attached to type ("CNode* p")', 'ptr_type_side_ratio', lambda v: v > 0.7), ('// line comments, not /* */', 'line_comment_ratio', lambda v: v > 0.7), ('Satoshi-style Hungarian (n/f/str/vch/map/p...) >20% of declared names', 'decl_satoshi_prefix_pct', lambda v: v > 20), ('No MS-Windows Hungarian (dw/lp/sz/b/h...) >10%', 'decl_win_hungarian_pct', lambda v: v < 10), ('Little snake_case (<5% of declared names)', 'decl_snake_pct', lambda v: v < 5), ('No m/m_ member prefix', 'decl_m_prefix_pct', lambda v: v < 2), ('No goto (<1 per KLOC)', 'goto_per_kloc', lambda v: v < 1), ('Rare "== NULL" comparisons (<5 per KLOC)', 'eq_null_per_kloc', lambda v: v < 5), ('Few #defines (<10 per KLOC)', 'define_per_kloc', lambda v: v < 10), ('printf-family debug output (>10 per KLOC)', 'printf_family_per_kloc', lambda v: v > 10), ('One-line inline bodies "{ return x; }" (>5 per KLOC)', 'oneline_body_per_kloc', lambda v: v > 5), ('Lowercase hex literals', 'hex_lower_ratio', lambda v: v > 0.7), ('Named 64-bit int typedef used (>3 per KLOC)', 'int64_name_per_kloc', lambda v: v > 3), ('C-prefixed class names (C++ only)', 'class_C_prefix_ratio', lambda v: v > 0.5), ('Custom foreach/loop/lock-block macros', None, None), ('No space between function name and "(" in calls', 'call_space_before_paren_ratio', lambda v: v < 0.1), ('Return type on same line as function name (not GNU style)', 'gnu_return_type_own_line_ratio', lambda v: v < 0.1), ('PascalCase function/method names (>60%)', 'func_name_pascal_pct', lambda v: v > 60), ('Boolean flags prefixed f (fFoo), not b (bFoo)', 'BOOLPREFIX', None), ] out = {} for name, r in rows.items(): res = [] for label, k, t in checks: if k == 'BOOLPREFIX': fc, bc = f(r,'bool_f_prefix') or 0, f(r,'bool_b_prefix') or 0 res.append((label, f'f={int(fc)},b={int(bc)}', None if fc+bc < 5 else fc > bc)); continue if k is None: v = (f(r,'foreach_macro_per_kloc') or 0) + (f(r,'loop_macro_per_kloc') or 0) + (f(r,'critical_block_per_kloc') or 0) res.append((label, round(v,2), v > 1)); continue v = f(r, k) res.append((label, v, None if v is None else bool(t(v)))) out[name] = res names = list(rows) print('| Habit (Satoshi v0.1.0) | ' + ' | '.join(names) + ' |') print('|---|' + '---|' * len(names)) for i, (label, _, _) in enumerate(checks): cells = [] for n in names: lab, v, ok = out[n][i] cells.append('n/a' if ok is None else ('✔' if ok else '✘') + f' ({v})') print(f'| {label} | ' + ' | '.join(cells) + ' |') print() for n in names: applicable = [x for x in out[n] if x[2] is not None] print(n, sum(1 for x in applicable if x[2]), '/', len(applicable)) # z-scored distance on continuous features (language-neutral subset) feats = ['tab_indent_ratio','allman_ratio','kw_space_ratio','comma_space_ratio','assign_spaced_ratio','ptr_type_side_ratio','line_comment_ratio', 'hex_lower_ratio','comment_lines_pct','mean_line_len','p95_line_len','decl_satoshi_prefix_pct','decl_win_hungarian_pct','decl_lowercase_word_pct', 'decl_snake_pct','decl_camel_lower_pct','decl_m_prefix_pct','cuddled_else_per_kloc','oneline_body_per_kloc','goto_per_kloc','eq_null_per_kloc', 'define_per_kloc','return_paren_per_kloc','printf_family_per_kloc','preinc_ratio','call_space_before_paren_ratio','gnu_return_type_own_line_ratio','func_name_pascal_pct','func_name_camel_pct'] # Standardisation stats are frozen to the ORIGINAL full-file pool so that distances stay comparable across report versions pool = [n for n in ['back_hashcash_1.21','finney_rpow_1.2.0','leroux_e4m_2.01','mccaleb_newcoin_2011','mixmaster_3.0_project','satoshi_nov2008','satoshi_v0.1.0','weidai_cryptopp_5.5.2'] if n in rows] stats = {} for k in feats: vals = [f(rows[n],k) for n in pool if f(rows[n],k) is not None] mu = sum(vals)/len(vals); sd = math.sqrt(sum((x-mu)**2 for x in vals)/len(vals)) or 1 stats[k] = (mu, sd) print('\nStandardized Euclidean distance to', REF, '(lower = more similar), over', len(feats), 'features:') d = {} for n in names: s = 0; c = 0 for k in feats: a, b = f(rows[n],k), f(rows[REF],k) if a is None or b is None: continue s += ((a-b)/stats[k][1])**2; c += 1 d[n] = round(math.sqrt(s / c), 3) for n, v in sorted(d.items(), key=lambda x: x[1]): print(f' {n:28s} {v}') groups = { 'layout/formatting': ['tab_indent_ratio','allman_ratio','kw_space_ratio','comma_space_ratio','assign_spaced_ratio','ptr_type_side_ratio','cuddled_else_per_kloc','oneline_body_per_kloc','call_space_before_paren_ratio','gnu_return_type_own_line_ratio','mean_line_len','p95_line_len','return_paren_per_kloc'], 'naming': ['decl_satoshi_prefix_pct','decl_win_hungarian_pct','decl_lowercase_word_pct','decl_snake_pct','decl_camel_lower_pct','decl_m_prefix_pct','func_name_pascal_pct','func_name_camel_pct'], 'comments/idioms': ['line_comment_ratio','comment_lines_pct','hex_lower_ratio','goto_per_kloc','eq_null_per_kloc','define_per_kloc','printf_family_per_kloc','preinc_ratio'], } print('\nSub-distances to', REF) print('corpus'.ljust(28), ' '.join(g.ljust(20) for g in groups)) for n in sorted(names, key=lambda x: d[x]): out_ = [] for g, fs in groups.items(): s = 0; c = 0 for k in fs: a, b = f(rows[n],k), f(rows[REF],k) if a is None or b is None: continue s += ((a-b)/stats[k][1])**2; c += 1 out_.append(str(round(math.sqrt(s/c),3)).ljust(20)) print(n.ljust(28), ' '.join(out_))