-
Notifications
You must be signed in to change notification settings - Fork 182
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
Add Support for Python 3 #379
Changes from 3 commits
d0fbfc0
ba46b57
97511cd
5b006f7
533aea5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -104,13 +104,17 @@ def next(self): | |
else: | ||
if self.rehearse: | ||
gc.collect() | ||
print ("-" * 59) | ||
print("-" * 59) | ||
print() | ||
print_header() | ||
|
||
self.count -= 1 | ||
return self | ||
|
||
def __next__(self): | ||
# Python 3.x Version | ||
self.next() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should probably |
||
|
||
def report(self, name): | ||
""" | ||
Returns a report for the current step of the benchmark. | ||
|
@@ -124,22 +128,25 @@ def print_rehearsal_header(): | |
Prints the header for the rehearsal phase of a benchmark. | ||
""" | ||
print "Rehearsal -------------------------------------------------" | ||
print("Rehearsal -------------------------------------------------") | ||
|
||
|
||
def print_report(label, user, system, real): | ||
""" | ||
Prints the report of one step of a benchmark. | ||
""" | ||
print "{:<12s} {:12f} {:12f} ( {:12f} )".format(label, user, system, real) | ||
print("{:<12s} {:12f} {:12f} ( {:12f} )".format(label, | ||
user, | ||
system, | ||
real)) | ||
|
||
|
||
def print_header(): | ||
""" | ||
Prints the header for the normal phase of a benchmark. | ||
""" | ||
print "{:<12s} {:<12s} {:<12s} ( {:<12s} )"\ | ||
.format('', 'user', 'system', 'real') | ||
print("{:<12s} {:<12s} {:<12s} ( {:<12s} )" | ||
.format('', 'user', 'system', 'real')) | ||
|
||
|
||
class BenchmarkReport(object): | ||
|
@@ -164,5 +171,5 @@ def __exit__(self, exc_type, exc_val, exc_tb): | |
elif exc_type is KeyboardInterrupt: | ||
return False | ||
else: | ||
print "EXCEPTION! %r" % ((exc_type, exc_val, exc_tb),) | ||
print("EXCEPTION! %r" % ((exc_type, exc_val, exc_tb),)) | ||
return True |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,15 +34,46 @@ | |
from riak.transports.http import RiakHttpPool | ||
from riak.transports.pbc import RiakPbcPool | ||
from riak.security import SecurityCreds | ||
from riak.util import lazy_property | ||
from riak.util import lazy_property, bytes_to_str, str_to_bytes | ||
from six import string_types, PY2 | ||
|
||
|
||
def default_encoder(obj): | ||
""" | ||
Default encoder for JSON datatypes, which returns UTF-8 encoded | ||
json instead of the default bloated \uXXXX escaped ASCII strings. | ||
json instead of the default bloated backslash u XXXX escaped ASCII strings. | ||
""" | ||
return json.dumps(obj, ensure_ascii=False).encode("utf-8") | ||
if type(obj) == bytes: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not |
||
return json.dumps(bytes_to_str(obj), | ||
ensure_ascii=False).encode("utf-8") | ||
else: | ||
return json.dumps(obj, ensure_ascii=False).encode("utf-8") | ||
|
||
|
||
def binary_json_encoder(obj): | ||
""" | ||
Default encoder for JSON datatypes, which returns UTF-8 encoded | ||
json instead of the default bloated backslash u XXXX escaped ASCII strings. | ||
""" | ||
if type(obj) == bytes: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same question here. |
||
return json.dumps(bytes_to_str(obj), | ||
ensure_ascii=False).encode("utf-8") | ||
else: | ||
return json.dumps(obj, ensure_ascii=False).encode("utf-8") | ||
|
||
|
||
def binary_json_decoder(obj): | ||
""" | ||
Default decoder from JSON datatypes. | ||
""" | ||
return json.loads(bytes_to_str(obj)) | ||
|
||
|
||
def binary_encoder_decoder(obj): | ||
""" | ||
Assumes value is already in binary format, so passes unchanged. | ||
""" | ||
return obj | ||
|
||
|
||
class RiakClient(RiakMapReduceChain, RiakClientOperations): | ||
|
@@ -90,12 +121,22 @@ def __init__(self, protocol='pbc', transport_options={}, nodes=None, | |
self._http_pool = RiakHttpPool(self, **transport_options) | ||
self._pb_pool = RiakPbcPool(self, **transport_options) | ||
|
||
self._encoders = {'application/json': default_encoder, | ||
'text/json': default_encoder, | ||
'text/plain': str} | ||
self._decoders = {'application/json': json.loads, | ||
'text/json': json.loads, | ||
'text/plain': str} | ||
if PY2: | ||
self._encoders = {'application/json': default_encoder, | ||
'text/json': default_encoder, | ||
'text/plain': str} | ||
self._decoders = {'application/json': json.loads, | ||
'text/json': json.loads, | ||
'text/plain': str} | ||
else: | ||
self._encoders = {'application/json': binary_json_encoder, | ||
'text/json': binary_json_encoder, | ||
'text/plain': str_to_bytes, | ||
'binary/octet-stream': binary_encoder_decoder} | ||
self._decoders = {'application/json': binary_json_decoder, | ||
'text/json': binary_json_decoder, | ||
'text/plain': bytes_to_str, | ||
'binary/octet-stream': binary_encoder_decoder} | ||
self._buckets = WeakValueDictionary() | ||
self._bucket_types = WeakValueDictionary() | ||
|
||
|
@@ -167,7 +208,7 @@ def set_encoder(self, content_type, encoder): | |
:param content_type: the requested media type | ||
:type content_type: str | ||
:param encoder: an encoding function, takes a single object | ||
argument and returns a string | ||
argument and returns encoded data | ||
:type encoder: function | ||
""" | ||
self._encoders[content_type] = encoder | ||
|
@@ -188,7 +229,7 @@ def set_decoder(self, content_type, decoder): | |
|
||
:param content_type: the requested media type | ||
:type content_type: str | ||
:param decoder: a decoding function, takes a string and | ||
:param decoder: a decoding function, takes encoded data and | ||
returns a Python type | ||
:type decoder: function | ||
""" | ||
|
@@ -217,10 +258,10 @@ def bucket(self, name, bucket_type='default'): | |
:rtype: :class:`RiakBucket <riak.bucket.RiakBucket>` | ||
|
||
""" | ||
if not isinstance(name, basestring): | ||
if not isinstance(name, string_types): | ||
raise TypeError('Bucket name must be a string') | ||
|
||
if isinstance(bucket_type, basestring): | ||
if isinstance(bucket_type, string_types): | ||
bucket_type = self.bucket_type(bucket_type) | ||
elif not isinstance(bucket_type, BucketType): | ||
raise TypeError('bucket_type must be a string ' | ||
|
@@ -243,7 +284,7 @@ def bucket_type(self, name): | |
:type name: str | ||
:rtype: :class:`BucketType <riak.bucket.BucketType>` | ||
""" | ||
if not isinstance(name, basestring): | ||
if not isinstance(name, string_types): | ||
raise TypeError('Bucket name must be a string') | ||
|
||
if name in self._bucket_types: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For safety, shouldn't the Python 2 version
from __future__ import print_function
?