[segyio] 119/376: Line.__getitem__ split to _get and _get_iter

Jørgen Kvalsvik jokva-guest at moszumanska.debian.org
Wed Sep 20 08:04:18 UTC 2017


This is an automated email from the git hooks/post-receive script.

jokva-guest pushed a commit to branch debian
in repository segyio.

commit e677d1b4af768e45b36ceb1357ff3588301bc389
Author: Jørgen Kvalsvik <jokva at statoil.com>
Date:   Thu Nov 10 16:29:31 2016 +0100

    Line.__getitem__ split to _get and _get_iter
    
    The __getitem__ for line method has been split up in two parts, _get and
    _get_iter, each handling the distinct cases where a generator or a
    regular value is expected.
---
 python/segyio/_line.py | 62 ++++++++++++++++++++++++++------------------------
 tests/test_segy.py     |  4 +---
 2 files changed, 33 insertions(+), 33 deletions(-)

diff --git a/python/segyio/_line.py b/python/segyio/_line.py
index 902b9ee..7c02094 100644
--- a/python/segyio/_line.py
+++ b/python/segyio/_line.py
@@ -29,41 +29,43 @@ class Line:
         self.readfn = readfn
         self.writefn = writefn
 
-    def __getitem__(self, lineno, buf=None):
-        """ :rtype: numpy.ndarray|collections.Iterable[numpy.ndarray]"""
-        if isinstance(lineno, tuple):
-            return self.__getitem__(lineno[0], lineno[1])
+    def _get(self, lineno, buf):
+        """ :rtype: numpy.ndarray"""
+        try:
+            lineno = int(lineno)
+        except TypeError:
+            raise TypeError("Must be int or slice")
+        else:
+            t0 = self.trace0fn(lineno)
+            return self.readfn(t0, self.len, self.stride, buf)
 
-        buf = self.buffn(buf)
+    def _get_iter(self, lineno, buf):
+        """ :rtype: collections.Iterable[numpy.ndarray]"""
 
-        if isinstance(lineno, slice):
-            # in order to support [:end] syntax, we must make sure
-            # start has a non-None value. lineno.indices() would set it
-            # to 0, but we don't know if that's a reasonable value or
-            # not. If start is None we set it to the first line
-            if lineno.start is None:
-                lineno = slice(self.lines[0], lineno.stop, lineno.step)
+        # in order to support [:end] syntax, we must make sure
+        # start has a non-None value. lineno.indices() would set it
+        # to 0, but we don't know if that's a reasonable value or
+        # not. If start is None we set it to the first line
+        if lineno.start is None:
+            lineno = slice(self.lines[0], lineno.stop, lineno.step)
+
+        def gen():
+            s = set(self.lines)
+            rng = xrange(*lineno.indices(self.lines[-1] + 1))
+
+            for i in itertools.ifilter(s.__contains__, rng):
+                yield self._get(i, buf)
 
-            def gen():
-                s = set(self.lines)
-                rng = range(*lineno.indices(self.lines[-1] + 1))
+        return gen()
 
-                # use __getitem__ lookup to avoid tuple
-                # construction and unpacking and fast-forward
-                # into the interesting code path
-                for i in itertools.ifilter(s.__contains__, rng):
-                    yield self.__getitem__(i, buf)
+    def __getitem__(self, lineno):
+        """ :rtype: numpy.ndarray|collections.Iterable[numpy.ndarray]"""
+        buf = self.buffn()
 
-            return gen()
+        if isinstance(lineno, slice):
+            return self._get_iter(lineno, buf)
 
-        else:
-            try:
-                lineno = int(lineno)
-            except TypeError:
-                raise TypeError("Must be int or slice")
-            else:
-                t0 = self.trace0fn(lineno, len(self.segy.offsets))
-                return self.readfn(t0, self.len, self.stride, buf)
+        return self._get(lineno, buf)
 
     def trace0fn(self, lineno, offsets):
         return segyio._segyio.fread_trace0(lineno, len(self.other_lines), self.stride, offsets, self.lines, self.name)
@@ -90,4 +92,4 @@ class Line:
     def __iter__(self):
         buf = self.buffn()
         for i in self.lines:
-            yield self.__getitem__(i, buf)
+            yield self._get(i, buf)
diff --git a/tests/test_segy.py b/tests/test_segy.py
index 11a3208..729e834 100644
--- a/tests/test_segy.py
+++ b/tests/test_segy.py
@@ -425,10 +425,8 @@ class TestSegy(TestCase):
                 # alternative form using left-hand-side slices
                 dst.iline[2:4] = src.iline
 
-
-                buf = None # reuse buffer for speed
                 for lineno in dst.xlines:
-                    dst.xline[lineno] = src.xline[lineno, buf]
+                    dst.xline[lineno] = src.xline[lineno]
 
             with segyio.open(dstfile, "r") as dst:
                 self.assertEqual(20, dst.samples)

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/segyio.git



More information about the debian-science-commits mailing list