verysimpletree 1.2__tar.gz → 1.2.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: verysimpletree
3
- Version: 1.2
3
+ Version: 1.2.1
4
4
  Summary: lightweight tree data structure for musicxml and musicscore
5
5
  Home-page: https://github.com/alexgorji/verysimpletree.git
6
6
  License: MIT
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name="verysimpletree"
3
- version="1.2"
3
+ version="1.2.1"
4
4
  description = "lightweight tree data structure for musicxml and musicscore"
5
5
  authors = ["Alex Gorji <aligorji@hotmail.com>"]
6
6
  license = "MIT"
@@ -23,6 +23,10 @@ pytest-xdist = "^3.6.1"
23
23
  pytest-cov = "^6.0.0"
24
24
 
25
25
 
26
+
27
+ [tool.poetry.group.dev.dependencies]
28
+ ruff = "^0.8.4"
29
+
26
30
  [build-system]
27
31
  requires = ["poetry-core"]
28
32
  build-backend = "poetry.core.masonry.api"
@@ -167,7 +167,12 @@ class Test(TreeTestCase):
167
167
  self.grandchild3]
168
168
 
169
169
  def test_get_farthest_leaf(self):
170
- pass
170
+ self.assertEqual(self.root.get_farthest_leaf(), self.greatgrandchild1)
171
+ self.assertEqual(self.child1.get_farthest_leaf(), self.child1)
172
+ self.assertEqual(self.child2.get_farthest_leaf(), self.greatgrandchild1)
173
+ self.assertEqual(self.child3.get_farthest_leaf(), self.child3)
174
+ self.assertEqual(self.child4.get_farthest_leaf(), self.grandchild3)
175
+ self.assertEqual(self.grandchild2.get_farthest_leaf(), self.greatgrandchild1)
171
176
 
172
177
  def test_find_grandchild(self):
173
178
  assert [n for n in self.root.traverse() if n.get_level() == 2] == [self.grandchild1, self.grandchild2,
@@ -185,6 +190,36 @@ class Test(TreeTestCase):
185
190
  with self.assertRaises(TreeReferenceError):
186
191
  assert self.greatgrandchild1.get_distance(self.child1) == 'bla'
187
192
 
193
+ def test_get_distance(self):
194
+ self.assertEqual(self.root.get_distance(self.root), 0)
195
+ self.assertEqual(self.root.get_distance(self.child1), 1)
196
+ self.assertEqual(self.root.get_distance(self.child2), 1)
197
+ self.assertEqual(self.root.get_distance(self.child3), 1)
198
+ self.assertEqual(self.root.get_distance(self.child4), 1)
199
+ self.assertEqual(self.root.get_distance(self.grandchild1), 2)
200
+ self.assertEqual(self.root.get_distance(self.grandchild2), 2)
201
+ self.assertEqual(self.root.get_distance(self.grandchild3), 2)
202
+ self.assertEqual(self.root.get_distance(self.greatgrandchild1), 3)
203
+
204
+ self.assertEqual(self.child1.get_distance(self.child1), 0)
205
+ self.assertEqual(self.child1.get_distance(self.root), 1)
206
+
207
+ self.assertEqual(self.child2.get_distance(self.child2), 0)
208
+ self.assertEqual(self.child2.get_distance(self.root), 1)
209
+ self.assertEqual(self.child2.get_distance(self.grandchild1), 1)
210
+ self.assertEqual(self.child2.get_distance(self.grandchild2), 1)
211
+ self.assertEqual(self.child2.get_distance(self.greatgrandchild1), 2)
212
+
213
+
214
+ self.assertEqual(self.child3.get_distance(self.child3), 0)
215
+ self.assertEqual(self.child3.get_distance(self.root), 1)
216
+
217
+ self.assertEqual(self.child4.get_distance(self.child4), 0)
218
+ self.assertEqual(self.child4.get_distance(self.root), 1)
219
+ self.assertEqual(self.child4.get_distance(self.grandchild3), 1)
220
+
221
+
222
+
188
223
  def test_get_farthest_leave_of_root_without_children(self):
189
224
  root = A('root')
190
225
  assert root.get_farthest_leaf() == root
@@ -229,6 +264,15 @@ class Test(TreeTestCase):
229
264
  """
230
265
 
231
266
  assert self.root.get_tree_representation() == expected
267
+
268
+ def test_get_number_of_layers(self):
269
+ self.assertEqual(self.root.get_number_of_layers(), 3)
270
+ self.assertEqual(self.child1.get_number_of_layers(), 0)
271
+ self.assertEqual(self.child2.get_number_of_layers(), 2)
272
+ self.assertEqual(self.child4.get_number_of_layers(), 1)
273
+ self.assertEqual(self.grandchild1.get_number_of_layers(), 0)
274
+ self.assertEqual(self.grandchild2.get_number_of_layers(), 1)
275
+ self.assertEqual(self.greatgrandchild1.get_number_of_layers(), 0)
232
276
 
233
277
 
234
278
  class TreeListRepresentationTestCase(TreeTestCase):
@@ -211,21 +211,29 @@ class Tree(ABC, Generic[T]):
211
211
  >>> greatgrandchild1.get_distance(child2)
212
212
  2
213
213
  """
214
+ if not reference:
215
+ reference = self.get_root()
214
216
 
215
- if self.is_root:
217
+ if reference == self:
216
218
  return 0
219
+
220
+ count = 0
217
221
 
218
- if reference is None:
219
- reference = self.get_root()
222
+ for node in self.get_reversed_path_to_root():
223
+ if node == reference:
224
+ return count
225
+ else:
226
+ count += 1
227
+
228
+ count = 0
229
+ for node in reference.get_reversed_path_to_root():
230
+ print(node)
231
+ if node == self:
232
+ return count
233
+ else:
234
+ count += 1
220
235
 
221
- parent = self.up
222
- count = 1
223
- while parent is not None and parent is not reference:
224
- parent = parent.up
225
- count += 1
226
- if parent is not None and parent.is_root and parent is not reference:
227
- raise TreeReferenceError(f"Wrong reference {reference} not in path to root.")
228
- return count
236
+ raise TreeReferenceError(f"Wrong reference {reference} not in path to root.")
229
237
 
230
238
  def get_farthest_leaf(self: T) -> T:
231
239
  """
@@ -233,7 +241,7 @@ class Tree(ABC, Generic[T]):
233
241
  greatgrandchild1
234
242
  """
235
243
  leaves: list[T] = list(self.iterate_leaves())
236
- return max(leaves, key=lambda leaf: leaf.get_distance())
244
+ return max(leaves, key=lambda leaf: leaf.get_distance(self))
237
245
 
238
246
  def get_layer(self: T, level: int, key: Optional[Callable[[T], Any]] = None) -> Any:
239
247
  """
File without changes