##################################
#                                #
# Last modified 2017/04/18       # 
#                                #
# Georgi Marinov                 #
#                                # 
##################################

import sys
import os
import random
import string
from sets import Set

def run():

    if len(sys.argv) < 2:
        print 'usage: python %s file fraction' % sys.argv[0]
        print '\tNote: the script will read .gz and .bzip2 files as well as stdin (-)'
        print '\tNote: the script will print standard output'
        sys.exit(1)

    inputfilename = sys.argv[1]
    fraction = float(sys.argv[2])

    doStdIn = False
    if inputfilename != '-':
        if inputfilename.endswith('.bz2'):
            cmd = 'bzip2 -cd ' + inputfilename
        elif inputfilename.endswith('.gz') or inputfilename.endswith('.bgz'):
            cmd = 'zcat ' + inputfilename
        else:
            cmd = 'cat ' + inputfilename
        p = os.popen(cmd, "r")
    else:
        doStdIn = True

    line = 'line'
    while line != '':
        if doStdIn:
            line = sys.stdin.readline()
        else:
            line = p.readline()
        if line == '':
            break
        if line.startswith('#'):
            print line.strip()
            continue
        r = random.random()
        if r <= fraction:
            print line.strip()

run()
