Skip to content

Commit

Permalink
Adding support for Cell.labels.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhermes committed Nov 17, 2015
1 parent 5a2a1ac commit b77708f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
8 changes: 7 additions & 1 deletion gcloud_bigtable/row.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,9 @@ def commit_modifications(self, timeout_seconds=None):
# than actually listing every single argument. However, for the sake
# of users and documentation, listing every single argument is more
# useful.
# pylint: disable=too-many-instance-attributes
# pylint: disable=too-many-arguments
# pylint: disable=too-many-branches
class RowFilter(object):
"""Basic filter to apply to cells in a row.
Expand Down Expand Up @@ -845,6 +848,9 @@ def to_pb(self):
row_filter_kwargs['apply_label_transformer'] = (
self.apply_label_transformer)
return data_pb2.RowFilter(**row_filter_kwargs)
# pylint: enable=too-many-instance-attributes
# pylint: enable=too-many-arguments
# pylint: enable=too-many-branches


class TimestampRange(object):
Expand Down Expand Up @@ -963,7 +969,7 @@ def to_pb(self):
class CellValueRange(object):
"""A range of values to restrict to in a row filter.
With only match cells that have values in this range.
Will only match cells that have values in this range.
Both the start and end value can be included or excluded in the range.
By default, we include them both, but this can be changed with optional
Expand Down
14 changes: 11 additions & 3 deletions gcloud_bigtable/row_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@ class Cell(object):
:type timestamp: :class:`datetime.datetime`
:param timestamp: The timestamp when the cell was stored.
:type labels: list
:param labels: (Optional) List of strings. Labels applied to the cell.
"""

def __init__(self, value, timestamp):
def __init__(self, value, timestamp, labels=()):
self.value = value
self.timestamp = timestamp
self.labels = list(labels)

@classmethod
def from_pb(cls, cell_pb):
Expand All @@ -47,13 +51,17 @@ def from_pb(cls, cell_pb):
:returns: The cell corresponding to the protobuf.
"""
timestamp = _microseconds_to_timestamp(cell_pb.timestamp_micros)
return cls(cell_pb.value, timestamp)
if cell_pb.labels:
return cls(cell_pb.value, timestamp, labels=cell_pb.labels)
else:
return cls(cell_pb.value, timestamp)

def __eq__(self, other):
if not isinstance(other, self.__class__):
return False
return (other.value == self.value and
other.timestamp == self.timestamp)
other.timestamp == self.timestamp and
other.labels == self.labels)

def __ne__(self, other):
return not self.__eq__(other)
Expand Down
21 changes: 18 additions & 3 deletions gcloud_bigtable/test_row_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,36 @@ def _getTargetClass(self):
def _makeOne(self, *args, **kwargs):
return self._getTargetClass()(*args, **kwargs)

def test_from_pb(self):
def _from_pb_test_helper(self, labels=None):
import datetime
from gcloud_bigtable._generated import bigtable_data_pb2 as data_pb2
from gcloud_bigtable._helpers import EPOCH

timestamp_micros = 18738724000 # Make sure millis granularity
timestamp = EPOCH + datetime.timedelta(microseconds=timestamp_micros)
value = b'value-bytes'
cell_expected = self._makeOne(value, timestamp)

cell_pb = data_pb2.Cell(value=value, timestamp_micros=timestamp_micros)
if labels is None:
cell_pb = data_pb2.Cell(value=value,
timestamp_micros=timestamp_micros)
cell_expected = self._makeOne(value, timestamp)
else:
cell_pb = data_pb2.Cell(value=value,
timestamp_micros=timestamp_micros,
labels=labels)
cell_expected = self._makeOne(value, timestamp, labels=labels)

klass = self._getTargetClass()
result = klass.from_pb(cell_pb)
self.assertEqual(result, cell_expected)

def test_from_pb(self):
self._from_pb_test_helper()

def test_from_pb_with_labels(self):
labels = [u'label1', u'label2']
self._from_pb_test_helper(labels)

def test_constructor(self):
value = object()
timestamp = object()
Expand Down

0 comments on commit b77708f

Please sign in to comment.