def logsample_weighted(fh, num_samples, field, delimiter, **kwargs):
    """Implemented Weighted Reservoir Sampling, assuming integer weights.
    See Weighted random sampling with a reservoir, Efraimidis et al."""
    
    N = num_samples
    delimiter = delimiter
    # NOTE: Convert to 0-based indexing since we expose as 1-based
    field = field-1
    
    R = []
    min_val = float("inf")
    i = 0
    
    for line in fh:
        w = int(line.split(delimiter)[field])
        if w < 1: 
            continue
        
        r = random()
        k = r ** (1./w)            
        
        if i < N:
            heappush(R, (k, line))
            if min_val < k:
                min_val = k
        else:
            if k > min_val:
                # Replace smallest item in record list
                heapreplace(R, (k, line))
        i+=1
                
    # Emit output
    for key, record in R:
        yield key, record.strip()
dataset/ETHPy150Open adamhadani/logtools/logtools/_sample.py/logsample_weighted/swapped operands of `<`