From: Stefan Huber Date: Sun, 13 May 2012 16:45:22 +0000 (+0200) Subject: filter -> list comprehension X-Git-Tag: v1.0~20 X-Git-Url: https://git.sthu.org/?a=commitdiff_plain;h=13ede569b9acabd25f6609af3a86891fbebf1209;p=sitarba.git filter -> list comprehension --- diff --git a/shbackup.py b/shbackup.py index 0aedc6f..41b24fe 100755 --- a/shbackup.py +++ b/shbackup.py @@ -46,7 +46,7 @@ class Config: ", 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""" @@ -171,14 +171,14 @@ class BackupManager: 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 = [] - 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(): @@ -206,7 +206,7 @@ class BackupManager: 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 @@ -278,7 +278,7 @@ class BackupManager: 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: @@ -324,12 +324,13 @@ class BackupManager: 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() - 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:] @@ -344,16 +345,15 @@ class BackupManager: 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: - shutil.rmtree(os.path.join(basedir,d)) + shutil.rmtree(os.path.join(basedir, d)) if __name__ == "__main__": conffn = "shbackup.conf" - if len(sys.argv) > 1: conffn = sys.argv[1]