text: """
  get_samples.py gets a random sample of files from our training set.
  """
  import csv
  import random
  import zipfile
  
  def read_file(filename, delim=' '):
      """ Reads a file and fills a list with that data.
          Args:
              filename (str) : path to the file you want to open
              delim (str): a delimiter for the file
      """
      data_list = []
      with open(filename) as csvfile:
          reader = csv.reader(csvfile, delimiter=delim)
          data_list = [line for line in reader]
      return data_list
  
  def get_sample_list(filelist, label, n=50):
      """ gets a set of samples from a given class and returns a list of those
          Args:
              filelist (list): the general list of files from which you want to
              sample
              label (str): the string class label
              n (int): the size of the sample
      """
      # gets the names of a file list if
      s_list = [f for f in filelist if f[1] is label]
      # checks for size issues
      if n > len(s_list):
          n = len(s_list)
      # print("length of class %s: %d" % (label, len(s_list)))
      rand_smpl = [s_list[i]
                   for i in
                   sorted(random.sample(xrange(len(s_list)), n))]
      return rand_smpl
  
  def make_zip_from_files(filelist, label):
      """Makes a zipfile of all files in a class"""
      s_list = [f[0] + '.bytes' for f in filelist if f[1] is label]
      print('class ' + label)
      print(len(s_list))
      zipname = str(label) + '.zip'
      print(zipname)
      zipf = zipfile.ZipFile(zipname, 'w', allowZip64=True)
      for name in s_list:
          print(name)
          zipf.write(name)
  
  def main():
      traincsv = '/nfs/research/agonzales/git/microsoft_malware_challenge/data/trainLabels.csv'
      FILES = read_file(traincsv, ',')
      print(len(FILES))
      print(FILES[0])
      print(FILES[1])
      SAMPLE_LIST = []
      # specific for our project
      for label in range(1, 10):
          # make_zip_from_files(FILES, str(label))
          SAMPLE_LIST.append(get_sample_list(FILES, str(label)))
      # flattens and writes it as a list
      flat_samples = [item for sublist in SAMPLE_LIST for item in sublist]
      with open("trainSamples.csv", "wb") as f:
          writer = csv.writer(f)
          writer.writerows(flat_samples)
  
  if __name__ == "__main__":
      main()
  