Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Large number of style-related changes to pass pylint #238

Merged
merged 2 commits into from
Oct 18, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions gcloud/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
"""GCloud API access in idiomatic Python."""


__version__ = '0.02.2'
4 changes: 3 additions & 1 deletion gcloud/connection.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
""" Shared implementation of connections to API servers."""

from pkg_resources import get_distribution

import httplib2
Expand Down Expand Up @@ -28,6 +29,7 @@ def __init__(self, credentials=None):
:type credentials: :class:`oauth2client.client.OAuth2Credentials`
:param credentials: The OAuth2 Credentials to use for this connection.
"""
self._http = None
self._credentials = credentials

@property
Expand All @@ -45,7 +47,7 @@ def http(self):
:rtype: :class:`httplib2.Http`
:returns: A Http object used to transport data.
"""
if not hasattr(self, '_http'):
if self._http is None:
self._http = httplib2.Http()
if self._credentials:
self._http = self._credentials.authorize(self._http)
Expand Down
14 changes: 7 additions & 7 deletions gcloud/datastore/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
These functions are *not* part of the API.
"""
import calendar
from datetime import datetime, timedelta
import datetime

from google.protobuf.internal.type_checkers import Int64ValueChecker
import pytz
Expand Down Expand Up @@ -43,7 +43,7 @@ def _get_protobuf_attribute_and_value(val):
:returns: A tuple of the attribute name and proper value type.
"""

if isinstance(val, datetime):
if isinstance(val, datetime.datetime):
name = 'timestamp_microseconds'
# If the datetime is naive (no timezone), consider that it was
# intended to be UTC and replace the tzinfo to that effect.
Expand Down Expand Up @@ -91,8 +91,8 @@ def _get_value_from_value_pb(value_pb):
result = None
if value_pb.HasField('timestamp_microseconds_value'):
microseconds = value_pb.timestamp_microseconds_value
naive = (datetime.utcfromtimestamp(0) +
timedelta(microseconds=microseconds))
naive = (datetime.datetime.utcfromtimestamp(0) +
datetime.timedelta(microseconds=microseconds))
result = naive.replace(tzinfo=pytz.utc)

elif value_pb.HasField('key_value'):
Expand Down Expand Up @@ -163,9 +163,9 @@ def _set_protobuf_value(value_pb, val):
key = val.key()
if key is not None:
e_pb.key.CopyFrom(key.to_protobuf())
for k, v in val.items():
for item_key, value in val.iteritems():

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

p_pb = e_pb.property.add()
p_pb.name = k
_set_protobuf_value(p_pb.value, v)
p_pb.name = item_key
_set_protobuf_value(p_pb.value, value)
else: # scalar, just assign
setattr(value_pb, attr, val)
14 changes: 8 additions & 6 deletions gcloud/datastore/connection.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Connections to gcloud datastore API servers."""

from gcloud import connection
from gcloud.datastore import datastore_v1_pb2 as datastore_pb
from gcloud.datastore import _helpers
Expand All @@ -23,7 +24,7 @@ class Connection(connection.Connection):
"""A template for the URL of a particular API call."""

def __init__(self, credentials=None):
self._credentials = credentials
super(Connection, self).__init__(credentials=credentials)
self._current_transaction = None

def _request(self, dataset_id, method, data):
Expand Down Expand Up @@ -240,11 +241,12 @@ def run_query(self, dataset_id, query_pb, namespace=None):
request.query.CopyFrom(query_pb)
response = self._rpc(dataset_id, 'runQuery', request,
datastore_pb.RunQueryResponse)
return ([e.entity for e in response.batch.entity_result],
response.batch.end_cursor,
response.batch.more_results,
response.batch.skipped_results,
)
return (
[e.entity for e in response.batch.entity_result],
response.batch.end_cursor,
response.batch.more_results,
response.batch.skipped_results,
)

def lookup(self, dataset_id, key_pbs):
"""Lookup keys from a dataset in the Cloud Datastore.
Expand Down
3 changes: 3 additions & 0 deletions gcloud/datastore/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def query(self, *args, **kwargs):
:rtype: :class:`gcloud.datastore.query.Query`
:returns: a new Query instance, bound to this dataset.
"""
# This import is here to avoid circular references.
from gcloud.datastore.query import Query
kwargs['dataset'] = self
return Query(*args, **kwargs)
Expand All @@ -83,6 +84,7 @@ def entity(self, kind):
:rtype: :class:`gcloud.datastore.entity.Entity`
:returns: a new Entity instance, bound to this dataset.
"""
# This import is here to avoid circular references.
from gcloud.datastore.entity import Entity
return Entity(dataset=self, kind=kind)

Expand All @@ -96,6 +98,7 @@ def transaction(self, *args, **kwargs):
:rtype: :class:`gcloud.datastore.transaction.Transaction`
:returns: a new Transaction instance, bound to this dataset.
"""
# This import is here to avoid circular references.
from gcloud.datastore.transaction import Transaction
kwargs['dataset'] = self
return Transaction(*args, **kwargs)
Expand Down
8 changes: 5 additions & 3 deletions gcloud/datastore/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class NoKey(RuntimeError):
"""Exception raised by Entity methods which require a key."""


class Entity(dict): # pylint: disable=too-many-public-methods
class Entity(dict):
""":type dataset: :class:`gcloud.datastore.dataset.Dataset`
:param dataset: The dataset in which this entity belongs.

Expand Down Expand Up @@ -95,7 +95,9 @@ def key(self, key=None):
:type key: :class:`glcouddatastore.key.Key`
:param key: The key you want to set on the entity.

:returns: Either the current key or the :class:`Entity`.
:rtype: :class:`gcloud.datastore.key.Key` or :class:`Entity`.
:returns: Either the current key (on get) or the current
object (on set).

>>> entity.key(my_other_key) # This returns the original entity.
<Entity[{'kind': 'OtherKeyKind', 'id': 1234}] {'property': 'value'}>
Expand Down Expand Up @@ -141,7 +143,7 @@ def from_key(cls, key):
return cls().key(key)

@classmethod
def from_protobuf(cls, pb, dataset=None): # pylint: disable=invalid-name
def from_protobuf(cls, pb, dataset=None):

This comment was marked as spam.

This comment was marked as spam.

"""Factory method for creating an entity based on a protobuf.

The protobuf should be one returned from the Cloud Datastore
Expand Down
14 changes: 7 additions & 7 deletions gcloud/datastore/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def is_partial(self):
:returns: True if the last element of the key's path does not have
an 'id' or a 'name'.
"""
return (self.id_or_name() is None)
return self.id_or_name() is None

def dataset(self, dataset=None):
"""Dataset setter / getter.
Expand Down Expand Up @@ -231,19 +231,19 @@ def kind(self, kind=None):
elif self.path():
return self._path[-1]['kind']

def id(self, id=None):
def id(self, id_to_set=None):
"""ID setter / getter. Based on the last element of path.

:type kind: :class:`str`
:param kind: The new kind for the key.
:type id_to_set: :class:`int`
:param id_to_set: The new ID for the key.

:rtype: :class:`Key` (for setter); or :class:`int` (for getter)
:returns: a new key, cloned from self., with the given id (setter);
or self's id (getter).
or self's id (getter).
"""
if id:
if id_to_set:
clone = self._clone()
clone._path[-1]['id'] = id
clone._path[-1]['id'] = id_to_set
return clone
elif self.path():
return self._path[-1].get('id')
Expand Down
14 changes: 6 additions & 8 deletions gcloud/datastore/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,11 +315,9 @@ def fetch(self, limit=None):
if limit:
clone = self.limit(limit)

(entity_pbs,
end_cursor,
more_results,
skipped_results) = self.dataset().connection().run_query(
query_results = self.dataset().connection().run_query(
query_pb=clone.to_protobuf(), dataset_id=self.dataset().id())
entity_pbs, end_cursor = query_results[:2]

self._cursor = end_cursor
return [Entity.from_protobuf(entity, dataset=self.dataset())
Expand Down Expand Up @@ -379,14 +377,14 @@ def order(self, *properties):
"""
clone = self._clone()

for p in properties:
for prop in properties:
property_order = clone._pb.order.add()

if p.startswith('-'):
property_order.property.name = p[1:]
if prop.startswith('-'):
property_order.property.name = prop[1:]
property_order.direction = property_order.DESCENDING
else:
property_order.property.name = p
property_order.property.name = prop
property_order.direction = property_order.ASCENDING

return clone
22 changes: 12 additions & 10 deletions gcloud/datastore/test___init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ def test_it(self):
found = self._callFUT(CLIENT_EMAIL, f.name)
self.assertTrue(isinstance(found, Connection))
self.assertTrue(found._credentials is client._signed)
self.assertEqual(client._called_with,
{'service_account_name': CLIENT_EMAIL,
'private_key': PRIVATE_KEY,
'scope': SCOPE,
})
expected_called_with = {
'service_account_name': CLIENT_EMAIL,
'private_key': PRIVATE_KEY,
'scope': SCOPE,
}
self.assertEqual(client._called_with, expected_called_with)


class Test_get_dataset(unittest2.TestCase):
Expand Down Expand Up @@ -59,8 +60,9 @@ def test_it(self):
self.assertTrue(isinstance(found, Dataset))
self.assertTrue(isinstance(found.connection(), Connection))
self.assertEqual(found.id(), DATASET_ID)
self.assertEqual(client._called_with,
{'service_account_name': CLIENT_EMAIL,
'private_key': PRIVATE_KEY,
'scope': SCOPE,
})
expected_called_with = {
'service_account_name': CLIENT_EMAIL,
'private_key': PRIVATE_KEY,
'scope': SCOPE,
}
self.assertEqual(client._called_with, expected_called_with)
2 changes: 1 addition & 1 deletion gcloud/datastore/test__helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def test_unknown(self):
from gcloud.datastore.datastore_v1_pb2 import Value

pb = Value()
self.assertEqual(self._callFUT(pb), None) # XXX desirable?
self.assertEqual(self._callFUT(pb), None)


class Test__get_value_from_property_pb(unittest2.TestCase):
Expand Down
Loading