From 0356f10785851b98266e9b81a70cc143f1389630 Mon Sep 17 00:00:00 2001 From: Josef Barta Date: Tue, 14 Nov 2017 13:12:50 +0000 Subject: [PATCH 01/15] added methods for getting keys, items and dict This change enables to retrieve the row as a dict and iterate the keys and/or items, like with a normal dict; in other words, making the Row object a dict-like object: >>> row.dict() {'name': 'Isabel', 'profession': 'bridge builder'} >>> for k, v in row.items(): >>> for k in row.keys(): --- bigquery/google/cloud/bigquery/table.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/bigquery/google/cloud/bigquery/table.py b/bigquery/google/cloud/bigquery/table.py index 191eba2f20ce..a1e544776702 100644 --- a/bigquery/google/cloud/bigquery/table.py +++ b/bigquery/google/cloud/bigquery/table.py @@ -785,6 +785,15 @@ def __init__(self, values, field_to_index): def values(self): return self._xxx_values + def keys(self): + return self._xxx_field_to_index.keys() + + def items(self): + return [(k, self._xxx_values[i]) for k, i in self._xxx_field_to_index.items()] + + def dict(self): + return {k: self._xxx_values[i] for k, i in self._xxx_field_to_index.items()} + def __getattr__(self, name): value = self._xxx_field_to_index.get(name) if value is None: From 91d007dc13cc7e33fe2d183393fb7288a4ba1ac8 Mon Sep 17 00:00:00 2001 From: Josef Barta Date: Tue, 14 Nov 2017 13:27:07 +0000 Subject: [PATCH 02/15] fixed row length --- bigquery/google/cloud/bigquery/table.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/bigquery/google/cloud/bigquery/table.py b/bigquery/google/cloud/bigquery/table.py index a1e544776702..2cd3e99360e8 100644 --- a/bigquery/google/cloud/bigquery/table.py +++ b/bigquery/google/cloud/bigquery/table.py @@ -789,10 +789,18 @@ def keys(self): return self._xxx_field_to_index.keys() def items(self): - return [(k, self._xxx_values[i]) for k, i in self._xxx_field_to_index.items()] + return [ + (k, self._xxx_values[i]) + for k, i + in self._xxx_field_to_index.items() + ] def dict(self): - return {k: self._xxx_values[i] for k, i in self._xxx_field_to_index.items()} + return { + k: self._xxx_values[i] + for k, i + in self._xxx_field_to_index.items() + } def __getattr__(self, name): value = self._xxx_field_to_index.get(name) From deebcd1b10bd2575a072d4c69a84c416c9e1cff3 Mon Sep 17 00:00:00 2001 From: Josef Barta Date: Tue, 14 Nov 2017 13:30:48 +0000 Subject: [PATCH 03/15] removed whitespace from blank lines --- bigquery/google/cloud/bigquery/table.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bigquery/google/cloud/bigquery/table.py b/bigquery/google/cloud/bigquery/table.py index 2cd3e99360e8..b3704ab626ec 100644 --- a/bigquery/google/cloud/bigquery/table.py +++ b/bigquery/google/cloud/bigquery/table.py @@ -787,21 +787,21 @@ def values(self): def keys(self): return self._xxx_field_to_index.keys() - + def items(self): return [ (k, self._xxx_values[i]) for k, i in self._xxx_field_to_index.items() ] - + def dict(self): return { k: self._xxx_values[i] for k, i in self._xxx_field_to_index.items() } - + def __getattr__(self, name): value = self._xxx_field_to_index.get(name) if value is None: From 84964530a728b2a568b0e08ebdc243ed5864dc9b Mon Sep 17 00:00:00 2001 From: Josef Barta Date: Tue, 14 Nov 2017 13:37:40 +0000 Subject: [PATCH 04/15] removed trailing whitespace --- bigquery/google/cloud/bigquery/table.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bigquery/google/cloud/bigquery/table.py b/bigquery/google/cloud/bigquery/table.py index b3704ab626ec..41e337ed2197 100644 --- a/bigquery/google/cloud/bigquery/table.py +++ b/bigquery/google/cloud/bigquery/table.py @@ -790,15 +790,15 @@ def keys(self): def items(self): return [ - (k, self._xxx_values[i]) - for k, i + (k, self._xxx_values[i]) + for k, i in self._xxx_field_to_index.items() ] def dict(self): return { - k: self._xxx_values[i] - for k, i + k: self._xxx_values[i] + for k, i in self._xxx_field_to_index.items() } From f9b926b1d4070fae1acb74ac3fd1f0e985e58c5b Mon Sep 17 00:00:00 2001 From: Josef Barta Date: Tue, 14 Nov 2017 13:44:22 +0000 Subject: [PATCH 05/15] responding to failed cover check --- bigquery/google/cloud/bigquery/table.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/bigquery/google/cloud/bigquery/table.py b/bigquery/google/cloud/bigquery/table.py index 41e337ed2197..5e72a8f5110f 100644 --- a/bigquery/google/cloud/bigquery/table.py +++ b/bigquery/google/cloud/bigquery/table.py @@ -786,21 +786,24 @@ def values(self): return self._xxx_values def keys(self): - return self._xxx_field_to_index.keys() + keys = self._xxx_field_to_index.keys() + return keys def items(self): - return [ + items = [ (k, self._xxx_values[i]) for k, i in self._xxx_field_to_index.items() ] + return items def dict(self): - return { + data = { k: self._xxx_values[i] for k, i in self._xxx_field_to_index.items() } + return data def __getattr__(self, name): value = self._xxx_field_to_index.get(name) From b90326b09457e442e743f502feaa2b27bd980b5b Mon Sep 17 00:00:00 2001 From: Josef Barta Date: Tue, 14 Nov 2017 13:54:08 +0000 Subject: [PATCH 06/15] added docstrings --- bigquery/google/cloud/bigquery/table.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/bigquery/google/cloud/bigquery/table.py b/bigquery/google/cloud/bigquery/table.py index 5e72a8f5110f..a20ecd8bcc98 100644 --- a/bigquery/google/cloud/bigquery/table.py +++ b/bigquery/google/cloud/bigquery/table.py @@ -786,10 +786,20 @@ def values(self): return self._xxx_values def keys(self): + """ + Return keys as of a dict: + >>> Row(('a', 'b'), {'x': 0, 'y': 1}).keys() + ['x', 'y'] + """ keys = self._xxx_field_to_index.keys() return keys def items(self): + """ + Return items as of a dict: + >>> Row(('a', 'b'), {'x': 0, 'y': 1}).items() + [('x', 'a'), ('y', 'b')] + """ items = [ (k, self._xxx_values[i]) for k, i @@ -798,6 +808,11 @@ def items(self): return items def dict(self): + """ + Return row as a dict object: + >>> Row(('a', 'b'), {'x': 0, 'y': 1}).dict() + {'x': 'a', 'y': 'b'} + """ data = { k: self._xxx_values[i] for k, i From c07e0b12febe3acac35b6f3207e630f1c382f86c Mon Sep 17 00:00:00 2001 From: Josef Barta Date: Tue, 14 Nov 2017 14:38:48 +0000 Subject: [PATCH 07/15] update unit tests --- bigquery/tests/unit/test_table.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bigquery/tests/unit/test_table.py b/bigquery/tests/unit/test_table.py index 888974c3cdaf..eec8882d687c 100644 --- a/bigquery/tests/unit/test_table.py +++ b/bigquery/tests/unit/test_table.py @@ -765,6 +765,9 @@ def test_row(self): self.assertEqual(row['c'], 3) self.assertEqual(len(row), 3) self.assertEqual(row.values(), VALUES) + self.assertEqual(row.keys(), ['a', 'b', 'c']) + self.assertEqual(row.items(), [('a', 1), ('b', 2), ('c', 3)]) + self.assertEqual(row.dict(), {'a': 1, 'b': 2, 'c': 3}) self.assertEqual(repr(row), "Row((1, 2, 3), {'a': 0, 'b': 1, 'c': 2})") self.assertFalse(row != row) From 44b66107e9468d96eb98263f1cf2bad0c815862c Mon Sep 17 00:00:00 2001 From: Josef Barta Date: Tue, 14 Nov 2017 14:51:15 +0000 Subject: [PATCH 08/15] Update test_table.py --- bigquery/tests/unit/test_table.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bigquery/tests/unit/test_table.py b/bigquery/tests/unit/test_table.py index eec8882d687c..a60ac11a3ac0 100644 --- a/bigquery/tests/unit/test_table.py +++ b/bigquery/tests/unit/test_table.py @@ -765,8 +765,8 @@ def test_row(self): self.assertEqual(row['c'], 3) self.assertEqual(len(row), 3) self.assertEqual(row.values(), VALUES) - self.assertEqual(row.keys(), ['a', 'b', 'c']) - self.assertEqual(row.items(), [('a', 1), ('b', 2), ('c', 3)]) + self.assertEqual(set(row.keys()), set(['a', 'b', 'c'])) + self.assertEqual(set(row.items()), set([('a', 1), ('b', 2), ('c', 3)])) self.assertEqual(row.dict(), {'a': 1, 'b': 2, 'c': 3}) self.assertEqual(repr(row), "Row((1, 2, 3), {'a': 0, 'b': 1, 'c': 2})") From 53145bfac7fc8c9fe9a2f128282bb661315c5430 Mon Sep 17 00:00:00 2001 From: Josef Barta Date: Tue, 14 Nov 2017 15:18:53 +0000 Subject: [PATCH 09/15] .keys() and .items() into generators --- bigquery/google/cloud/bigquery/table.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/bigquery/google/cloud/bigquery/table.py b/bigquery/google/cloud/bigquery/table.py index a20ecd8bcc98..efe474d0264f 100644 --- a/bigquery/google/cloud/bigquery/table.py +++ b/bigquery/google/cloud/bigquery/table.py @@ -791,8 +791,8 @@ def keys(self): >>> Row(('a', 'b'), {'x': 0, 'y': 1}).keys() ['x', 'y'] """ - keys = self._xxx_field_to_index.keys() - return keys + for k in self._xxx_field_to_index.keys(): + yield k def items(self): """ @@ -800,12 +800,8 @@ def items(self): >>> Row(('a', 'b'), {'x': 0, 'y': 1}).items() [('x', 'a'), ('y', 'b')] """ - items = [ - (k, self._xxx_values[i]) - for k, i - in self._xxx_field_to_index.items() - ] - return items + for k, i in self._xxx_field_to_index.items(): + yield k, self._xxx_values[i] def dict(self): """ From 1f85f07970a6db6ebd2dea2bd70abafdbe566637 Mon Sep 17 00:00:00 2001 From: Josef Barta Date: Fri, 17 Nov 2017 11:15:18 +0000 Subject: [PATCH 10/15] get method for Row --- bigquery/google/cloud/bigquery/table.py | 40 +++++++++++++++---------- bigquery/tests/unit/test_table.py | 9 ++++-- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/bigquery/google/cloud/bigquery/table.py b/bigquery/google/cloud/bigquery/table.py index efe474d0264f..1ce86c2c76a1 100644 --- a/bigquery/google/cloud/bigquery/table.py +++ b/bigquery/google/cloud/bigquery/table.py @@ -791,8 +791,8 @@ def keys(self): >>> Row(('a', 'b'), {'x': 0, 'y': 1}).keys() ['x', 'y'] """ - for k in self._xxx_field_to_index.keys(): - yield k + keys = self._xxx_field_to_index.keys() + return keys def items(self): """ @@ -800,21 +800,31 @@ def items(self): >>> Row(('a', 'b'), {'x': 0, 'y': 1}).items() [('x', 'a'), ('y', 'b')] """ - for k, i in self._xxx_field_to_index.items(): - yield k, self._xxx_values[i] - - def dict(self): - """ - Return row as a dict object: - >>> Row(('a', 'b'), {'x': 0, 'y': 1}).dict() - {'x': 'a', 'y': 'b'} - """ - data = { - k: self._xxx_values[i] + items = [ + (k, self._xxx_values[i]) for k, i in self._xxx_field_to_index.items() - } - return data + ] + return items + + def get(self, key, default = None): + """ + Return value under specified key + Defaults to None or specified default + if key does not exist: + >>> Row(('a', 'b'), {'x': 0, 'y': 1}).get('x') + 'a' + >>> Row(('a', 'b'), {'x': 0, 'y': 1}).get('z') + None + >>> Row(('a', 'b'), {'x': 0, 'y': 1}).get('z', '') + '' + >>> Row(('a', 'b'), {'x': 0, 'y': 1}).get('z', default = '') + '' + """ + index = self._xxx_field_to_index.get(key) + if index is None: + return default + return values[index] def __getattr__(self, name): value = self._xxx_field_to_index.get(name) diff --git a/bigquery/tests/unit/test_table.py b/bigquery/tests/unit/test_table.py index a60ac11a3ac0..a6731c0709c1 100644 --- a/bigquery/tests/unit/test_table.py +++ b/bigquery/tests/unit/test_table.py @@ -765,9 +765,12 @@ def test_row(self): self.assertEqual(row['c'], 3) self.assertEqual(len(row), 3) self.assertEqual(row.values(), VALUES) - self.assertEqual(set(row.keys()), set(['a', 'b', 'c'])) - self.assertEqual(set(row.items()), set([('a', 1), ('b', 2), ('c', 3)])) - self.assertEqual(row.dict(), {'a': 1, 'b': 2, 'c': 3}) + self.assertEqual(row.keys(), {'a': 1, 'b': 2, 'c': 3}.keys()) + self.assertEqual(row.items(), {'a': 1, 'b': 2, 'c': 3}.items()) + self.assertEqual(row.get('a'), 1) + self.assertEqual(row.get('d'), None) + self.assertEqual(row.get('d', ''), '') + self.assertEqual(row.get('d', default = ''), '') self.assertEqual(repr(row), "Row((1, 2, 3), {'a': 0, 'b': 1, 'c': 2})") self.assertFalse(row != row) From 20692e43d2ddae5c756c92fe87465b76b4740f41 Mon Sep 17 00:00:00 2001 From: Josef Barta Date: Fri, 17 Nov 2017 11:21:03 +0000 Subject: [PATCH 11/15] get method for Row --- bigquery/tests/unit/test_table.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bigquery/tests/unit/test_table.py b/bigquery/tests/unit/test_table.py index a6731c0709c1..1547e467a99e 100644 --- a/bigquery/tests/unit/test_table.py +++ b/bigquery/tests/unit/test_table.py @@ -765,8 +765,8 @@ def test_row(self): self.assertEqual(row['c'], 3) self.assertEqual(len(row), 3) self.assertEqual(row.values(), VALUES) - self.assertEqual(row.keys(), {'a': 1, 'b': 2, 'c': 3}.keys()) - self.assertEqual(row.items(), {'a': 1, 'b': 2, 'c': 3}.items()) + self.assertEqual(set(row.keys()), set({'a': 1, 'b': 2, 'c': 3}.keys())) + self.assertEqual(set(row.items()), set({'a': 1, 'b': 2, 'c': 3}.items())) self.assertEqual(row.get('a'), 1) self.assertEqual(row.get('d'), None) self.assertEqual(row.get('d', ''), '') From ba9d772c2ec53151f8f17964791d99e4c05791f7 Mon Sep 17 00:00:00 2001 From: Josef Barta Date: Fri, 17 Nov 2017 11:30:37 +0000 Subject: [PATCH 12/15] get method for Row --- bigquery/google/cloud/bigquery/table.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bigquery/google/cloud/bigquery/table.py b/bigquery/google/cloud/bigquery/table.py index 1ce86c2c76a1..278261964900 100644 --- a/bigquery/google/cloud/bigquery/table.py +++ b/bigquery/google/cloud/bigquery/table.py @@ -824,7 +824,7 @@ def get(self, key, default = None): index = self._xxx_field_to_index.get(key) if index is None: return default - return values[index] + return self._xxx_values[index] def __getattr__(self, name): value = self._xxx_field_to_index.get(name) From 06775da717857a81cade7f6f48659f47acc6718c Mon Sep 17 00:00:00 2001 From: Josef Barta Date: Fri, 17 Nov 2017 11:39:49 +0000 Subject: [PATCH 13/15] Update table.py --- bigquery/google/cloud/bigquery/table.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bigquery/google/cloud/bigquery/table.py b/bigquery/google/cloud/bigquery/table.py index 278261964900..ce68e18ef735 100644 --- a/bigquery/google/cloud/bigquery/table.py +++ b/bigquery/google/cloud/bigquery/table.py @@ -807,7 +807,7 @@ def items(self): ] return items - def get(self, key, default = None): + def get(self, key, default=None): """ Return value under specified key Defaults to None or specified default From 47e2d9323ce6369f09ac0144de52b83d510b5aca Mon Sep 17 00:00:00 2001 From: Josef Barta Date: Fri, 17 Nov 2017 11:50:16 +0000 Subject: [PATCH 14/15] Update test_table.py --- bigquery/tests/unit/test_table.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bigquery/tests/unit/test_table.py b/bigquery/tests/unit/test_table.py index 1547e467a99e..2d4df4625fb0 100644 --- a/bigquery/tests/unit/test_table.py +++ b/bigquery/tests/unit/test_table.py @@ -769,8 +769,8 @@ def test_row(self): self.assertEqual(set(row.items()), set({'a': 1, 'b': 2, 'c': 3}.items())) self.assertEqual(row.get('a'), 1) self.assertEqual(row.get('d'), None) - self.assertEqual(row.get('d', ''), '') - self.assertEqual(row.get('d', default = ''), '') + self.assertEqual(row.get('d', ''), '') + self.assertEqual(row.get('d', default=''), '') self.assertEqual(repr(row), "Row((1, 2, 3), {'a': 0, 'b': 1, 'c': 2})") self.assertFalse(row != row) From 190980ed7cbb0d2cfd04faa851a554d68974af3f Mon Sep 17 00:00:00 2001 From: Josef Barta Date: Fri, 17 Nov 2017 11:57:22 +0000 Subject: [PATCH 15/15] Update test_table.py --- bigquery/tests/unit/test_table.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bigquery/tests/unit/test_table.py b/bigquery/tests/unit/test_table.py index 2d4df4625fb0..49471f436be7 100644 --- a/bigquery/tests/unit/test_table.py +++ b/bigquery/tests/unit/test_table.py @@ -766,7 +766,8 @@ def test_row(self): self.assertEqual(len(row), 3) self.assertEqual(row.values(), VALUES) self.assertEqual(set(row.keys()), set({'a': 1, 'b': 2, 'c': 3}.keys())) - self.assertEqual(set(row.items()), set({'a': 1, 'b': 2, 'c': 3}.items())) + self.assertEqual(set(row.items()), + set({'a': 1, 'b': 2, 'c': 3}.items())) self.assertEqual(row.get('a'), 1) self.assertEqual(row.get('d'), None) self.assertEqual(row.get('d', ''), '')