Skip to content

Commit 3136b9c

Browse files
authored
Merge pull request #235 from Davi0kProgramsThings/fix/refactoring
Merge branch `Davi0kProgramsThings:fix/refactoring` into branch `bitfinexcom:master`.
2 parents 59c3090 + f94096b commit 3136b9c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+2223
-1685
lines changed

.flake8

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[flake8]
2+
max-line-length = 80
3+
extend-select = B950
4+
extend-ignore = E203,E501,E701
5+
6+
exclude =
7+
__pycache__
8+
build
9+
dist
10+
venv
11+
12+
per-file-ignores =
13+
*/__init__.py:F401

.github/ISSUE_TEMPLATE.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ A possible solution could be...
2020
## Steps to reproduce (for bugs)
2121
<!-- You can delete this section if you are not submitting a bug report -->
2222

23-
1. &nbsp;
24-
2. &nbsp;
25-
3. &nbsp;
23+
1.
24+
2.
25+
3.
2626

2727
### Python version
2828
<!-- Indicate your python version here -->

.github/PULL_REQUEST_TEMPLATE.md

+8-7
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ PR fixes the following issue:
2020

2121
# Checklist:
2222

23-
- [ ] My code follows the style guidelines of this project;
24-
- [ ] I have performed a self-review of my code;
25-
- [ ] I have commented my code, particularly in hard-to-understand areas;
26-
- [ ] I have made corresponding changes to the documentation;
27-
- [ ] My changes generate no new warnings;
28-
- [ ] Mypy returns no errors or warnings when run on the root package;
29-
- [ ] Pylint returns a score of 10.00/10.00 when run on the root package;
23+
- [ ] I've done a self-review of my code;
24+
- [ ] I've made corresponding changes to the documentation;
25+
- [ ] I've made sure my changes generate no warnings;
26+
- [ ] mypy returns no errors when run on the root package;
27+
<!-- If you use pre-commit hooks you can always check off the following tasks -->
28+
- [ ] I've run black to format my code;
29+
- [ ] I've run isort to format my code's import statements;
30+
- [ ] flake8 reports no errors when run on the entire code base;

.github/workflows/bitfinex-api-py-ci.yml

+3-6
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ on:
88
branches:
99
- master
1010

11-
permissions:
12-
contents: read
13-
1411
jobs:
1512
build:
1613
runs-on: ubuntu-latest
@@ -23,7 +20,7 @@ jobs:
2320
python-version: '3.8'
2421
- name: Install bitfinex-api-py's dependencies
2522
run: python -m pip install -r dev-requirements.txt
26-
- name: Lint the project with pylint (and fail if score is lower than 10.00/10.00)
27-
run: python -m pylint bfxapi
28-
- name: Run mypy to check the correctness of type hinting (and fail if any error or warning is found)
23+
- name: Run pre-commit hooks (see .pre-commit-config.yaml)
24+
uses: pre-commit/[email protected]
25+
- name: Run mypy to ensure correct type hinting
2926
run: python -m mypy bfxapi

.gitignore

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1+
.venv
2+
.DS_Store
13
.vscode
2-
*.pyc
3-
*.log
4+
.python-version
5+
__pycache__
46

57
bitfinex_api_py.egg-info
8+
bitfinex_api_py.dist-info
9+
build/
10+
dist/
11+
pip-wheel-metadata/
12+
.eggs
613

7-
__pycache__
14+
.idea
815

9-
dist
10-
venv
11-
!.gitkeep
12-
MANIFEST
16+
venv/

.isort.cfg

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[settings]
2+
profile = black

.pre-commit-config.yaml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
repos:
2+
- repo: https://github.com/PyCQA/isort
3+
rev: 5.13.2
4+
hooks:
5+
- id: isort
6+
- repo: https://github.com/psf/black-pre-commit-mirror
7+
rev: 24.2.0
8+
hooks:
9+
- id: black
10+
- repo: https://github.com/PyCQA/flake8
11+
rev: 7.0.0
12+
hooks:
13+
- id: flake8
14+
15+
additional_dependencies: [
16+
flake8-bugbear
17+
]

.pylintrc

-25
This file was deleted.

README.md

+35-8
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,9 @@ Contributors must uphold the [Contributor Covenant code of conduct](https://gith
312312
1. [Installation and setup](#installation-and-setup)
313313
* [Cloning the repository](#cloning-the-repository)
314314
* [Installing the dependencies](#installing-the-dependencies)
315+
* [Set up the pre-commit hooks (optional)](#set-up-the-pre-commit-hooks-optional)
315316
2. [Before opening a PR](#before-opening-a-pr)
317+
* [Tip](#tip)
316318
3. [License](#license)
317319

318320
## Installation and setup
@@ -333,23 +335,48 @@ git clone --branch v3-beta --single-branch https://github.com/bitfinexcom/bitfin
333335
python3 -m pip install -r dev-requirements.txt
334336
```
335337

336-
Make sure to install `dev-requirements.txt` instead of `requirements.txt`. \
337-
`dev-requirements.txt` will install all dependencies in `requirements.txt` plus any development dependencies. \
338-
This will also install the versions in use of [`pylint`](https://github.com/pylint-dev/pylint) and [`mypy`](https://github.com/python/mypy), which you should both use before opening your PRs.
338+
Make sure to install `dev-requirements.txt` (and not `requirements.txt`!). \
339+
`dev-requirements.txt` will install all dependencies in `requirements.txt` plus any development dependency. \
340+
dev-requirements includes [mypy](https://github.com/python/mypy), [black](https://github.com/psf/black), [isort](https://github.com/PyCQA/isort), [flake8](https://github.com/PyCQA/flake8), and [pre-commit](https://github.com/pre-commit/pre-commit) (more on these tools in later chapters).
339341

340342
All done, your Python 3.8+ environment should now be able to run `bitfinex-api-py`'s source code.
341343

344+
### Set up the pre-commit hooks (optional)
345+
346+
**Do not skip this paragraph if you intend to contribute to the project.**
347+
348+
This repository includes a pre-commit configuration file that defines the following hooks:
349+
1. [isort](https://github.com/PyCQA/isort)
350+
2. [black](https://github.com/psf/black)
351+
3. [flake8](https://github.com/PyCQA/flake8)
352+
353+
To set up pre-commit use:
354+
```console
355+
python3 -m pre-commit install
356+
```
357+
358+
These will ensure that isort, black and flake8 are run on each git commit.
359+
360+
[Visit this page to learn more about git hooks and pre-commit.](https://pre-commit.com/#introduction)
361+
362+
#### Manually triggering the pre-commit hooks
363+
364+
You can also manually trigger the execution of all hooks with:
365+
```console
366+
python3 -m pre-commit run --all-files
367+
```
368+
342369
## Before opening a PR
343370

344-
**We won't accept your PR or we will request changes if the following requirements aren't met.**
371+
**We won't accept your PR or we'll request changes if the following requirements aren't met.**
345372

346373
Wheter you're submitting a bug fix, a new feature or a documentation change, you should first discuss it in an issue.
347374

348-
All PRs must follow this [PULL_REQUEST_TEMPLATE](https://github.com/bitfinexcom/bitfinex-api-py/blob/v3-beta/.github/PULL_REQUEST_TEMPLATE.md) and include an exhaustive description.
375+
You must be able to check off all tasks listed in [PULL_REQUEST_TEMPLATE](https://raw.githubusercontent.com/bitfinexcom/bitfinex-api-py/master/.github/PULL_REQUEST_TEMPLATE.md) before opening a pull request.
376+
377+
### Tip
349378

350-
Before opening a pull request, you should also make sure that:
351-
- [ ] [`pylint`](https://github.com/pylint-dev/pylint) returns a score of 10.00/10.00 when run against your code.
352-
- [ ] [`mypy`](https://github.com/python/mypy) doesn't throw any error code when run on the project (excluding notes).
379+
Setting up the project's pre-commit hooks will help automate this process ([more](#set-up-the-pre-commit-hooks-optional)).
353380

354381
## License
355382

bfxapi/__init__.py

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1 @@
1-
from ._client import \
2-
Client, \
3-
REST_HOST, \
4-
WSS_HOST, \
5-
PUB_REST_HOST, \
6-
PUB_WSS_HOST
1+
from ._client import PUB_REST_HOST, PUB_WSS_HOST, REST_HOST, WSS_HOST, Client

bfxapi/_client.py

+27-23
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,48 @@
1-
from typing import \
2-
TYPE_CHECKING, List, Optional
1+
from typing import TYPE_CHECKING, List, Optional
32

43
from bfxapi._utils.logging import ColorLogger
5-
4+
from bfxapi.exceptions import IncompleteCredentialError
65
from bfxapi.rest import BfxRestInterface
76
from bfxapi.websocket import BfxWebSocketClient
8-
from bfxapi.exceptions import IncompleteCredentialError
97

108
if TYPE_CHECKING:
11-
from bfxapi.websocket._client.bfx_websocket_client import \
12-
_Credentials
9+
from bfxapi.websocket._client.bfx_websocket_client import _Credentials
1310

1411
REST_HOST = "https://api.bitfinex.com/v2"
1512
WSS_HOST = "wss://api.bitfinex.com/ws/2"
1613

1714
PUB_REST_HOST = "https://api-pub.bitfinex.com/v2"
1815
PUB_WSS_HOST = "wss://api-pub.bitfinex.com/ws/2"
1916

17+
2018
class Client:
2119
def __init__(
22-
self,
23-
api_key: Optional[str] = None,
24-
api_secret: Optional[str] = None,
25-
*,
26-
rest_host: str = REST_HOST,
27-
wss_host: str = WSS_HOST,
28-
filters: Optional[List[str]] = None,
29-
timeout: Optional[int] = 60 * 15,
30-
log_filename: Optional[str] = None
20+
self,
21+
api_key: Optional[str] = None,
22+
api_secret: Optional[str] = None,
23+
*,
24+
rest_host: str = REST_HOST,
25+
wss_host: str = WSS_HOST,
26+
filters: Optional[List[str]] = None,
27+
timeout: Optional[int] = 60 * 15,
28+
log_filename: Optional[str] = None,
3129
) -> None:
3230
credentials: Optional["_Credentials"] = None
3331

3432
if api_key and api_secret:
35-
credentials = \
36-
{ "api_key": api_key, "api_secret": api_secret, "filters": filters }
33+
credentials = {
34+
"api_key": api_key,
35+
"api_secret": api_secret,
36+
"filters": filters,
37+
}
3738
elif api_key:
38-
raise IncompleteCredentialError( \
39-
"You must provide both an API-KEY and an API-SECRET (missing API-KEY).")
39+
raise IncompleteCredentialError(
40+
"You must provide both API-KEY and API-SECRET (missing API-KEY)."
41+
)
4042
elif api_secret:
41-
raise IncompleteCredentialError( \
42-
"You must provide both an API-KEY and an API-SECRET (missing API-SECRET).")
43+
raise IncompleteCredentialError(
44+
"You must provide both API-KEY and API-SECRET (missing API-SECRET)."
45+
)
4346

4447
self.rest = BfxRestInterface(rest_host, api_key, api_secret)
4548

@@ -48,5 +51,6 @@ def __init__(
4851
if log_filename:
4952
logger.register(filename=log_filename)
5053

51-
self.wss = BfxWebSocketClient(wss_host, \
52-
credentials=credentials, timeout=timeout, logger=logger)
54+
self.wss = BfxWebSocketClient(
55+
wss_host, credentials=credentials, timeout=timeout, logger=logger
56+
)

bfxapi/_utils/json_decoder.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
from typing import Dict, Any
1+
import json
2+
import re
3+
from typing import Any, Dict
24

3-
import re, json
45

56
def _to_snake_case(string: str) -> str:
67
return re.sub(r"(?<!^)(?=[A-Z])", "_", string).lower()
78

9+
810
def _object_hook(data: Dict[str, Any]) -> Any:
9-
return { _to_snake_case(key): value for key, value in data.items() }
11+
return {_to_snake_case(key): value for key, value in data.items()}
12+
1013

1114
class JSONDecoder(json.JSONDecoder):
1215
def __init__(self, *args: Any, **kwargs: Any) -> None:
13-
super().__init__(object_hook=_object_hook, *args, **kwargs)
16+
super().__init__(*args, **kwargs, object_hook=_object_hook)

bfxapi/_utils/json_encoder.py

+11-13
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
1-
from typing import \
2-
Union, List, Dict, \
3-
Any
4-
51
import json
6-
72
from decimal import Decimal
3+
from typing import Any, Dict, List, Union
4+
5+
_ExtJSON = Union[
6+
Dict[str, "_ExtJSON"], List["_ExtJSON"], bool, int, float, str, Decimal, None
7+
]
88

9-
_ExtJSON = Union[Dict[str, "_ExtJSON"], List["_ExtJSON"], \
10-
bool, int, float, str, Decimal, None]
9+
_StrictJSON = Union[Dict[str, "_StrictJSON"], List["_StrictJSON"], int, str, None]
1110

12-
_StrictJSON = Union[Dict[str, "_StrictJSON"], List["_StrictJSON"], \
13-
int, str, None]
1411

1512
def _clear(dictionary: Dict[str, Any]) -> Dict[str, Any]:
16-
return { key: value for key, value in dictionary.items() \
17-
if value is not None }
13+
return {key: value for key, value in dictionary.items() if value is not None}
14+
1815

1916
def _adapter(data: _ExtJSON) -> _StrictJSON:
2017
if isinstance(data, bool):
@@ -25,12 +22,13 @@ def _adapter(data: _ExtJSON) -> _StrictJSON:
2522
return format(data, "f")
2623

2724
if isinstance(data, list):
28-
return [ _adapter(sub_data) for sub_data in data ]
25+
return [_adapter(sub_data) for sub_data in data]
2926
if isinstance(data, dict):
30-
return _clear({ key: _adapter(value) for key, value in data.items() })
27+
return _clear({key: _adapter(value) for key, value in data.items()})
3128

3229
return data
3330

31+
3432
class JSONEncoder(json.JSONEncoder):
3533
def encode(self, o: _ExtJSON) -> str:
3634
return super().encode(_adapter(o))

0 commit comments

Comments
 (0)