Skip to content

Instantly share code, notes, and snippets.

@maehler
Created April 16, 2014 17:29
Show Gist options
  • Save maehler/10910745 to your computer and use it in GitHub Desktop.
Save maehler/10910745 to your computer and use it in GitHub Desktop.
Quick and dirty python script for performing set operations on lines in files.
#!/usr/bin/env python
import argparse
import os
def parse_file(fname):
with open(fname) as f:
return set(line.strip() for line in f)
def print_set(s, count=False):
if count:
print len(s)
else:
print '\n'.join(s)
def parse_args():
parser = argparse.ArgumentParser(description='''
Perform set operations on the lines of two files
''')
parser.add_argument('file', nargs=2, help='files to compare')
parser.add_argument('-t', help='set operation to perform (default: intersection)',
choices=('union', 'intersection', 'difference'),
default='intersection')
parser.add_argument('-c', help='count the number of resulting lines',
action='store_true')
args = parser.parse_args()
for fname in args.file:
if not os.path.exists(fname):
parser.error('file not found: {}'.format(fname))
return args
def main():
args = parse_args()
f1 = parse_file(args.file[0])
f2 = parse_file(args.file[1])
if args.t == 'union':
print_set(f1.union(f2), args.c)
elif args.t == 'intersection':
print_set(f1.intersection(f2), args.c)
elif args.t == 'difference':
print_set(f1 - f2, args.c)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment