예제 #1
0
def _rows2dict_field_helper(fields, i, row, mappings, rowdict):
    """Called per column in a row to convert it to a dict and fix field types.

    fields.DECIMALIZE2 and fields.DATETIMEDIR are used to determine special field handling.
    Also fields ending in '_date' are converted to datetime objects.

    >>> _rows2dict_field_helper(['LFARTN'], 0, ['12345'], {'LFARTN': 'artnr'}, {})
    {'artnr': '12345'}
    >>> _rows2dict_field_helper(['TST'], 0, ['1030821'], {'TST': 'test_date'}, {})
    {'test_date': datetime.date(2003, 8, 21)}
    >>> _rows2dict_field_helper(['LFARTN'], 0, [12345], {}, {'stuff': 'ruff'})
    {'LFARTN': 12345, 'stuff': 'ruff'}
    >>> _rows2dict_field_helper(['PNPRB'], 0, [12345], {}, {})
    {'PNPRB': Decimal('12345.00')}
    """

    feldname = fields[i]
    data = _fix_field(row[i], feldname)

    # fixup based on verbose field names / dict keys
    if feldname in mappings:
        # key ist der "schöne" Feldname
        finalkey = mappings[feldname]
    else:
        # key ist das AS400 Feldname
        finalkey = feldname

    if finalkey.endswith('_date'):
        # special mapping for date time fields
        if not data:
            rowdict[finalkey] = None
        else:
            rowdict[finalkey] = softm2date(data)
            # check if there is also a time field
            if feldname in DATETIMEDIR:
                rowdict.update(_combine_date_and_time(mappings, feldname, data, fields, row))
    else:
        # key ist der "schöne" feldname
        rowdict[finalkey] = data

    return rowdict
예제 #2
0
    def _rows2dict(self, fields, mappings, rows):
        """Convert the list of rows we get from the server to a dict of columnames."""

        ret = []
        for row in rows:
            rowdict = {}
            for i in range(len(fields)):
                data = self._fix_field(row[i])
                if fields[i] in mappings:
                    if mappings[fields[i]].endswith('_date'):
                        if not row[i]:
                            rowdict[mappings[fields[i]]] = None
                        else:
                            rowdict[mappings[fields[i]]] = softm2date(row[i])
                            # check if there is also a time field
                            if fields[i] in DATETIMEDIR:
                                _combine_date_and_time(mappings, fields, i, row, rowdict)
                    else:
                        rowdict[mappings[fields[i]]] = data
                else:
                    rowdict[fields[i]] = data
            ret.append(rowdict)
        return ret