Skip to content

Commit 36e1898

Browse files
committed
pylint'ing and pyflaks'ing sources. Add some docstrings and pep8'fying code
1 parent 1683bf8 commit 36e1898

File tree

6 files changed

+145
-56
lines changed

6 files changed

+145
-56
lines changed

src/tarantool/__init__.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,26 @@
44
__version__ = "0.3.1"
55

66
from tarantool.connection import Connection
7-
from tarantool.const import *
8-
from tarantool.error import *
9-
from tarantool.schema import *
7+
from tarantool.const import (
8+
SOCKET_TIMEOUT,
9+
RECONNECT_MAX_ATTEMPTS,
10+
RECONNECT_DELAY
11+
)
1012

13+
from tarantool.schema import (
14+
Schema,
15+
RAW,
16+
STR,
17+
NUM,
18+
NUM64
19+
)
20+
from tarantool.error import (
21+
Error,
22+
DatabaseError,
23+
NetworkError,
24+
NetworkWarning,
25+
RetryWarning
26+
)
1127

1228
def connect(host="localhost", port=33013, schema=None):
1329
'''\
@@ -28,3 +44,6 @@ def connect(host="localhost", port=33013, schema=None):
2844
reconnect_delay=RECONNECT_DELAY,
2945
connect_now=True,
3046
schema=schema)
47+
48+
__all__ = ['connect', 'Connection', 'Schema', 'Error', 'DatabaseError', 'NetworkError', 'NetworkWarning', 'RetryWarning', 'RAW', 'STR', 'NUM', 'NUM64']
49+

src/tarantool/connection.py

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
This module provides low-level API for Tarantool
55
'''
66

7-
import ctypes
87
import errno
98
import socket
109
import time
@@ -18,12 +17,27 @@
1817
RequestPing,
1918
RequestSelect,
2019
RequestUpdate)
21-
from tarantool.space import Space
22-
from tarantool.const import *
23-
from tarantool.error import *
24-
from tarantool.schema import *
25-
2620

21+
from tarantool.space import Space
22+
from tarantool.const import (
23+
struct_L,
24+
SOCKET_TIMEOUT,
25+
RECONNECT_MAX_ATTEMPTS,
26+
RECONNECT_DELAY,
27+
RETRY_MAX_ATTEMPTS,
28+
BOX_RETURN_TUPLE,
29+
BOX_ADD,
30+
BOX_REPLACE,
31+
REQUEST_TYPE_PING
32+
)
33+
from tarantool.error import (
34+
NetworkError,
35+
DatabaseError,
36+
warn,
37+
RetryWarning,
38+
NetworkWarning
39+
)
40+
from tarantool.schema import Schema
2741

2842
class Connection(object):
2943
'''\
@@ -62,15 +76,13 @@ def __init__(self, host, port,
6276
if connect_now:
6377
self.connect()
6478

65-
6679
def close(self):
6780
'''\
6881
Close connection to the server
6982
'''
7083
self._socket.close()
7184
self._socket = None
7285

73-
7486
def connect(self):
7587
'''\
7688
Create connection to the host and port specified in __init__().
@@ -93,7 +105,6 @@ def connect(self):
93105
except socket.error as e:
94106
raise NetworkError(e)
95107

96-
97108
def _read_response(self):
98109
'''
99110
Read response from the transport (socket)
@@ -117,8 +128,6 @@ def _read_response(self):
117128

118129
return buf[0:12], buf[12:]
119130

120-
121-
122131
def _send_request_wo_reconnect(self, request, space_name = None, field_defs = None, default_type = None):
123132
'''\
124133
:rtype: `Response` instance
@@ -152,15 +161,13 @@ def _opt_reconnect(self):
152161
if e.errno == errno.EAGAIN:
153162
break
154163
else:
155-
time.slelep(self.reconnect_delay)
164+
time.sleep(self.reconnect_delay)
156165
self.connect()
157166
warn("%s : Reconnect attempt %d of %d"%(e.message, attempt, self.reconnect_max_attempts), NetworkWarning)
158167
if attempt == self.reconnect_max_attempts:
159168
raise
160169
attempt += 1
161170

162-
163-
164171
def _send_request(self, request, space_name = None, field_defs = None, default_type = None):
165172
'''\
166173
Send the request to the server through the socket.
@@ -173,8 +180,7 @@ def _send_request(self, request, space_name = None, field_defs = None, default_t
173180
'''
174181
assert isinstance(request, Request)
175182

176-
connected = True
177-
attempt = 1
183+
178184
self._opt_reconnect()
179185
response = self._send_request_wo_reconnect(request, space_name, field_defs, default_type)
180186

@@ -236,10 +242,10 @@ def replace(self, space_name, values, return_tuple):
236242
:type values: tuple
237243
:param return_tuple: True indicates that it is required to return the inserted tuple back
238244
:type return_tuple: bool
239-
245+
240246
:rtype: `Response` instance
241247
'''
242-
self._insert(space_name, values, (BOX_REPLACE_TUPLE if return_tuple else 0) | BOX_REPLACE)
248+
self._insert(space_name, values, (BOX_RETURN_TUPLE if return_tuple else 0) | BOX_REPLACE)
243249

244250
def store(self, space_name, values, return_tuple):
245251
'''
@@ -255,7 +261,7 @@ def store(self, space_name, values, return_tuple):
255261
256262
:rtype: `Response` instance
257263
'''
258-
self._insert(space_name, values, (BOX_REPLACE_TUPLE if return_tuple else 0))
264+
self._insert(space_name, values, (BOX_RETURN_TUPLE if return_tuple else 0))
259265

260266
def insert(self, space_name, values, return_tuple):
261267
'''
@@ -271,8 +277,7 @@ def insert(self, space_name, values, return_tuple):
271277
272278
:rtype: `Response` instance
273279
'''
274-
self._insert(space_name, values, (BOX_REPLACE_TUPLE if return_tuple else 0) | BOX_ADD)
275-
280+
self._insert(space_name, values, (BOX_RETURN_TUPLE if return_tuple else 0) | BOX_ADD)
276281

277282
def delete(self, space_name, key, return_tuple=False):
278283
'''\
@@ -293,7 +298,6 @@ def delete(self, space_name, key, return_tuple=False):
293298
request = RequestDelete(self, space_name, key, return_tuple)
294299
return self._send_request(request, space_name)
295300

296-
297301
def update(self, space_name, key, op_list, return_tuple=False):
298302
'''\
299303
Execute UPDATE request.
@@ -317,7 +321,6 @@ def update(self, space_name, key, op_list, return_tuple=False):
317321
request = RequestUpdate(self, space_name, key, op_list, return_tuple)
318322
return self._send_request(request, space_name)
319323

320-
321324
def ping(self, notime=False):
322325
'''\
323326
Execute PING request.
@@ -339,7 +342,6 @@ def ping(self, notime=False):
339342
return "Success"
340343
return t1 - t0
341344

342-
343345
def _select(self, space_name, index_name, values, offset=0, limit=0xffffffff):
344346
'''\
345347
Low level version of select() method.
@@ -365,7 +367,6 @@ def _select(self, space_name, index_name, values, offset=0, limit=0xffffffff):
365367
response = self._send_request(request, space_name)
366368
return response
367369

368-
369370
def select(self, space_name, values=None, **kwargs):
370371
'''\
371372
Execute SELECT request.
@@ -383,7 +384,7 @@ def select(self, space_name, values=None, **kwargs):
383384
:type limit: int
384385
385386
:rtype: `Response` instance
386-
387+
387388
Select one single record (from space=0 and using index=0)
388389
>>> select(0, 0, 1)
389390
@@ -425,7 +426,6 @@ def select(self, space_name, values=None, **kwargs):
425426

426427
return self._select(space_name, index, values, offset, limit)
427428

428-
429429
def space(self, space_name):
430430
'''\
431431
Create `Space` instance for particular space
@@ -439,4 +439,3 @@ def space(self, space_name):
439439
:rtype: `Space` instance
440440
'''
441441
return Space(self, space_name)
442-

src/tarantool/request.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,25 @@
66

77
import struct
88

9-
from tarantool.const import *
10-
9+
from tarantool.const import (
10+
struct_B,
11+
struct_BB,
12+
struct_BBB,
13+
struct_BBBB,
14+
struct_BBBBB,
15+
struct_L,
16+
struct_LL,
17+
struct_LLL,
18+
struct_LLLLL,
19+
struct_LB,
20+
UPDATE_OPERATION_CODE,
21+
REQUEST_TYPE_INSERT,
22+
REQUEST_TYPE_DELETE,
23+
REQUEST_TYPE_SELECT,
24+
REQUEST_TYPE_UPDATE,
25+
REQUEST_TYPE_PING,
26+
REQUEST_TYPE_CALL
27+
)
1128

1229
class Request(object):
1330
'''\
@@ -79,12 +96,10 @@ def pack_int_base128(cls, value):
7996

8097
raise OverflowError("Number too large to be packed")
8198

82-
8399
def pack_field(self, value):
84100
value_len_packed = Request.pack_int_base128(len(value))
85101
return struct.pack("<%ds%ds"%(len(value_len_packed), len(value)), value_len_packed, value)
86102

87-
88103
def pack_fields(self, packed_values):
89104
'''\
90105
Pack tuple of values
@@ -104,7 +119,6 @@ def pack_fields(self, packed_values):
104119
packed_items.append(self.pack_field(value))
105120
return b"".join(packed_items)
106121

107-
108122
def pack_tuple(self, values, space_no = None):
109123
'''\
110124
Pack tuple of values
@@ -118,7 +132,6 @@ def pack_tuple(self, values, space_no = None):
118132
'''
119133
return self.pack_fields(self.conn.schema.pack_values(values, space_no))
120134

121-
122135
def pack_key_tuple(self, values, space_no = None, index_no = None):
123136
'''\
124137
Pack key tuple
@@ -159,7 +172,6 @@ def __init__(self, conn, space_name, values, flags): # pylint: disable=W0231
159172
self._bytes = self.header(len(request_body)) + request_body
160173

161174

162-
163175
class RequestDelete(Request):
164176
'''
165177
Represents DELETE request
@@ -217,7 +229,6 @@ def __init__(self, conn, space_name, index_name, tuple_list, offset, limit):
217229
self._bytes = self.header(len(request_body)) + request_body
218230

219231

220-
221232
class RequestUpdate(Request):
222233
'''
223234
<update_request_body> ::= <space_no><flags><tuple><count><operation>+
@@ -264,7 +275,6 @@ def pack_operations(self, op_list):
264275
return b"".join(result)
265276

266277

267-
268278
class RequestCall(Request):
269279
'''
270280
<call_request_body> ::= <flags><proc_name><tuple>
@@ -289,6 +299,7 @@ def __init__(self, conn, proc_name, args, return_tuple): # pylint: disable=W0
289299

290300
self._bytes = self.header(len(request_body)) + request_body
291301

302+
292303
class RequestPing(Request):
293304
'''
294305
Ping body is empty, so body_length == 0 and there's no body
@@ -299,4 +310,4 @@ class RequestPing(Request):
299310

300311
def __init__(self, conn):
301312
super(RequestPing, self).__init__(conn)
302-
self._bytes = struct_LLL.pack(REQUEST_TYPE_PING, 0, 0)
313+
self._bytes = struct_LLL.pack(REQUEST_TYPE_PING, 0, 0)

src/tarantool/response.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,17 @@
44
import struct
55
import sys
66

7-
from tarantool.const import *
8-
from tarantool.error import *
7+
from tarantool.const import (
8+
REQUEST_TYPE_INSERT,
9+
REQUEST_TYPE_SELECT,
10+
REQUEST_TYPE_UPDATE,
11+
REQUEST_TYPE_DELETE,
12+
struct_L,
13+
struct_LL,
14+
struct_LLL,
15+
struct_Q
16+
)
17+
from tarantool.error import DatabaseError
918

1019

1120

src/tarantool/schema.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
NUM64 = 2
1212
STR = 3
1313

14-
from tarantool.const import *
15-
from tarantool.request import Request
14+
from tarantool.const import (
15+
struct_L,
16+
struct_Q
17+
)
1618

1719
class Schema(object):
1820
'''\
@@ -59,7 +61,7 @@ def __init__(self, schema):
5961

6062
for (space_no, space_descr) in schema.iteritems():
6163
if not isinstance(space_no, (int, long)):
62-
ValueError('Invalid space_no: %s'%field_no)
64+
ValueError('Invalid space_no: %s' % space_no)
6365

6466
# Space name
6567
space_name = space_descr.get('name', None)
@@ -176,7 +178,7 @@ def index_no(self, space_name, index_name):
176178
for (index_no, (index_name2, _indexed_fields)) in enumerate(index_defs):
177179
if index_name2 == index_name:
178180
return index_no
179-
raise KeyError(index_no)
181+
raise KeyError(index_name)
180182

181183
def _pack_value_int(self, value):
182184
if __debug__:

0 commit comments

Comments
 (0)