Skip to content

Commit 7f64a33

Browse files
author
Malcolm White
committed
Resolve Python 2/3 inconsitencies causing tests to fail.
Resolve Python 2/3 inconsitencies causing tests to fail. Merge remote-tracking branch 'origin/dev/waveform_references' into dev/waveform_references
1 parent 8d806c5 commit 7f64a33

File tree

1 file changed

+48
-73
lines changed

1 file changed

+48
-73
lines changed

pyasdf/asdf_data_set.py

Lines changed: 48 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,40 +1325,28 @@ def create_reference(self, ref, starttime, endtime, net=None, sta=None,
13251325
etc...
13261326
"""
13271327

1328-
if isinstance(net, str) or isinstance(net, unicode):
1329-
net = (net,)
1330-
elif isinstance(net, tuple) or isinstance(net, list) or net is None:
1331-
pass
1332-
else:
1333-
raise(TypeError(net))
1334-
1335-
if isinstance(sta, str) or isinstance(sta, unicode):
1336-
sta = (sta,)
1337-
elif isinstance(sta, tuple) or isinstance(sta, list) or sta is None:
1338-
pass
1339-
else:
1340-
raise(TypeError(sta))
1341-
1342-
if isinstance(loc, str) or isinstance(loc, unicode):
1343-
loc = (loc,)
1344-
elif isinstance(loc, tuple) or isinstance(loc, list) or loc is None:
1345-
pass
1346-
else:
1347-
raise(TypeError(loc))
1328+
def _coerce(obj):
1329+
if isinstance(obj, str):
1330+
obj = (obj,)
1331+
elif isinstance(obj, tuple)\
1332+
or isinstance(obj, list)\
1333+
or obj is None:
1334+
pass
1335+
else:
1336+
try:
1337+
if isinstance(obj, unicode):
1338+
obj = (obj,)
1339+
else:
1340+
raise(TypeError(obj))
1341+
except NameError:
1342+
pass
1343+
return(obj)
13481344

1349-
if isinstance(chan, str) or isinstance(chan, unicode):
1350-
chan = (chan,)
1351-
elif isinstance(chan, tuple) or isinstance(chan, list) or chan is None:
1352-
pass
1353-
else:
1354-
raise(TypeError(chan))
1355-
1356-
if isinstance(tag, str) or isinstance(tag, unicode):
1357-
tag = (tag,)
1358-
elif isinstance(tag, tuple) or isinstance(tag, list) or tag is None:
1359-
pass
1360-
else:
1361-
raise(TypeError(tag))
1345+
net = _coerce(net)
1346+
sta = _coerce(sta)
1347+
loc = _coerce(loc)
1348+
chan = _coerce(chan)
1349+
tag = _coerce(tag)
13621350

13631351
_ref_dtype = h5py.special_dtype(ref=h5py.RegionReference)
13641352

@@ -1386,10 +1374,10 @@ def _predicate_locchantag(_key):
13861374
and _predicate_tag(_key))
13871375

13881376
_wf_grp = self._waveform_group
1389-
for _station_name in itertools.ifilter(_predicate_netsta,
1390-
self._waveform_group.keys()):
1391-
for _key in itertools.ifilter(_predicate_locchantag,
1392-
_wf_grp[_station_name].keys()):
1377+
for _station_name in filter(_predicate_netsta,
1378+
self._waveform_group.keys()):
1379+
for _key in filter(_predicate_locchantag,
1380+
_wf_grp[_station_name].keys()):
13931381

13941382
_net, _sta, _loc, _remainder = _key.split(".")
13951383
_chan = _remainder.split("__")[0]
@@ -1494,38 +1482,29 @@ def get_data_for_reference(self, ref, net=None, sta=None, loc=None,
14941482
14951483
etc...
14961484
"""
1497-
if not isinstance(ref, str) and not isinstance(ref, unicode):
1498-
raise(TypeError("reference must be type ::str::"))
14991485
if ref not in self._reference_group:
15001486
raise(IOError("reference does not exist: %s" % ref))
15011487

1502-
if isinstance(net, str) or isinstance(net, unicode):
1503-
net = (net,)
1504-
elif isinstance(net, tuple) or isinstance(net, list) or net is None:
1505-
pass
1506-
else:
1507-
raise(TypeError(net))
1508-
1509-
if isinstance(sta, str) or isinstance(sta, unicode):
1510-
sta = (sta,)
1511-
elif isinstance(sta, tuple) or isinstance(sta, list) or sta is None:
1512-
pass
1513-
else:
1514-
raise(TypeError(sta))
1515-
1516-
if isinstance(loc, str) or isinstance(loc, unicode):
1517-
loc = (loc,)
1518-
elif isinstance(loc, tuple) or isinstance(loc, list) or loc is None:
1519-
pass
1520-
else:
1521-
raise(TypeError(loc))
1488+
def _coerce(obj):
1489+
if isinstance(obj, str):
1490+
obj = (obj,)
1491+
elif isinstance(obj, tuple)\
1492+
or isinstance(obj, list)\
1493+
or obj is None:
1494+
pass
1495+
else:
1496+
try:
1497+
if isinstance(obj, unicode):
1498+
obj = (obj,)
1499+
else:
1500+
raise(TypeError(obj))
1501+
except NameError:
1502+
pass
15221503

1523-
if isinstance(chan, str) or isinstance(chan, unicode):
1524-
chan = (chan,)
1525-
elif isinstance(chan, tuple) or isinstance(chan, list) or chan is None:
1526-
pass
1527-
else:
1528-
raise(TypeError(chan))
1504+
net = _coerce(net)
1505+
sta = _coerce(sta)
1506+
loc = _coerce(loc)
1507+
chan = _coerce(chan)
15291508

15301509
def _predicate_net(_key):
15311510
return(net is None or _key in net)
@@ -1541,20 +1520,16 @@ def _predicate_chan(_key):
15411520

15421521
_st = obspy.Stream()
15431522
_ref_grp = self._reference_group[ref]
1544-
for _net in itertools.ifilter(_predicate_net,
1545-
_ref_grp.keys()):
1523+
for _net in filter(_predicate_net, _ref_grp.keys()):
15461524
_net_grp = _ref_grp[_net]
15471525

1548-
for _sta in itertools.ifilter(_predicate_sta,
1549-
_net_grp.keys()):
1526+
for _sta in filter(_predicate_sta, _net_grp.keys()):
15501527
_sta_grp = _net_grp[_sta]
15511528

1552-
for _loc in itertools.ifilter(_predicate_loc,
1553-
_sta_grp.keys()):
1529+
for _loc in filter(_predicate_loc, _sta_grp.keys()):
15541530
_loc_grp = _sta_grp[_loc]
15551531

1556-
for _chan in itertools.ifilter(_predicate_chan,
1557-
_loc_grp.keys()):
1532+
for _chan in filter(_predicate_chan, _loc_grp.keys()):
15581533
_ds = _loc_grp[_chan]
15591534
_ref = _ds[0]
15601535
_tr = obspy.Trace(data=self.__file[_ref][_ref])

0 commit comments

Comments
 (0)