Esempio n. 1
0
    def get_cols(self, lines):
        """Initialize the header Column objects from the table ``lines``.

        Based on the previously set Header attributes find or create the column names.
        Sets ``self.cols`` with the list of Columns.  This list only includes the actual
        requested columns after filtering by the include_names and exclude_names
        attributes.  See ``self.names`` for the full list.

        :param lines: list of table lines
        :returns: list of table Columns
        """

        if self.names is None:
            # No column names supplied so first try to get them from NumPy structured array
            try:
                self.names = lines.dtype.names
            except AttributeError:
                # Still no col names available so auto-generate them
                try:
                    first_data_vals = next(iter(lines))
                except StopIteration:
                    raise core.InconsistentTableError('No data lines found so cannot autogenerate column names')
                n_data_cols = len(first_data_vals)
                self.names = [self.auto_format % i for i in range(1, n_data_cols+1)]

        names = set(self.names)
        if self.include_names is not None:
            names.intersection_update(self.include_names)
        if self.exclude_names is not None:
            names.difference_update(self.exclude_names)
            
        self.cols = [core.Column(name=x, index=i) for i, x in enumerate(self.names)
                     if x in names]

        # ``lines`` could be one of: NumPy recarray, DictLikeNumpy obj, Python
        # list of lists. If NumPy recarray then set col.type accordingly.  In
        # the other two cases convert the data values to strings so the usual
        # data converter processing will get the correct type.
        if core.has_numpy and isinstance(lines, numpy.ndarray):
            for col in self.cols:
                type_name = lines[col.name].dtype.name
                if 'int' in type_name:
                    col.type = core.IntType
                elif 'float' in type_name:
                    col.type = core.FloatType
                elif 'string' in type_name:
                    col.type = core.StrType
        else:
            basic_reader = core._get_reader(Reader=basic.BasicReader,
                                            names=[col.name for col in self.cols], quotechar="'")
            data_in = tuple(' '.join(repr(vals[col.index]) for col in self.cols)
                            for vals in lines)
            data_out = basic_reader.read(data_in)
            for col, data_col in zip(self.cols, basic_reader.cols):
                col.type = data_col.type
Esempio n. 2
0
    def get_cols(self, lines):
        """Initialize the header Column objects from the table ``lines``.

        Based on the previously set Header attributes find or create the column names.
        Sets ``self.cols`` with the list of Columns.  This list only includes the actual
        requested columns after filtering by the include_names and exclude_names
        attributes.  See ``self.names`` for the full list.

        :param lines: list of table lines
        :returns: list of table Columns
        """

        if self.names is None:
            # No column names supplied so first try to get them from NumPy structured array
            try:
                self.names = lines.dtype.names
            except AttributeError:
                # Still no col names available so auto-generate them
                try:
                    first_data_vals = next(iter(lines))
                except StopIteration:
                    raise core.InconsistentTableError(
                        'No data lines found so cannot autogenerate column names'
                    )
                n_data_cols = len(first_data_vals)
                self.names = [
                    self.auto_format % i for i in range(1, n_data_cols + 1)
                ]

        self._set_cols_from_names()

        # ``lines`` could be one of: NumPy recarray, DictLikeNumpy obj, Python
        # list of lists. If NumPy recarray then set col.type accordingly.  In
        # the other two cases convert the data values to strings so the usual
        # data converter processing will get the correct type.
        if core.has_numpy and isinstance(lines, numpy.ndarray):
            for col in self.cols:
                type_name = lines[col.name].dtype.name
                if 'int' in type_name:
                    col.type = core.IntType
                elif 'float' in type_name:
                    col.type = core.FloatType
                elif 'str' in type_name:
                    col.type = core.StrType
        else:
            # lines is a list of lists or DictLikeNumpy.
            col_types = {}
            col_indexes = [col.index for col in self.cols]
            for vals in lines:
                for col_index in col_indexes:
                    val = vals[col_index]
                    col_type_set = col_types.setdefault(col_index, set())
                    col_type_set.add(get_val_type(val))
            for col in self.cols:
                col.type = get_lowest_type(col_types[col.index])
Esempio n. 3
0
    def get_lines(self, table, names):
        """Get the lines from the ``table`` input.
        
        :param table: table input
        :param names: list of column names (only used for dict of lists to set column order)
        :returns: list of lines

        """
        try:
            # If table is dict-like this will return the first key.
            # If table is list-like this will return the first row.
            first_row_or_key = next(iter(table))
        except TypeError:
            # Not iterable, is it an asciitable Reader instance?
            if isinstance(table, core.BaseReader):
                lines = table.table
            else:
                # None of the possible choices so raise exception
                raise TypeError(
                    'Input table must be iterable or else be a Reader object')
        else:
            # table is iterable, now see if it is dict-like or list-like
            try:
                # If first_row_or_key is a row (in the case that table is
                # list-like) then this will raise exception
                table[first_row_or_key]
            except (TypeError, IndexError, ValueError):
                # Table is list-like (python list-of-lists or numpy recarray)
                lines = table
            else:
                # Table is dict-like.  Turn this into a DictLikeNumpy that has
                # an API similar to a numpy recarray.
                lines = core.DictLikeNumpy(table)
                if names is None:
                    lines.dtype.names = sorted(lines.keys())
                else:
                    lines.dtype.names = names

        # ``lines`` could now be one of the following iterable objects:
        # - NumPy recarray
        # - DictLikeNumpy object
        # - Python list of lists
        return lines
Esempio n. 4
0
    def get_lines(self, table, names):
        """Get the lines from the ``table`` input.
        
        :param table: table input
        :param names: list of column names (only used for dict of lists to set column order)
        :returns: list of lines

        """
        try:  
            # If table is dict-like this will return the first key.
            # If table is list-like this will return the first row.
            first_row_or_key = next(iter(table))
        except TypeError:
            # Not iterable, is it an asciitable Reader instance?
            if isinstance(table, core.BaseReader):
                lines = table.table
            else:
                # None of the possible choices so raise exception
                raise TypeError('Input table must be iterable or else be a Reader object')
        else:
            # table is iterable, now see if it is dict-like or list-like
            try:
                # If first_row_or_key is a row (in the case that table is
                # list-like) then this will raise exception
                table[first_row_or_key]
            except (TypeError, IndexError):
                # Table is list-like (python list-of-lists or numpy recarray)
                lines = table
            else:
                # Table is dict-like.  Turn this into a DictLikeNumpy that has
                # an API similar to a numpy recarray.
                lines = core.DictLikeNumpy(table)
                if names is None:
                    lines.dtype.names = sorted(lines.keys())
                else:
                    lines.dtype.names = names

        # ``lines`` could now be one of the following iterable objects:
        # - NumPy recarray
        # - DictLikeNumpy object
        # - Python list of lists
        return lines