filter -> list comprehension
authorStefan Huber <shuber2@gmx.at>
Sun, 13 May 2012 16:45:22 +0000 (18:45 +0200)
committerStefan Huber <shuber2@gmx.at>
Sun, 13 May 2012 16:45:22 +0000 (18:45 +0200)
shbackup.py

index 0aedc6f623589bdc79b872245ec09f744bb19dec..41b24fe125686121c4a082d788546d8a66b27dc7 100755 (executable)
@@ -46,7 +46,7 @@ class Config:
                                  ", keeps: " + str(self.epochkeeps) + \
                                  ", modes: " + str(self.epochmodes) + \
                                  ", exclpatterns: " + str(self.exclpatterns) + \
                                  ", keeps: " + str(self.epochkeeps) + \
                                  ", modes: " + str(self.epochmodes) + \
                                  ", exclpatterns: " + str(self.exclpatterns) + \
-                                 ", sets: " + str([str(s) for s in self.sets]) + "]";
+                                 ", sets: " + str([str(s) for s in self.sets]) + "]"
 
     def read(self, filename):
         """Read configuration from file"""
 
     def read(self, filename):
         """Read configuration from file"""
@@ -171,14 +171,14 @@ class BackupManager:
         basedir = self.conf.directory
         dirs = os.listdir(basedir)
         # Filter directories
         basedir = self.conf.directory
         dirs = os.listdir(basedir)
         # Filter directories
-        return filter( lambda d: os.path.isdir(os.path.join(basedir, d)), dirs)
+        return [ d for d in dirs if os.path.isdir(os.path.join(basedir, d)) ]
 
     def listOldBackups(self):
         """Returns a list of old backups."""
 
         backups = []
 
 
     def listOldBackups(self):
         """Returns a list of old backups."""
 
         backups = []
 
-        for entry in filter(Backup.isBackupDir, self.listAllDirs()):
+        for entry in [ b for b in self.listAllDirs() if Backup.isBackupDir(b) ]:
             [strdate, strtime, epoch, mode] = entry.split("-")
 
             if not epoch in Epoch.keys():
             [strdate, strtime, epoch, mode] = entry.split("-")
 
             if not epoch in Epoch.keys():
@@ -206,7 +206,7 @@ class BackupManager:
                 continue
 
             # Get backups of that epoch
                 continue
 
             # Get backups of that epoch
-            byepoch = list(sorted(filter( lambda b: b.epoch==e, backups), \
+            byepoch = list(sorted( [ b for b in backups if b.epoch==e], \
                 key=lambda b: b.date))
 
             # If there are any, determine the latest
                 key=lambda b: b.date))
 
             # If there are any, determine the latest
@@ -278,7 +278,7 @@ class BackupManager:
         mode = self.conf.epochmodes[epoch]
         print("Making a backup. Epoch: " + epoch + ", mode: " + mode)
 
         mode = self.conf.epochmodes[epoch]
         print("Making a backup. Epoch: " + epoch + ", mode: " + mode)
 
-        oldfullbackups = list(filter(lambda b: b.mode=="full", oldbackups))
+        oldfullbackups = [ b for b in oldbackups if b.mode=="full" ]
 
         # No old full backups existing
         if mode != "full" and len(oldfullbackups)==0:
 
         # No old full backups existing
         if mode != "full" and len(oldfullbackups)==0:
@@ -324,12 +324,13 @@ class BackupManager:
     def prune(self):
         """Prune old backup files"""
 
     def prune(self):
         """Prune old backup files"""
 
-        noBackupDir = lambda d: not Backup.isBackupDir(d)        
-        dirs = list(filter(noBackupDir, self.listAllDirs()))
+        # Collect all directories not matching backup name
+        dirs = [ d for d in self.listAllDirs() if not Backup.isBackupDir(d) ]
 
 
+        # Get all directories which are outdated
         backups = self.listOldBackups()
         backups = self.listOldBackups()
-        byepoch = { e : list(reversed(sorted(filter(lambda b: b.epoch==e, backups), \
-                key=lambda b : b.date))) for e in Epoch }
+        byepoch = { e : list(sorted( [ b for b in backups if b.epoch == e ], \
+                key=lambda b : b.date, reverse=True)) for e in Epoch }
         for e in byepoch:
             keep = self.conf.epochkeeps[e]
             old = byepoch[e][keep:]
         for e in byepoch:
             keep = self.conf.epochkeeps[e]
             old = byepoch[e][keep:]
@@ -344,16 +345,15 @@ class BackupManager:
             print("  " + d)
 
         basedir = self.conf.directory
             print("  " + d)
 
         basedir = self.conf.directory
-        yesno = input("Remove listed entries? [y,N] ")
+        yesno = input("Remove listed entries? [y, N] ")
         if yesno == "y":
             for d in dirs:
         if yesno == "y":
             for d in dirs:
-                shutil.rmtree(os.path.join(basedir,d))
+                shutil.rmtree(os.path.join(basedir, d))
 
 
 if __name__ == "__main__":
 
     conffn = "shbackup.conf"
 
 
 if __name__ == "__main__":
 
     conffn = "shbackup.conf"
-
     if len(sys.argv) > 1:
         conffn = sys.argv[1]
 
     if len(sys.argv) > 1:
         conffn = sys.argv[1]