Skip to content

Instantly share code, notes, and snippets.

@jzempel
Created September 8, 2012 22:02
Show Gist options
  • Save jzempel/3680140 to your computer and use it in GitHub Desktop.
Save jzempel/3680140 to your computer and use it in GitHub Desktop.
Constant Diff
def diff(module1, module2):
"""Determine the difference in constant definitions between two modules.
:param module1: First module with constants.
:param module2: Second module with constants.
"""
_and = {}
_or = {}
_xor = {module1: {}, module2: {}}
for attribute in dir(module1):
if attribute == attribute.upper():
constant = getattr(module2, attribute, None)
if constant is None:
_xor[module1][attribute] = constant
elif constant == getattr(module1, attribute):
_and[attribute] = constant
else:
_or[attribute] = constant
for attribute in dir(module2):
if attribute == attribute.upper():
constant = getattr(module1, attribute, None)
if constant is None:
_xor[module2][attribute] = constant
if constant != getattr(module2, attribute):
_or[attribute] = constant
return (_and, _or, _xor)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment