Skip to content

Commit

Permalink
Merge pull request #47 from msabramo/pep8
Browse files Browse the repository at this point in the history
pep8 cleanup
  • Loading branch information
Sean Reifschneider committed Jun 10, 2014
2 parents 918e88c + 7272650 commit 2251916
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 52 deletions.
76 changes: 43 additions & 33 deletions memcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,16 @@

from __future__ import print_function

import sys
import socket
import time
import binascii
import os
import pickle
import re
import socket
import sys
import threading
import time
import zlib

import six


Expand Down Expand Up @@ -514,7 +515,9 @@ def _deletetouch(self, expected, cmd, key, time=0):
return 0

def incr(self, key, delta=1):
"""Sends a command to the server to atomically increment the
"""Increment value for C{key} by C{delta}
Sends a command to the server to atomically increment the
value for C{key} by C{delta}, or by 1 if C{delta} is
unspecified. Returns None if C{key} doesn't exist on server,
otherwise it returns the new value after incrementing.
Expand Down Expand Up @@ -542,7 +545,9 @@ def incr(self, key, delta=1):
return self._incrdecr("incr", key, delta)

def decr(self, key, delta=1):
"""Like L{incr}, but decrements. Unlike L{incr}, underflow is
"""Decrement value for C{key} by C{delta}
Like L{incr}, but decrements. Unlike L{incr}, underflow is
checked and new values are capped at 0. If server value is 1,
a decrement of 2 returns 0, not -1.
Expand Down Expand Up @@ -651,7 +656,9 @@ def set(self, key, val, time=0, min_compress_len=0):
return self._set("set", key, val, time, min_compress_len)

def cas(self, key, val, time=0, min_compress_len=0):
'''Sets a key to a given value in the memcache if it hasn't been
'''Check and set (CAS)
Sets a key to a given value in the memcache if it hasn't been
altered since last fetched. (See L{gets}).
The C{key} can optionally be an tuple, with the first element
Expand Down Expand Up @@ -883,8 +890,8 @@ def _val_to_store_info(self, val, min_compress_len):
val = comp_val

# silently do not store if value length exceeds maximum
if self.server_max_value_length != 0 and \
len(val) > self.server_max_value_length:
if (self.server_max_value_length != 0 and
len(val) > self.server_max_value_length):
return(0)

return (flags, len(val), val)
Expand Down Expand Up @@ -1153,7 +1160,10 @@ def _recv_value(self, server, flags, rlen):
return val

def check_key(self, key, key_extra_len=0):
"""Checks sanity of key. Fails if:
"""Checks sanity of key.
Fails if:
Key length is > SERVER_MAX_KEY_LENGTH (Raises MemcachedKeyLength).
Contains control characters (Raises MemcachedKeyCharacterError).
Is not a string (Raises MemcachedStringEncodingError)
Expand All @@ -1170,14 +1180,14 @@ def check_key(self, key, key_extra_len=0):
# it's a separate type.
if _has_unicode is True and isinstance(key, unicode):
raise Client.MemcachedStringEncodingError(
"Keys must be str()'s, not unicode. Convert your unicode "
"strings using mystring.encode(charset)!")
"Keys must be str()'s, not unicode. Convert your unicode "
"strings using mystring.encode(charset)!")
if not isinstance(key, str):
raise Client.MemcachedKeyTypeError("Key must be str()'s")

if isinstance(key, _str_cls):
if self.server_max_key_length != 0 and \
len(key) + key_extra_len > self.server_max_key_length:
if (self.server_max_key_length != 0 and
len(key) + key_extra_len > self.server_max_key_length):
raise Client.MemcachedKeyLengthError(
"Key length is > %s" % self.server_max_key_length
)
Expand Down Expand Up @@ -1385,7 +1395,8 @@ def to_s(val):

def test_setget(key, val):
global failures
print("Testing set/get {'%s': %s} ..." % (to_s(key), to_s(val)), end=" ")
print("Testing set/get {'%s': %s} ..."
% (to_s(key), to_s(val)), end=" ")
mc.set(key, val)
newval = mc.get(key)
if newval == val:
Expand All @@ -1411,15 +1422,15 @@ def __eq__(self, other):

test_setget("a_string", "some random string")
test_setget("an_integer", 42)
if test_setget("long", long(1<<30)):
if test_setget("long", long(1 << 30)):
print("Testing delete ...", end=" ")
if mc.delete("long"):
print("OK")
else:
print("FAIL")
failures += 1
print("Checking results of delete ...", end=" ")
if mc.get("long") == None:
if mc.get("long") is None:
print("OK")
else:
print("FAIL")
Expand All @@ -1428,19 +1439,19 @@ def __eq__(self, other):
print(mc.get_multi(["a_string", "an_integer"]))

# removed from the protocol
#if test_setget("timed_delete", 'foo'):
# print "Testing timed delete ...",
# if mc.delete("timed_delete", 1):
# print("OK")
# else:
# print("FAIL")
# failures += 1
# print "Checking results of timed delete ..."
# if mc.get("timed_delete") == None:
# print("OK")
# else:
# print("FAIL")
# failures += 1
# if test_setget("timed_delete", 'foo'):
# print "Testing timed delete ...",
# if mc.delete("timed_delete", 1):
# print("OK")
# else:
# print("FAIL")
# failures += 1
# print "Checking results of timed delete ..."
# if mc.get("timed_delete") is None:
# print("OK")
# else:
# print("FAIL")
# failures += 1

print("Testing get(unknown value) ...", end=" ")
print(to_s(mc.get("unknown_value")))
Expand Down Expand Up @@ -1511,12 +1522,11 @@ def __eq__(self, other):
failures += 1
try:
x = mc.set((unicode('a')*SERVER_MAX_KEY_LENGTH).encode('utf-8'), 1)
except:
except Client.MemcachedKeyError:
print("FAIL", end=" ")
failures += 1
else:
print("OK", end=" ")
import pickle
s = pickle.loads('V\\u4f1a\np0\n.')
try:
x = mc.set((s * SERVER_MAX_KEY_LENGTH).encode('utf-8'), 1)
Expand All @@ -1529,13 +1539,13 @@ def __eq__(self, other):
print("Testing using a value larger than the memcached value limit...")
print('NOTE: "MemCached: while expecting[...]" is normal...')
x = mc.set('keyhere', 'a'*SERVER_MAX_VALUE_LENGTH)
if mc.get('keyhere') == None:
if mc.get('keyhere') is None:
print("OK", end=" ")
else:
print("FAIL", end=" ")
failures += 1
x = mc.set('keyhere', 'a'*SERVER_MAX_VALUE_LENGTH + 'aaa')
if mc.get('keyhere') == None:
if mc.get('keyhere') is None:
print("OK")
else:
print("FAIL")
Expand Down
32 changes: 16 additions & 16 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@
py_modules=["memcache"],
install_requires=open('requirements.txt').read().split(),
classifiers=[
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Developers",
"License :: OSI Approved :: Python Software Foundation License",
"Operating System :: OS Independent",
"Programming Language :: Python",
"Topic :: Internet",
"Topic :: Software Development :: Libraries :: Python Modules",
"Programming Language :: Python",
"Programming Language :: Python :: 2",
"Programming Language :: Python :: 2.6",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.2",
"Programming Language :: Python :: 3.3",
"Programming Language :: Python :: 3.4"
])
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Developers",
"License :: OSI Approved :: Python Software Foundation License",
"Operating System :: OS Independent",
"Programming Language :: Python",
"Topic :: Internet",
"Topic :: Software Development :: Libraries :: Python Modules",
"Programming Language :: Python",
"Programming Language :: Python :: 2",
"Programming Language :: Python :: 2.6",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.2",
"Programming Language :: Python :: 3.3",
"Programming Language :: Python :: 3.4"
])
6 changes: 3 additions & 3 deletions tests/test_setmulti.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
#
# Tests for set_multi.
#
#===============
# ==============
# This is based on a skeleton test file, more information at:
#
# https://github.com/linsomniac/python-unittest-skeleton

from __future__ import print_function

import socket
import sys
import unittest

import sys
sys.path.append('..')
import memcache
import socket

DEBUG = False

Expand Down

0 comments on commit 2251916

Please sign in to comment.