示例#1
0
文件: ipac.py 项目: Cadair/astropy
    def str_vals(self):

        if self.DBMS:
            IpacFormatE = IpacFormatErrorDBMS
        else:
            IpacFormatE = IpacFormatError

        namelist = self.colnames
        if self.DBMS:
            countnamelist = defaultdict(int)
            for name in self.colnames:
                countnamelist[name.lower()] += 1
            doublenames = [x for x in countnamelist if countnamelist[x] > 1]
            if doublenames != []:
                raise IpacFormatE('IPAC DBMS tables are not case sensitive. '
                                  'This causes duplicate column names: {0}'.format(doublenames))

        for name in namelist:
            m = re.match(r'\w+', name)
            if m.end() != len(name):
                raise IpacFormatE('{0} - Only alphanumeric characters and _ '
                                  'are allowed in column names.'.format(name))
            if self.DBMS and not(name[0].isalpha() or (name[0] == '_')):
                raise IpacFormatE('Column name cannot start with numbers: {}'.format(name))
            if self.DBMS:
                if name in ['x', 'y', 'z', 'X', 'Y', 'Z']:
                    raise IpacFormatE('{0} - x, y, z, X, Y, Z are reserved names and '
                                      'cannot be used as column names.'.format(name))
                if len(name) > 16:
                    raise IpacFormatE(
                        '{0} - Maximum length for column name is 16 characters'.format(name))
            else:
                if len(name) > 40:
                    raise IpacFormatE(
                        '{0} - Maximum length for column name is 40 characters.'.format(name))

        dtypelist = []
        unitlist = []
        nullist = []
        for col in self.cols:
            col_dtype = col.info.dtype
            col_unit = col.info.unit
            col_format = col.info.format

            if col_dtype.kind in ['i', 'u']:
                if col_dtype.itemsize <= 2:
                    dtypelist.append('int')
                else:
                    dtypelist.append('long')
            elif col_dtype.kind == 'f':
                if col_dtype.itemsize <= 4:
                    dtypelist.append('float')
                else:
                    dtypelist.append('double')
            else:
                dtypelist.append('char')

            if col_unit is None:
                unitlist.append('')
            else:
                unitlist.append(str(col.info.unit))
            # This may be incompatible with mixin columns
            null = col.fill_values[core.masked]
            try:
                auto_format_func = get_auto_format_func(col)
                format_func = col.info._format_funcs.get(col_format, auto_format_func)
                nullist.append((format_func(col_format, null)).strip())
            except Exception:
                # It is possible that null and the column values have different
                # data types (e.g. number and null = 'null' (i.e. a string).
                # This could cause all kinds of exceptions, so a catch all
                # block is needed here
                nullist.append(str(null).strip())

        return [namelist, dtypelist, unitlist, nullist]
示例#2
0
    def str_vals(self):

        if self.DBMS:
            IpacFormatE = IpacFormatErrorDBMS
        else:
            IpacFormatE = IpacFormatError

        namelist = self.colnames
        if self.DBMS:
            countnamelist = defaultdict(int)
            for name in self.colnames:
                countnamelist[name.lower()] += 1
            doublenames = [x for x in countnamelist if countnamelist[x] > 1]
            if doublenames != []:
                raise IpacFormatE(
                    'IPAC DBMS tables are not case sensitive. '
                    'This causes duplicate column names: {0}'.format(
                        doublenames))

        for name in namelist:
            m = re.match(r'\w+', name)
            if m.end() != len(name):
                raise IpacFormatE('{0} - Only alphanumeric characters and _ '
                                  'are allowed in column names.'.format(name))
            if self.DBMS and not (name[0].isalpha() or (name[0] == '_')):
                raise IpacFormatE(
                    'Column name cannot start with numbers: {}'.format(name))
            if self.DBMS:
                if name in ['x', 'y', 'z', 'X', 'Y', 'Z']:
                    raise IpacFormatE(
                        '{0} - x, y, z, X, Y, Z are reserved names and '
                        'cannot be used as column names.'.format(name))
                if len(name) > 16:
                    raise IpacFormatE(
                        '{0} - Maximum length for column name is 16 characters'
                        .format(name))
            else:
                if len(name) > 40:
                    raise IpacFormatE(
                        '{0} - Maximum length for column name is 40 characters.'
                        .format(name))

        dtypelist = []
        unitlist = []
        nullist = []
        for col in self.cols:
            col_dtype = col.info.dtype
            col_unit = col.info.unit
            col_format = col.info.format

            if col_dtype.kind in ['i', 'u']:
                if col_dtype.itemsize <= 2:
                    dtypelist.append('int')
                else:
                    dtypelist.append('long')
            elif col_dtype.kind == 'f':
                if col_dtype.itemsize <= 4:
                    dtypelist.append('float')
                else:
                    dtypelist.append('double')
            else:
                dtypelist.append('char')

            if col_unit is None:
                unitlist.append('')
            else:
                unitlist.append(str(col.info.unit))
            # This may be incompatible with mixin columns
            null = col.fill_values[core.masked]
            try:
                auto_format_func = get_auto_format_func(col)
                format_func = col.info._format_funcs.get(
                    col_format, auto_format_func)
                nullist.append((format_func(col_format, null)).strip())
            except Exception:
                # It is possible that null and the column values have different
                # data types (e.g. number and null = 'null' (i.e. a string).
                # This could cause all kinds of exceptions, so a catch all
                # block is needed here
                nullist.append(str(null).strip())

        return [namelist, dtypelist, unitlist, nullist]