Skip to content

Instantly share code, notes, and snippets.

@iwillspeak
Last active December 18, 2015 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iwillspeak/5841159 to your computer and use it in GitHub Desktop.
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.
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