Attempts to convert infer dialect from csv file lines. 
    
    Essentially a small extension of the "sniff" function from Python CSV 
    module.   csv.Sniffer().sniff attempts to infer the delimiter from a 
    putative delimited text file by analyzing character frequencies.  This 
    function adds additional analysis in which guesses are checked again the 
    number of entries in each line that would result from splitting relative to 
    that guess. If no plausable guess if found, delimiter is inferred from file 
    name  ('csv' yields ',', everything else yields '\t'.)
    
    **Parameters** 
    
        **fname** : pathstring
        
            Name of file.
        
        **datalines** : list of strings
        
            List of lines in the data file.
            
        **lineterminator** : single-character string
        
            Line terminator to join split/join line strings.
        
    **Returns**
    
        csv.Dialect obejct

def inferdialect(fname=None, datalines=None, delimiter_regex=None, 
                 verbosity=DEFAULT_VERBOSITY):
    """
    Attempts to convert infer dialect from csv file lines. 
    
    Essentially a small extension of the "sniff" function from Python CSV 
    module.   csv.Sniffer().sniff attempts to infer the delimiter from a 
    putative delimited text file by analyzing character frequencies.  This 
    function adds additional analysis in which guesses are checked again the 
    number of entries in each line that would result from splitting relative to 
    that guess. If no plausable guess if found, delimiter is inferred from file 
    name  ('csv' yields ',', everything else yields '\t'.)
    
    **Parameters** 
    
        **fname** : pathstring
        
            Name of file.
        
        **datalines** : list of strings
        
            List of lines in the data file.
            
        **lineterminator** : single-character string
        
            Line terminator to join split/join line strings.
        
    **Returns**
    
        csv.Dialect obejct      
    
    """
    if datalines is None:
        if is_string_like(fname):
            fh = file(fname, 'rU')
        elif hasattr(fname, 'readline'):
            fh = fname
        else:
            raise ValueError('fname must be a string or file handle')

        datalines = fh.read().strip().split('\n')
        fh.close() 
    
    if delimiter_regex:
        matches = []
        for l in datalines[:10]:
            matches += delimiter_regex.findall(l)
        poss = {}
        for m in matches:
            for x in set(m):
                poss[x] = m.count(x) + (poss[x] if x in poss.keys() else 0) 
        MaxVal = max(poss.values())
        assert MaxVal > 0, 'delimiter_regex found no matches'
        amax = [x for x in poss.keys() if poss[x] == MaxVal][0]
        return csv.Sniffer().sniff(amax)

    else:
        if not is_string_like(fname):
            fname = None
    
        tries = [10, 30, 60, 100, 200, 400, 800]
    
        if len(datalines) > 100:
            starts = [int(len(datalines) / 5) * i for i in range(5)]
        else:
            starts = [0, int(len(datalines) / 2)]
            
        G = []
        for s in starts:
            for t in [tt for (i, tt) in enumerate(tries) 
                      if i == 0 or s + tries[i-1] <= len(datalines)]:
                try:
                    g = csv.Sniffer().sniff('\n'.join(datalines[s:(s+t)]))
                except:
                    pass
                else:
                    G += [g]
                    break
    
                    
        delims = [g.delimiter for g in G]   
        G = [g for (i, g) in enumerate(G) if g.delimiter not in delims[:i]]
        V = []
        for g in G:
            lvec = np.array([len(r) for r in 
                             list(csv.reader(datalines[:1000], dialect=g))])  
            V += [lvec.var()]
    
    
        if len(G) > 0:
            V = np.array(V)
            if V.min() > 0:
                fnamedelim = inferdelimiterfromname(fname)
                if fnamedelim not in delims:
                    fnamevar = np.array([len(r) for r in 
                                         list(csv.reader(datalines[:1000], 
                                                  delimiter=fnamedelim))]).var()
                    if fnamevar < V.min():
                        return csv.Sniffer().sniff(fnamedelim)
            return G[V.argmin()]
        else:
            if verbosity > 2:
                print 'dialect inference failed, infering dialect to be', inferdelimiterfromname(fname) , 'from filename extension.'
            return csv.Sniffer().sniff(inferdelimiterfromname(fname))