[segyio] 334/376: Accept any key-value iterable in header.update

Jørgen Kvalsvik jokva-guest at moszumanska.debian.org
Wed Sep 20 08:04:54 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 d713b7d74b2e7140f07f5f10d67de684953dbb82
Author: Jørgen Kvalsvik <jokva at statoil.com>
Date:   Fri Aug 11 15:13:55 2017 +0200

    Accept any key-value iterable in header.update
---
 python/segyio/_field.py | 15 ++++++++++-----
 python/test/segy.py     | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 59 insertions(+), 5 deletions(-)

diff --git a/python/segyio/_field.py b/python/segyio/_field.py
index 02ac76f..c9f83d5 100644
--- a/python/segyio/_field.py
+++ b/python/segyio/_field.py
@@ -63,12 +63,17 @@ class Field(object):
         self._write(self.buf, self.traceno)
 
     def update(self, value):
-        buf = self.buf
-        if isinstance(value, dict):
-            for k, v in value.items():
-                self._set_field(int(k), v)
-        else:
+        if type(self) is type(value):
             buf = value.buf
+        else:
+            buf = self.buf
+
+            # iter() on a dict only gives values, need key-value pairs
+            try: value = value.items()
+            except AttributeError: pass
+
+            for k, v in value:
+                self._set_field(int(k), v)
 
         self._write(buf, self.traceno)
 
diff --git a/python/test/segy.py b/python/test/segy.py
index 35b6a1e..f68fa32 100644
--- a/python/test/segy.py
+++ b/python/test/segy.py
@@ -534,6 +534,36 @@ class TestSegy(TestCase):
                 self.assertEqual(5, len(list(f.header[10:5:-1])))
                 self.assertEqual(0, len(list(f.header[10:5])))
 
+                d = { TraceField.INLINE_3D: 45,
+                      TraceField.CROSSLINE_3D: 10,
+                      TraceField.offset: 16 }
+
+                # assign multiple values using alternative syntax
+                f.header[5].update(d)
+                f.flush()
+                self.assertEqual(45, f.header[5][TraceField.INLINE_3D])
+                self.assertEqual(10, f.header[5][segyio.su.xline])
+                self.assertEqual(16, f.header[5][segyio.su.offset])
+
+                # accept anything with a key-value structure
+                f.header[5].update( [(segyio.su.ns, 12), (segyio.su.dt, 4)] )
+                f.header[5].update( ((segyio.su.muts, 3), (segyio.su.mute, 7)) )
+
+                with self.assertRaises(TypeError):
+                    f.header[0].update(10)
+
+                with self.assertRaises(TypeError):
+                    f.header[0].update(None)
+
+                with self.assertRaises(ValueError):
+                    f.header[0].update('foo')
+
+                f.flush()
+                self.assertEqual(12, f.header[5][segyio.su.ns])
+                self.assertEqual( 4, f.header[5][segyio.su.dt])
+                self.assertEqual( 3, f.header[5][segyio.su.muts])
+                self.assertEqual( 7, f.header[5][segyio.su.mute])
+
                 # for-each support
                 for th in f.header:
                     pass
@@ -575,6 +605,25 @@ class TestSegy(TestCase):
                 self.assertEqual(43, f.bin[segyio.su.ntrpr])
                 self.assertEqual(11, f.bin[segyio.su.hsfs])
 
+                d = { BinField.Traces: 45,
+                      BinField.SweepFrequencyStart: 10 }
+
+                # assign multiple values using alternative syntax
+                f.bin.update(d)
+                f.flush()
+                self.assertEqual(45, f.bin[segyio.su.ntrpr])
+                self.assertEqual(10, f.bin[segyio.su.hsfs])
+
+                # accept anything with a key-value structure
+                f.bin.update( [(segyio.su.jobid, 12), (segyio.su.lino, 4)] )
+                f.bin.update( ((segyio.su.reno, 3), (segyio.su.hdt, 7)) )
+
+                f.flush()
+                self.assertEqual(12, f.bin[segyio.su.jobid])
+                self.assertEqual( 4, f.bin[segyio.su.lino])
+                self.assertEqual( 3, f.bin[segyio.su.reno])
+                self.assertEqual( 7, f.bin[segyio.su.hdt])
+
                 # looking up multiple values at once returns a { TraceField: value } dict
                 self.assertEqual(d, f.bin[BinField.Traces, BinField.SweepFrequencyStart])
 

-- 
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