Skip to content

Commit e544d27

Browse files
committed
Selectively apply patches suggested by 'python-modernize --six-unicode'.
1 parent 7f2d6e1 commit e544d27

18 files changed

Lines changed: 137 additions & 118 deletions

gcloud/datastore/connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ def save_entity(self, dataset_id, key_pb, properties,
406406

407407
insert.key.CopyFrom(key_pb)
408408

409-
for name, value in properties.iteritems():
409+
for name, value in properties.items():
410410
prop = insert.property.add()
411411
# Set the name of the property.
412412
prop.name = name

gcloud/datastore/demo/demo.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,47 +16,47 @@
1616
toy.save()
1717

1818
# If we look it up by its key, we should find it...
19-
print dataset.get_entities([toy.key()])
19+
print(dataset.get_entities([toy.key()]))
2020

2121
# And we should be able to delete it...
2222
toy.delete()
2323

2424
# Since we deleted it, if we do another lookup it shouldn't be there again:
25-
print dataset.get_entities([toy.key()])
25+
print(dataset.get_entities([toy.key()]))
2626

2727
# Now let's try a more advanced query.
2828
# We'll start by look at all Thing entities:
2929
query = dataset.query().kind('Thing')
3030

3131
# Let's look at the first two.
32-
print query.limit(2).fetch()
32+
print(query.limit(2).fetch())
3333

3434
# Now let's check for Thing entities named 'Computer'
35-
print query.filter('name =', 'Computer').fetch()
35+
print(query.filter('name =', 'Computer').fetch())
3636

3737
# If you want to filter by multiple attributes,
3838
# you can string .filter() calls together.
39-
print query.filter('name =', 'Computer').filter('age =', 10).fetch()
39+
print(query.filter('name =', 'Computer').filter('age =', 10).fetch())
4040

4141
# You can also work inside a transaction.
4242
# (Check the official docs for explanations of what's happening here.)
4343
with dataset.transaction():
44-
print 'Creating and savng an entity...'
44+
print('Creating and savng an entity...')
4545
thing = dataset.entity('Thing')
4646
thing.key(thing.key().name('foo'))
4747
thing['age'] = 10
4848
thing.save()
4949

50-
print 'Creating and saving another entity...'
50+
print('Creating and saving another entity...')
5151
thing2 = dataset.entity('Thing')
5252
thing2.key(thing2.key().name('bar'))
5353
thing2['age'] = 15
5454
thing2.save()
5555

56-
print 'Committing the transaction...'
56+
print('Committing the transaction...')
5757

5858
# Now that the transaction is commited, let's delete the entities.
59-
print thing.delete(), thing2.delete()
59+
print(thing.delete(), thing2.delete())
6060

6161
# To rollback a transaction, just call .rollback()
6262
with dataset.transaction() as t:
@@ -67,16 +67,16 @@
6767

6868
# Let's check if the entity was actually created:
6969
created = dataset.get_entities([thing.key()])
70-
print 'yes' if created else 'no'
70+
print('yes' if created else 'no')
7171

7272
# Remember, a key won't be complete until the transaction is commited.
7373
# That is, while inside the transaction block, thing.key() will be incomplete.
7474
with dataset.transaction():
7575
thing = dataset.entity('Thing')
7676
thing.save()
77-
print thing.key() # This will be partial
77+
print(thing.key()) # This will be partial
7878

79-
print thing.key() # This will be complete
79+
print(thing.key()) # This will be complete
8080

8181
# Now let's delete the entity.
8282
thing.delete()

gcloud/datastore/helpers.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
The non-private functions are part of the API.
44
"""
5+
import six
56
__all__ = ('entity_from_protobuf', 'key_from_protobuf')
67

78
import calendar
@@ -117,17 +118,17 @@ def _pb_attr_value(val):
117118
# Regardless of what timezone is on the value, convert it to UTC.
118119
val = val.astimezone(pytz.utc)
119120
# Convert the datetime to a microsecond timestamp.
120-
value = long(calendar.timegm(val.timetuple()) * 1e6) + val.microsecond
121+
value = int(calendar.timegm(val.timetuple()) * 1e6) + val.microsecond
121122
elif isinstance(val, Key):
122123
name, value = 'key', val.to_protobuf()
123124
elif isinstance(val, bool):
124125
name, value = 'boolean', val
125126
elif isinstance(val, float):
126127
name, value = 'double', val
127-
elif isinstance(val, (int, long)):
128+
elif isinstance(val, six.integer_types):
128129
INT_VALUE_CHECKER.CheckValue(val) # Raise an exception if invalid.
129-
name, value = 'integer', long(val) # Always cast to a long.
130-
elif isinstance(val, unicode):
130+
name, value = 'integer', int(val) # Always cast to an integer.
131+
elif isinstance(val, six.text_type):
131132
name, value = 'string', val
132133
elif isinstance(val, (bytes, str)):
133134
name, value = 'blob', val
@@ -239,7 +240,7 @@ def _set_protobuf_value(value_pb, val):
239240
key = val.key()
240241
if key is not None:
241242
e_pb.key.CopyFrom(key.to_protobuf())
242-
for item_key, value in val.iteritems():
243+
for item_key, value in val.items():
243244
p_pb = e_pb.property.add()
244245
p_pb.name = item_key
245246
_set_protobuf_value(p_pb.value, value)

gcloud/datastore/key.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import copy
44
from itertools import izip
55

6+
import six
7+
68
from gcloud.datastore import datastore_v1_pb2 as datastore_pb
79

810

@@ -100,7 +102,7 @@ def from_path(cls, *args, **kwargs):
100102

101103
for kind, id_or_name in izip(items, items):
102104
entry = {'kind': kind}
103-
if isinstance(id_or_name, basestring):
105+
if isinstance(id_or_name, six.string_types):
104106
entry['name'] = id_or_name
105107
else:
106108
entry['id'] = id_or_name

gcloud/datastore/test_connection.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import unittest2
2+
import six
23

34

45
class TestConnection(unittest2.TestCase):
@@ -439,7 +440,7 @@ def test_commit_wo_transaction(self):
439440
insert.key.CopyFrom(key_pb)
440441
prop = insert.property.add()
441442
prop.name = 'foo'
442-
prop.value.string_value = u'Foo'
443+
prop.value.string_value = six.u('Foo')
443444
conn = self._makeOne()
444445
URI = '/'.join([
445446
conn.API_BASE_URL,
@@ -481,7 +482,7 @@ def id(self):
481482
insert.key.CopyFrom(key_pb)
482483
prop = insert.property.add()
483484
prop.name = 'foo'
484-
prop.value.string_value = u'Foo'
485+
prop.value.string_value = six.u('Foo')
485486
conn = self._makeOne()
486487
conn.transaction(Xact())
487488
URI = '/'.join([
@@ -643,7 +644,7 @@ def test_save_entity_wo_transaction_w_upsert(self):
643644
'commit',
644645
])
645646
http = conn._http = Http({'status': '200'}, rsp_pb.SerializeToString())
646-
result = conn.save_entity(DATASET_ID, key_pb, {'foo': u'Foo'})
647+
result = conn.save_entity(DATASET_ID, key_pb, {'foo': six.u('Foo')})
647648
self.assertEqual(result, True)
648649
cw = http._called_with
649650
self.assertEqual(cw['uri'], URI)
@@ -664,7 +665,7 @@ def test_save_entity_wo_transaction_w_upsert(self):
664665
props = list(upsert.property)
665666
self.assertEqual(len(props), 1)
666667
self.assertEqual(props[0].name, 'foo')
667-
self.assertEqual(props[0].value.string_value, u'Foo')
668+
self.assertEqual(props[0].value.string_value, six.u('Foo'))
668669
self.assertEqual(props[0].value.indexed, True)
669670
self.assertEqual(len(mutation.delete), 0)
670671
self.assertEqual(request.mode, rq_class.NON_TRANSACTIONAL)
@@ -686,7 +687,7 @@ def test_save_entity_w_exclude_from_indexes(self):
686687
'commit',
687688
])
688689
http = conn._http = Http({'status': '200'}, rsp_pb.SerializeToString())
689-
result = conn.save_entity(DATASET_ID, key_pb, {'foo': u'Foo'},
690+
result = conn.save_entity(DATASET_ID, key_pb, {'foo': six.u('Foo')},
690691
exclude_from_indexes=['foo'])
691692
self.assertEqual(result, True)
692693
cw = http._called_with
@@ -708,7 +709,7 @@ def test_save_entity_w_exclude_from_indexes(self):
708709
props = list(upsert.property)
709710
self.assertEqual(len(props), 1)
710711
self.assertEqual(props[0].name, 'foo')
711-
self.assertEqual(props[0].value.string_value, u'Foo')
712+
self.assertEqual(props[0].value.string_value, six.u('Foo'))
712713
self.assertEqual(props[0].value.indexed, False)
713714
self.assertEqual(len(mutation.delete), 0)
714715
self.assertEqual(request.mode, rq_class.NON_TRANSACTIONAL)
@@ -735,7 +736,7 @@ def test_save_entity_wo_transaction_w_auto_id(self):
735736
'commit',
736737
])
737738
http = conn._http = Http({'status': '200'}, rsp_pb.SerializeToString())
738-
result = conn.save_entity(DATASET_ID, key_pb, {'foo': u'Foo'})
739+
result = conn.save_entity(DATASET_ID, key_pb, {'foo': six.u('Foo')})
739740
self.assertEqual(result, updated_key_pb)
740741
cw = http._called_with
741742
self.assertEqual(cw['uri'], URI)
@@ -754,7 +755,7 @@ def test_save_entity_wo_transaction_w_auto_id(self):
754755
props = list(insert.property)
755756
self.assertEqual(len(props), 1)
756757
self.assertEqual(props[0].name, 'foo')
757-
self.assertEqual(props[0].value.string_value, u'Foo')
758+
self.assertEqual(props[0].value.string_value, six.u('Foo'))
758759
self.assertEqual(len(inserts), 1)
759760
upserts = list(mutation.upsert)
760761
self.assertEqual(len(upserts), 0)
@@ -776,7 +777,7 @@ def mutation(self):
776777
conn = self._makeOne()
777778
conn.transaction(Xact())
778779
http = conn._http = Http({'status': '200'}, rsp_pb.SerializeToString())
779-
result = conn.save_entity(DATASET_ID, key_pb, {'foo': u'Foo'})
780+
result = conn.save_entity(DATASET_ID, key_pb, {'foo': six.u('Foo')})
780781
self.assertEqual(result, True)
781782
self.assertEqual(http._called_with, None)
782783
mutation = conn.mutation()
@@ -794,7 +795,7 @@ def mutation(self):
794795
return mutation
795796
DATASET_ID = 'DATASET'
796797
nested = Entity()
797-
nested['bar'] = u'Bar'
798+
nested['bar'] = six.u('Bar')
798799
key_pb = Key(path=[{'kind': 'Kind', 'id': 1234}]).to_protobuf()
799800
rsp_pb = datastore_pb.CommitResponse()
800801
conn = self._makeOne()

gcloud/datastore/test_helpers.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import unittest2
2+
import six
23

34

45
class Test_entity_from_protobuf(unittest2.TestCase):
@@ -200,9 +201,9 @@ def test_bytes(self):
200201
self.assertEqual(value, b'bytes')
201202

202203
def test_unicode(self):
203-
name, value = self._callFUT(u'str')
204+
name, value = self._callFUT(six.u('str'))
204205
self.assertEqual(name, 'string_value')
205-
self.assertEqual(value, u'str')
206+
self.assertEqual(value, six.u('str'))
206207

207208
def test_entity(self):
208209
from gcloud.datastore.entity import Entity
@@ -276,8 +277,8 @@ def test_bytes(self):
276277
self.assertEqual(self._callFUT(pb), b'str')
277278

278279
def test_unicode(self):
279-
pb = self._makePB('string_value', u'str')
280-
self.assertEqual(self._callFUT(pb), u'str')
280+
pb = self._makePB('string_value', six.u('str'))
281+
self.assertEqual(self._callFUT(pb), six.u('str'))
281282

282283
def test_entity(self):
283284
from gcloud.datastore.datastore_v1_pb2 import Value
@@ -375,9 +376,9 @@ def test_none(self):
375376
self._callFUT(pb, (1 << 63) - 1)
376377
self._callFUT(pb, 'str')
377378
self._callFUT(pb, b'str')
378-
self._callFUT(pb, u'str')
379+
self._callFUT(pb, six.u('str'))
379380
self._callFUT(pb, entity)
380-
self._callFUT(pb, [u'a', 0, 3.14])
381+
self._callFUT(pb, [six.u('a'), 0, 3.14])
381382

382383
self._callFUT(pb, None)
383384
self.assertEqual(len(pb.ListFields()), 0)
@@ -421,9 +422,9 @@ def test_bytes(self):
421422

422423
def test_unicode(self):
423424
pb = self._makePB()
424-
self._callFUT(pb, u'str')
425+
self._callFUT(pb, six.u('str'))
425426
value = pb.string_value
426-
self.assertEqual(value, u'str')
427+
self.assertEqual(value, six.u('str'))
427428

428429
def test_entity_empty_wo_key(self):
429430
from gcloud.datastore.entity import Entity
@@ -443,18 +444,18 @@ def test_entity_w_key(self):
443444
pb = self._makePB()
444445
key = Key(path=[{'kind': 'KIND', 'id': 123}])
445446
entity = Entity().key(key)
446-
entity['foo'] = u'Foo'
447+
entity['foo'] = six.u('Foo')
447448
self._callFUT(pb, entity)
448449
value = pb.entity_value
449450
self.assertEqual(value.key, key.to_protobuf())
450451
props = list(value.property)
451452
self.assertEqual(len(props), 1)
452453
self.assertEqual(props[0].name, 'foo')
453-
self.assertEqual(props[0].value.string_value, u'Foo')
454+
self.assertEqual(props[0].value.string_value, six.u('Foo'))
454455

455456
def test_list(self):
456457
pb = self._makePB()
457-
values = [u'a', 0, 3.14]
458+
values = [six.u('a'), 0, 3.14]
458459
self._callFUT(pb, values)
459460
marshalled = pb.list_value
460461
self.assertEqual(len(marshalled), len(values))

0 commit comments

Comments
 (0)