Skip to content

Commit

Permalink
Merge branch 'master' of github.com:GoogleCloudPlatform/google-cloud-…
Browse files Browse the repository at this point in the history
…python into arrays
  • Loading branch information
tswast committed Dec 27, 2016
2 parents 909cf52 + 455eaf6 commit 96489a1
Show file tree
Hide file tree
Showing 80 changed files with 1,079 additions and 4,386 deletions.
53 changes: 0 additions & 53 deletions Makefile.datastore

This file was deleted.

9 changes: 6 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ Cloud Platform services:
- `Google Cloud DNS`_ (`DNS README`_)
- `Stackdriver Error Reporting`_ (`Error Reporting README`_)
- `Google Cloud Natural Language`_ (`Natural Language README`_)
- `Google Translate`_ (`Translate README`_)
- `Google Cloud Translation`_ (`Translation README`_)
- `Google Cloud Speech`_ (`Speech README`_)
- `Google Cloud Vision`_ (`Vision README`_)
- `Google Cloud Bigtable - HappyBase`_ (`HappyBase README`_)
- `Google Cloud Runtime Configuration`_ (`Runtime Config README`_)
Expand Down Expand Up @@ -68,8 +69,10 @@ updates. See `versioning`_ for more details.
.. _Error Reporting README: https://github.com/GoogleCloudPlatform/google-cloud-python/tree/master/error_reporting
.. _Google Cloud Natural Language: https://pypi.python.org/pypi/google-cloud-language
.. _Natural Language README: https://github.com/GoogleCloudPlatform/google-cloud-python/tree/master/language
.. _Google Translate: https://pypi.python.org/pypi/google-cloud-translate
.. _Translate README: https://github.com/GoogleCloudPlatform/google-cloud-python/tree/master/translate
.. _Google Cloud Translation: https://pypi.python.org/pypi/google-cloud-translate
.. _Translation README: https://github.com/GoogleCloudPlatform/google-cloud-python/tree/master/translate
.. _Google Cloud Speech: https://pypi.python.org/pypi/google-cloud-speech
.. _Speech README: https://github.com/GoogleCloudPlatform/google-cloud-python/tree/master/speech
.. _Google Cloud Vision: https://pypi.python.org/pypi/google-cloud-vision
.. _Vision README: https://github.com/GoogleCloudPlatform/google-cloud-python/tree/master/vision
.. _Google Cloud Bigtable - HappyBase: https://pypi.python.org/pypi/google-cloud-happybase/
Expand Down
19 changes: 13 additions & 6 deletions bigquery/google/cloud/bigquery/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
from collections import OrderedDict
import datetime

from google.cloud._helpers import UTC
from google.cloud._helpers import _date_from_iso8601_date
from google.cloud._helpers import _datetime_from_microseconds
from google.cloud._helpers import _datetime_to_rfc3339
from google.cloud._helpers import _microseconds_from_datetime
from google.cloud._helpers import _RFC3339_NO_FRACTION
from google.cloud._helpers import _time_from_iso8601_time_naive
from google.cloud._helpers import _to_bytes
Expand Down Expand Up @@ -150,7 +150,11 @@ def _bytes_to_json(value):
def _timestamp_to_json(value):
"""Coerce 'value' to an JSON-compatible representation."""
if isinstance(value, datetime.datetime):
value = _microseconds_from_datetime(value) / 1.0e6
if value.tzinfo not in (None, UTC):
# Convert to UTC and remove the time zone info.
value = value.replace(tzinfo=None) - value.utcoffset()
value = '%s %s+00:00' % (
value.date().isoformat(), value.time().isoformat())
return value


Expand Down Expand Up @@ -553,10 +557,12 @@ def from_api_repr(cls, resource):
instance = cls(name)
types = instance.struct_types
for item in resource['parameterType']['structTypes']:
types[item['name']] = item['type']
types[item['name']] = item['type']['type']
struct_values = resource['parameterValue']['structValues']
for key, value in struct_values.items():
converted = _CELLDATA_FROM_JSON[types[key]](value, None)
type_ = types[key]
value = value['value']
converted = _CELLDATA_FROM_JSON[type_](value, None)
instance.struct_values[key] = converted
return instance

Expand All @@ -567,18 +573,19 @@ def to_api_repr(self):
:returns: JSON mapping
"""
types = [
{'name': key, 'type': value}
{'name': key, 'type': {'type': value}}
for key, value in self.struct_types.items()
]
values = {}
for name, value in self.struct_values.items():
converter = _SCALAR_VALUE_TO_JSON.get(self.struct_types[name])
if converter is not None:
value = converter(value)
values[name] = value
values[name] = {'value': value}

resource = {
'parameterType': {
'type': 'STRUCT',
'structTypes': types,
},
'parameterValue': {
Expand Down
25 changes: 15 additions & 10 deletions bigquery/google/cloud/bigquery/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,25 @@ class Client(JSONClient):
passed when creating a dataset / job. If not passed,
falls back to the default inferred from the environment.
:type credentials: :class:`oauth2client.client.OAuth2Credentials` or
:class:`NoneType`
:param credentials: The OAuth2 Credentials to use for the connection
owned by this client. If not passed (and if no ``http``
object is passed), falls back to the default inferred
from the environment.
:type http: :class:`httplib2.Http` or class that defines ``request()``.
:param http: An optional HTTP object to make requests. If not passed, an
:type credentials: :class:`~google.auth.credentials.Credentials`
:param credentials: (Optional) The OAuth2 Credentials to use for this
client. If not passed (and if no ``http`` object is
passed), falls back to the default inferred from the
environment.
:type http: :class:`~httplib2.Http`
:param http: (Optional) HTTP object to make requests. Can be any object
that defines ``request()`` with the same interface as
:meth:`~httplib2.Http.request`. If not passed, an
``http`` object is created that is bound to the
``credentials`` for the current object.
"""

_connection_class = Connection
def __init__(self, project=None, credentials=None, http=None):
super(Client, self).__init__(
project=project, credentials=credentials, http=http)
self._connection = Connection(
credentials=self._credentials, http=self._http)

def list_projects(self, max_results=None, page_token=None):
"""List projects for the project associated with this client.
Expand Down
86 changes: 64 additions & 22 deletions bigquery/unit_tests/test__helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,13 +546,35 @@ def _call_fut(self, value):
def test_w_float(self):
self.assertEqual(self._call_fut(1.234567), 1.234567)

def test_w_datetime(self):
def test_w_string(self):
ZULU = '2016-12-20 15:58:27.339328+00:00'
self.assertEqual(self._call_fut(ZULU), ZULU)

def test_w_datetime_wo_zone(self):
import datetime
ZULU = '2016-12-20 15:58:27.339328+00:00'
when = datetime.datetime(2016, 12, 20, 15, 58, 27, 339328)
self.assertEqual(self._call_fut(when), ZULU)

def test_w_datetime_w_non_utc_zone(self):
import datetime

class _Zone(datetime.tzinfo):

def utcoffset(self, _):
return datetime.timedelta(minutes=-240)

ZULU = '2016-12-20 19:58:27.339328+00:00'
when = datetime.datetime(
2016, 12, 20, 15, 58, 27, 339328, tzinfo=_Zone())
self.assertEqual(self._call_fut(when), ZULU)

def test_w_datetime_w_utc_zone(self):
import datetime
from google.cloud._helpers import UTC
from google.cloud._helpers import _microseconds_from_datetime
when = datetime.datetime(2016, 12, 3, 14, 11, 27, tzinfo=UTC)
self.assertEqual(self._call_fut(when),
_microseconds_from_datetime(when) / 1e6)
ZULU = '2016-12-20 15:58:27.339328+00:00'
when = datetime.datetime(2016, 12, 20, 15, 58, 27, 339328, tzinfo=UTC)
self.assertEqual(self._call_fut(when), ZULU)


class Test_datetime_to_json(unittest.TestCase):
Expand Down Expand Up @@ -907,20 +929,20 @@ def test_to_api_repr_w_bool(self):
self.assertEqual(param.to_api_repr(), EXPECTED)

def test_to_api_repr_w_timestamp_datetime(self):
from google.cloud._helpers import UTC
import datetime
from google.cloud._helpers import _microseconds_from_datetime
now = datetime.datetime.utcnow()
seconds = _microseconds_from_datetime(now) / 1.0e6
STAMP = '2016-12-20 15:58:27.339328+00:00'
when = datetime.datetime(2016, 12, 20, 15, 58, 27, 339328, tzinfo=UTC)
EXPECTED = {
'parameterType': {
'type': 'TIMESTAMP',
},
'parameterValue': {
'value': seconds,
'value': STAMP,
},
}
klass = self._get_target_class()
param = klass.positional(type_='TIMESTAMP', value=now)
param = klass.positional(type_='TIMESTAMP', value=when)
self.assertEqual(param.to_api_repr(), EXPECTED)

def test_to_api_repr_w_timestamp_micros(self):
Expand Down Expand Up @@ -1060,6 +1082,7 @@ def test_from_api_repr_w_name(self):
def test_from_api_repr_wo_name(self):
RESOURCE = {
'parameterType': {
'type': 'ARRAY',
'arrayType': 'INT64',
},
'parameterValue': {
Expand All @@ -1076,6 +1099,7 @@ def test_to_api_repr_w_name(self):
EXPECTED = {
'name': 'foo',
'parameterType': {
'type': 'ARRAY',
'arrayType': 'INT64',
},
'parameterValue': {
Expand All @@ -1088,6 +1112,7 @@ def test_to_api_repr_w_name(self):
def test_to_api_repr_wo_name(self):
EXPECTED = {
'parameterType': {
'type': 'ARRAY',
'arrayType': 'INT64',
},
'parameterValue': {
Expand All @@ -1101,6 +1126,7 @@ def test_to_api_repr_wo_name(self):
def test_to_api_repr_w_unknown_type(self):
EXPECTED = {
'parameterType': {
'type': 'ARRAY',
'arrayType': 'UNKNOWN',
},
'parameterValue': {
Expand Down Expand Up @@ -1148,13 +1174,17 @@ def test_from_api_repr_w_name(self):
RESOURCE = {
'name': 'foo',
'parameterType': {
'type': 'STRUCT',
'structTypes': [
{'name': 'bar', 'type': 'INT64'},
{'name': 'baz', 'type': 'STRING'},
{'name': 'bar', 'type': {'type': 'INT64'}},
{'name': 'baz', 'type': {'type': 'STRING'}},
],
},
'parameterValue': {
'structValues': {'bar': 123, 'baz': 'abc'},
'structValues': {
'bar': {'value': 123},
'baz': {'value': 'abc'},
},
},
}
klass = self._get_target_class()
Expand All @@ -1166,13 +1196,17 @@ def test_from_api_repr_w_name(self):
def test_from_api_repr_wo_name(self):
RESOURCE = {
'parameterType': {
'type': 'STRUCT',
'structTypes': [
{'name': 'bar', 'type': 'INT64'},
{'name': 'baz', 'type': 'STRING'},
{'name': 'bar', 'type': {'type': 'INT64'}},
{'name': 'baz', 'type': {'type': 'STRING'}},
],
},
'parameterValue': {
'structValues': {'bar': 123, 'baz': 'abc'},
'structValues': {
'bar': {'value': 123},
'baz': {'value': 'abc'},
},
},
}
klass = self._get_target_class()
Expand All @@ -1185,13 +1219,17 @@ def test_to_api_repr_w_name(self):
EXPECTED = {
'name': 'foo',
'parameterType': {
'type': 'STRUCT',
'structTypes': [
{'name': 'bar', 'type': 'INT64'},
{'name': 'baz', 'type': 'STRING'},
{'name': 'bar', 'type': {'type': 'INT64'}},
{'name': 'baz', 'type': {'type': 'STRING'}},
],
},
'parameterValue': {
'structValues': {'bar': '123', 'baz': 'abc'},
'structValues': {
'bar': {'value': '123'},
'baz': {'value': 'abc'},
},
},
}
sub_1 = self._make_subparam('bar', 'INT64', 123)
Expand All @@ -1202,13 +1240,17 @@ def test_to_api_repr_w_name(self):
def test_to_api_repr_wo_name(self):
EXPECTED = {
'parameterType': {
'type': 'STRUCT',
'structTypes': [
{'name': 'bar', 'type': 'INT64'},
{'name': 'baz', 'type': 'STRING'},
{'name': 'bar', 'type': {'type': 'INT64'}},
{'name': 'baz', 'type': {'type': 'STRING'}},
],
},
'parameterValue': {
'structValues': {'bar': '123', 'baz': 'abc'},
'structValues': {
'bar': {'value': '123'},
'baz': {'value': 'abc'},
},
},
}
sub_1 = self._make_subparam('bar', 'INT64', 123)
Expand Down
3 changes: 2 additions & 1 deletion circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ general:

deployment:
release:
tag: /(([a-z]+)-)?([0-9]+)\.([0-9]+)\.([0-9]+)/
# See "scripts/circleci_tagged_pkg.py" for info on REGEX
tag: /(([a-z]+)-)*([0-9]+)\.([0-9]+)\.([0-9]+)/
owner: GoogleCloudPlatform
commands:
- pip install --upgrade twine
Expand Down
Loading

0 comments on commit 96489a1

Please sign in to comment.