Skip to content

Instantly share code, notes, and snippets.

@pranavraja
Created May 31, 2013 01:59
Show Gist options
  • Save pranavraja/5682538 to your computer and use it in GitHub Desktop.
Save pranavraja/5682538 to your computer and use it in GitHub Desktop.
Count length of methods in iOS codebases
import sys
from collections import defaultdict
def name_of_method(signature):
start_method_name = signature.find(')') + 1
end_method_name = signature.rfind('{')
return signature[start_method_name:end_method_name]
if __name__ == '__main__':
if len(sys.argv) <= 1:
print 'Usage: python {} filename [filename2] ...'.format(__file__)
sys.exit(0)
filenames = sys.argv[1:]
for filename in filenames:
with open(filename) as f:
methods = defaultdict(int)
current_method = None
for line in f:
if line.startswith('-') or line.startswith('+'):
# method declaration
current_method = line
if not current_method:
continue # Ignore lines not inside a method
if not line.strip():
continue # Ignore blank lines
methods[current_method] += 1
if line.startswith('}'):
current_method = None
for method, lines in methods.items():
print '{}: {}\t{}'.format(filename, name_of_method(method), lines)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment