Giter Club home page Giter Club logo

Comments (4)

brechmos-stsci avatar brechmos-stsci commented on July 18, 2024

@stscieisenhamer , would something like this work:

    if six.PY3:
        items = iteritems
    else:
        def items(self):
            """
            Get all of the schema items in a flat way.

            Each element is a pair (`key`, `value`).  Each `key` is a
            dot-separated name.  For example, the schema element
            `meta.observation.date` will end up in the result as::

                ("meta.observation.date": "2012-04-22T03:22:05.432")
            """
            def recurse(tree, path=[]):
                if isinstance(tree, dict):
                    for key, val in six.iteritems(tree):
                        for x in recurse(val, path + [key]):
                            yield x
                elif isinstance(tree, (list, tuple)):
                    for i, val in enumerate(tree):
                        for x in recurse(val, path + [i]):
                            yield x
                elif tree is not None:
                    yield (str('.'.join(six.text_type(x) for x in path)), tree)

            for x in recurse(self._instance):
                yield x

In Python 2.7:

Python 2.7.12 |Continuum Analytics, Inc.| (default, Jul  2 2016, 17:43:17)
Type "copyright", "credits" or "license" for more information.

IPython 5.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: from jwst.datamodels import MultiSlitModel

In [2]: m = MultiSlitModel('/grp/jwst//ssb/bushouse/jwst_data/NIRSpec/FS/jwst_nod2_cal.fits')

In [3]: a = m.items()

In [4]: [x for x in a if 'meta.target' in x[0]]
Out[4]:
[('meta.target.dec', 0.0),
 ('meta.target.dec_uncertainty', 0.0),
 ('meta.target.ra_uncertainty', 0.0),
 ('meta.target.proposer_epoch', u'2000.0'),
 ('meta.target.catalog_name', 'NGC 104'),
 ('meta.target.proper_motion_epoch', '2000.0'),
 ('meta.target.proposer_ra', 0.0),
 ('meta.target.proper_motion_dec', 0.0),
 ('meta.target.ra', 0.0),
 ('meta.target.proposer_name', 'UNKNOWN'),
 ('meta.target.type', 'FIXED'),
 ('meta.target.proposer_dec', 0.0),
 ('meta.target.proper_motion_ra', 0.0)]

In Python 3:

crjones@creag 03:19:32 ~ →  ipython
Python 3.5.1 |Continuum Analytics, Inc.| (default, Jun 15 2016, 16:14:02)
Type "copyright", "credits" or "license" for more information.

IPython 5.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [2]: from jwst.datamodels import MultiSlitModel

In [3]: m = MultiSlitModel('/grp/jwst//ssb/bushouse/jwst_data/NIRSpec/FS/jwst_nod2_cal.fits')
a = m.ite
In [4]: a = m.items()

In [5]: [x for x in a if 'meta.target' in x[0]]
Out[5]:
[('meta.target.ra_uncertainty', 0.0),
 ('meta.target.ra', 0.0),
 ('meta.target.catalog_name', 'NGC 104'),
 ('meta.target.proposer_ra', 0.0),
 ('meta.target.dec', 0.0),
 ('meta.target.proper_motion_dec', 0.0),
 ('meta.target.type', 'FIXED'),
 ('meta.target.proposer_name', 'UNKNOWN'),
 ('meta.target.proposer_dec', 0.0),
 ('meta.target.proper_motion_ra', 0.0),
 ('meta.target.proposer_epoch', '2000.0'),
 ('meta.target.dec_uncertainty', 0.0),
 ('meta.target.proper_motion_epoch', '2000.0')]

It is just a small hack on the iteritems from above... If that makes you happy I can create a PR.

(though there is still a string/unicode thing going on...)

from jwst.

stscieisenhamer avatar stscieisenhamer commented on July 18, 2024

👍

from jwst.

brechmos-stsci avatar brechmos-stsci commented on July 18, 2024

Umm... hmmm... Other than the str() call to make the first element of the tuple a string (rather than unicode) it is the same as the iteritems(self) definition above.

Ok. Scrap the above... I can just do:

    def iteritems(self):
        """
        Iterates over all of the schema items in a flat way.

        Each element is a pair (`key`, `value`).  Each `key` is a
        dot-separated name.  For example, the schema element
        `meta.observation.date` will end up in the result as::

            ("meta.observation.date": "2012-04-22T03:22:05.432")
        """
        def recurse(tree, path=[]):
            if isinstance(tree, dict):
                for key, val in six.iteritems(tree):
                    for x in recurse(val, path + [key]):
                        yield x
            elif isinstance(tree, (list, tuple)):
                for i, val in enumerate(tree):
                    for x in recurse(val, path + [i]):
                        yield x
            elif tree is not None:
                yield (str('.'.join(six.text_type(x) for x in path)), tree)

        for x in recurse(self._instance):
            yield x

    items = iteritems

So, there is no more check for PY3 and then defining items differently. And I added an str() in the iteritems definition.

If that still makes you happy, I'll PR it.

from jwst.

stscieisenhamer avatar stscieisenhamer commented on July 18, 2024

👍 👍 hey, anything that runs it the same under all pythons. +1 on the no more check on pythons.

from jwst.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.