[Tutor] Is there a simpler way to remove from a set?

Leam Hall leamhall at gmail.com
Thu May 6 20:52:36 EDT 2021


Base code:

###
def build_dir_set(my_list, exclude_dirs):
   my_set  = set()
   for item in my_list:
     path = Path(item)
     parent = str(path.parent)
     my_set.add(parent)
   my_new_set = set()
   for exclude_dir in exclude_dirs:
     for parent in my_set:
       if re.match(exclude_dir, parent):
         my_new_set.add(parent)
   return my_set - my_new_set
###

Where "my_list" and "exclude_dirs" are lists of directories

For example, "my_list" might include:
	/opt/backup/home/me/Desktop/Documents/my_cool_file.txt
	/opt/backup/home/me/lang/git/software/src/cute_image.jpg
	/home/me/lang/git/this/noop.py
	/home/me/lang/git/that/anop.py

And "exclude_dirs" might be"
	/home/me/lang/git

The return should be a set of directories that do not start with any
of the excluded directory names:
	/opt/backup/home/me/Desktop/Documents
	/opt/backup/home/me/lang/git/software/src

I couldn't remove from the set during iteration, python complained. 
Hence having to make a second set. The project is pretty useful for 
those of us who make a lot of backup copies of files. I have 30-50 
copies of some files, and the returned set will be the directories that 
are safe to remove files from. The exclude_dirs are where the 'to keep' 
files will be kept.

If you care to read Perl, I'm converting some old code to Python:

  https://github.com/LeamHall/admin_tools/blob/master/find_copies.pl

Can this be made cleaner?

Thanks!

Leam

-- 
Site Reliability Engineer  (reuel.net/resume)
Scribe: The Domici War     (domiciwar.net)
General Ne'er-do-well      (github.com/LeamHall)


More information about the Tutor mailing list