earnestt1234 / seedir

A Python package for creating, editing, and reading folder tree diagrams
MIT License
122 stars 9 forks source link

Deleting multiple entries from a FakeDir only works with list #8

Closed earnestt1234 closed 2 years ago

earnestt1234 commented 2 years ago

Issue: I can't delete multiple items using FakeDir.delete() when not using a list

>>> import seedir as sd
>>> r = sd.randomdir(seed=5, folders=0)
>>> r
MyFakeDir/
├─senor.txt
├─verb.txt
└─takeoff.txt

>>> r.delete(('senor.txt', 'verb.txt'))
Traceback (most recent call last):

  File "/Users/earnestt1234/Documents/python/SO.py", line 4, in <module>
    r.delete(('senor.txt', 'verb.txt'))

  File "/Users/earnestt1234/opt/anaconda3/lib/python3.8/site-packages/seedir/fakedir.py", line 417, in delete
    child_copy = child.copy()

AttributeError: 'tuple' object has no attribute 'copy'

The problem seems to stem from the fact that the copy method is invoked before iterating over the collection of items to delete. But it seems like this was an over-caution that is not actually needed? tuple and many other collections won't have the copy method; I think it should be removed.

earnestt1234 commented 2 years ago

This was fixed in 02a4092. Some sort of copy was in fact needed, since delete can remove objects from the _children attribute that is being iterated over. But a call the specific copy method was removed, in favor of copying (to list) via list comprehension).