Created
September 8, 2012 22:06
-
-
Save jzempel/3680194 to your computer and use it in GitHub Desktop.
Constant Duplicates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
def duplicates(module): | |
"""Determine if the given module contains multiple definitions for the | |
same constant. | |
:param module: The module to check. | |
""" | |
ret_val = set() | |
constants = {} | |
filename = "{0}.py".format(module.__name__) | |
pattern = re.compile(r"^(?P<constant>([A-Z0-9_])+)[ \t]*=.*$") | |
with open(filename) as module_py: | |
for line in module_py.readlines(): | |
match = pattern.match(line) | |
if match: | |
constant = match.group("constant") | |
attribute = getattr(module, constant) | |
if constant in constants: | |
ret_val.add(constant) | |
else: | |
constants[constant] = attribute | |
return ret_val |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment