Created
November 6, 2013 01:52
-
-
Save jacobh/7329631 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Proposal: | |
def _get_state_log(self, memo={}): | |
print memo | |
# return cached log if it appears to be correct | |
if 'log' in memo: | |
if self.status == memo['log'].state: | |
print 'returning cached value' | |
return memo['log'] | |
print 'executing query' | |
log = StateLog.objects.for_(self).order_by('-id')[0] | |
memo['log'] = log | |
return log | |
... | |
In [3]: proposal._get_state_log() | |
{} | |
executing query | |
Out[3]: <StateLog: StateLog object> | |
In [4]: proposal._get_state_log().state | |
{'log': <StateLog: StateLog object>} | |
executing query | |
Out[4]: u'submitted' | |
In [5]: proposal.approve() | |
In [6]: proposal._get_state_log() | |
{'log': <StateLog: StateLog object>} | |
executing query | |
Out[6]: <StateLog: StateLog object> | |
In [7]: proposal._get_state_log() | |
{'log': <StateLog: StateLog object>} | |
returning cached value | |
Out[7]: <StateLog: StateLog object> | |
In [8]: proposal._get_state_log() | |
{'log': <StateLog: StateLog object>} | |
returning cached value | |
Out[8]: <StateLog: StateLog object> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment