Skip to content
Closed
Show file tree
Hide file tree
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
Update imports and docstrings.
  • Loading branch information
Luke Sneeringer
Luke Sneeringer committed Aug 31, 2017
commit 315159bbf1e2af2ca9a2c32b8a3cde347efac1cd
47 changes: 23 additions & 24 deletions spanner/google/cloud/spanner/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@
import six

from google.gax import CallOptions
from google.protobuf.struct_pb2 import ListValue
from google.protobuf.struct_pb2 import Value
from google.cloud.proto.spanner.v1 import type_pb2
from google.cloud.spanner_v1 import types

This comment was marked as spam.

This comment was marked as spam.


from google.cloud._helpers import _date_from_iso8601_date
from google.cloud._helpers import _datetime_to_rfc3339
Expand Down Expand Up @@ -105,7 +104,7 @@ def _try_to_coerce_bytes(bytestring):
# Attempt to coerce using google.protobuf.Value, which will expect
# something that is utf-8 (and base64 consistently is).
try:
Value(string_value=bytestring)
types.Value(string_value=bytestring)
return bytestring
except ValueError:
raise ValueError('Received a bytes that is not base64 encoded. '
Expand All @@ -120,38 +119,38 @@ def _make_value_pb(value):
:type value: scalar value
:param value: value to convert

:rtype: :class:`~google.protobuf.struct_pb2.Value`
:rtype: :class:`~google.cloud.spanner_v1.types.Value`
:returns: value protobufs
:raises ValueError: if value is not of a known scalar type.
"""
if value is None:
return Value(null_value='NULL_VALUE')
return types.Value(null_value='NULL_VALUE')
if isinstance(value, list):
return Value(list_value=_make_list_value_pb(value))
return types.Value(list_value=_make_list_value_pb(value))
if isinstance(value, bool):
return Value(bool_value=value)
return types.Value(bool_value=value)
if isinstance(value, six.integer_types):
return Value(string_value=str(value))
return types.Value(string_value=str(value))
if isinstance(value, float):
if math.isnan(value):
return Value(string_value='NaN')
return types.Value(string_value='NaN')
if math.isinf(value):
if value > 0:
return Value(string_value='Infinity')
return types.Value(string_value='Infinity')
else:
return Value(string_value='-Infinity')
return Value(number_value=value)
return types.Value(string_value='-Infinity')
return types.Value(number_value=value)
if isinstance(value, TimestampWithNanoseconds):
return Value(string_value=value.rfc3339())
return types.Value(string_value=value.rfc3339())
if isinstance(value, datetime.datetime):
return Value(string_value=_datetime_to_rfc3339(value))
return types.Value(string_value=_datetime_to_rfc3339(value))
if isinstance(value, datetime.date):
return Value(string_value=value.isoformat())
return types.Value(string_value=value.isoformat())
if isinstance(value, six.binary_type):
value = _try_to_coerce_bytes(value)
return Value(string_value=value)
return types.Value(string_value=value)
if isinstance(value, six.text_type):
return Value(string_value=value)
return types.Value(string_value=value)
raise ValueError("Unknown type: %s" % (value,))
# pylint: enable=too-many-return-statements,too-many-branches

Expand All @@ -162,10 +161,10 @@ def _make_list_value_pb(values):
:type values: list of scalar
:param values: Row data

:rtype: :class:`~google.protobuf.struct_pb2.ListValue`
:rtype: :class:`~google.cloud.spanner_v1.types.ListValue`
:returns: protobuf
"""
return ListValue(values=[_make_value_pb(value) for value in values])
return types.ListValue(values=[_make_value_pb(value) for value in values])


def _make_list_value_pbs(values):
Expand All @@ -174,7 +173,7 @@ def _make_list_value_pbs(values):
:type values: list of list of scalar
:param values: Row data

:rtype: list of :class:`~google.protobuf.struct_pb2.ListValue`
:rtype: list of :class:`~google.cloud.spanner_v1.types.ListValue`
:returns: sequence of protobufs
"""
return [_make_list_value_pb(row) for row in values]
Expand All @@ -184,10 +183,10 @@ def _make_list_value_pbs(values):
def _parse_value_pb(value_pb, field_type):
"""Convert a Value protobuf to cell data.

:type value_pb: :class:`~google.protobuf.struct_pb2.Value`
:type value_pb: :class:`~google.cloud.spanner_v1.types.Value`
:param value_pb: protobuf to convert

:type field_type: :class:`~google.cloud.proto.spanner.v1.type_pb2.Type`
:type field_type: :class:`~google.cloud.spanner_v1.types.Type`
:param field_type: type code for the value

:rtype: varies on field_type
Expand Down Expand Up @@ -230,10 +229,10 @@ def _parse_value_pb(value_pb, field_type):
def _parse_list_value_pbs(rows, row_type):
"""Convert a list of ListValue protobufs into a list of list of cell data.

:type rows: list of :class:`~google.protobuf.struct_pb2.ListValue`
:type rows: list of :class:`~google.cloud.spanner_v1.types.ListValue`
:param rows: row data returned from a read/query

:type row_type: :class:`~google.cloud.proto.spanner.v1.type_pb2.StructType`
:type row_type: :class:`~google.cloud.spanner_v1.types.StructType`
:param row_type: row schema specification

:rtype: list of list of cell data
Expand Down
4 changes: 2 additions & 2 deletions spanner/google/cloud/spanner/batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

"""Context manager for Cloud Spanner batched writes."""

from google.cloud.proto.spanner.v1.mutation_pb2 import Mutation
from google.cloud.proto.spanner.v1.transaction_pb2 import TransactionOptions
from google.cloud.spanner_v1.types import Mutation
from google.cloud.spanner_v1.types import TransactionOptions

# pylint: disable=ungrouped-imports
from google.cloud._helpers import _pb_timestamp_to_datetime
Expand Down
2 changes: 1 addition & 1 deletion spanner/google/cloud/spanner/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import google.auth.credentials
from google.gax.errors import GaxError
from google.gax.grpc import exc_to_code
from google.cloud.gapic.spanner.v1.spanner_client import SpannerClient
from google.cloud.spanner_v1 import SpannerClient
from grpc import StatusCode
import six

Expand Down
8 changes: 4 additions & 4 deletions spanner/google/cloud/spanner/keyset.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

"""Wrap representation of Spanner keys / ranges."""

from google.cloud.proto.spanner.v1.keys_pb2 import KeyRange as KeyRangePB
from google.cloud.proto.spanner.v1.keys_pb2 import KeySet as KeySetPB
from google.cloud.spanner_v1.types import KeyRange as KeyRangePB
from google.cloud.spanner_v1.types import KeySet as KeySetPB

from google.cloud.spanner._helpers import _make_list_value_pb
from google.cloud.spanner._helpers import _make_list_value_pbs
Expand Down Expand Up @@ -55,7 +55,7 @@ def __init__(self, start_open=None, start_closed=None,
def to_pb(self):
"""Construct a KeyRange protobuf.

:rtype: :class:`~google.cloud.proto.spanner.v1.keys_pb2.KeyRange`
:rtype: :class:`~google.cloud.spanner_v1.types.KeyRange`
:returns: protobuf corresponding to this instance.
"""
kwargs = {}
Expand Down Expand Up @@ -97,7 +97,7 @@ def __init__(self, keys=(), ranges=(), all_=False):
def to_pb(self):
"""Construct a KeySet protobuf.

:rtype: :class:`~google.cloud.proto.spanner.v1.keys_pb2.KeySet`
:rtype: :class:`~google.cloud.spanner_v1.types.KeySet`
:returns: protobuf corresponding to this instance.
"""
if self.all_:
Expand Down
4 changes: 2 additions & 2 deletions spanner/google/cloud/spanner/snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
"""Model a set of read-only queries to a database as a snapshot."""

from google.protobuf.struct_pb2 import Struct
from google.cloud.proto.spanner.v1.transaction_pb2 import TransactionOptions
from google.cloud.proto.spanner.v1.transaction_pb2 import TransactionSelector
from google.cloud.spanner_v1.types import TransactionOptions
from google.cloud.spanner_v1.types import TransactionSelector

from google.cloud._helpers import _datetime_to_pb_timestamp
from google.cloud._helpers import _timedelta_to_duration_pb
Expand Down
32 changes: 15 additions & 17 deletions spanner/google/cloud/spanner/streamed.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@

"""Wrapper for streaming results."""

from google.protobuf.struct_pb2 import ListValue
from google.protobuf.struct_pb2 import Value
from google.cloud import exceptions
from google.cloud.proto.spanner.v1 import type_pb2
from google.cloud.spanner_v1 import types
import six

# pylint: disable=ungrouped-imports
Expand All @@ -29,10 +28,9 @@ class StreamedResultSet(object):
"""Process a sequence of partial result sets into a single set of row data.

:type response_iterator:
:param response_iterator:
Iterator yielding
:class:`google.cloud.proto.spanner.v1.result_set_pb2.PartialResultSet`
instances.
Iterator[:class:`~.spanner_v1.types.PartialResultSet`]
:param response_iterator: Iterator yielding
:class:`google.cloud.spanner_v1.types.PartialResultSet` instances.

:type source: :class:`~google.cloud.spanner.snapshot.Snapshot`
:param source: Snapshot from which the result set was fetched.
Expand Down Expand Up @@ -61,7 +59,7 @@ def rows(self):
def fields(self):
"""Field descriptors for result set columns.

:rtype: list of :class:`~google.cloud.proto.spanner.v1.type_pb2.Field`
:rtype: list of :class:`~google.cloud.proto.spanner.v1.types.Field`

This comment was marked as spam.

This comment was marked as spam.

:returns: list of fields describing column names / types.
"""
return self._metadata.row_type.fields
Expand Down Expand Up @@ -97,11 +95,11 @@ def resume_token(self):
def _merge_chunk(self, value):
"""Merge pending chunk with next value.

:type value: :class:`~google.protobuf.struct_pb2.Value`
:type value: :class:`~google.cloud.spanner_v1.types.Value`
:param value: continuation of chunked value from previous
partial result set.

:rtype: :class:`~google.protobuf.struct_pb2.Value`
:rtype: :class:`~google.cloud.spanner_v1.types.Value`
:returns: the merged value
"""
current_column = len(self._current_row)
Expand All @@ -113,7 +111,7 @@ def _merge_chunk(self, value):
def _merge_values(self, values):
"""Merge values into rows.

:type values: list of :class:`~google.protobuf.struct_pb2.Value`
:type values: list of :class:`~google.cloud.spanner_v1.types.Value`
:param values: non-chunked values from partial result set.
"""
width = len(self.fields)
Expand Down Expand Up @@ -216,13 +214,13 @@ def one_or_none(self):
class Unmergeable(ValueError):
"""Unable to merge two values.

:type lhs: :class:`google.protobuf.struct_pb2.Value`
:type lhs: :class:`google.cloud.spanner_v1.types.Value`
:param lhs: pending value to be merged

:type rhs: :class:`google.protobuf.struct_pb2.Value`
:type rhs: :class:`google.cloud.spanner_v1.types.Value`
:param rhs: remaining value to be merged

:type type_: :class:`google.cloud.proto.spanner.v1.type_pb2.Type`
:type type_: :class:`google.cloud.spanner_v1.types.Type`
:param type_: field type of values being merged
"""
def __init__(self, lhs, rhs, type_):
Expand All @@ -240,7 +238,7 @@ def _merge_float64(lhs, rhs, type_): # pylint: disable=unused-argument
"""Helper for '_merge_by_type'."""
lhs_kind = lhs.WhichOneof('kind')
if lhs_kind == 'string_value':
return Value(string_value=lhs.string_value + rhs.string_value)
return types.Value(string_value=lhs.string_value + rhs.string_value)
rhs_kind = rhs.WhichOneof('kind')
array_continuation = (
lhs_kind == 'number_value' and
Expand All @@ -253,7 +251,7 @@ def _merge_float64(lhs, rhs, type_): # pylint: disable=unused-argument

def _merge_string(lhs, rhs, type_): # pylint: disable=unused-argument
"""Helper for '_merge_by_type'."""
return Value(string_value=lhs.string_value + rhs.string_value)
return types.Value(string_value=lhs.string_value + rhs.string_value)


_UNMERGEABLE_TYPES = (type_pb2.BOOL,)
Expand All @@ -279,7 +277,7 @@ def _merge_array(lhs, rhs, type_):
lhs.append(first)
else:
lhs.append(merged)
return Value(list_value=ListValue(values=(lhs + rhs)))
return types.Value(list_value=types.ListValue(values=(lhs + rhs)))


def _merge_struct(lhs, rhs, type_):
Expand All @@ -294,7 +292,7 @@ def _merge_struct(lhs, rhs, type_):
else:
last = lhs.pop()
lhs.append(_merge_by_type(last, first, candidate_type))
return Value(list_value=ListValue(values=lhs + rhs))
return types.Value(list_value=types.ListValue(values=lhs + rhs))


_MERGE_BY_TYPE = {
Expand Down
7 changes: 3 additions & 4 deletions spanner/google/cloud/spanner/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

"""Spanner read-write transaction support."""

from google.cloud.proto.spanner.v1.transaction_pb2 import TransactionSelector
from google.cloud.proto.spanner.v1.transaction_pb2 import TransactionOptions
from google.cloud.spanner_v1.types import TransactionSelector
from google.cloud.spanner_v1.types import TransactionOptions

from google.cloud._helpers import _pb_timestamp_to_datetime
from google.cloud.spanner._helpers import _options_with_prefix
Expand Down Expand Up @@ -60,8 +60,7 @@ def _check_state(self):
def _make_txn_selector(self):
"""Helper for :meth:`read`.

:rtype:
:class:`~.transaction_pb2.TransactionSelector`
:rtype: :class:`~.spanner_v1.types.TransactionSelector`
:returns: a selector configured for read-write transaction semantics.
"""
self._check_state()
Expand Down