Skip to content

Instantly share code, notes, and snippets.

@jzempel
Created September 8, 2012 22:06
Show Gist options
  • Save jzempel/3680194 to your computer and use it in GitHub Desktop.
Save jzempel/3680194 to your computer and use it in GitHub Desktop.
Constant Duplicates
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