Skip to content

Commit fb48263

Browse files
committed
BigQuery: docs for Row.keys, items, values, and get functions.
Refactors items, keys to return iterators.
1 parent e89ab7e commit fb48263

1 file changed

Lines changed: 57 additions & 28 deletions

File tree

bigquery/google/cloud/bigquery/table.py

Lines changed: 57 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -783,43 +783,72 @@ def __init__(self, values, field_to_index):
783783
self._xxx_field_to_index = field_to_index
784784

785785
def values(self):
786+
"""Return the values included in this row.
787+
788+
Returns:
789+
Sequence[object]: A sequence of length ``len(row)``.
790+
"""
786791
return self._xxx_values
787792

788793
def keys(self):
794+
"""Return the keys for using a row as a dict.
795+
796+
Returns:
797+
Sequence[str]: The keys corresponding to the columns of a row
798+
799+
Examples:
800+
801+
>>> list(Row(('a', 'b'), {'x': 0, 'y': 1}).keys())
802+
['x', 'y']
789803
"""
790-
Return keys as of a dict:
791-
>>> Row(('a', 'b'), {'x': 0, 'y': 1}).keys()
792-
['x', 'y']
793-
"""
794-
keys = self._xxx_field_to_index.keys()
795-
return keys
804+
return six.iterkeys(self._xxx_field_to_index)
796805

797806
def items(self):
807+
"""Return items as ``(key, value)`` pairs.
808+
809+
Returns:
810+
Sequence[Tuple[str, object]]:
811+
The ``(key, value)`` pairs representing this row.
812+
813+
Examples:
814+
815+
>>> list(Row(('a', 'b'), {'x': 0, 'y': 1}).items())
816+
[('x', 'a'), ('y', 'b')]
798817
"""
799-
Return items as of a dict:
800-
>>> Row(('a', 'b'), {'x': 0, 'y': 1}).items()
801-
[('x', 'a'), ('y', 'b')]
802-
"""
803-
items = [
804-
(k, self._xxx_values[i])
805-
for k, i
806-
in self._xxx_field_to_index.items()
807-
]
808-
return items
818+
for key, index in six.iteritems(self._xxx_field_to_index):
819+
yield (key, self._xxx_values[index])
809820

810821
def get(self, key, default=None):
811-
"""
812-
Return value under specified key
813-
Defaults to None or specified default
814-
if key does not exist:
815-
>>> Row(('a', 'b'), {'x': 0, 'y': 1}).get('x')
816-
'a'
817-
>>> Row(('a', 'b'), {'x': 0, 'y': 1}).get('z')
818-
None
819-
>>> Row(('a', 'b'), {'x': 0, 'y': 1}).get('z', '')
820-
''
821-
>>> Row(('a', 'b'), {'x': 0, 'y': 1}).get('z', default = '')
822-
''
822+
"""Return a value for key, with a default value if it does not exist.
823+
824+
Args:
825+
key (str): The key of the column to access
826+
default (object):
827+
The default value to use if the key does not exist. (Defaults
828+
to :data:`None`.)
829+
830+
Returns:
831+
object:
832+
The value associated with the provided key, or a default value.
833+
834+
Examples:
835+
When the key exists, the value associated with it is returned.
836+
837+
>>> Row(('a', 'b'), {'x': 0, 'y': 1}).get('x')
838+
'a'
839+
840+
The default value is ``None`` when the key does not exist.
841+
842+
>>> Row(('a', 'b'), {'x': 0, 'y': 1}).get('z')
843+
None
844+
845+
The default value can be overrided with the ``default`` parameter.
846+
847+
>>> Row(('a', 'b'), {'x': 0, 'y': 1}).get('z', '')
848+
''
849+
850+
>>> Row(('a', 'b'), {'x': 0, 'y': 1}).get('z', default = '')
851+
''
823852
"""
824853
index = self._xxx_field_to_index.get(key)
825854
if index is None:

0 commit comments

Comments
 (0)