Skip to content

Commit

Permalink
Adding PartialRowData.to_dict().
Browse files Browse the repository at this point in the history
  • Loading branch information
dhermes committed Sep 11, 2015
1 parent c2bf350 commit 1ec4eed
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
17 changes: 17 additions & 0 deletions gcloud_bigtable/row_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@


import copy
import six

from gcloud_bigtable._helpers import _microseconds_to_timestamp

Expand Down Expand Up @@ -84,6 +85,22 @@ def __eq__(self, other):
def __ne__(self, other):
return not self.__eq__(other)

def to_dict(self):
"""Convert the cells to a dictionary.
This is intended to be used with HappyBase, so the column family and
column qualiers are combined (with ``:``).
:rtype: dict
:returns: Dictionary containing all the data in the cells of this row.
"""
result = {}
for column_family_id, columns in six.iteritems(self._cells):
for column_qual, cells in six.iteritems(columns):
key = column_family_id + ':' + column_qual
result[key] = cells
return result

@property
def cells(self):
"""Property returning all the cells accumulated on this partial row.
Expand Down
33 changes: 33 additions & 0 deletions gcloud_bigtable/test_row_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,39 @@ def test___ne__cells(self):
partial_row_data2 = self._makeOne(row_key)
self.assertNotEqual(partial_row_data1, partial_row_data2)

def test_to_dict(self):
cell1 = object()
cell2 = object()
cell3 = object()

family_name1 = u'name1'
family_name2 = u'name2'
qual1 = b'col1'
qual2 = b'col2'
qual3 = b'col3'

partial_row_data = self._makeOne(None)
partial_row_data._cells = {
family_name1: {
qual1: cell1,
qual2: cell2,
},
family_name2: {
qual3: cell3,
},
}

result = partial_row_data.to_dict()
col1 = family_name1 + b':' + qual1
col2 = family_name1 + b':' + qual2
col3 = family_name2 + b':' + qual3
expected_result = {
col1: cell1,
col2: cell2,
col3: cell3,
}
self.assertEqual(result, expected_result)

def test_cells_property(self):
partial_row_data = self._makeOne(None)
cells = {1: 2}
Expand Down

0 comments on commit 1ec4eed

Please sign in to comment.