Skip to content

Commit 80cf6fe

Browse files
authored
Black + various pre-commit hooks (#176)
1 parent 34be613 commit 80cf6fe

16 files changed

+702
-570
lines changed

.pre-commit-config.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
repos:
2+
- repo: https://github.com/ambv/black
3+
rev: stable
4+
hooks:
5+
- id: black
6+
language_version: python3
7+
8+
- repo: https://github.com/pre-commit/pre-commit-hooks
9+
rev: v2.2.3
10+
hooks:
11+
- id: trailing-whitespace
12+
- id: end-of-file-fixer
13+
- id: check-docstring-first
14+
- id: check-json
15+
- id: check-added-large-files
16+
- id: check-yaml
17+
- id: debug-statements
18+
19+
- repo: https://gitlab.com/pycqa/flake8
20+
rev: 3.7.1
21+
hooks:
22+
- id: flake8

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,3 +267,12 @@ TABLES
267267
> BYE;
268268
GoodBye!
269269
```
270+
271+
# Contributing
272+
273+
Contributions are welcomed of course. We like to use `black` and `flake8`.
274+
275+
```bash
276+
pip install -r requirements-dev.txt # installs useful dev deps
277+
pre-commit install # installs useful commit hooks
278+
```

pydruid/async_client.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from tornado import gen
2525
from tornado.httpclient import AsyncHTTPClient, HTTPError
2626
except ImportError:
27-
print('Warning: unable to import Tornado. The asynchronous client will not work.')
27+
print("Warning: unable to import Tornado. The asynchronous client will not work.")
2828

2929

3030
class AsyncPyDruid(BaseDruidClient):
@@ -110,7 +110,8 @@ def _post(self, query):
110110
try:
111111
headers, querystr, url = self._prepare_url_headers_and_body(query)
112112
response = yield http_client.fetch(
113-
url, method='POST', headers=headers, body=querystr)
113+
url, method="POST", headers=headers, body=querystr
114+
)
114115
except HTTPError as e:
115116
self.__handle_http_error(e, query)
116117
else:
@@ -127,9 +128,12 @@ def __handle_http_error(e, query):
127128
except ValueError:
128129
pass
129130
else:
130-
err = err.get('error', None)
131-
raise IOError('{0} \n Druid Error: {1} \n Query is: {2}'.format(
132-
e, err, json.dumps(query.query_dict, indent=4)))
131+
err = err.get("error", None)
132+
raise IOError(
133+
"{0} \n Druid Error: {1} \n Query is: {2}".format(
134+
e, err, json.dumps(query.query_dict, indent=4)
135+
)
136+
)
133137

134138
@gen.coroutine
135139
def topn(self, **kwargs):

pydruid/client.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727

2828
# extract error from the <PRE> tag inside the HTML response
29-
HTML_ERROR = re.compile('<pre>\s*(.*?)\s*</pre>', re.IGNORECASE)
29+
HTML_ERROR = re.compile("<pre>\\s*(.*?)\\s*</pre>", re.IGNORECASE)
3030

3131

3232
class BaseDruidClient(object):
@@ -49,16 +49,16 @@ def set_proxies(self, proxies):
4949
urllib.request.install_opener(opener)
5050

5151
def _prepare_url_headers_and_body(self, query):
52-
querystr = json.dumps(query.query_dict).encode('utf-8')
53-
if self.url.endswith('/'):
52+
querystr = json.dumps(query.query_dict).encode("utf-8")
53+
if self.url.endswith("/"):
5454
url = self.url + self.endpoint
5555
else:
56-
url = self.url + '/' + self.endpoint
57-
headers = {'Content-Type': 'application/json'}
56+
url = self.url + "/" + self.endpoint
57+
headers = {"Content-Type": "application/json"}
5858
if (self.username is not None) and (self.password is not None):
59-
authstring = '{}:{}'.format(self.username, self.password)
59+
authstring = "{}:{}".format(self.username, self.password)
6060
b64string = b64encode(authstring.encode()).decode()
61-
headers['Authorization'] = 'Basic {}'.format(b64string)
61+
headers["Authorization"] = "Basic {}".format(b64string)
6262

6363
return headers, querystr, url
6464

@@ -449,7 +449,8 @@ def export_tsv(self, dest_path):
449449
"""
450450
if self.query_builder.last_query is None:
451451
raise AttributeError(
452-
"There was no query executed by this client yet. Can't export!")
452+
"There was no query executed by this client yet. Can't export!"
453+
)
453454
else:
454455
return self.query_builder.last_query.export_tsv(dest_path)
455456

@@ -462,7 +463,8 @@ def export_pandas(self):
462463
"""
463464
if self.query_builder.last_query is None:
464465
raise AttributeError(
465-
"There was no query executed by this client yet. Can't export!")
466+
"There was no query executed by this client yet. Can't export!"
467+
)
466468
else:
467469
return self.query_builder.last_query.export_pandas()
468470

@@ -539,6 +541,7 @@ class PyDruid(BaseDruidClient):
539541
0 7 2013-10-04T00:00:00.000Z user_1
540542
1 6 2013-10-04T00:00:00.000Z user_2
541543
"""
544+
542545
def __init__(self, url, endpoint):
543546
super(PyDruid, self).__init__(url, endpoint)
544547

@@ -561,12 +564,18 @@ def _post(self, query):
561564
except (ValueError, AttributeError, KeyError):
562565
pass
563566

564-
raise IOError('{0} \n Druid Error: {1} \n Query is: {2}'.format(
565-
e, err, json.dumps(
567+
raise IOError(
568+
"{0} \n Druid Error: {1} \n Query is: {2}".format(
569+
e,
570+
err,
571+
json.dumps(
566572
query.query_dict,
567573
indent=4,
568574
sort_keys=True,
569-
separators=(',', ': '))))
575+
separators=(",", ": "),
576+
),
577+
)
578+
)
570579
else:
571580
query.parse(data)
572581
return query

0 commit comments

Comments
 (0)