Skip to content

Instantly share code, notes, and snippets.

@SmileyChris
Created November 29, 2009 09:08
Show Gist options
  • Save SmileyChris/244861 to your computer and use it in GitHub Desktop.
Save SmileyChris/244861 to your computer and use it in GitHub Desktop.
A class which could be used to represent a missing context variable in a Django template
class NotFound(object):
"""
A class representing a context variable which was not found.
TODO: this could be extended to use the ``TEMPLATE_STRING_IF_INVALID``
Django setting for the ``__str__`` and ``__unicode__`` methods.
"""
def __init__(self, name=None):
"""
It is recommended (but not required) to create the NotFound instance
with a context variable name:
>>> var = NotFound(name='user')
"""
self.name = name
def __nonzero__(self):
"""
Return ``False`` for truth testing.
>>> var = NotFound()
>>> bool(var)
False
But instances don't *equal* False:
>>> var is False
False
>>> var == False
False
"""
return False
def __str__(self):
"""
Return an empty string when converting to a bytestring.
>>> var = NotFound()
>>> str(var)
''
"""
return ''
def __unicode__(self):
"""
Return an empty string when converting to a unicode string.
>>> var = NotFound()
>>> unicode(var)
u''
"""
return u''
def __eq__(self, other):
"""
Compare if the NotFound instance matches another object.
Instances don't equal instances with different names:
>>> var = NotFound(name='user')
>>> different_context_var = NotFound('other_user')
>>> var == different_context_var
False
Instances equal other instances with the same defined name:
>>> var == var
True
>>> same_context_var = NotFound('user')
>>> var == same_context_var
True
Instances never match if either name was unspecfied (or ``None``):
>>> match_nothing_var = NotFound()
>>> var == match_nothing_var
False
>>> match_nothing_var == match_nothing_var
False
"""
if not isinstance(other, NotFound):
return False
if other.name is None:
return False
return self.name == other.name
def __ne__(self, other):
"""
Compare if the NotFound instance does not match another object.
This is just the inverse of the ``__eq__`` method (the method is
required because setting ``__eq__`` does not imply the inverse
comparison reflection):
>>> var = NotFound(name='user')
>>> same_context_var = NotFound('user')
>>> different_context_var = NotFound('other_user')
>>> match_nothing_var = NotFound()
>>> var != different_context_var
True
>>> var != var
False
>>> var != same_context_var
False
>>> var != match_nothing_var
True
>>> match_nothing_var != match_nothing_var
True
"""
return not self.__eq__(other)
def __cmp__(self, other):
"""
Compare if the NotFound instance is greater or less than another
object.
>>> var = NotFound()
>>> var < 1
True
>>> var > 1
False
"""
return cmp(None, other)
def __repr__(self):
"""
Return a representation of the NotFound instance.
>>> var = NotFound()
>>> repr(var)
'NotFound()'
>>> var = NotFound(name='user')
>>> repr(var)
"NotFound(name='user')"
"""
args = []
if self.name is not None:
args = ['name=%r' % self.name]
return 'NotFound(%s)' % ', '.join(args)
def test():
"""
Run the doctests in this module.
"""
import doctest
doctest.testmod()
if __name__ == '__main__':
test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment