Skip to content

Instantly share code, notes, and snippets.

/gist:8947229
Created Feb 12, 2014

Embed
What would you like to do?
Nearly half of the functions/methods I scanned were zero args (48.7). Which was more than I expected. I haven’t dug enough to see if there’s a flock of canaries in there or not. The code to produce that table is here:import inspect
from collections import Counter
import importlib
import sys
import os
def countRoutine(routine):
try:
spec = inspect.getfullargspec(routine)
except TypeError:
return -1
count = len(spec.args)
if len(spec.args) > 0 and spec.args[0] in {'self', 'cls'}:
count -= 1
if spec.defaults is not None:
count -= len(spec.defaults)
return count
def analyze(targetModule=inspect):
histogram = Counter()
for name, f in inspect.getmembers(targetModule, inspect.isfunction):
histogram[countRoutine(f)] += 1
for _, c in inspect.getmembers(targetModule, inspect.isclass):
for name, m in inspect.getmembers(c, inspect.isroutine):
histogram[countRoutine(m)] += 1
ascending = [' '.join((str(a), str(b))) for a, b in sorted(histogram.items())]
return ' '.join(ascending)
def printHistogram(histogram):
total = sum(histogram.values())
for argCount, occurences in sorted(histogram.items()):
print('{:2} x {:5} ({:4.1f}%)'.format(argCount, occurences, (occurences * 100 / total)))
def analyzeAll():
blacklist = {'ctypes.test', 'idlelib.idle', 'test.', 'tkinter.__main__'}
base = '/Users/travisg/Downloads/pypy3-2.1-beta1-osx64/lib-python/3'
histogram = Counter()
keepGoing = 0
for root, dirs, files in os.walk(base):
for file in files:
if file.endswith('.py') and not file.startswith('__init__'):
path = root[len(base):].split('/')[1:] + [os.path.splitext(file)[0]]
path = '.'.join(path)
if any(path.startswith(prefix) for prefix in blacklist):
continue
keepGoing += 1
if not keepGoing:
printHistogram(histogram)
return
print(keepGoing, path)
r, w = os.pipe()
pid = os.fork()
if pid:
os.close(w)
r = os.fdopen(r)
result = r.read()
os.waitpid(pid, 0)
else:
os.close(r)
w = os.fdopen(w, 'w')
module = importlib.import_module(path)
w.write(analyze(module))
w.close()
sys.exit(0)
print(result)
numbers = (int(a) for a in result.split())
histogram.update(dict(zip(numbers, numbers)))
printHistogram(histogram)
if __name__ == '__main__':
analyzeAll()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.