Skip to content

Commit c376cae

Browse files
committed
QDB-9440 - Format.
1 parent 74b3757 commit c376cae

21 files changed

+194
-130
lines changed

quasardb/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,5 @@
3737

3838
try:
3939
from quasardb.quasardb import *
40-
except:
40+
except BaseException:
4141
from quasardb import *

quasardb/firehose.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,36 @@
88

99
logger = logging.getLogger('quasardb.firehose')
1010

11+
1112
def _init():
1213
"""
1314
Initialize our internal state.
1415
"""
1516
return {'last': None,
1617
'seen': set()}
1718

19+
1820
def _get_transactions_since(conn, table_name, last):
1921
"""
2022
Retrieve all transactions since a certain timestamp. `last` is expected to be a dict
2123
firehose row with at least a $timestamp attached.
2224
"""
2325
if last is None:
24-
q = "SELECT $timestamp, transaction_id, begin, end FROM \"{}\" WHERE table = '{}' ORDER BY $timestamp".format(FIREHOSE_TABLE, table_name)
26+
q = "SELECT $timestamp, transaction_id, begin, end FROM \"{}\" WHERE table = '{}' ORDER BY $timestamp".format(
27+
FIREHOSE_TABLE, table_name)
2528
else:
26-
q = "SELECT $timestamp, transaction_id, begin, end FROM \"{}\" IN RANGE ({}, +1y) WHERE table = '{}' ORDER BY $timestamp".format(FIREHOSE_TABLE, last['$timestamp'], table_name)
29+
q = "SELECT $timestamp, transaction_id, begin, end FROM \"{}\" IN RANGE ({}, +1y) WHERE table = '{}' ORDER BY $timestamp".format(
30+
FIREHOSE_TABLE, last['$timestamp'], table_name)
2731

2832
return conn.query(q)
2933

34+
3035
def _get_transaction_data(conn, table_name, begin, end):
3136
"""
3237
Gets all data from a certain table.
3338
"""
34-
q = "SELECT * FROM \"{}\" IN RANGE ({}, {}) ".format(table_name, begin, end)
39+
q = "SELECT * FROM \"{}\" IN RANGE ({}, {}) ".format(
40+
table_name, begin, end)
3541
return conn.query(q)
3642

3743

@@ -50,13 +56,13 @@ def _get_next(conn, table_name, state):
5056
for tx in txs:
5157
txid = tx['transaction_id']
5258

53-
if state['last'] != None and tx['$timestamp'] > state['last']['$timestamp']:
59+
if state['last'] is not None and tx['$timestamp'] > state['last']['$timestamp']:
5460
# At this point we are guaranteed that the transaction we encounter is
5561
# 'new', will not conflict with any other transaction ids. It is thus
5662
# safe to reset the txid set.
5763
state['seen'] = set()
5864

59-
if not txid in state['seen']:
65+
if txid not in state['seen']:
6066
xs = xs + _get_transaction_data(conn,
6167
table_name,
6268
tx['begin'],
@@ -66,10 +72,10 @@ def _get_next(conn, table_name, state):
6672
tx['end'] + np.timedelta64(1, 'ns'))
6773

6874
# Because it is possible that multiple firehose changes are stored with the
69-
# exact same $timestamp, we also keep track of the actually seen transaction ids.
75+
# exact same $timestamp, we also keep track of the actually seen
76+
# transaction ids.
7077
state['seen'].add(txid)
7178

72-
7379
state['last'] = tx
7480

7581
return (state, xs)

quasardb/stats.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66

77

88
stats_prefix = '$qdb.statistics.'
9-
user_pattern = re.compile('\$qdb.statistics.(.*).uid_([0-9]+)$')
10-
total_pattern = re.compile('\$qdb.statistics.(.*)$(?<!uid_[0-9])')
9+
user_pattern = re.compile(r'\$qdb.statistics.(.*).uid_([0-9]+)$')
10+
total_pattern = re.compile(r'\$qdb.statistics.(.*)$(?<!uid_[0-9])')
11+
1112

1213
def by_node(conn):
1314
"""
@@ -19,6 +20,7 @@ def by_node(conn):
1920
"""
2021
return {x: of_node(conn, x) for x in conn.endpoints()}
2122

23+
2224
def of_node(conn, uri):
2325
"""
2426
Returns statistic for a single node.
@@ -32,36 +34,45 @@ def of_node(conn, uri):
3234
"""
3335
dconn = conn.node(uri)
3436

35-
raw = {k: _get_stat(dconn, k) for k in dconn.prefix_get(stats_prefix, 1000)}
37+
raw = {
38+
k: _get_stat(
39+
dconn,
40+
k) for k in dconn.prefix_get(
41+
stats_prefix,
42+
1000)}
3643
return {'by_uid': _by_uid(raw),
3744
'cumulative': _cumulative(raw)}
3845

46+
3947
def _get_stat(dconn, k):
40-
# Ugly, but works: try to retrieve as integer, if not an int, retrieve as blob
48+
# Ugly, but works: try to retrieve as integer, if not an int, retrieve as
49+
# blob
4150
try:
4251
return dconn.integer(k).get()
4352
except quasardb.quasardb.Error:
4453
blob = dconn.blob(k).get()
4554
return blob.decode('utf-8', 'replace')
4655

56+
4757
def _by_uid(stats):
4858
xs = {}
49-
for k,v in stats.items():
59+
for k, v in stats.items():
5060
matches = user_pattern.match(k)
5161
if matches:
5262
(metric, uid_str) = matches.groups()
5363
uid = int(uid_str)
54-
if not uid in xs:
64+
if uid not in xs:
5565
xs[uid] = {}
5666

5767
xs[uid][metric] = v
5868

5969
return xs
6070

71+
6172
def _cumulative(stats):
6273
xs = {}
6374

64-
for k,v in stats.items():
75+
for k, v in stats.items():
6576
matches = total_pattern.match(k)
6677
if matches:
6778
metric = matches.groups()[0]

tests/conftest.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
def connect(uri):
1212
return quasardb.Cluster(uri)
1313

14+
1415
def direct_connect(conn, node_uri):
1516
return conn.node(node_uri)
1617

18+
1719
def config():
1820
return {"uri":
1921
{"insecure": "qdb://127.0.0.1:2836",
@@ -43,6 +45,7 @@ def qdbd_settings(scope="module"):
4345
def qdbd_connection(qdbd_settings):
4446
return connect(qdbd_settings.get("uri").get("insecure"))
4547

48+
4649
@pytest.fixture
4750
def qdbd_secure_connection(qdbd_settings):
4851
return quasardb.Cluster(
@@ -54,13 +57,17 @@ def qdbd_secure_connection(qdbd_settings):
5457

5558
@pytest.fixture
5659
def qdbd_direct_connection(qdbd_settings, qdbd_connection):
57-
return direct_connect(qdbd_connection, qdbd_settings.get("uri").get("insecure").replace("qdb://", ""))
60+
return direct_connect(qdbd_connection, qdbd_settings.get(
61+
"uri").get("insecure").replace("qdb://", ""))
5862

5963

6064
@pytest.fixture
6165
def qdbd_direct_secure_connection(qdbd_settings, qdbd_secure_connection):
62-
return direct_connect(qdbd_secure_connection,
63-
qdbd_settings.get("uri").get("secure").replace("qdb://", ""))
66+
return direct_connect(
67+
qdbd_secure_connection,
68+
qdbd_settings.get("uri").get("secure").replace(
69+
"qdb://",
70+
""))
6471

6572

6673
@pytest.fixture
@@ -101,6 +108,7 @@ def entry_name(random_string):
101108
def random_blob(random_string):
102109
return np.random.bytes(16)
103110

111+
104112
@pytest.fixture
105113
def random_integer():
106114
return random.randint(-1000000000, 1000000000)
@@ -120,14 +128,16 @@ def integer_entry(qdbd_connection, entry_name):
120128
def table_name(entry_name):
121129
return entry_name
122130

131+
123132
def _create_table(c, table_name):
124133
t = c.table(table_name)
125134
double_col = quasardb.ColumnInfo(quasardb.ColumnType.Double, "the_double")
126135
blob_col = quasardb.ColumnInfo(quasardb.ColumnType.Blob, "the_blob")
127136
string_col = quasardb.ColumnInfo(quasardb.ColumnType.String, "the_string")
128137
int64_col = quasardb.ColumnInfo(quasardb.ColumnType.Int64, "the_int64")
129138
ts_col = quasardb.ColumnInfo(quasardb.ColumnType.Timestamp, "the_ts")
130-
symbol_col = quasardb.ColumnInfo(quasardb.ColumnType.Symbol, "the_symbol", "symtable")
139+
symbol_col = quasardb.ColumnInfo(
140+
quasardb.ColumnType.Symbol, "the_symbol", "symtable")
131141

132142
t.create([double_col, blob_col, string_col, int64_col, ts_col, symbol_col])
133143
return t
@@ -142,6 +152,7 @@ def table(qdbd_connection, entry_name):
142152
def secure_table(qdbd_secure_connection, entry_name):
143153
return _create_table(qdbd_secure_connection, entry_name)
144154

155+
145156
@pytest.fixture
146157
def intervals():
147158
start_time = np.datetime64('2017-01-01', 'ns')
@@ -152,6 +163,8 @@ def create_many_intervals():
152163
start_time = np.datetime64('2017-01-01', 'ns')
153164
return np.array([(start_time + np.timedelta64(i, 's'))
154165
for i in range(10000)]).astype('datetime64[ns]')
166+
167+
155168
@pytest.fixture
156169
def many_intervals():
157170
return create_many_intervals()

tests/test_batch_inserter.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,10 @@ def test_successful_bulk_row_insert(qdbd_connection, table, many_intervals):
185185
_regular_push)
186186

187187

188-
def test_successful_secure_bulk_row_insert(qdbd_secure_connection, secure_table, many_intervals):
188+
def test_successful_secure_bulk_row_insert(
189+
qdbd_secure_connection,
190+
secure_table,
191+
many_intervals):
189192
inserter = qdbd_secure_connection.inserter(
190193
_make_inserter_info(secure_table))
191194

@@ -316,7 +319,8 @@ def test_push_truncate_explicit_range(qdbd_connection, table, many_intervals):
316319
np.testing.assert_array_equal(results[1], doubles[1:])
317320

318321

319-
def test_push_truncate_throws_error_on_invalid_range(qdbd_connection, table, many_intervals):
322+
def test_push_truncate_throws_error_on_invalid_range(
323+
qdbd_connection, table, many_intervals):
320324
whole_range = (
321325
many_intervals[0], many_intervals[-1:][0] + np.timedelta64(2, 's'))
322326

tests/test_buf_size.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ def test_client_query_buf_size_error(qdbd_connection, table, many_intervals):
2929
inserter = qdbd_connection.inserter(
3030
batchlib._make_inserter_info(table))
3131

32-
# doubles, blobs, strings, integers, timestamps = batchlib._test_with_table(
32+
# doubles, blobs, strings, integers, timestamps =
33+
# batchlib._test_with_table(
3334
batchlib._test_with_table(
3435
inserter,
3536
table,

tests/test_firehose.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import multiprocessing
1111
from sys import platform
1212

13+
1314
def _ensure_timeout(f, timeout=3):
1415
# HACKS(leon): apparently there is no reliable way to do this kind of stuff
1516
# on Windows or OSX, due to the way threading works.
@@ -59,16 +60,12 @@ def test_subscribe_single_table(qdbd_connection, table, many_intervals):
5960
xs = table.subscribe(qdbd_connection)
6061

6162
doubles, blobs, strings, integers, timestamps, symbols = batchlib._test_with_table(
62-
inserter,
63-
table,
64-
many_intervals,
65-
batchlib._regular_push)
63+
inserter, table, many_intervals, batchlib._regular_push)
6664

6765
time.sleep(4)
6866

6967
n = len(doubles)
7068

71-
7269
# After insertion, we expect exactly `n` items in the database, and not a
7370
# single element more.
7471
offset = 0
@@ -87,7 +84,7 @@ def next_row():
8784

8885
# Ensure that acquiring the next row times out after 3 seconds, which ensures
8986
# that we have reached 'the end'.
90-
assert _ensure_timeout(next_row, timeout=3) == True
87+
assert _ensure_timeout(next_row, timeout=3)
9188

9289
# Now, we insert additional data, which should wake up our subscription.
9390
# What we're going to do is just insert the exact same data, but this time
@@ -97,10 +94,7 @@ def next_row():
9794
many_intervals_.append(x + np.timedelta64(365, 'D'))
9895

9996
doubles, blobs, strings, integers, timestamps, symbols = batchlib._test_with_table(
100-
inserter,
101-
table,
102-
many_intervals_,
103-
batchlib._regular_push)
97+
inserter, table, many_intervals_, batchlib._regular_push)
10498

10599
# Note that we only reset `offset`; since we just inserted exactly double
106100
# the data, `n` is still 10000 which is perfect!
@@ -115,7 +109,6 @@ def next_row():
115109
assert row['the_symbol'] == symbols[offset]
116110
offset = offset + 1
117111

118-
119112
# Ensure that acquiring the next row times out after 3 seconds, which ensures
120113
# that we have reached 'the end'.
121-
assert _ensure_timeout(next_row, timeout=3) == True
114+
assert _ensure_timeout(next_row, timeout=3)

tests/test_logging.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,4 @@ def test_invalid_utf8_logs_qdb3361(qdbd_connection, caplog):
7171
for lr in caplog.records:
7272
seen = seen or '\x80' in lr.message
7373

74-
assert seen == True
74+
assert seen

tests/test_node.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ def test_node_find_prefixes(qdbd_direct_connection):
2424
def test_node_blob_get(qdbd_direct_connection):
2525
entry = qdbd_direct_connection.blob("$qdb.statistics.node_id")
2626
got = entry.get()
27-
assert type(got) is bytes and len(got) > 0
27+
assert isinstance(got, bytes) and len(got) > 0
2828

2929

3030
def test_node_integer_get(qdbd_direct_connection):
3131
entry = qdbd_direct_connection.integer("$qdb.statistics.cpu.system")
3232
got = entry.get()
3333

34-
assert type(got) is int and got > 0
34+
assert isinstance(got, int) and got > 0

tests/test_pandas.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,12 @@ def _sparsify(xs):
280280
quasardb.ColumnType.Blob,
281281
quasardb.ColumnType.String,
282282
quasardb.ColumnType.Timestamp])
283-
def test_inference(qdbd_connection, table_name, input_gen, column_type, sparse):
283+
def test_inference(
284+
qdbd_connection,
285+
table_name,
286+
input_gen,
287+
column_type,
288+
sparse):
284289

285290
# Create table
286291
t = qdbd_connection.ts(table_name)

0 commit comments

Comments
 (0)