Skip to content

Commit 73c9c43

Browse files
committed
Minor code improvements
1 parent 9af5140 commit 73c9c43

File tree

8 files changed

+67
-31
lines changed

8 files changed

+67
-31
lines changed

lib/mysqlx/authentication.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
# along with this program; if not, write to the Free Software
2222
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2323

24+
"""Implementation of MySQL Authentication Plugin."""
25+
2426
import hashlib
2527

2628

lib/mysqlx/connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
# along with this program; if not, write to the Free Software
2222
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2323

24-
"""Implementation of MySQLx servers enabled communication."""
24+
"""Implementation of communication for MySQL X servers."""
2525

2626
import socket
2727

lib/mysqlx/crud.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
# along with this program; if not, write to the Free Software
2222
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2323

24+
"""Implementation of the CRUD database objects."""
25+
2426
from .statement import (FindStatement, AddStatement, RemoveStatement,
2527
ModifyStatement, SelectStatement, InsertStatement,
2628
DeleteStatement, UpdateStatement,

lib/mysqlx/dbdoc.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
# along with this program; if not, write to the Free Software
2222
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2323

24+
"""Implementation of the DbDoc."""
25+
2426
import json
2527
import uuid
2628

lib/mysqlx/errors.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
# along with this program; if not, write to the Free Software
2222
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2323

24-
"""Python Database API Specification v2.0 exceptions implementation.
25-
"""
24+
"""Implementation of the Python Database API Specification v2.0 exceptions."""
2625

2726
import sys
2827
import struct

lib/mysqlx/protocol.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
# along with this program; if not, write to the Free Software
2222
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2323

24+
"""Implementation of the X protocol for MySQL servers."""
25+
2426
import struct
2527

2628
from .protobuf import mysqlx_pb2 as MySQLx

lib/mysqlx/result.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,17 @@
2121
# along with this program; if not, write to the Free Software
2222
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2323

24+
"""Implementation of the Result Set classes."""
25+
2426
import decimal
2527
import struct
2628
import sys
2729

2830
from datetime import datetime, timedelta
31+
2932
from .dbdoc import DbDoc
3033

34+
3135
def from_protobuf(col_type, payload):
3236
if len(payload) == 0:
3337
return None
@@ -69,7 +73,7 @@ def double_from_protobuf(payload):
6973

7074
def varint_from_protobuf_stream(payload):
7175
if len(payload) == 0:
72-
raise ValueError("payload is empty")
76+
raise ValueError("Payload is empty")
7377

7478
cur = 0
7579
i = 0
@@ -86,21 +90,21 @@ def varint_from_protobuf_stream(payload):
8690
cur += 1
8791
shift += 7
8892

89-
raise EOFError("payload too short")
93+
raise EOFError("Payload too short")
9094

9195

9296
def varint_from_protobuf(payload):
9397
i, payload = varint_from_protobuf_stream(payload)
9498
if len(payload) != 0:
95-
raise ValueError("payload too long")
99+
raise ValueError("Payload too long")
96100

97101
return i
98102

99103

100104
def varsint_from_protobuf(payload):
101105
i, payload = varint_from_protobuf_stream(payload)
102106
if len(payload) != 0:
103-
raise ValueError("payload too long")
107+
raise ValueError("Payload too long")
104108

105109
# Zigzag encoded, revert it
106110
if i & 0x1:
@@ -122,7 +126,7 @@ def set_from_protobuf(payload):
122126
if len(payload) == 0 and field_len == 1 and len(s) == 0:
123127
# Special case for empty set
124128
return []
125-
raise ValueError("invalid Set encoding")
129+
raise ValueError("Invalid Set encoding")
126130

127131
s.append(payload[:field_len])
128132
payload = payload[field_len:]
@@ -397,6 +401,7 @@ def is_padded(self):
397401
# TODO: To implement
398402
raise NotImplementedError
399403

404+
400405
class Warning(object):
401406
def __init__(self, level, code, msg):
402407
self._level = level
@@ -433,17 +438,18 @@ def get_string(self, str_index):
433438
if int_index >= len(self._fields):
434439
raise Exception("Argument out of range")
435440
if int_index == -1:
436-
raise Exception("Column name '" + str_index + "' not found")
441+
raise Exception("Column name '{0}' not found".format(str_index))
437442
return str(self._fields[int_index])
438443

444+
439445
class BaseResult(object):
440446
def __init__(self, connection):
441447
self._connection = connection
442448
self._protocol = self._connection.protocol
443449
self._closed = False
444450
self._rows_affected = 0
445451
self._warnings = []
446-
if connection._active_result != None:
452+
if connection._active_result is not None:
447453
connection._active_result.fetch_all()
448454
connection._active_result = None
449455

@@ -454,6 +460,7 @@ def get_warnings(self):
454460
def get_warnings_count(self):
455461
return len(self._warnings)
456462

463+
457464
class Result(BaseResult):
458465
def __init__(self, connection):
459466
super(Result, self).__init__(connection)
@@ -467,6 +474,7 @@ def get_affected_items_count(self):
467474
def get_autoincrement_value(self):
468475
pass
469476

477+
470478
class BufferingResult(BaseResult):
471479
def __init__(self, connection):
472480
super(BufferingResult, self).__init__(connection)
@@ -478,7 +486,7 @@ def _init_result(self):
478486
self._items = []
479487
self._page_size = 20
480488
self._position = -1
481-
self._connection._active_result = self if self._has_more_data == True else None
489+
self._connection._active_result = self if self._has_more_data else None
482490

483491
@property
484492
def count(self):
@@ -520,11 +528,12 @@ def _page_in_items(self):
520528
return count
521529

522530
def fetch_all(self):
523-
while (True):
531+
while True:
524532
if not self._page_in_items():
525533
break
526534
return self._items
527535

536+
528537
class RowResult(BufferingResult):
529538
def __init__(self, connection):
530539
super(RowResult, self).__init__(connection)
@@ -533,18 +542,20 @@ def __init__(self, connection):
533542
def columns(self):
534543
return self._columns
535544

545+
536546
class SqlResult(RowResult):
537547
def __init__(self, connection):
538548
super(SqlResult, self).__init__(connection)
539549
self._has_more_results = False
540550

541551
def next_result(self):
542-
if self._closed == True:
552+
if self._closed:
543553
return False
544554
self._has_more_results = False
545555
self._init_result()
546556
return True
547557

558+
548559
class DocResult(BufferingResult):
549560
def __init__(self, connection):
550561
super(DocResult, self).__init__(connection)
@@ -554,4 +565,3 @@ def _read_item(self, dumping):
554565
if row is None:
555566
return None
556567
return DbDoc(row[0])
557-

lib/mysqlx/statement.py

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@
2121
# along with this program; if not, write to the Free Software
2222
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2323

24+
"""Implementation of Statements."""
2425

2526
from .protobuf import mysqlx_crud_pb2 as MySQLxCrud
2627
from .result import SqlResult
2728
from .expr import ExprParser
2829
from .dbdoc import DbDoc
2930

31+
3032
class Statement(object):
3133
"""Provides base functionality for statement objects.
3234
@@ -98,7 +100,9 @@ def where(self, condition):
98100

99101
def _projection(self, *fields):
100102
self._has_projection = True
101-
self._projection_expr = ExprParser(",".join(fields), not self._doc_based).parse_table_select_projection()
103+
self._projection_expr = ExprParser(
104+
",".join(fields),
105+
not self._doc_based).parse_table_select_projection()
102106
return self
103107

104108
def limit(self, row_count, offset=0):
@@ -120,12 +124,14 @@ def sort(self, *sort_clauses):
120124
*sort_clauses: The expression strings defining the sort criteria.
121125
"""
122126
self._has_sort = True
123-
self._sort_expr = ExprParser(",".join(sort_clauses), not self._doc_based).parse_order_spec()
127+
self._sort_expr = ExprParser(",".join(sort_clauses),
128+
not self._doc_based).parse_order_spec()
124129
return self
125130

126131
def _group_by(self, *fields):
127132
self._has_group_by = True
128-
self._grouping = ExprParser(",".join(fields), not self._doc_based).parse_expr_list()
133+
self._grouping = ExprParser(",".join(fields),
134+
not self._doc_based).parse_expr_list()
129135

130136
def _having(self, condition):
131137
self._has_having = True
@@ -137,7 +143,7 @@ def execute(self):
137143
Raises:
138144
NotImplementedError: This method must be implemented.
139145
"""
140-
raise Exception("This should never be called")
146+
raise NotImplementedError
141147

142148

143149
class SqlStatement(Statement):
@@ -198,23 +204,26 @@ def execute(self):
198204
doc.ensure_id()
199205
return self._connection.send_insert(self)
200206

207+
201208
class UpdateSpec(object):
202-
def __init__(self, type, source, value=None):
203-
if type == MySQLxCrud.UpdateOperation.SET:
209+
def __init__(self, update_type, source, value=None):
210+
if update_type == MySQLxCrud.UpdateOperation.SET:
204211
self._table_set(source, value)
205212
else:
206-
self.update_type = type
213+
self.update_type = update_type
207214
self.source = source
208215
if len(source) > 0 and source[0] == '$':
209216
self.source = source[1:]
210-
self.source = ExprParser(self.source, False).document_field().identifier
217+
self.source = ExprParser(self.source,
218+
False).document_field().identifier
211219
self.value = value
212220

213221
def _table_set(self, source, value):
214222
self.update_type = MySQLxCrud.UpdateOperation.SET
215223
self.source = ExprParser(source, True).parse_table_update_field()
216224
self.value = value
217225

226+
218227
class ModifyStatement(FilterableStatement):
219228
"""A statement for document update operations on a Collection.
220229
@@ -224,7 +233,8 @@ class ModifyStatement(FilterableStatement):
224233
documents to be updated.
225234
"""
226235
def __init__(self, collection, condition=None):
227-
super(ModifyStatement, self).__init__(target=collection, condition=condition)
236+
super(ModifyStatement, self).__init__(target=collection,
237+
condition=condition)
228238
self._update_ops = []
229239

230240
def set(self, doc_path, value):
@@ -237,7 +247,8 @@ def set(self, doc_path, value):
237247
Returns:
238248
mysqlx.ModifyStatement: ModifyStatement object.
239249
"""
240-
self._update_ops.append(UpdateSpec(MySQLxCrud.UpdateOperation.ITEM_SET, doc_path, value))
250+
self._update_ops.append(
251+
UpdateSpec(MySQLxCrud.UpdateOperation.ITEM_SET, doc_path, value))
241252
return self
242253

243254
def change(self, doc_path, value):
@@ -251,7 +262,9 @@ def change(self, doc_path, value):
251262
Returns:
252263
mysqlx.ModifyStatement: ModifyStatement object.
253264
"""
254-
self._update_ops.append(UpdateSpec(MySQLxCrud.UpdateOperation.ITEM_REPLACE, doc_path, value))
265+
self._update_ops.append(
266+
UpdateSpec(MySQLxCrud.UpdateOperation.ITEM_REPLACE, doc_path,
267+
value))
255268
return self
256269

257270
def unset(self, doc_path):
@@ -264,7 +277,8 @@ def unset(self, doc_path):
264277
Returns:
265278
mysqlx.ModifyStatement: ModifyStatement object.
266279
"""
267-
self._update_ops.append(UpdateSpec(MySQLxCrud.UpdateOperation.ITEM_REMOVE, doc_path))
280+
self._update_ops.append(
281+
UpdateSpec(MySQLxCrud.UpdateOperation.ITEM_REMOVE, doc_path))
268282
return self
269283

270284
def array_insert(self, field, value):
@@ -279,7 +293,8 @@ def array_insert(self, field, value):
279293
Returns:
280294
mysqlx.ModifyStatement: ModifyStatement object.
281295
"""
282-
self._update_ops.append(UpdateSpec(MySQLxCrud.UpdateOperation.ARRAY_INSERT, field, value))
296+
self._update_ops.append(
297+
UpdateSpec(MySQLxCrud.UpdateOperation.ARRAY_INSERT, field, value))
283298
return self
284299

285300
def array_append(self, doc_path, value):
@@ -295,7 +310,9 @@ def array_append(self, doc_path, value):
295310
Returns:
296311
mysqlx.ModifyStatement: ModifyStatement object.
297312
"""
298-
self._update_ops.append(UpdateSpec(MySQLxCrud.UpdateOperation.UpdateType.ARRAY_APPEND, doc_path, value))
313+
self._update_ops.append(
314+
UpdateSpec(MySQLxCrud.UpdateOperation.UpdateType.ARRAY_APPEND,
315+
doc_path, value))
299316
return self
300317

301318
def execute(self):
@@ -442,6 +459,7 @@ def execute(self):
442459
"""
443460
return self._connection.send_insert(self)
444461

462+
445463
class UpdateStatement(FilterableStatement):
446464
"""A statement for record update operations on a Table.
447465
@@ -460,7 +478,8 @@ def set(self, field, value):
460478
field (string): The column name to be updated.
461479
value (object): The value to be set on the specified column.
462480
"""
463-
self._update_ops.append(UpdateSpec(MySQLxCrud.UpdateOperation.SET, field, value))
481+
self._update_ops.append(
482+
UpdateSpec(MySQLxCrud.UpdateOperation.SET, field, value))
464483
return self
465484

466485
def execute(self):
@@ -500,8 +519,8 @@ class DeleteStatement(FilterableStatement):
500519
"""
501520
def __init__(self, table, condition=None):
502521
super(DeleteStatement, self).__init__(target=table,
503-
condition=condition,
504-
doc_based=False)
522+
condition=condition,
523+
doc_based=False)
505524

506525
def execute(self):
507526
"""Execute the statement.

0 commit comments

Comments
 (0)