Gfy

list_compare.py

Gfy
Feb 8th, 2012
114
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. # -*- coding: latin-1 -*-
  3.  
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program.  If not, see <http://www.gnu.org/licenses/>
  16.  
  17. """ Python alternative for these commands:
  18.  
  19. Shows which lines in new.txt aren't in mine.txt:
  20. cat mine.txt new.txt | sort | uniq -d | cat new.txt - | sort | uniq -u
  21. change -u at the end to -d to get the duplicates
  22.  
  23. No problems with different line endings of the text files in Windows.
  24.  
  25. Author: Gfy <tsl@yninovg.pbz> """
  26.  
  27. import sys
  28. import optparse
  29.  
  30. def read_txt(txt_file):
  31.     with open(txt_file, "U") as f:
  32.         return set(f.readlines())
  33.    
  34. def print_releases(rellist):
  35.     for line in rellist:
  36.         print(line.replace("\n", ""))
  37.                
  38. def main(options, args):
  39.     mainl = read_txt(args[0])
  40.     newl = read_txt(args[1])
  41.    
  42. #   print(len(newl.difference(mainl)))
  43. #   print(len(mainl.intersection(newl)))
  44.    
  45.     if options.dupe:
  46.         print_releases(mainl.intersection(newl))
  47.     if options.uniq:
  48.         print_releases(newl.difference(mainl))
  49.  
  50. if __name__ == '__main__':
  51.     parser = optparse.OptionParser(
  52.         usage="Usage: %prog [main db txt] [list of new rels]\n",
  53.         version="%prog 0.1 (2012-02-05)") # --help, --version
  54.    
  55.     parser.add_option("-d", "--duplicates", help="prints dupes",
  56.                       action="store_true", default=False, dest="dupe")
  57.     parser.add_option("-u", "--uniques", help="prints uniques",
  58.                       action="store_true", default=False, dest="uniq")
  59.    
  60.     # no arguments given
  61.     if len(sys.argv) < 2:
  62.         print(parser.format_help())
  63.     else:
  64.         (options, args) = parser.parse_args()
  65.         main(options, args)
RAW Paste Data