Пример #1
0
def merge(items, key=(lambda x: x)):
    """
    Given sorted lists of iterables, return new iterable that returns
    elements of all iterables sorted with respect to key.
    """
    state = {}
    for item in map(iter, items):
        try:
            first = next(item)
        except StopIteration:
            continue
        else:
            state[item] = (first, key(first))

    while state:
        for item, (value, tk) in six.iteritems(state):
            # Value is biggest.
            if all(tk >= k for it, (v, k) in six.iteritems(state)
                   if it is not item):
                yield value
                break
        try:
            n = next(item)
            state[item] = (n, key(n))
        except StopIteration:
            del state[item]
Пример #2
0
def merge(items, key=(lambda x: x)):
    """
    Given sorted lists of iterables, return new iterable that returns
    elements of all iterables sorted with respect to key.
    """
    state = {}
    for item in map(iter, items):
        try:
            first = next(item)
        except StopIteration:
            continue
        else:
            state[item] = (first, key(first))

    while state:
        for item, (value, tk) in six.iteritems(state):
            # Value is biggest.
            if all(tk >= k for it, (v, k) in six.iteritems(state)
                   if it is not item):
                yield value
                break
        try:
            n = next(item)
            state[item] = (n, key(n))
        except StopIteration:
            del state[item]
Пример #3
0
def print_table(lst, colsep=' ', linesep='\n'):
    width = [max(map(len, col)) for col in zip(*lst)]
    return linesep.join(
        colsep.join(
            col.ljust(n) for n, col in zip(width, row)
        ) for row in lst
    )
Пример #4
0
def print_table(lst, colsep=' ', linesep='\n'):
    """
    ?

    Parameters
    ----------
    lst : ?
        ?
    colsep : ?
        ?
    linesep : ?
        ?

    Returns
    -------
    ?

    .. todo::
        improve documentation.

    """
    width = [max(map(len, col)) for col in zip(*lst)]
    return linesep.join(
        colsep.join(
            col.ljust(n) for n, col in zip(width, row)
        ) for row in lst
    )
Пример #5
0
def _create_display_table(database_entries, columns=None, sort=False):
    """Generate a table to display the database entries.

    Parameters
    ----------
    database_entries : iterable of :class:`DatabaseEntry` instances
        The database entries will be the rows in the resulting table.

    columns : iterable of str
        The columns that will be displayed in the resulting table. Possible
        values for the strings are all attributes of :class:`DatabaseEntry`.

    sort : bool (optional)
        If True, sorts the entries before displaying them.

    Returns
    -------
    str
        An astropy table that can be printed on the console or written to a
        file.

    """
    if columns is None:
        columns = [
            'id', 'observation_time_start', 'observation_time_end',
            'instrument', 'source', 'provider', 'physobs', 'wavemin',
            'wavemax', 'path', 'fileid', 'tags', 'starred', 'download_time',
            'size'
        ]

    data = []
    for entry in database_entries:
        row = []
        for col in columns:
            if col == 'starred':
                row.append('Yes' if entry.starred else 'No')
            elif col == 'tags':
                row.append(', '.join(map(str, entry.tags)) or 'N/A')
            elif col == 'hdu_index':
                row.append(entry.hdu_index)
            # do not display microseconds in datetime columns
            elif col in ('observation_time_start', 'observation_time_end',
                         'download_time'):
                time = getattr(entry, col, None)
                if time is None:
                    formatted_time = 'N/A'
                else:
                    formatted_time = time.strftime(TIME_FORMAT)
                row.append(formatted_time)
            else:
                row.append(str(getattr(entry, col) or 'N/A'))
        if not row:
            raise TypeError('at least one column must be given')
        data.append(row)
    if not data:
        raise TypeError('given iterable is empty')
    if sort:
        data.sort()
    return astropy.table.Table(rows=data, names=columns)
Пример #6
0
def _create_display_table(database_entries, columns=None, sort=False):
    """Generate a table to display the database entries.

    Parameters
    ----------
    database_entries : iterable of :class:`DatabaseEntry` instances
        The database entries will be the rows in the resulting table.

    columns : iterable of str
        The columns that will be displayed in the resulting table. Possible
        values for the strings are all attributes of :class:`DatabaseEntry`.

    sort : bool (optional)
        If True, sorts the entries before displaying them.

    Returns
    -------
    str
        An astropy table that can be printed on the console or written to a
        file.

    """
    if columns is None:
        columns = ['id', 'observation_time_start', 'observation_time_end',
                   'instrument', 'source', 'provider', 'physobs', 'wavemin',
                   'wavemax', 'path', 'fileid', 'tags', 'starred',
                   'download_time', 'size']

    data = []
    for entry in database_entries:
        row = []
        for col in columns:
            if col == 'starred':
                row.append('Yes' if entry.starred else 'No')
            elif col == 'tags':
                row.append(', '.join(map(str, entry.tags)) or 'N/A')
            elif col == 'hdu_index':
                row.append(entry.hdu_index)
            # do not display microseconds in datetime columns
            elif col in (
                    'observation_time_start',
                    'observation_time_end',
                    'download_time'):
                time = getattr(entry, col, None)
                if time is None:
                    formatted_time = 'N/A'
                else:
                    formatted_time = time.strftime(TIME_FORMAT)
                row.append(formatted_time)
            else:
                row.append(str(getattr(entry, col) or 'N/A'))
        if not row:
            raise TypeError('at least one column must be given')
        data.append(row)
    if not data:
        raise TypeError('given iterable is empty')
    if sort:
        data.sort()
    return astropy.table.Table(rows=data, names=columns)
Пример #7
0
def display_entries(database_entries, columns, sort=False):
    """Generate a table to display the database entries.

    Parameters
    ----------
    database_entries : iterable of :class:`DatabaseEntry` instances
        The database entries will be the rows in the resulting table.

    columns : iterable of str
        The columns that will be displayed in the resulting table. Possible
        values for the strings are all attributes of :class:`DatabaseEntry`.

    sort : bool (optional)
        If True, sorts the entries before displaying them.

    Returns
    -------
    str
        A formatted table that can be printed on the console or written to a
        file.

    """
    header = [columns]
    rulers = [['-' * len(col) for col in columns]]
    data = []
    for entry in database_entries:
        row = []
        for col in columns:
            if col == 'starred':
                row.append('Yes' if entry.starred else 'No')
            elif col == 'tags':
                row.append(', '.join(map(str, entry.tags)) or 'N/A')
            # do not display microseconds in datetime columns
            elif col in ('observation_time_start', 'observation_time_end',
                         'download_time'):
                time = getattr(entry, col, None)
                if time is None:
                    formatted_time = 'N/A'
                else:
                    formatted_time = time.strftime(TIME_FORMAT)
                row.append(formatted_time)
            else:
                row.append(str(getattr(entry, col) or 'N/A'))
        if not row:
            raise TypeError('at least one column must be given')
        data.append(row)
    if not data:
        raise TypeError('given iterable is empty')
    if sort:
        data.sort()
    return print_table(header + rulers + data)
Пример #8
0
def display_entries(database_entries, columns, sort=False):
    """Generate a table to display the database entries.

    Parameters
    ----------
    database_entries : iterable of :class:`DatabaseEntry` instances
        The database entries will be the rows in the resulting table.

    columns : iterable of str
        The columns that will be displayed in the resulting table. Possible
        values for the strings are all attributes of :class:`DatabaseEntry`.

    sort : bool (optional)
        If True, sorts the entries before displaying them.

    Returns
    -------
    str
        A formatted table that can be printed on the console or written to a
        file.

    """
    header = [columns]
    rulers = [["-" * len(col) for col in columns]]
    data = []
    for entry in database_entries:
        row = []
        for col in columns:
            if col == "starred":
                row.append("Yes" if entry.starred else "No")
            elif col == "tags":
                row.append(", ".join(map(str, entry.tags)) or "N/A")
            # do not display microseconds in datetime columns
            elif col in ("observation_time_start", "observation_time_end", "download_time"):
                time = getattr(entry, col, None)
                if time is None:
                    formatted_time = "N/A"
                else:
                    formatted_time = time.strftime(TIME_FORMAT)
                row.append(formatted_time)
            else:
                row.append(str(getattr(entry, col) or "N/A"))
        if not row:
            raise TypeError("at least one column must be given")
        data.append(row)
    if not data:
        raise TypeError("given iterable is empty")
    if sort:
        data.sort()
    return print_table(header + rulers + data)
Пример #9
0
    def __call__(self, *args, **kwargs):
        objs = self.get(*args, **kwargs)

        # pylint: disable=W0141
        types = tuple(map(type, objs))

        # This code is duplicate for performance reasons.
        cached = self.cache.get(types, None)
        if cached is not None:
            return cached(*args, **kwargs)

        for signature, fun in reversed(self.methods):
            if all(issubclass(ty, sig) for ty, sig in zip(types, signature)):
                self.cache[types] = fun
                return fun(*args, **kwargs)
        raise TypeError('{0!r}'.format(types))
Пример #10
0
    def __call__(self, *args, **kwargs):
        objs = self.get(*args, **kwargs)

        # pylint: disable=W0141
        types = tuple(map(type, objs))

        # This code is duplicate for performance reasons.
        cached = self.cache.get(types, None)
        if cached is not None:
            return cached(*args, **kwargs)

        for signature, fun in reversed(self.methods):
            if all(issubclass(ty, sig) for ty, sig in zip(types, signature)):
                self.cache[types] = fun
                return fun(*args, **kwargs)
        raise TypeError('{0!r}'.format(types))
Пример #11
0
def print_table(lst, colsep=' ', linesep='\n'):
    """
    ?

    Parameters
    ----------
    lst : ?
        ?
    colsep : ?
        ?
    linesep : ?
        ?

    Returns
    -------
    ?

    .. todo::
        improve documentation.

    """
    width = [max(map(len, col)) for col in zip(*lst)]
    return linesep.join(
        colsep.join(col.ljust(n) for n, col in zip(width, row)) for row in lst)
Пример #12
0
def print_table(lst, colsep=' ', linesep='\n'):
    width = [max(map(len, col)) for col in zip(*lst)]
    return linesep.join(
        colsep.join(col.ljust(n) for n, col in zip(width, row)) for row in lst)
Пример #13
0
 def from_files(cls, filenames):
     """ Return list of object read from given list of
     filenames. """
     filenames = list(map(os.path.expanduser, filenames))
     return cls.read_many(filenames)
Пример #14
0
 def read_many(cls, filenames):
     return list(map(cls.read, filenames))
Пример #15
0
 def from_files(cls, filenames):
     """ Return list of object read from given list of
     filenames. """
     filenames = list(map(os.path.expanduser, filenames))
     return cls.read_many(filenames)
Пример #16
0
 def read_many(cls, filenames):
     return list(map(cls.read, filenames))