pile_js / CartoDB__cartodb.jsonl
Hamhams's picture
commit files to HF hub
c7f4bd0
{"nwo":"CartoDB\/cartodb","sha":"46b91228a01123976363f55a5e1d712a600785a6","path":"lib\/importer\/misc\/dbfUtils.py","language":"python","identifier":"dbfreader","parameters":"(f)","argument_list":"","return_statement":"","docstring":"Returns an iterator over records in a Xbase DBF file.\n\n The first row returned contains the field names.\n The second row contains field specs: (type, size, decimal places).\n Subsequent rows contain the data records.\n If a record is marked as deleted, it is skipped.\n\n File should be opened for binary reads.","docstring_summary":"Returns an iterator over records in a Xbase DBF file.","docstring_tokens":["Returns","an","iterator","over","records","in","a","Xbase","DBF","file","."],"function":"def dbfreader(f):\n \"\"\"Returns an iterator over records in a Xbase DBF file.\n\n The first row returned contains the field names.\n The second row contains field specs: (type, size, decimal places).\n Subsequent rows contain the data records.\n If a record is marked as deleted, it is skipped.\n\n File should be opened for binary reads.\n\n \"\"\"\n # See DBF format spec at:\n # http:\/\/www.pgts.com.au\/download\/public\/xbase.htm#DBF_STRUCT\n\n numrec, lenheader = struct.unpack('<xxxxLH22x', f.read(32)) \n numfields = (lenheader - 33) \/\/ 32\n\n fields = []\n for fieldno in range(numfields):\n name, typ, size, deci = struct.unpack('<11sc4xBB14x', f.read(32))\n name = name.replace('\\0', '') # eliminate NULs from string \n fields.append((name, typ, size, deci))\n yield [field[0] for field in fields]\n yield [tuple(field[1:]) for field in fields]\n\n terminator = f.read(1)\n assert terminator == '\\r'\n\n fields.insert(0, ('DeletionFlag', 'C', 1, 0))\n fmt = ''.join(['%ds' % fieldinfo[2] for fieldinfo in fields])\n fmtsiz = struct.calcsize(fmt)\n for i in range(numrec):\n record = struct.unpack(fmt, f.read(fmtsiz))\n if record[0] != ' ':\n continue # deleted record\n result = []\n for (name, typ, size, deci), value in zip(fields, record):\n if name == 'DeletionFlag':\n continue\n if typ == \"N\":\n value = value.replace('\\0', '').lstrip()\n if value == '':\n value = 0\n elif deci:\n try:\n value = decimal.Decimal(value)\n except decimal.InvalidOperation:\n # Could be '************************' (for NULL)\n value = 0\n else:\n value = value\n elif typ == 'D':\n y, m, d = int(value[:4]), int(value[4:6]), int(value[6:8])\n try:\n value = datetime.date(y, m, d)\n except ValueError:\n # Could be '00000000' (for NULL)\n value = 0\n elif typ == 'L':\n value = (value in 'YyTt' and 'T') or (value in 'NnFf' and 'F') or '?'\n result.append(value)\n yield result","function_tokens":["def","dbfreader","(","f",")",":","# See DBF format spec at:","# http:\/\/www.pgts.com.au\/download\/public\/xbase.htm#DBF_STRUCT","numrec",",","lenheader","=","struct",".","unpack","(","'<xxxxLH22x'",",","f",".","read","(","32",")",")","numfields","=","(","lenheader","-","33",")","\/\/","32","fields","=","[","]","for","fieldno","in","range","(","numfields",")",":","name",",","typ",",","size",",","deci","=","struct",".","unpack","(","'<11sc4xBB14x'",",","f",".","read","(","32",")",")","name","=","name",".","replace","(","'\\0'",",","''",")","# eliminate NULs from string ","fields",".","append","(","(","name",",","typ",",","size",",","deci",")",")","yield","[","field","[","0","]","for","field","in","fields","]","yield","[","tuple","(","field","[","1",":","]",")","for","field","in","fields","]","terminator","=","f",".","read","(","1",")","assert","terminator","==","'\\r'","fields",".","insert","(","0",",","(","'DeletionFlag'",",","'C'",",","1",",","0",")",")","fmt","=","''",".","join","(","[","'%ds'","%","fieldinfo","[","2","]","for","fieldinfo","in","fields","]",")","fmtsiz","=","struct",".","calcsize","(","fmt",")","for","i","in","range","(","numrec",")",":","record","=","struct",".","unpack","(","fmt",",","f",".","read","(","fmtsiz",")",")","if","record","[","0","]","!=","' '",":","continue","# deleted record","result","=","[","]","for","(","name",",","typ",",","size",",","deci",")",",","value","in","zip","(","fields",",","record",")",":","if","name","==","'DeletionFlag'",":","continue","if","typ","==","\"N\"",":","value","=","value",".","replace","(","'\\0'",",","''",")",".","lstrip","(",")","if","value","==","''",":","value","=","0","elif","deci",":","try",":","value","=","decimal",".","Decimal","(","value",")","except","decimal",".","InvalidOperation",":","# Could be '************************' (for NULL)","value","=","0","else",":","value","=","value","elif","typ","==","'D'",":","y",",","m",",","d","=","int","(","value","[",":","4","]",")",",","int","(","value","[","4",":","6","]",")",",","int","(","value","[","6",":","8","]",")","try",":","value","=","datetime",".","date","(","y",",","m",",","d",")","except","ValueError",":","# Could be '00000000' (for NULL)","value","=","0","elif","typ","==","'L'",":","value","=","(","value","in","'YyTt'","and","'T'",")","or","(","value","in","'NnFf'","and","'F'",")","or","'?'","result",".","append","(","value",")","yield","result"],"url":"https:\/\/github.com\/CartoDB\/cartodb\/blob\/46b91228a01123976363f55a5e1d712a600785a6\/lib\/importer\/misc\/dbfUtils.py#L3-L64"}
{"nwo":"CartoDB\/cartodb","sha":"46b91228a01123976363f55a5e1d712a600785a6","path":"lib\/importer\/misc\/dbfUtils.py","language":"python","identifier":"dbfwriter","parameters":"(f, fieldnames, fieldspecs, records)","argument_list":"","return_statement":"","docstring":"Return a string suitable for writing directly to a binary dbf file.\n\n File f should be open for writing in a binary mode.\n\n Fieldnames should be no longer than ten characters and not include \\x00.\n Fieldspecs are in the form (type, size, deci) where\n type is one of:\n C for ascii character data\n M for ascii character memo data (real memo fields not supported)\n D for datetime objects\n N for ints or decimal objects\n L for logical values 'T', 'F', or '?'\n size is the field width\n deci is the number of decimal places in the provided decimal object\n Records can be an iterable over the records (sequences of field values).","docstring_summary":"Return a string suitable for writing directly to a binary dbf file.","docstring_tokens":["Return","a","string","suitable","for","writing","directly","to","a","binary","dbf","file","."],"function":"def dbfwriter(f, fieldnames, fieldspecs, records):\n \"\"\" Return a string suitable for writing directly to a binary dbf file.\n\n File f should be open for writing in a binary mode.\n\n Fieldnames should be no longer than ten characters and not include \\x00.\n Fieldspecs are in the form (type, size, deci) where\n type is one of:\n C for ascii character data\n M for ascii character memo data (real memo fields not supported)\n D for datetime objects\n N for ints or decimal objects\n L for logical values 'T', 'F', or '?'\n size is the field width\n deci is the number of decimal places in the provided decimal object\n Records can be an iterable over the records (sequences of field values).\n \n \"\"\"\n # header info\n ver = 3\n now = datetime.datetime.now()\n yr, mon, day = now.year-1900, now.month, now.day\n numrec = len(records)\n numfields = len(fieldspecs)\n lenheader = numfields * 32 + 33\n lenrecord = sum(field[1] for field in fieldspecs) + 1\n hdr = struct.pack('<BBBBLHH20x', ver, yr, mon, day, numrec, lenheader, lenrecord)\n f.write(hdr)\n \n # field specs\n for name, (typ, size, deci) in zip(fieldnames, fieldspecs):\n name = name.ljust(11, '\\x00')\n fld = struct.pack('<11sc4xBB14x', name, typ, size, deci)\n f.write(fld)\n\n # terminator\n f.write('\\r')\n\n # records\n for record in records:\n f.write(' ') # deletion flag\n for (typ, size, deci), value in zip(fieldspecs, record):\n if typ == \"N\":\n value = str(value).rjust(size, ' ')\n elif typ == 'D':\n value = value.strftime('%Y%m%d')\n elif typ == 'L':\n value = str(value)[0].upper()\n else:\n value = str(value)[:size].ljust(size, ' ')\n assert len(value) == size\n f.write(value)\n\n # End of file\n f.write('\\x1A')","function_tokens":["def","dbfwriter","(","f",",","fieldnames",",","fieldspecs",",","records",")",":","# header info","ver","=","3","now","=","datetime",".","datetime",".","now","(",")","yr",",","mon",",","day","=","now",".","year","-","1900",",","now",".","month",",","now",".","day","numrec","=","len","(","records",")","numfields","=","len","(","fieldspecs",")","lenheader","=","numfields","*","32","+","33","lenrecord","=","sum","(","field","[","1","]","for","field","in","fieldspecs",")","+","1","hdr","=","struct",".","pack","(","'<BBBBLHH20x'",",","ver",",","yr",",","mon",",","day",",","numrec",",","lenheader",",","lenrecord",")","f",".","write","(","hdr",")","# field specs","for","name",",","(","typ",",","size",",","deci",")","in","zip","(","fieldnames",",","fieldspecs",")",":","name","=","name",".","ljust","(","11",",","'\\x00'",")","fld","=","struct",".","pack","(","'<11sc4xBB14x'",",","name",",","typ",",","size",",","deci",")","f",".","write","(","fld",")","# terminator","f",".","write","(","'\\r'",")","# records","for","record","in","records",":","f",".","write","(","' '",")","# deletion flag","for","(","typ",",","size",",","deci",")",",","value","in","zip","(","fieldspecs",",","record",")",":","if","typ","==","\"N\"",":","value","=","str","(","value",")",".","rjust","(","size",",","' '",")","elif","typ","==","'D'",":","value","=","value",".","strftime","(","'%Y%m%d'",")","elif","typ","==","'L'",":","value","=","str","(","value",")","[","0","]",".","upper","(",")","else",":","value","=","str","(","value",")","[",":","size","]",".","ljust","(","size",",","' '",")","assert","len","(","value",")","==","size","f",".","write","(","value",")","# End of file","f",".","write","(","'\\x1A'",")"],"url":"https:\/\/github.com\/CartoDB\/cartodb\/blob\/46b91228a01123976363f55a5e1d712a600785a6\/lib\/importer\/misc\/dbfUtils.py#L67-L121"}