This repository was archived by the owner on Oct 24, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +21
-4
lines changed
Expand file tree Collapse file tree 3 files changed +21
-4
lines changed Original file line number Diff line number Diff line change @@ -227,9 +227,18 @@ def test_overwrite_child(self):
227227 assert marys_evil_twin .parent is john
228228
229229
230- # TODO write and test all the del methods
231230class TestPruning :
232- ...
231+ def test_del_child (self ):
232+ john = TreeNode ()
233+ mary = TreeNode ()
234+ john ._set_item ("Mary" , mary )
235+
236+ del john ["Mary" ]
237+ assert "Mary" not in john .children
238+ assert mary .parent is None
239+
240+ with pytest .raises (KeyError ):
241+ del john ["Mary" ]
233242
234243
235244def create_test_tree ():
Original file line number Diff line number Diff line change @@ -438,8 +438,14 @@ def _set_item(
438438 else :
439439 current_node ._set (name , item )
440440
441- def del_node (self , path : str ):
442- raise NotImplementedError
441+ def __delitem__ (self : Tree , key : str ):
442+ """Remove a child node from this tree object."""
443+ if key in self .children :
444+ child = self ._children [key ]
445+ del self ._children [key ]
446+ child .orphan ()
447+ else :
448+ raise KeyError ("Cannot delete" )
443449
444450 def update (self : Tree , other : Mapping [str , Tree ]) -> None :
445451 """
Original file line number Diff line number Diff line change @@ -36,6 +36,8 @@ New Features
3636 By `Tom Nicholas <https://github.com/TomNicholas >`_.
3737- New HTML repr, which will automatically display in a jupyter notebook. (:pull: `78 `)
3838 By `Tom Nicholas <https://github.com/TomNicholas >`_.
39+ - New delitem method so you can delete nodes. (:pull: `88 `)
40+ By `Tom Nicholas <https://github.com/TomNicholas >`_.
3941
4042Breaking changes
4143~~~~~~~~~~~~~~~~
You can’t perform that action at this time.
0 commit comments