[Patches] Consistent Case: A Simple Linter

Moshe Zadka Moshe Zadka <moshez@math.huji.ac.il>
Sat, 27 May 2000 04:11:33 +0300 (IDT)


Hi.

This sounds like a cute thing to put in Tools/ or Demo/
(Code follows)

Legal:
I confirm that, to the best of my knowledge and belief, this
contribution is free of any claims of third parties under
copyright, patent or other rights or interests ("claims").  To
the extent that I have any such claims, I hereby grant to CNRI a
nonexclusive, irrevocable, royalty-free, worldwide license to
reproduce, distribute, perform and/or display publicly, prepare
derivative versions, and otherwise use this contribution as part
of the Python software and its related documentation, or any
derivative versions thereof, at no cost to CNRI or its licensed
users, and to authorize others to do so.

I acknowledge that CNRI may, at its sole discretion, decide
whether or not to incorporate this contribution in the Python
software and its related documentation.  I further grant CNRI
permission to use my name and other identifying information
provided to CNRI by me for use in connection with the Python
software and its related documentation.



--
Moshe Zadka <moshez@math.huji.ac.il>
http://www.oreilly.com/news/prescod_0300.html
http://www.linux.org.il -- we put the penguin in .com

---------- Forwarded message ----------
Date: Wed, 24 May 2000 08:23:26 +0300 (IDT)
From: Moshe Zadka <moshez@math.huji.ac.il>
To: python-list@python.org
Subject: Consistent Case: A Simple Linter


# Released to the public domain by Moshe Zadka
# use like: get_identifiers <module1> <module2> ....
import parser, keyword

def get_from_tuple(l, so_far = None):
        if so_far is None:
                so_far = []
        if l[0] == 1 and not keyword.iskeyword(l[1]):
                so_far.append(l[1:])
        else:
                for t in l:
                        if type(t) is type(()):
                                get_from_tuple(t, so_far)
        return so_far

def get_from_file(file, res=None):
        tup = parser.suite(file.read()).totuple(1)
        return get_from_tuple(tup, res)

def by_case(words_by_file):
        import string
        res = {}
        for file, words in words_by_file.items():
                for word, lineno in words:
                        lword = string.lower(word)
                        if not res.has_key(lword):
                                res[lword] = []
                        res[lword].append((lineno, file, word))
        return res

def get_suspicious(words_by_case):
        susp = []
        for word, cases in words_by_case.items():
                c = cases[0][2]
                for tup in cases[1:]:
                        if  tup[2] != c:
                                susp.append(word)
                                break
        return susp
def main():
        import sys
        res = {}
        for file in sys.argv[1:]:
                f = open(file)
                try:
                        res[file] = get_from_file(f)
                finally:
                        f.close()
        res = by_case(res)
        susp = get_suspicious(res)
        susp.sort()
        for word in susp:
                print word, 'is used with incosistent case:'
                for line, file, case in res[word]:
                        print '\t%(file)s[%(line)d]: %(case)s' % vars()

if __name__=='__main__':
        main()


--
Moshe Zadka <moshez@math.huji.ac.il>
http://www.oreilly.com/news/prescod_0300.html
http://www.linux.org.il -- we put the penguin in .com