Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
added str and repr for row filters
  • Loading branch information
daniel-sanche committed Mar 16, 2023
commit fcabba5767316b67131620e7bca871cb67314015
54 changes: 54 additions & 0 deletions google/cloud/bigtable/row_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ def to_dict(self) -> dict[str, Any]:
# unimplemented on base class
raise NotImplementedError
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think of creating an ABC using abc.ABC for RowFilter so that to_dict can be one of its @abstractmethod

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think that makes sense. The only complication is that it requires us to make other filter subclasses abstract as well (_BoolFilter, _FilterCombination, _CellCountFilter), but that's probably good to prevent users from instantiating them directly anyway

I made the changes, let me know what you think


def __repr__(self) -> str:
return f"{self.__class__.__name__}()"


class _BoolFilter(RowFilter):
"""Row filter that uses a boolean flag.
Expand All @@ -73,6 +76,9 @@ def __eq__(self, other):
def __ne__(self, other):
return not self == other

def __repr__(self) -> str:
return f"{self.__class__.__name__}(flag={self.flag})"


class SinkFilter(_BoolFilter):
"""Advanced row filter to skip parent filters.
Expand Down Expand Up @@ -142,6 +148,9 @@ def __eq__(self, other):
def __ne__(self, other):
return not self == other

def __repr__(self) -> str:
return f"{self.__class__.__name__}(regex={self.regex!r})"


class RowKeyRegexFilter(_RegexFilter):
"""Row filter for a row key regular expression.
Expand Down Expand Up @@ -194,6 +203,8 @@ def to_dict(self) -> dict[str, Any]:
"""Converts the row filter to a dict representation."""
return {"row_sample_filter": self.sample}

def __repr__(self) -> str:
return f"{self.__class__.__name__}(sample={self.sample})"

class FamilyNameRegexFilter(_RegexFilter):
"""Row filter for a family name regular expression.
Expand Down Expand Up @@ -287,6 +298,8 @@ def to_dict(self) -> dict[str, int]:
timestamp_range_kwargs["end_timestamp_micros"] = end_time
return timestamp_range_kwargs

def __repr__(self) -> str:
return f"{self.__class__.__name__}(start={self.start}, end={self.end})"

class TimestampRangeFilter(RowFilter):
"""Row filter that limits cells to a range of time.
Expand Down Expand Up @@ -321,6 +334,9 @@ def to_dict(self) -> dict[str, Any]:
"""Converts the row filter to a dict representation."""
return {"timestamp_range_filter": self.range_.to_dict()}

def __repr__(self) -> str:
return f"{self.__class__.__name__}(start={self.range_.start}, end={self.range_.end})"


class ColumnRangeFilter(RowFilter):
"""A row filter to restrict to a range of columns.
Expand Down Expand Up @@ -434,6 +450,8 @@ def to_dict(self) -> dict[str, Any]:
"""Converts the row filter to a dict representation."""
return {"column_range_filter": self.range_to_dict()}

def __repr__(self) -> str:
return f"{self.__class__.__name__}(column_family_id={self.column_family_id}, start_column={self.start_column}, end_column={self.end_column}, inclusive_start={self.inclusive_start}, inclusive_end={self.inclusive_end})"

class ValueRegexFilter(_RegexFilter):
"""Row filter for a value regular expression.
Expand Down Expand Up @@ -476,6 +494,9 @@ def __init__(self, value: bytes | str | int):
value = _PACK_I64(value)
super(ExactValueFilter, self).__init__(value)

def __repr__(self) -> str:
return f"{self.__class__.__name__}(value={self.regex})"


class ValueRangeFilter(RowFilter):
"""A range of values to restrict to in a row filter.
Expand Down Expand Up @@ -584,6 +605,9 @@ def to_dict(self) -> dict[str, Any]:
"""Converts the row filter to a dict representation."""
return {"value_range_filter": self.range_to_dict()}

def __repr__(self) -> str:
return f"{self.__class__.__name__}(start_value={self.start_value}, end_value={self.end_value}, inclusive_start={self.inclusive_start}, inclusive_end={self.inclusive_end})"


class _CellCountFilter(RowFilter):
"""Row filter that uses an integer count of cells.
Expand All @@ -606,6 +630,9 @@ def __eq__(self, other):
def __ne__(self, other):
return not self == other

def __repr__(self) -> str:
return f"{self.__class__.__name__}(num_cells={self.num_cells})"


class CellsRowOffsetFilter(_CellCountFilter):
"""Row filter to skip cells in a row.
Expand Down Expand Up @@ -693,6 +720,8 @@ def to_dict(self) -> dict[str, str]:
"""Converts the row filter to a dict representation."""
return {"apply_label_transformer": self.label}

def __repr__(self) -> str:
return f"{self.__class__.__name__}(label={self.label})"

class _FilterCombination(RowFilter):
"""Chain of row filters.
Expand All @@ -718,6 +747,16 @@ def __eq__(self, other):
def __ne__(self, other):
return not self == other

def __repr__(self) -> str:
return f"{self.__class__.__name__}(filters={[repr(f) for f in self.filters]})"

def __str__(self) -> str:
output = [f"{self.__class__.__name__}(["]
for filter_ in self.filters:
filter_lines = f"{filter_},".splitlines()
output.extend([f" {line}" for line in filter_lines])
output.append("])")
return "\n".join(output)

class RowFilterChain(_FilterCombination):
"""Chain of row filters.
Expand Down Expand Up @@ -853,3 +892,18 @@ def condition_to_dict(self) -> dict[str, Any]:
def to_dict(self) -> dict[str, Any]:
"""Converts the row filter to a dict representation."""
return {"condition": self.condition_to_dict()}

def __repr__(self) -> str:
return f"{self.__class__.__name__}(base_filter={self.base_filter!r}, true_filter={self.true_filter!r}, false_filter={self.false_filter!r})"

def __str__(self) -> str:
output = [f"{self.__class__.__name__}("]
for filter_type in ("base_filter", "true_filter", "false_filter"):
filter_ = getattr(self, filter_type)
if filter_ is None:
continue
# add the new filter set, adding indentations for readability
output.append(f" {filter_type}={filter_!r},")
output.append(")")
return "\n".join(output)