%prog mergecsv *.csv

    Combine CSV into binary array.
511701
def mergecsv(args):
    
    p = OptionParser(mergecsv.__doc__)
    opts, args = p.parse_args(args)

    if len(args) < 1:
        sys.exit(not p.print_help())

    csvfiles = args
    arrays = []
    samplekeys = []
    for csvfile in csvfiles:
        samplekey = op.basename(csvfile).split(".")[0]
        a = np.fromfile(csvfile, sep=",", dtype=np.int32)
        x1 = a[::2]
        x2 = a[1::2]
        a = x1 * 1000 + x2
        a[a < 0] = -1
        arrays.append(a)
        samplekeys.append(samplekey)
        print(samplekey, a, file=sys.stderr)
    print("Merging", file=sys.stderr)
    b = np.concatenate(arrays)
    b.tofile("data.bin")

    fw = open("samples", "w")
    print("\n".join(samplekeys), file=fw)
    fw.close()
390153
def merge(data,skip=50,fraction=1.0):
    """"""

    w,s,d = data.chains.shape
    
    start = int((1.0 - fraction) * s)
    total = int((s - start) / skip)
        
    return data.chains[:,start::skip,:].reshape((w*total,d))
1124889
def generate_csv(data):
  """"""
  print(",".join([
      "ability_id",
      "link_name",
      "link_index",
      "button_name",
      "hotkey",
      "friendly_name",
      "remap_to",
      "mismatch",
  ]))
  for ability in sorted(six.itervalues(data.abilities),
                        key=lambda a: sort_key(data, a)):
    ab_id = ability.ability_id
    if ab_id in skip_abilities or (ab_id not in data.general_abilities and
                                   ab_id not in used_abilities):
      continue

    general = ""
    if ab_id in data.general_abilities:
      general = "general"
    elif ability.remaps_to_ability_id:
      general = ability.remaps_to_ability_id

    mismatch = ""
    if ability.remaps_to_ability_id:
      def check_mismatch(ability, parent, attr):
        if getattr(ability, attr) != getattr(parent, attr):
          return "%s: %s" % (attr, getattr(ability, attr))

      parent = data.abilities[ability.remaps_to_ability_id]
      mismatch = "; ".join(filter(None, [
          check_mismatch(ability, parent, "available"),
          check_mismatch(ability, parent, "target"),
          check_mismatch(ability, parent, "allow_minimap"),
          check_mismatch(ability, parent, "allow_autocast"),
          check_mismatch(ability, parent, "is_building"),
          check_mismatch(ability, parent, "footprint_radius"),
          check_mismatch(ability, parent, "is_instant_placement"),
          check_mismatch(ability, parent, "cast_range"),
      ]))

    print(",".join(str(s) for s in [
        ability.ability_id,
        ability.link_name,
        ability.link_index,
        ability.button_name,
        ability.hotkey,
        ability.friendly_name,
        general,
        mismatch,
    ]))
1285643
def merge_coords(objs, compat='minimal', join='outer', priority_arg=None,
                 indexes=None):
    
    _assert_compat_valid(compat)
    coerced = coerce_pandas_values(objs)
    aligned = deep_align(coerced, join=join, copy=False, indexes=indexes)
    expanded = expand_variable_dicts(aligned)
    priority_vars = _get_priority_vars(aligned, priority_arg, compat=compat)
    variables = merge_variables(expanded, priority_vars, compat=compat)
    assert_unique_multiindex_level_names(variables)

    return variables
1157765
function csv(arg) {
  return ( _.isArray(arg) ? arg :
           _.isObject(arg) ? _.values(arg) :
           [arg]).join(', ');
}
571307
def WriteRow(stream, values):
  ""
  stream.write(','.join([EncodeForCSV(val) for val in values]))
  stream.write('\n')
483039
def make_csv(self):
        
        import csv
        try:
            from StringIO import StringIO  # Python 2.7
        except ImportError:
            from io import StringIO

        out = StringIO()
        writer = csv.writer(out, delimiter='|', lineterminator='\n', quoting=csv.QUOTE_MINIMAL)

        if self.function == 'total':
            writer.writerows(self.results)                

        elif self.function == 'top':
            rows = [['Value', self.headers.strip('"')]]
            if self.results[0] is not None:
                for res in self.results:
                    if res is not None:
                        rows.append(tuple([res[0], ','.join(res[1])]))
                writer.writerows(rows)
                
        elif self.function == 'table':
            rows = [[header.strip('"') for header in re.split('\s*,\s*', self.headers)]]

            for res in sorted(self.results, key=lambda x: x[0]):
                row = list(res[:-1])
                lastcol = get_fmt_results(res[-1], limit=10)
                if lastcol[-1][0] == '[' and lastcol[-1][-1] == ']':
                    row.append(u'{0} {1}'.format(u', '.join(lastcol[:-1]), lastcol[-1]))
                else:
                    row.append(u', '.join(lastcol))
                rows.append(row)
            writer.writerows(rows)

        self.csv = out.getvalue()
765579
def merge(var1, var2):
    
    out = {}
    out['value'] = var2.get('value', var1.get('value', None))
    out['mimetype'] = var2.get('mimetype', var1.get('mimetype', None))
    out['types'] = var2.get('types') + [x for x in var1.get('types') if x not in var2.get('types')]
    out['optional'] = var2.get('optional', var1.get('optional', False))
    out['filename'] = var2.get('filename', var2.get('filename', None))
    return Variable(var1.default_type, **out)
77392
def concat(args):
    
    import pdb
    pdb.set_trace()
    t = [matlabarray(a) for a in args]
    return np.concatenate(t)
398502
def merge_values(src, new):
    """"""
    if isinstance(src, dict) and isinstance(new, dict):
        return merge_dicts(src, new)
    else:
        if not isinstance(src, list):
            src = [src]
        if not isinstance(new, list):
            new = [new]

        return merge_lists(src, new)
1175820
def merge(self, *args):
        
        values = []

        for entry in args:
            values = values + list(entry.items())

        return dict(values)
381550
def csv(args):
    
    from xlrd import open_workbook

    p = OptionParser(csv.__doc__)
    p.set_sep(sep=',')
    opts, args = p.parse_args(args)

    if len(args) != 1:
        sys.exit(not p.print_help())

    excelfile, = args
    sep = opts.sep
    csvfile = excelfile.rsplit(".", 1)[0] + ".csv"
    wb = open_workbook(excelfile)
    fw = open(csvfile, "w")
    for s in wb.sheets():
        print('Sheet:',s.name, file=sys.stderr)
        for row in range(s.nrows):
            values = []
            for col in range(s.ncols):
                values.append(s.cell(row, col).value)
            print(sep.join(str(x) for x in values), file=fw)
596510
def joinCSVs(csvFilePaths, column, ouputFileName, separator = ',') :
	
	
	res = ''
	legend = []
	csvs = []
	
	for f in csvFilePaths :
		c = CSVFile()
		c.parse(f)
		csvs.append(c)
		legend.append(separator.join(c.legend.keys()))
	legend = separator.join(legend)
	
	lines = []
	for i in range(len(csvs[0])) :
		val = csvs[0].get(i, column)
		line = separator.join(csvs[0][i])
		for c in csvs[1:] :
			for j in range(len(c)) :
				if val == c.get(j, column) :
					line += separator + separator.join(c[j])
					
		lines.append( line )
	
	res = legend + '\n' + '\n'.join(lines)
	
	f = open(ouputFileName, 'w')
	f.write(res)
	f.flush()
	f.close()
	
	return res
367164
def read_csv_arg_preprocess(abspath, memory_usage=100 * 1000 * 1000):
    
    if memory_usage < 1000 * 1000:
        raise ValueError("Please specify a valid memory usage for read_csv, "
                         "the value should larger than 1MB and less than "
                         "your available memory.")

    size = os.path.getsize(abspath)  # total size in bytes
    n_time = math.ceil(size * 1.0 / memory_usage)  # at lease read n times

    lines = count_lines(abspath)  # total lines
    chunksize = math.ceil(lines / n_time)  # lines to read each time

    if chunksize >= lines:
        iterator = False
        chunksize = None
    else:
        iterator = True
    return iterator, chunksize
181111
def csv_to_map(fields, delimiter=','):
    

    def _csv_to_list(csv_input):
        """
        Util function to overcome the use of files by in-memory io buffer

        :param csv_input:
        :return:
        """
        io_file = io.StringIO(csv_input)
        return next(csv.reader(io_file, delimiter=delimiter))

    def _app(current_tuple, e=None):
        if current_tuple is None or len(current_tuple) == 0:
            return None, "no input"
        csv_list = _csv_to_list(current_tuple)
        if len(csv_list) != len(fields):
            e = {"input": "unexpected number of fields {} obtained {} expected".format(len(csv_list), len(fields))}
            return None, e
        return {k: v for (k, v) in zip(fields, csv_list)}, e
    if fields is None or len(fields) == 0:
        return fixed_input(None, "no fields provided, cannot proceed without order")
    return _app
755131
def self_generate(output_filename, filename="iso3166-1.csv"):  # pragma: no cover
    
    import csv
    import re

    countries = []
    alt_codes = []
    with open(filename, "r") as csv_file:
        for row in csv.reader(csv_file):
            name = row[0].rstrip("*")
            name = re.sub(r"\(the\)", "", name)
            if name:
                countries.append((name, row[1]))
                alt_codes.append((row[1], row[2], int(row[3])))
    with open(__file__, "r") as source_file:
        contents = source_file.read()
    # Write countries.
    bits = re.match("(.*\nCOUNTRIES = \{\n)(.*?)(\n\}.*)", contents, re.DOTALL).groups()
    country_list = []
    for name, code in countries:
        name = name.replace('"', r"\"").strip()
        country_list.append('    "{code}": _("{name}"),'.format(name=name, code=code))
    content = bits[0]
    content += "\n".join(country_list)
    # Write alt codes.
    alt_bits = re.match(
        "(.*\nALT_CODES = \{\n)(.*)(\n\}.*)", bits[2], re.DOTALL
    ).groups()
    alt_list = []
    for code, code3, codenum in alt_codes:
        name = name.replace('"', r"\"").strip()
        alt_list.append(
            '    "{code}": ("{code3}", {codenum}),'.format(
                code=code, code3=code3, codenum=codenum
            )
        )
    content += alt_bits[0]
    content += "\n".join(alt_list)
    content += alt_bits[2]
    # Generate file.
    with open(output_filename, "w") as output_file:
        output_file.write(content)
    return countries
1158682
def reorder(args):
    
    import csv

    p = OptionParser(reorder.__doc__)
    p.set_sep()
    opts, args = p.parse_args(args)

    if len(args) != 2:
        sys.exit(not p.print_help())

    tabfile, order = args
    sep = opts.sep
    order = [int(x) - 1 for x in order.split(",")]
    reader = csv.reader(must_open(tabfile), delimiter=sep)
    writer = csv.writer(sys.stdout, delimiter=sep)
    for row in reader:
        newrow = [row[x] for x in order]
        writer.writerow(newrow)