Here's my code and output for the curious. I'm sure someone will come up with a five-line version. :)

import os, time

def main():

    def_str = 'def '

    line_limit = 5

    call_tally = []

    import_str = 'from bg_helper import '

    path = 'scripts'

    startTime = time.time()

    # pull call names from helper file

    with open(os.path.join(path, 'bg_helper.py')) as f:

        for line in f:

            search = line.find(def_str)

            if search != -1:

                call_tally.append([(line[len(def_str):].split('('))[0], 0])

    call_tally.sort()

    # walk through the scripts directory

    fileList = next(os.walk(path))[2]

    fileList.remove('bg_helper.py')

    fileList.remove('bg_helper.pyc')

    # check files for import statements

    for filename in fileList:

        i = 0

        with open(os.path.join(path, filename)) as f:

            for line in f:

                if i < line_limit:

                    search = line.find(import_str)

                    if search != -1:

                        found = line[len(import_str):].strip('\n').split(', ')

                        found.sort()

                        for found in enumerate(found):

                            for tally in enumerate(call_tally):

                                if tally[1][0] == found[1]:

                                   tally[1][1] += 1

                    i += 1

                else:

                    break

    # print the results

    print 'FUNCTION CALLS\t\tTALLY\tNOTES\n'

    for tally in enumerate(call_tally):

        print tally[1][0],'\t\t', tally[1][1],

        if tally[1][1] == 0:

            print '\t** NO CALLS **'

        else:

            print ''

    print '\nEvaluated',len(fileList),'files in',

    print '{0:.1g}'.format(time.time() - startTime),'seconds\n'

if __name__ == "__main__":

    main()



FUNCTION CALLS          TALLY   NOTES

card_faces              1

pick_card               1

replay_game             3

roll_dice               2

slot_faces              0       ** NO CALLS **

Evaluated 10 files in 0.08 seconds

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to