Simplify run function

This commit is contained in:
vrde 2017-01-26 17:12:35 +01:00
parent ca49718d7e
commit 54544f66a0
No known key found for this signature in database
GPG Key ID: 6581C7C39B3D397D
2 changed files with 17 additions and 2 deletions

View File

@ -180,8 +180,11 @@ class Lazy:
def run(self, instance):
last = instance
for method, (args, kwargs) in zip(self.stack[::2], self.stack[1::2]):
last = getattr(last, method)(*args, **kwargs)
for item in self.stack:
if isinstance(item, str):
last = getattr(last, item)
else:
last = last(*item[0], **item[1])
self.stack = []
return last

View File

@ -141,7 +141,19 @@ def test_is_genesis_block_returns_true_if_genesis(b):
def test_lazy_execution():
from bigchaindb.utils import Lazy
l = Lazy()
l.split(',')[1].split(' ').pop(1).strip()
result = l.run('Like humans, cats tend to favor one paw over another')
assert result == 'cats'
class Cat:
def __init__(self, name):
self.name = name
cat = Cat('Shmui')
l = Lazy()
l.name.upper()
result = l.run(cat)
assert result == 'SHMUI'