當前位置: 首頁>>代碼示例>>Python>>正文


Python OrderedDict.keys方法代碼示例

本文整理匯總了Python中astropy.utils.OrderedDict.keys方法的典型用法代碼示例。如果您正苦於以下問題:Python OrderedDict.keys方法的具體用法?Python OrderedDict.keys怎麽用?Python OrderedDict.keys使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在astropy.utils.OrderedDict的用法示例。


在下文中一共展示了OrderedDict.keys方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: _parse_result

# 需要導入模塊: from astropy.utils import OrderedDict [as 別名]
# 或者: from astropy.utils.OrderedDict import keys [as 別名]
    def _parse_result(self, response, get_catalog_names=False, verbose=False):
        """
        Parses the HTTP response to create an `astropy.table.Table`.
        Returns the raw result as a string in case of parse errors.

        Parameters
        ----------
        response : `requests.Response`
            The response of the HTTP POST request
        get_catalog_names : bool
            If specified, return only the table names (useful for table
            discovery)

        Returns
        -------
        `astroquery.utils.commons.TableList`
            An OrderedDict of `astropy.table.Table` objects.
            If there are errors in the parsing, then returns the raw results as a string.
        """
        if not verbose:
            commons.suppress_vo_warnings()
        try:
            tf = tempfile.NamedTemporaryFile()
            if PY3:
                tf.write(response.content)
            else:
                tf.write(response.content.encode('utf-8'))
            tf.file.flush()
            vo_tree = votable.parse(tf.name, pedantic=False)
            if get_catalog_names:
                return dict([(R.name,R) for R in vo_tree.resources])
            else:
                table_dict = OrderedDict()
                for t in vo_tree.iter_tables():
                    if len(t.array) > 0:
                        if t.ref is not None:
                            name = vo_tree.get_table_by_id(t.ref).name
                        else:
                            name = t.name
                        if name not in table_dict.keys():
                            table_dict[name] = []
                        table_dict[name] += [t.to_table()]
                for name in table_dict.keys():
                    if len(table_dict[name]) > 1:
                        table_dict[name] = tbl.vstack(table_dict[name])
                    else:
                        table_dict[name] = table_dict[name][0]
                return commons.TableList(table_dict)

        except:
            traceback.print_exc()  # temporary for debugging
            warnings.warn(
                "Error in parsing result, returning raw result instead")
            return response.content
開發者ID:anitameh,項目名稱:astroquery,代碼行數:56,代碼來源:core.py

示例2: VizierKeyword

# 需要導入模塊: from astropy.utils import OrderedDict [as 別名]
# 或者: from astropy.utils.OrderedDict import keys [as 別名]
class VizierKeyword(list):

    """Helper class for setting keywords for Vizier queries"""

    def __init__(self, keywords):
        file_name = aud.get_pkg_data_filename(
            os.path.join("data", "inverse_dict.json"))
        with open(file_name, 'r') as f:
            kwd = json.load(f)
            self.keyword_types = sorted(kwd.values())
            self.keyword_dict = OrderedDict([(k, kwd[k]) for k in sorted(kwd)])
        self._keywords = None
        self.keywords = keywords

    @property
    def keywords(self):
        """List or string for keyword(s) that must be set for the Vizier object."""
        return self._keywords

    @keywords.setter
    def keywords(self, values):
        if isinstance(values, six.string_types):
            values = list(values)
        keys = [key.lower() for key in self.keyword_dict]
        values = [val.lower() for val in values]
        # warn about unknown keywords
        for val in set(values) - set(keys):
            warnings.warn("{val} : No such keyword".format(val=val))
        valid_keys = [
            key for key in self.keyword_dict.keys()
            if key.lower() in list(map(str.lower, values))]
        # create a dict for each type of keyword
        set_keywords = OrderedDict()
        for key in self.keyword_dict:
            if key in valid_keys:
                if self.keyword_dict[key] in set_keywords:
                    set_keywords[self.keyword_dict[key]].append(key)
                else:
                    set_keywords[self.keyword_dict[key]] = [key]
        self._keywords = OrderedDict(
                [(k, sorted(set_keywords[k]))
                 for k in set_keywords]
                )

    @keywords.deleter
    def keywords(self):
        del self._keywords

    def __repr__(self):
        return "\n".join([x for key in self.keywords for x in self.get_keyword_str(key)])

    def get_keyword_str(self, key):
        """
        Helper function that returns the keywords, grouped into appropriate
        categories and suitable for the Vizier votable CGI.

        Comma-separated is not valid!!!
        """
        keyword_name = "-kw." + key
        return [keyword_name + "=" + s for s in self.keywords[key]]
開發者ID:AlexaVillaume,項目名稱:astroquery,代碼行數:62,代碼來源:core.py

示例3: parse_vizier_votable

# 需要導入模塊: from astropy.utils import OrderedDict [as 別名]
# 或者: from astropy.utils.OrderedDict import keys [as 別名]
def parse_vizier_votable(data, verbose=False, invalid='warn',
                         get_catalog_names=False):
    """
    Given a votable as string, parse it into tables
    """
    if not verbose:
        commons.suppress_vo_warnings()

    tf = BytesIO(data)

    if invalid == 'mask':
        vo_tree = votable.parse(tf, pedantic=False, invalid='mask')
    elif invalid == 'warn':
        try:
            vo_tree = votable.parse(tf, pedantic=False, invalid='exception')
        except Exception as ex:
            warnings.warn("VOTABLE parsing raised exception: {0}".format(ex))
            vo_tree = votable.parse(tf, pedantic=False, invalid='mask')
    elif invalid == 'exception':
        vo_tree = votable.parse(tf, pedantic=False, invalid='exception')
    else:
        raise ValueError("Invalid keyword for 'invalid'. "
                         "Must be exception, mask, or warn")

    if get_catalog_names:
        return dict([(R.name, R) for R in vo_tree.resources])
    else:
        table_dict = OrderedDict()
        for t in vo_tree.iter_tables():
            if len(t.array) > 0:
                if t.ref is not None:
                    name = vo_tree.get_table_by_id(t.ref).name
                else:
                    name = t.name
                if name not in table_dict.keys():
                    table_dict[name] = []
                table_dict[name] += [t.to_table()]
        for name in table_dict.keys():
            if len(table_dict[name]) > 1:
                table_dict[name] = tbl.vstack(table_dict[name])
            else:
                table_dict[name] = table_dict[name][0]
        return commons.TableList(table_dict)
開發者ID:knutago,項目名稱:astroquery,代碼行數:45,代碼來源:core.py

示例4: _parse_result

# 需要導入模塊: from astropy.utils import OrderedDict [as 別名]
# 或者: from astropy.utils.OrderedDict import keys [as 別名]
    def _parse_result(self, response, get_catalog_names=False, verbose=False, invalid='warn'):
        """
        Parses the HTTP response to create a `~astropy.table.Table`.

        Returns the raw result as a string in case of parse errors.

        Parameters
        ----------
        response : `requests.Response`
            The response of the HTTP POST request
        get_catalog_names : bool
            If specified, return only the table names (useful for table
            discovery)
        invalid : 'warn', 'mask' or 'raise'
            The behavior if a VOTABLE cannot be parsed.  Default is 'warn',
            which will try to parse the table, then if an exception is raised,
            it will be printent but the masked table will be returned

        Returns
        -------
        table_list : `astroquery.utils.TableList` or str
            If there are errors in the parsing, then returns the raw results as a string.
        """
        if not verbose:
            commons.suppress_vo_warnings()
        try:
            tf = six.BytesIO(response.content)

            if invalid == 'mask':
                vo_tree = votable.parse(tf, pedantic=False, invalid='mask')
            elif invalid == 'warn':
                try:
                    vo_tree = votable.parse(tf, pedantic=False, invalid='raise')
                except Exception as ex:
                    warnings.warn("VOTABLE parsing raised exception: {0}".format(ex))
                    vo_tree = votable.parse(tf, pedantic=False, invalid='mask')
            elif invalid == 'raise':
                vo_tree = votable.parse(tf, pedantic=False, invalid='raise')
            else:
                raise ValueError("Invalid keyword 'invalid'.  Must be raise, mask, or warn")

            if get_catalog_names:
                return dict([(R.name, R) for R in vo_tree.resources])
            else:
                table_dict = OrderedDict()
                for t in vo_tree.iter_tables():
                    if len(t.array) > 0:
                        if t.ref is not None:
                            name = vo_tree.get_table_by_id(t.ref).name
                        else:
                            name = t.name
                        if name not in table_dict.keys():
                            table_dict[name] = []
                        table_dict[name] += [t.to_table()]
                for name in table_dict.keys():
                    if len(table_dict[name]) > 1:
                        table_dict[name] = tbl.vstack(table_dict[name])
                    else:
                        table_dict[name] = table_dict[name][0]
                return commons.TableList(table_dict)

        except Exception as ex:
            self.response = response
            self.table_parse_error = ex
            raise TableParseError("Failed to parse VIZIER result! The raw response can be found "
                                  "in self.response, and the error in self.table_parse_error."
                                  "  The attempted parsed result is in self.parsed_result.\n"
                                  "Exception: " + str(self.table_parse_error))
開發者ID:AlexaVillaume,項目名稱:astroquery,代碼行數:70,代碼來源:core.py

示例5: read_table_fits

# 需要導入模塊: from astropy.utils import OrderedDict [as 別名]
# 或者: from astropy.utils.OrderedDict import keys [as 別名]
def read_table_fits(input, hdu=None):
    """
    Read a Table object from an FITS file

    Parameters
    ----------
    input : str or file-like object or compatible `astropy.io.fits` HDU object
        If a string, the filename to read the table from. If a file object, or
        a compatible HDU object, the object to extract the table from. The
        following `astropy.io.fits` HDU objects can be used as input:
        - :class:`~astropy.io.fits.hdu.table.TableHDU`
        - :class:`~astropy.io.fits.hdu.table.BinTableHDU`
        - :class:`~astropy.io.fits.hdu.table.GroupsHDU`
        - :class:`~astropy.io.fits.hdu.hdulist.HDUList`
    hdu : int or str, optional
        The HDU to read the table from.
    """

    if isinstance(input, basestring):
        input = fits_open(input)
        to_close = input
    else:
        to_close = None

    if hasattr(input, 'read'):
        input = fits_open(input)

    try:

        # Parse all table objects
        tables = OrderedDict()
        if isinstance(input, HDUList):
            for ihdu, hdu_item in enumerate(input):
                if isinstance(hdu_item, (TableHDU, BinTableHDU, GroupsHDU)):
                    tables[ihdu] = hdu_item

            if len(tables) > 1:

                if hdu is None:
                    warnings.warn("hdu= was not specified but multiple tables"
                                  " are present, reading in first available"
                                  " table (hdu={0})".format(tables.keys()[0]))
                    hdu = tables.keys()[0]

                # hdu might not be an integer, so we first need to convert it
                # to the correct HDU index
                hdu = input.index_of(hdu)

                if hdu in tables:
                    table = tables[hdu]
                else:
                    raise ValueError("No table found in hdu={0}".format(hdu))

            elif len(tables) == 1:
                table = tables[tables.keys()[0]]
            else:
                raise ValueError("No table found")

        elif isinstance(input, (TableHDU, BinTableHDU, GroupsHDU)):

            table = input

        else:

            raise ValueError("Input should be a string, a file-like object, "
                             "an  HDUList, TableHDU, BinTableHDU, or "
                             "GroupsHDU instance")

        # Check if table is masked
        masked = False
        for col in table.columns:
            if col.null is not None:
                masked = True
                break

        # Convert to an astropy.table.Table object
        t = Table(table.data, masked=masked)

        # Copy over null values if needed
        if masked:
            for col in table.columns:
                t[col.name].set_fill_value(col.null)
                t[col.name].mask[t[col.name] == col.null] = True

        # Copy over units
        for col in table.columns:
            if col.unit is not None:
                try:
                    t[col.name].units = u.Unit(col.unit, format='fits')
                except ValueError:
                    t[col.name].units = u.UnrecognizedUnit(col.unit)

        # TODO: deal properly with unsigned integers

        for key, value, comment in table.header.cards:

            if key in ['COMMENT', 'HISTORY']:
                if key in t.meta:
                    t.meta[key].append(value)
                else:
#.........這裏部分代碼省略.........
開發者ID:andyras,項目名稱:glue,代碼行數:103,代碼來源:fits_io.py

示例6: _parse_result

# 需要導入模塊: from astropy.utils import OrderedDict [as 別名]
# 或者: from astropy.utils.OrderedDict import keys [as 別名]
    def _parse_result(self, response, get_catalog_names=False, verbose=False):
        """
        Parses the HTTP response to create a `~astropy.table.Table`.

        Returns the raw result as a string in case of parse errors.

        Parameters
        ----------
        response : `requests.Response`
            The response of the HTTP POST request
        get_catalog_names : bool
            If specified, return only the table names (useful for table
            discovery)

        Returns
        -------
        table_list : `astroquery.utils.TableList` or str
            If there are errors in the parsing, then returns the raw results as a string.
        """
        if not verbose:
            commons.suppress_vo_warnings()
        try:
            tf = tempfile.NamedTemporaryFile()
            if six.PY3:
                # This is an exceedingly confusing section
                # It is likely to be doubly wrong, but has caused issue #185
                try:
                    # Case 1: data is read in as unicode
                    tf.write(response.content.encode())
                except AttributeError:
                    # Case 2: data is read in as a byte string
                    tf.write(response.content.decode().encode('utf-8'))
            else:
                tf.write(response.content.encode('utf-8'))
            tf.file.flush()
            vo_tree = votable.parse(tf, pedantic=False)
            if get_catalog_names:
                return dict([(R.name,R) for R in vo_tree.resources])
            else:
                table_dict = OrderedDict()
                for t in vo_tree.iter_tables():
                    if len(t.array) > 0:
                        if t.ref is not None:
                            name = vo_tree.get_table_by_id(t.ref).name
                        else:
                            name = t.name
                        if name not in table_dict.keys():
                            table_dict[name] = []
                        table_dict[name] += [t.to_table()]
                for name in table_dict.keys():
                    if len(table_dict[name]) > 1:
                        table_dict[name] = tbl.vstack(table_dict[name])
                    else:
                        table_dict[name] = table_dict[name][0]
                return commons.TableList(table_dict)

        except Exception as ex:
            self.response = response
            self.table_parse_error = ex
            raise TableParseError("Failed to parse VIZIER result! The raw response can be found "
                                  "in self.response, and the error in self.table_parse_error."
                                  "  The attempted parsed result is in self.parsed_result.\n"
                                  "Exception: " + str(self.table_parse_error))
開發者ID:demitri,項目名稱:astroquery,代碼行數:65,代碼來源:core.py


注:本文中的astropy.utils.OrderedDict.keys方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。