import os, re, time
import numpy as np



def clean_dir(dir, pattern):
    """
    Remove all files matching a pattern
    :param dir:
    :param pattern:
    :return:
    """

    for f in os.listdir(dir):
    	if re.search(pattern, f):
    		os.remove(os.path.join(dir, f))

def sec_to_time(sec):
    """
    Convert seconds to time string
    """
    days = sec / 86400
    sec -= 86400*days

    hrs = sec / 3600
    sec -= 3600*hrs

    mins = sec / 60
    sec -= 60*mins
    return '%s:%s:%s:%s' % (days, hrs, mins, sec)

def timestamp_to_time(unix_timestamp, formatString = "%Y-%m-%d"):
    return time.strftime(formatString, time.localtime(unix_timestamp))


def get_intersection(c1,c2):
    return set(c1).intersection( set(c2) )

def concatenate_files(filenames,outputfile):
    with open(outputfile, 'w') as outfile:
        for fname in filenames:
            with open(fname) as infile:
                for line in infile:
                    outfile.write(line)

def shuffle_dict(input_dict):
    dict_values = input_dict.values()
    np.random.shuffle(dict_values)
    return dict(zip(input_dict.keys(), dict_values))

def remove_duplicates_without_changing_order(seq):
    seen = set()
    seen_add = seen.add
    return [ x for x in seq if not (x in seen or seen_add(x))]