Skip to content

Commit c80712c

Browse files
committed
Adding ability to flatten threads
1 parent 29c8c65 commit c80712c

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

jwzthreading/jwzthreading.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,19 @@ def size(self):
119119

120120
return 1 + sum([child.size for child in self.children])
121121

122+
def flatten(self):
123+
""" Return a flatten version of the thread
124+
125+
Returns
126+
list [Messages]: a list of messages
127+
"""
128+
from itertools import chain
129+
130+
in_list = [[self.message]] + [child.flatten() for child in self.children]
131+
132+
return list(chain.from_iterable(in_list))
133+
134+
122135

123136

124137
class Message(object):

jwzthreading/tests/test_jwz.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def test_container():
2424
assert c.children == []
2525
assert c.parent is None
2626
assert not c.has_descendant(c2)
27+
assert len(c2.flatten()) == 1
2728

2829
# Add a child
2930
c3 = Container()
@@ -49,11 +50,13 @@ def test_container():
4950
assert c3.parent == c2
5051

5152
def test_deep_container():
52-
"""Build a 50000-deep list of nested Containers."""
53+
"""Build a 100-deep list of nested Containers."""
54+
55+
N = 100
5356
parent = Container()
5457
L = [parent]
5558

56-
for _ in range(50000):
59+
for _ in range(N):
5760
child = Container()
5861
parent.add_child(child)
5962
L.append(child)
@@ -65,6 +68,9 @@ def test_deep_container():
6568
# Test a search that fails
6669
assert not L[0].has_descendant(Container())
6770

71+
assert L[0].size == N + 1
72+
assert len(L[0].flatten()) == N + 1
73+
6874

6975
def test_unique():
7076
assert unique((1, 2, 3, 1, 2, 3)) == [1, 2, 3]

0 commit comments

Comments
 (0)