mirror of
https://github.com/bigchaindb/bigchaindb.git
synced 2024-10-13 13:34:05 +00:00
Simplify run function
This commit is contained in:
parent
ca49718d7e
commit
54544f66a0
@ -180,8 +180,11 @@ class Lazy:
|
|||||||
def run(self, instance):
|
def run(self, instance):
|
||||||
last = instance
|
last = instance
|
||||||
|
|
||||||
for method, (args, kwargs) in zip(self.stack[::2], self.stack[1::2]):
|
for item in self.stack:
|
||||||
last = getattr(last, method)(*args, **kwargs)
|
if isinstance(item, str):
|
||||||
|
last = getattr(last, item)
|
||||||
|
else:
|
||||||
|
last = last(*item[0], **item[1])
|
||||||
|
|
||||||
self.stack = []
|
self.stack = []
|
||||||
return last
|
return last
|
||||||
|
@ -141,7 +141,19 @@ def test_is_genesis_block_returns_true_if_genesis(b):
|
|||||||
|
|
||||||
def test_lazy_execution():
|
def test_lazy_execution():
|
||||||
from bigchaindb.utils import Lazy
|
from bigchaindb.utils import Lazy
|
||||||
|
|
||||||
l = Lazy()
|
l = Lazy()
|
||||||
l.split(',')[1].split(' ').pop(1).strip()
|
l.split(',')[1].split(' ').pop(1).strip()
|
||||||
result = l.run('Like humans, cats tend to favor one paw over another')
|
result = l.run('Like humans, cats tend to favor one paw over another')
|
||||||
assert result == 'cats'
|
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'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user