Comments (4)
@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.
👍
from jwst.
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.
👍 👍 hey, anything that runs it the same under all pythons. +1 on the no more check on pythons.
from jwst.
Related Issues (20)
- Error in ApCorr propagation to variances HOT 2
- Background error propagation in non-IFU background subtraction HOT 2
- Add group_id to Level 3 associations HOT 2
- Investigate refpix differences across systems
- Investigate coron3 differences across systems
- Nod Flux oversubtraction correction for NIRSpec Fixed-Slit and MOS point-source spectra
- Move NIRSpec FS point source position assignment out of wavecorr HOT 2
- Spec3 step mrs_imatch crashing HOT 1
- extract_1d SOSS ATOCA failures on saturated data HOT 3
- Remove drizpars handling from resample steps HOT 3
- Flux differences in stage 3 products when combining multiple grating/filters HOT 1
- Improve runtime of NSClean step HOT 4
- Modify NSClean behaviour for subarray data HOT 3
- Ensure NaN values are excluded from sum in spectral extraction
- Remove "R_DRZPAR" definition from datamodels schema HOT 1
- Improve NIRSpec spec2 runtime for BOTS/MOS HOT 15
- Update the JWST pipeline to accommodate the new likelihood algorithm in ramp fitting
- Error in NIRSpec spectral WCS with all-NaN input to resample HOT 1
- Bug in extract1d._coalesce_bounds HOT 1
- extract1d._coalesce_bounds can return unexpected results HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jwst.