Skip to content

Commit 93eb949

Browse files
committed
LinearCollection basic functionality and tests
1 parent c3ae365 commit 93eb949

File tree

3 files changed

+144
-4
lines changed

3 files changed

+144
-4
lines changed

Diff for: src/manim_data_structures/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from .m_array import *
44
from .m_enum import *
55
from .m_variable import *
6+
from .mlinearcollection import *
67

78
__all__ = [
89
"MArrayElement",
@@ -12,4 +13,5 @@
1213
"MArrayDirection",
1314
"MArrayElementComp",
1415
"MVariable",
16+
"LinearCollection",
1517
]

Diff for: src/manim_data_structures/mlinearcollection.py

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import copy
2+
from typing import Iterable, List
3+
4+
import numpy as np
5+
from manim import RIGHT, Integer, Mobject, Square, VMobject
6+
7+
8+
class LinearCollection(VMobject):
9+
__data: List[Mobject]
10+
__data_template: Mobject
11+
__container_template: Mobject
12+
__delimiter: Mobject
13+
__direction: np.ndarray
14+
15+
def __init__(
16+
self,
17+
data: Iterable,
18+
data_template=Integer,
19+
container_template=Square,
20+
delimiter=None,
21+
direction=RIGHT,
22+
*args,
23+
**kwargs,
24+
):
25+
super().__init__(*args, **kwargs)
26+
27+
self.__data = []
28+
self.__data_template = data_template
29+
self.__container_template = container_template
30+
self.__delimiter = delimiter
31+
self.__direction = direction
32+
33+
for value in data:
34+
self.__data.append(data_template(value))
35+
new_container = container_template()
36+
new_container.add(self.__data[-1])
37+
self.add(new_container)
38+
39+
self.arrange(direction=self.__direction)
40+
41+
def insert(self, value):
42+
self.__data.append(self.__data_template(value))
43+
new_container = self.__container_template()
44+
new_container.add(self.__data[-1])
45+
self.add(new_container)
46+
self.arrange(direction=self.__direction)
47+
48+
def remove(self):
49+
pass
50+
51+
def __getitem__(self, key):
52+
return self.__data.__getitem__(key).get_value()
53+
54+
def __setitem__(self, key, value):
55+
return self.__data.__getitem__(key).set_value(value)
56+
57+
def __iter__(self):
58+
# Initialize a Data iterator when we start iterating
59+
self.__iter__data = iter(self.__data)
60+
return self
61+
62+
def __next__(self):
63+
try:
64+
# Return the value of the next Data in the iterator
65+
return next(self.__iter__data).get_value()
66+
except StopIteration as e:
67+
# Clean up the iterator when we're done
68+
del self.__iter__data
69+
raise e
70+
71+
def __add__(self, rhs):
72+
self_copy = copy.copy(self)
73+
for container in rhs.submobjects:
74+
self_copy.add(copy.copy(container))
75+
for data in rhs._LinearCollection__data:
76+
self_copy.__data.append(copy.copy(data))
77+
78+
return self_copy

Diff for: tests/test_mlinearcollection.py

+64-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,71 @@
1-
# Fill with appropriate tests
2-
def test_getitem():
1+
from manim import Integer, Mobject, Square
2+
3+
from manim_data_structures import LinearCollection
4+
5+
6+
def test_constructor():
7+
data = [i + 1 for i in range(10)]
8+
lc = LinearCollection(data)
9+
assert type(lc) == LinearCollection
10+
assert type(lc._LinearCollection__data) == list
11+
assert isinstance(lc, Mobject)
12+
assert isinstance(lc._LinearCollection__data[0], Mobject)
13+
assert isinstance(lc.submobjects[0], Mobject)
14+
assert type(lc._LinearCollection__data[0]) == Integer
15+
assert type(lc.submobjects[0]) == Square
16+
17+
for i in range(len(data)):
18+
assert lc._LinearCollection__data[i].get_value() == i + 1
19+
assert lc.submobjects[i].submobjects[0].get_value() == i + 1
20+
21+
22+
def test_insertion():
323
pass
424

525

6-
def test_setitem():
26+
def test_removal():
727
pass
828

929

30+
def test_getitem():
31+
data = [i + 1 for i in range(10)]
32+
lc = LinearCollection(data)
33+
34+
for i in range(len(data)):
35+
assert lc[i] == data[i]
36+
37+
38+
def test_setitem():
39+
data = [i + 1 for i in range(10)]
40+
lc = LinearCollection(data)
41+
42+
for i in range(len(data)):
43+
assert lc[i] == data[i]
44+
lc[i] = data[-(1 + i)]
45+
46+
for i in range(len(data)):
47+
assert lc[i] == data[-(1 + i)]
48+
49+
1050
def test_iteration():
11-
pass
51+
data = [i + 1 for i in range(10)]
52+
lc = LinearCollection(data)
53+
54+
for lc_value, data_value in zip(lc, data):
55+
assert lc_value == data_value
56+
57+
58+
def test_add():
59+
data = [i + 1 for i in range(10)]
60+
lc1 = LinearCollection(data)
61+
lc2 = LinearCollection(data[::-1])
62+
63+
for i in range(len(data)):
64+
assert lc1[i] == data[i]
65+
assert lc2[i] == data[-(1 + i)]
66+
67+
for i, value in enumerate(lc1 + lc2):
68+
if i < len(data):
69+
assert value == lc1[i]
70+
else:
71+
assert value == lc2[i - len(data)]

0 commit comments

Comments
 (0)