示例#1
0
文件: ui.py 项目: phn/asciitable
def get_reader(Reader=None, Inputter=None, Outputter=None, numpy=True, **kwargs):
    """Initialize a table reader allowing for common customizations.  Most of the
    default behavior for various parameters is determined by the Reader class.

    :param Reader: Reader class (default= :class:`BasicReader`)
    :param Inputter: Inputter class 
    :param Outputter: Outputter class
    :param numpy: use the NumpyOutputter class else use BaseOutputter (default=True)
    :param delimiter: column delimiter string
    :param comment: regular expression defining a comment line in table
    :param quotechar: one-character string to quote fields containing special characters
    :param header_start: line index for the header line not counting comment lines
    :param data_start: line index for the start of data not counting comment lines
    :param data_end: line index for the end of data (can be negative to count from end)
    :param converters: dict of converters
    :param data_Splitter: Splitter class to split data columns
    :param header_Splitter: Splitter class to split header columns
    :param names: list of names corresponding to each data column
    :param include_names: list of names to include in output (default=None selects all names)
    :param exclude_names: list of names to exlude from output (applied after ``include_names``)
    :param fill_values: specification of fill values for bad or missing table values
    :param fill_include_names: list of names to include in fill_values (default=None selects all names)
    :param fill_exclude_names: list of names to exlude from fill_values (applied after ``fill_include_names``)
    """
    # This function is a light wrapper around core._get_reader to provide a public interface
    # with a default Reader.
    if Reader is None:
        Reader = basic.BasicReader
    reader = core._get_reader(Reader, Inputter=Inputter, Outputter=Outputter, numpy=numpy, **kwargs)
    return reader
示例#2
0
def get_reader(Reader=None, Inputter=None, Outputter=None, numpy=True, **kwargs):
    """Initialize a table reader allowing for common customizations.  Most of the
    default behavior for various parameters is determined by the Reader class.

    :param Reader: Reader class (default= :class:`BasicReader`)
    :param Inputter: Inputter class 
    :param Outputter: Outputter class
    :param numpy: use the NumpyOutputter class else use BaseOutputter (default=True)
    :param delimiter: column delimiter string
    :param comment: regular expression defining a comment line in table
    :param quotechar: one-character string to quote fields containing special characters
    :param header_start: line index for the header line not counting comment lines
    :param data_start: line index for the start of data not counting comment lines
    :param data_end: line index for the end of data (can be negative to count from end)
    :param converters: dict of converters
    :param data_Splitter: Splitter class to split data columns
    :param header_Splitter: Splitter class to split header columns
    :param names: list of names corresponding to each data column
    :param include_names: list of names to include in output (default=None selects all names)
    :param exclude_names: list of names to exlude from output (applied after ``include_names``)
    :param fill_values: specification of fill values for bad or missing table values
    :param fill_include_names: list of names to include in fill_values (default=None selects all names)
    :param fill_exclude_names: list of names to exlude from fill_values (applied after ``fill_include_names``)
    """
    # This function is a light wrapper around core._get_reader to provide a public interface
    # with a default Reader.
    if Reader is None:
        Reader = basic.BasicReader
    reader = core._get_reader(Reader, Inputter=Inputter, Outputter=Outputter, numpy=numpy, **kwargs)
    return reader
示例#3
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
示例#4
0
    def read(self, table):
        output = core.BaseReader.read(self, table)
        if core.has_numpy:
            reader = core._get_reader(Reader=basic.NoHeaderReader, comment=r'(?!#K)', 
                                      names = ['temp1','keyword','temp2','value','unit','format'])
            headerkeywords = reader.read(self.comment_lines)

            for line in headerkeywords:
                self.keywords.append(core.Keyword(line['keyword'], line['value'], 
                                                  units=line['unit'], format=line['format']))
        self.table = output
        self.cols = self.header.cols

        return self.table
示例#5
0
    def read(self, table):
        output = core.BaseReader.read(self, table)
        if core.has_numpy:
            reader = core._get_reader(
                Reader=basic.NoHeaderReader,
                comment=r'(?!#K)',
                names=['temp1', 'keyword', 'temp2', 'value', 'unit', 'format'])
            headerkeywords = reader.read(self.comment_lines)

            for line in headerkeywords:
                self.keywords.append(
                    core.Keyword(line['keyword'],
                                 line['value'],
                                 units=line['unit'],
                                 format=line['format']))
        self.table = output
        self.cols = self.header.cols

        return self.table