Last active
December 18, 2015 20:38
-
-
Save iwillspeak/5841159 to your computer and use it in GitHub Desktop.
Represents a read-only access to a dictionary through the . operator. Used
to allow easy access to options and suchlike. Should be both Python 2.7 and
3.x compatible.
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
class Namespace(object): | |
"""Namespace | |
Represents a read-only access to a dictionary through the . operator. Used | |
to allow easy access to options and suchlike. Should be both Python 2.7 and | |
3.x compatible. | |
""" | |
def __init__(self, dict): | |
self.__dict__.update(dict) | |
def __repr__(self): | |
return repr(self.__dict__) | |
def __contains__(self, item): | |
return item in self.__dict__ | |
# Not hashable | |
__hash__ = None | |
def __cmp__(self, other): | |
return self.__neq__(other) | |
def __eq__(self, other): | |
try: | |
return self.__dict__ == other.__dict__ | |
except: | |
return False | |
def __neq__(self, other): | |
return not self.__eq__(other) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment