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

1834226 python 3.9 nydus SyntaxWarning #44

Draft
wants to merge 17 commits into
base: master
Choose a base branch
from
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
*.egg-info
*.gz
*.egg
.tox
.coverage
.idea
.tox
/dist
/build
/cover
/htmlcov
/htmlcov
/env
8 changes: 4 additions & 4 deletions benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def test_redis_normal(cluster):

def test_redis_map(cluster):
with cluster.map() as conn:
for n in xrange(5):
for n in range(5):
conn.set('foo', 'bar')
conn.get('foo')
conn.set('biz', 'bar')
Expand All @@ -56,12 +56,12 @@ def test_redis_map(cluster):
def main(iterations=1000):
for cluster in ('partition_cluster', 'ketama_cluster', 'roundrobin_cluster'):
for func in ('test_redis_normal', 'test_redis_map'):
print "Running %r on %r" % (func, cluster)
print("Running %r on %r" % (func, cluster))
s = time.time()
for x in xrange(iterations):
for x in range(iterations):
globals()[func](globals()[cluster])
t = (time.time() - s) * 1000
print " %.3fms per iteration" % (t / iterations,)
print(" %.3fms per iteration" % (t / iterations,))


if __name__ == '__main__':
Expand Down
3 changes: 2 additions & 1 deletion nydus/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
"""

import warnings
import six

CONNECTIONS = {}


def configure(kwargs):
for k, v in kwargs.iteritems():
for k, v in six.iteritems(kwargs):
if k.upper() != k:
warnings.warn('Invalid setting, \'%s\' which is not defined by Nydus' % k)
elif k not in globals():
Expand Down
29 changes: 17 additions & 12 deletions nydus/contrib/ketama.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
Rewrited from the original source: http://www.audioscrobbler.net/development/ketama/

"""
from __future__ import print_function

__author__ = "Andrey Nikishaev"
__email__ = "[email protected]"
__version__ = 0.1
Expand All @@ -15,6 +17,9 @@
import math
from bisect import bisect

import six
from six.moves import range


class Ketama(object):

Expand Down Expand Up @@ -45,10 +50,10 @@ def _build_circle(self):

ks = math.floor((40 * len(self._nodes) * weight) / total_weight)

for i in xrange(0, int(ks)):
for i in range(0, int(ks)):
b_key = self._md5_digest('%s-%s-salt' % (node, i))

for l in xrange(0, 4):
for l in range(0, 4):
key = ((b_key[3 + l * 4] << 24)
| (b_key[2 + l * 4] << 16)
| (b_key[1 + l * 4] << 8)
Expand Down Expand Up @@ -90,7 +95,7 @@ def _hashi(self, b_key, fn):
| b_key[fn(0)])

def _md5_digest(self, key):
return map(ord, hashlib.md5(key).digest())
return list(six.iterbytes(hashlib.md5(key.encode('utf-8')).digest()))

def remove_node(self, node):
"""
Expand Down Expand Up @@ -130,18 +135,18 @@ def get_node(self, key):
if __name__ == '__main__':
def test(k):
data = {}
for i in xrange(REQUESTS):
for i in range(REQUESTS):
tower = k.get_node('a' + str(i))
data.setdefault(tower, 0)
data[tower] += 1
print 'Number of caches on each node: '
print data
print ''

print k.get_node('Aplple')
print k.get_node('Hello')
print k.get_node('Data')
print k.get_node('Computer')
print('Number of caches on each node: ')
print(data)
print('')

print(k.get_node('Aplple'))
print(k.get_node('Hello'))
print(k.get_node('Data'))
print(k.get_node('Computer'))

NODES = [
'192.168.0.1:6000', '192.168.0.1:6001', '192.168.0.1:6002',
Expand Down
8 changes: 5 additions & 3 deletions nydus/db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
from nydus.db.routers.base import BaseRouter
from nydus.utils import import_string, apply_defaults

import six


def create_cluster(settings):
"""
Expand All @@ -49,7 +51,7 @@ def create_cluster(settings):
# Pull in our client
settings = copy.deepcopy(settings)
backend = settings.pop('engine', settings.pop('backend', None))
if isinstance(backend, basestring):
if isinstance(backend, six.string_types):
Conn = import_string(backend)
elif backend:
Conn = backend
Expand All @@ -60,7 +62,7 @@ def create_cluster(settings):
cluster = settings.pop('cluster', None)
if not cluster:
Cluster = Conn.get_cluster()
elif isinstance(cluster, basestring):
elif isinstance(cluster, six.string_types):
Cluster = import_string(cluster)
else:
Cluster = cluster
Expand All @@ -69,7 +71,7 @@ def create_cluster(settings):
router = settings.pop('router', None)
if not router:
Router = BaseRouter
elif isinstance(router, basestring):
elif isinstance(router, six.string_types):
Router = import_string(router)
else:
Router = router
Expand Down
8 changes: 3 additions & 5 deletions nydus/db/backends/memcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
:copyright: (c) 2012 DISQUS.
:license: Apache License 2.0, see LICENSE for more details.
"""

from __future__ import absolute_import

import pylibmc
from six.moves import zip

from itertools import izip
from nydus.db.backends import BaseConnection, BasePipeline
from nydus.db.promise import EventualCommand
from nydus.utils import peek
Expand All @@ -21,8 +20,7 @@ class Memcache(BaseConnection):
retryable_exceptions = frozenset([pylibmc.Error])
supports_pipelines = True

def __init__(self, num, host='localhost', port=11211, binary=True,
behaviors=None, **options):
def __init__(self, num, host='localhost', port=11211, binary=True, behaviors=None):
self.host = host
self.port = port
self.binary = binary
Expand Down Expand Up @@ -197,7 +195,7 @@ def resolve_grouped_commands(grouped, connection):
for command in grouped_commands:
results[command] = result.get(command.get_args()[0])
else:
for command, value in izip(grouped_commands, result):
for command, value in zip(grouped_commands, result):
results[command] = value

return results
2 changes: 1 addition & 1 deletion nydus/db/backends/pycassa.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def __init__(self, hosts=None, keyspace=None, backend=Pycassa, **kwargs):
assert isinstance(hosts, collections.Iterable), 'hosts must be an iterable'
assert keyspace, 'keyspace must be set'

return super(PycassaCluster, self).__init__(
super(PycassaCluster, self).__init__(
hosts={
0: {
'hosts': hosts,
Expand Down
14 changes: 7 additions & 7 deletions nydus/db/backends/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@
:copyright: (c) 2011-2012 DISQUS.
:license: Apache License 2.0, see LICENSE for more details.
"""

from __future__ import absolute_import

from itertools import izip
from redis import Redis as RedisClient, StrictRedis
from redis import ConnectionError, InvalidResponse
from six.moves import zip

from nydus.db.backends import BaseConnection, BasePipeline


class RedisPipeline(BasePipeline):

def __init__(self, connection):
self.pending = []
self.connection = connection
super(RedisPipeline, self).__init__(connection)
self.pipe = connection.pipeline()

def add(self, command):
Expand All @@ -28,7 +27,7 @@ def add(self, command):
getattr(self.pipe, name)(*args, **kwargs)

def execute(self):
return dict(izip(self.pending, self.pipe.execute()))
return dict(zip(self.pending, self.pipe.execute()))


class Redis(BaseConnection):
Expand All @@ -38,13 +37,14 @@ class Redis(BaseConnection):

def __init__(self, num, host='localhost', port=6379, db=0, timeout=None,
password=None, unix_socket_path=None, identifier=None,
strict=True):
strict=True, ssl=False):
self.host = host
self.port = port
self.db = db
self.unix_socket_path = unix_socket_path
self.timeout = timeout
self.strict = strict
self.ssl = ssl
self.__identifier = identifier
self.__password = password
super(Redis, self).__init__(num)
Expand All @@ -65,7 +65,7 @@ def connect(self):
return cls(
host=self.host, port=self.port, db=self.db,
socket_timeout=self.timeout, password=self.__password,
unix_socket_path=self.unix_socket_path)
unix_socket_path=self.unix_socket_path, ssl=self.ssl)

def disconnect(self):
self.connection.disconnect()
Expand Down
5 changes: 2 additions & 3 deletions nydus/db/backends/riak.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@
from __future__ import absolute_import

import socket
import httplib

from riak import RiakClient, RiakError
from six.moves import http_client

from nydus.db.backends import BaseConnection


class Riak(BaseConnection):
# Exceptions that can be retried by this backend
retryable_exceptions = frozenset([socket.error, httplib.HTTPException, RiakError])
retryable_exceptions = frozenset([socket.error, http_client.HTTPException, RiakError])
supports_pipelines = False

def __init__(self, num, host='127.0.0.1', port=8098, prefix='riak', mapred_prefix='mapred', client_id=None,
Expand Down
19 changes: 11 additions & 8 deletions nydus/db/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@
from nydus.db.routers import BaseRouter, routing_params
from nydus.utils import apply_defaults

import six
from six.moves import range


def iter_hosts(hosts):
# this can either be a dictionary (with the key acting as the numeric
# index) or it can be a sorted list.
if isinstance(hosts, collections.Mapping):
return hosts.iteritems()
return six.iteritems(hosts)
return enumerate(hosts)


Expand Down Expand Up @@ -59,7 +62,7 @@ def __getattr__(self, name):
return CallProxy(self, name)

def __iter__(self):
for name in self.hosts.iterkeys():
for name in list(six.iterkeys(self.hosts)):
yield name

def install_router(self, router):
Expand All @@ -70,13 +73,13 @@ def execute(self, path, args, kwargs):

results = []
for conn in connections:
for retry in xrange(self.max_connection_retries):
for retry in range(self.max_connection_retries):
func = conn
for piece in path.split('.'):
func = getattr(func, piece)
try:
results.append(func(*args, **kwargs))
except tuple(conn.retryable_exceptions), e:
except tuple(conn.retryable_exceptions) as e:
if not self.router.retryable:
raise e
elif retry == self.max_connection_retries - 1:
Expand All @@ -94,7 +97,7 @@ def execute(self, path, args, kwargs):

def disconnect(self):
"""Disconnects all connections in cluster"""
for connection in self.hosts.itervalues():
for connection in six.itervalues(self.hosts):
connection.disconnect()

def get_conn(self, *args, **kwargs):
Expand All @@ -107,7 +110,7 @@ def get_conn(self, *args, **kwargs):
"""
connections = self.__connections_for('get_conn', args=args, kwargs=kwargs)

if len(connections) is 1:
if len(connections) == 1:
return connections[0]
else:
return connections
Expand Down Expand Up @@ -155,11 +158,11 @@ def is_ready(self):
def reload(self):
from nydus.db import create_cluster

for conn_alias, conn_settings in self.conf_callback().iteritems():
for conn_alias, conn_settings in six.iteritems(self.conf_callback()):
self[conn_alias] = create_cluster(conn_settings)
self._is_ready = True

def disconnect(self):
"""Disconnects all connections in cluster"""
for connection in self.itervalues():
for connection in six.itervalues(self):
connection.disconnect()
Loading