Skip to content

Instantly share code, notes, and snippets.

@VDigitall
Forked from daltonmatos/mocktrue.py
Created March 10, 2017 12:30
Show Gist options
  • Save VDigitall/015080248accd9ea7b8e4b1d77074781 to your computer and use it in GitHub Desktop.
Save VDigitall/015080248accd9ea7b8e4b1d77074781 to your computer and use it in GitHub Desktop.
Hack to mock the python True object
import mock
class AlmostAlwaysTrue(object):
def __init__(self, total_iterations=1):
self.total_iterations = total_iterations
self.current_iteration = 0
def __nonzero__(self):
if self.current_iteration < self.total_iterations:
self.current_iteration += 1
return bool(1)
return bool(0)
with mock.patch('__builtin__.True', AlmostAlwaysTrue(4)):
while True:
print "Loop!"
daltonmatos@jetta wsgid % python mocktrue.py
Loop!
Loop!
Loop!
Loop!
daltonmatos@jetta wsgid %
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment