Skip to content

Commit

Permalink
Moving _parse_family_pb from _helpers into row.
Browse files Browse the repository at this point in the history
This is because that was the only place it was used.
Also remove test___init__ since we don't fail in the
same way any longer.
  • Loading branch information
dhermes committed Dec 23, 2015
1 parent 0c58512 commit 764df9f
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 237 deletions.
40 changes: 0 additions & 40 deletions gcloud_bigtable/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@

from grpc.beta import implementations

from gcloud_bigtable._non_upstream_helpers import _microseconds_to_timestamp


# See https://gist.github.com/dhermes/bbc5b7be1932bfffae77
# for appropriate values on other systems.
Expand Down Expand Up @@ -106,41 +104,3 @@ def make_stub(client, stub_factory, host, port):
custom_metadata_transformer = MetadataTransformer(client)
return stub_factory(channel,
metadata_transformer=custom_metadata_transformer)


def _parse_family_pb(family_pb):
"""Parses a Family protobuf into a dictionary.
:type family_pb: :class:`._generated.bigtable_data_pb2.Family`
:param family_pb: A protobuf
:rtype: tuple
:returns: A string and dictionary. The string is the name of the
column family and the dictionary has column names (within the
family) as keys and cell lists as values. Each cell is
represented with a two-tuple with the value (in bytes) and the
timestamp for the cell. For example:
.. code:: python
{
b'col-name1': [
(b'cell-val', datetime.datetime(...)),
(b'cell-val-newer', datetime.datetime(...)),
],
b'col-name2': [
(b'altcol-cell-val', datetime.datetime(...)),
],
}
"""
result = {}
for column in family_pb.columns:
result[column.qualifier] = cells = []
for cell in column.cells:
val_pair = (
cell.value,
_microseconds_to_timestamp(cell.timestamp_micros),
)
cells.append(val_pair)

return family_pb.name, result
40 changes: 39 additions & 1 deletion gcloud_bigtable/row.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from gcloud_bigtable._generated import bigtable_data_pb2 as data_pb2
from gcloud_bigtable._generated import (
bigtable_service_messages_pb2 as messages_pb2)
from gcloud_bigtable._helpers import _parse_family_pb
from gcloud_bigtable._non_upstream_helpers import _microseconds_to_timestamp
from gcloud_bigtable._non_upstream_helpers import _timestamp_to_microseconds
from gcloud_bigtable._non_upstream_helpers import _to_bytes

Expand Down Expand Up @@ -1205,3 +1205,41 @@ def _parse_rmw_row_response(row_response):
column_family_id, curr_family = _parse_family_pb(column_family)
result[column_family_id] = curr_family
return result


def _parse_family_pb(family_pb):
"""Parses a Family protobuf into a dictionary.
:type family_pb: :class:`._generated.bigtable_data_pb2.Family`
:param family_pb: A protobuf
:rtype: tuple
:returns: A string and dictionary. The string is the name of the
column family and the dictionary has column names (within the
family) as keys and cell lists as values. Each cell is
represented with a two-tuple with the value (in bytes) and the
timestamp for the cell. For example:
.. code:: python
{
b'col-name1': [
(b'cell-val', datetime.datetime(...)),
(b'cell-val-newer', datetime.datetime(...)),
],
b'col-name2': [
(b'altcol-cell-val', datetime.datetime(...)),
],
}
"""
result = {}
for column in family_pb.columns:
result[column.qualifier] = cells = []
for cell in column.cells:
val_pair = (
cell.value,
_microseconds_to_timestamp(cell.timestamp_micros),
)
cells.append(val_pair)

return family_pb.name, result
136 changes: 0 additions & 136 deletions gcloud_bigtable/test___init__.py

This file was deleted.

60 changes: 0 additions & 60 deletions gcloud_bigtable/test__helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,63 +142,3 @@ def test_it(self):
{'private_key': None, 'certificate_chain': None}),
('secure_channel', (host, port, client_creds), {}),
])


class Test__parse_family_pb(unittest2.TestCase):

def _callFUT(self, family_pb):
from gcloud_bigtable._helpers import _parse_family_pb
return _parse_family_pb(family_pb)

def test_it(self):
from gcloud_bigtable._generated import bigtable_data_pb2 as data_pb2
from gcloud_bigtable._non_upstream_helpers import (
_microseconds_to_timestamp)

COL_FAM1 = u'col-fam-id'
COL_NAME1 = b'col-name1'
COL_NAME2 = b'col-name2'
CELL_VAL1 = b'cell-val'
CELL_VAL2 = b'cell-val-newer'
CELL_VAL3 = b'altcol-cell-val'

microseconds = 5554441037
timestamp = _microseconds_to_timestamp(microseconds)
expected_dict = {
COL_NAME1: [
(CELL_VAL1, timestamp),
(CELL_VAL2, timestamp),
],
COL_NAME2: [
(CELL_VAL3, timestamp),
],
}
expected_output = (COL_FAM1, expected_dict)
sample_input = data_pb2.Family(
name=COL_FAM1,
columns=[
data_pb2.Column(
qualifier=COL_NAME1,
cells=[
data_pb2.Cell(
value=CELL_VAL1,
timestamp_micros=microseconds,
),
data_pb2.Cell(
value=CELL_VAL2,
timestamp_micros=microseconds,
),
],
),
data_pb2.Column(
qualifier=COL_NAME2,
cells=[
data_pb2.Cell(
value=CELL_VAL3,
timestamp_micros=microseconds,
),
],
),
],
)
self.assertEqual(expected_output, self._callFUT(sample_input))
60 changes: 60 additions & 0 deletions gcloud_bigtable/test_row.py
Original file line number Diff line number Diff line change
Expand Up @@ -1524,6 +1524,66 @@ def test_to_pb_false_only(self):
self.assertEqual(filter_pb, expected_pb)


class Test__parse_family_pb(unittest2.TestCase):

def _callFUT(self, family_pb):
from gcloud_bigtable.row import _parse_family_pb
return _parse_family_pb(family_pb)

def test_it(self):
from gcloud_bigtable._generated import bigtable_data_pb2 as data_pb2
from gcloud_bigtable._non_upstream_helpers import (
_microseconds_to_timestamp)

COL_FAM1 = u'col-fam-id'
COL_NAME1 = b'col-name1'
COL_NAME2 = b'col-name2'
CELL_VAL1 = b'cell-val'
CELL_VAL2 = b'cell-val-newer'
CELL_VAL3 = b'altcol-cell-val'

microseconds = 5554441037
timestamp = _microseconds_to_timestamp(microseconds)
expected_dict = {
COL_NAME1: [
(CELL_VAL1, timestamp),
(CELL_VAL2, timestamp),
],
COL_NAME2: [
(CELL_VAL3, timestamp),
],
}
expected_output = (COL_FAM1, expected_dict)
sample_input = data_pb2.Family(
name=COL_FAM1,
columns=[
data_pb2.Column(
qualifier=COL_NAME1,
cells=[
data_pb2.Cell(
value=CELL_VAL1,
timestamp_micros=microseconds,
),
data_pb2.Cell(
value=CELL_VAL2,
timestamp_micros=microseconds,
),
],
),
data_pb2.Column(
qualifier=COL_NAME2,
cells=[
data_pb2.Cell(
value=CELL_VAL3,
timestamp_micros=microseconds,
),
],
),
],
)
self.assertEqual(expected_output, self._callFUT(sample_input))


class _Client(object):

data_stub = None
Expand Down

0 comments on commit 764df9f

Please sign in to comment.