]> git.jsancho.org Git - datasette-connectors.git/commitdiff
Include Row class for returning data from connectors
authorJavier Sancho <jsf@jsancho.org>
Mon, 8 Oct 2018 10:53:33 +0000 (12:53 +0200)
committerJavier Sancho <jsf@jsancho.org>
Mon, 8 Oct 2018 10:53:33 +0000 (12:53 +0200)
datasette_connectors/row.py [new file with mode: 0644]

diff --git a/datasette_connectors/row.py b/datasette_connectors/row.py
new file mode 100644 (file)
index 0000000..bf4e2cc
--- /dev/null
@@ -0,0 +1,26 @@
+class Row(list):
+    def __init__(self, values=None):
+        self.labels = []
+        self.values = []
+        if values:
+            for idx in values:
+                self.__setitem__(idx, values[idx])
+
+    def __setitem__(self, idx, value):
+        if type(idx) is str:
+            if idx in self.labels:
+                self.values[self.labels.index(idx)] = value
+            else:
+                self.labels.append(idx)
+                self.values.append(value)
+        else:
+            self.values[idx] = value
+
+    def __getitem__(self, idx):
+        if type(idx) is str:
+            return self.values[self.labels.index(idx)]
+        else:
+            return self.values[idx]
+
+    def __iter__(self):
+        return self.values.__iter__()