Skip to content

Commit 9814928

Browse files
authored
ARROW-60 Set up auto formatting (#52)
1 parent a46c29c commit 9814928

22 files changed

+519
-538
lines changed

bindings/python/addtags.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
# Usage:
55
# $ python addtags.py WHEEL_PATH TARGET_PLATFORM WHEEL_DIR
66
import os
7-
from os.path import abspath, isfile, exists, basename, join as pjoin
7+
from os.path import abspath, basename, exists, isfile
8+
from os.path import join as pjoin
89
from sys import argv
10+
911
from auditwheel.policy import get_priority_by_name, get_replace_platforms
1012
from auditwheel.wheel_abi import analyze_wheel_abi
1113
from auditwheel.wheeltools import InWheelCtx, add_platforms
@@ -15,14 +17,13 @@ def repair_wheel(wheel_path, abi, wheel_dir):
1517
wheel_fname = basename(wheel_path)
1618
with InWheelCtx(wheel_path) as ctx:
1719
ctx.out_wheel = pjoin(wheel_dir, wheel_fname)
18-
ctx.out_wheel = add_platforms(ctx, [abi],
19-
get_replace_platforms(abi))
20+
ctx.out_wheel = add_platforms(ctx, [abi], get_replace_platforms(abi))
2021
return ctx.out_wheel
2122

2223

2324
def main(wheel_path, abi, wheel_dir):
2425
if not isfile(wheel_path):
25-
raise FileNotFoundError('cannot access wheel file %s' % (wheel_path,))
26+
raise FileNotFoundError("cannot access wheel file %s" % (wheel_path,))
2627

2728
if not exists(wheel_dir):
2829
os.makedirs(wheel_dir)
@@ -33,17 +34,16 @@ def main(wheel_path, abi, wheel_dir):
3334
if out_wheel is not None:
3435
analyzed_tag = analyze_wheel_abi(out_wheel).overall_tag
3536
if reqd_tag < get_priority_by_name(analyzed_tag):
36-
print('Wheel is eligible for a higher priority tag. '
37-
'You requested %s but I have found this wheel is '
38-
'eligible for %s.' % (abi, analyzed_tag))
37+
print(
38+
"Wheel is eligible for a higher priority tag. "
39+
"You requested %s but I have found this wheel is "
40+
"eligible for %s." % (abi, analyzed_tag)
41+
)
3942
out_wheel = repair_wheel(wheel_path, analyzed_tag, wheel_dir)
4043

41-
print('Fixed-up wheel written to %s' % (out_wheel,))
44+
print("Fixed-up wheel written to %s" % (out_wheel,))
4245

4346

44-
if __name__ == '__main__':
47+
if __name__ == "__main__":
4548
WHEEL_PATH, TARGET_PLATFORM, WHEEL_DIR = argv[1], argv[2], argv[3]
46-
main(
47-
wheel_path=abspath(WHEEL_PATH),
48-
abi=TARGET_PLATFORM,
49-
wheel_dir=abspath(WHEEL_DIR))
49+
main(wheel_path=abspath(WHEEL_PATH), abi=TARGET_PLATFORM, wheel_dir=abspath(WHEEL_DIR))

bindings/python/benchmark.py

Lines changed: 48 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@
88

99
import numpy as np
1010
import pandas as pd
11+
import pyarrow
1112
import pymongo
12-
1313
from bson import BSON, CodecOptions, Int64, ObjectId
1414
from bson.raw_bson import RawBSONDocument
15-
16-
import pyarrow
17-
from pymongoarrow.api import find_arrow_all, find_numpy_all, Schema, find_pandas_all
15+
from pymongoarrow.api import Schema, find_arrow_all, find_numpy_all, find_pandas_all
1816

1917
assert pymongo.has_c()
2018

@@ -39,45 +37,49 @@ def _setup():
3937
small = db[collection_names[SMALL]]
4038
small.drop()
4139

42-
print("%d small docs, %d bytes each with 3 keys" % (
43-
N_SMALL_DOCS,
44-
len(BSON.encode({'_id': ObjectId(), 'x': 1, 'y': math.pi}))))
40+
print(
41+
"%d small docs, %d bytes each with 3 keys"
42+
% (N_SMALL_DOCS, len(BSON.encode({"_id": ObjectId(), "x": 1, "y": math.pi})))
43+
)
4544

46-
small.insert_many([
47-
collections.OrderedDict([('x', 1), ('y', math.pi)])
48-
for _ in range(N_SMALL_DOCS)])
45+
small.insert_many(
46+
[collections.OrderedDict([("x", 1), ("y", math.pi)]) for _ in range(N_SMALL_DOCS)]
47+
)
4948

50-
dtypes[SMALL] = np.dtype([('x', np.int64), ('y', np.float64)])
51-
schemas[SMALL] = Schema({'x': pyarrow.int64(), 'y': pyarrow.float64()})
49+
dtypes[SMALL] = np.dtype([("x", np.int64), ("y", np.float64)])
50+
schemas[SMALL] = Schema({"x": pyarrow.int64(), "y": pyarrow.float64()})
5251

5352
large = db[collection_names[LARGE]]
5453
large.drop()
5554
# 2600 keys: 'a', 'aa', 'aaa', .., 'zz..z'
56-
large_doc_keys = [c * i for c in string.ascii_lowercase
57-
for i in range(1, 101)]
55+
large_doc_keys = [c * i for c in string.ascii_lowercase for i in range(1, 101)]
5856
large_doc = collections.OrderedDict([(k, math.pi) for k in large_doc_keys])
59-
print("%d large docs, %dk each with %d keys" % (
60-
N_LARGE_DOCS, len(BSON.encode(large_doc)) // 1024, len(large_doc_keys)))
57+
print(
58+
"%d large docs, %dk each with %d keys"
59+
% (N_LARGE_DOCS, len(BSON.encode(large_doc)) // 1024, len(large_doc_keys))
60+
)
6161

6262
large.insert_many([large_doc.copy() for _ in range(N_LARGE_DOCS)])
6363

6464
dtypes[LARGE] = np.dtype([(k, np.float64) for k in large_doc_keys])
6565
schemas[LARGE] = Schema({k: pyarrow.float64() for k in large_doc_keys})
6666

6767
# Ignore for now that the first batch defaults to 101 documents.
68-
raw_bson_docs_small = [{'x': 1, 'y': math.pi} for _ in range(N_SMALL_DOCS)]
69-
raw_bson_small = BSON.encode({'ok': 1,
70-
'cursor': {
71-
'id': Int64(1234),
72-
'ns': 'db.collection',
73-
'firstBatch': raw_bson_docs_small}})
68+
raw_bson_docs_small = [{"x": 1, "y": math.pi} for _ in range(N_SMALL_DOCS)]
69+
raw_bson_small = BSON.encode(
70+
{
71+
"ok": 1,
72+
"cursor": {"id": Int64(1234), "ns": "db.collection", "firstBatch": raw_bson_docs_small},
73+
}
74+
)
7475

7576
raw_bson_docs_large = [large_doc.copy() for _ in range(N_LARGE_DOCS)]
76-
raw_bson_large = BSON.encode({'ok': 1,
77-
'cursor': {
78-
'id': Int64(1234),
79-
'ns': 'db.collection',
80-
'firstBatch': raw_bson_docs_large}})
77+
raw_bson_large = BSON.encode(
78+
{
79+
"ok": 1,
80+
"cursor": {"id": Int64(1234), "ns": "db.collection", "firstBatch": raw_bson_docs_large},
81+
}
82+
)
8183

8284
raw_bsons[SMALL] = raw_bson_small
8385
raw_bsons[LARGE] = raw_bson_large
@@ -99,62 +101,61 @@ def assign_name(fn):
99101
return assign_name
100102

101103

102-
@bench('conventional-to-ndarray')
104+
@bench("conventional-to-ndarray")
103105
def conventional_ndarray(use_large):
104106
collection = db[collection_names[use_large]]
105107
cursor = collection.find()
106108
dtype = dtypes[use_large]
107109

108110
if use_large:
109-
np.array([tuple(doc[k] for k in large_doc_keys) for doc in cursor],
110-
dtype=dtype)
111+
np.array([tuple(doc[k] for k in large_doc_keys) for doc in cursor], dtype=dtype)
111112
else:
112-
np.array([(doc['x'], doc['y']) for doc in cursor], dtype=dtype)
113+
np.array([(doc["x"], doc["y"]) for doc in cursor], dtype=dtype)
113114

114115

115116
# Note: this is called "to-numpy" and not "to-ndarray" because find_numpy_all
116117
# does not produce an ndarray.
117-
@bench('pymongoarrow-to-numpy')
118+
@bench("pymongoarrow-to-numpy")
118119
def to_numpy(use_large):
119120
c = db[collection_names[use_large]]
120121
schema = schemas[use_large]
121122
find_numpy_all(c, {}, schema=schema)
122123

123124

124-
@bench('conventional-to-pandas')
125+
@bench("conventional-to-pandas")
125126
def conventional_pandas(use_large):
126127
collection = db[collection_names[use_large]]
127128
dtype = dtypes[use_large]
128-
cursor = collection.find(projection={'_id': 0})
129+
cursor = collection.find(projection={"_id": 0})
129130
data_frame = pd.DataFrame(list(cursor))
130131

131132

132-
@bench('pymongoarrow-to-pandas')
133+
@bench("pymongoarrow-to-pandas")
133134
def to_pandas(use_large):
134135
c = db[collection_names[use_large]]
135136
schema = schemas[use_large]
136137
find_pandas_all(c, {}, schema=schema)
137138

138139

139-
@bench('pymongoarrow-to-arrow')
140+
@bench("pymongoarrow-to-arrow")
140141
def to_arrow(use_large):
141142
c = db[collection_names[use_large]]
142143
schema = schemas[use_large]
143144
find_arrow_all(c, {}, schema=schema)
144145

145146

146-
parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter,
147-
epilog="""
147+
parser = argparse.ArgumentParser(
148+
formatter_class=argparse.RawTextHelpFormatter,
149+
epilog="""
148150
Available benchmark functions:
149151
%s
150-
""" % ("\n ".join(bench_fns.keys()),))
151-
parser.add_argument('--large', action='store_true',
152-
help='only test with large documents')
153-
parser.add_argument('--small', action='store_true',
154-
help='only test with small documents')
155-
parser.add_argument('--test', action='store_true',
156-
help='quick test of benchmark.py')
157-
parser.add_argument('funcs', nargs='*', default=bench_fns.keys())
152+
"""
153+
% ("\n ".join(bench_fns.keys()),),
154+
)
155+
parser.add_argument("--large", action="store_true", help="only test with large documents")
156+
parser.add_argument("--small", action="store_true", help="only test with small documents")
157+
parser.add_argument("--test", action="store_true", help="quick test of benchmark.py")
158+
parser.add_argument("funcs", nargs="*", default=bench_fns.keys())
158159
options = parser.parse_args()
159160

160161
if options.test:
@@ -175,7 +176,7 @@ def to_arrow(use_large):
175176

176177
for name in options.funcs:
177178
if name not in bench_fns:
178-
sys.stderr.write("Unknown function \"%s\"\n" % name)
179+
sys.stderr.write('Unknown function "%s"\n' % name)
179180
sys.stderr.write("Available functions:\n%s\n" % ("\n".join(bench_fns)))
180181
sys.exit(1)
181182

bindings/python/docs/source/conf.py

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,32 @@
1313
import os
1414
import os.path as p
1515

16-
1716
# -- Project information -----------------------------------------------------
1817

19-
project = 'PyMongoArrow'
20-
copyright = 'MongoDB, Inc. 2021-present. MongoDB, Mongo, and the leaf logo are registered trademarks of MongoDB, Inc'
21-
author = 'Prashant Mital'
18+
project = "PyMongoArrow"
19+
copyright = "MongoDB, Inc. 2021-present. MongoDB, Mongo, and the leaf logo are registered trademarks of MongoDB, Inc"
20+
author = "Prashant Mital"
2221
html_show_sphinx = False
2322

2423
version_file = p.abspath(p.join("../../", "pymongoarrow/version.py"))
2524
version_data = {}
26-
with open(version_file, 'r') as vf:
25+
with open(version_file, "r") as vf:
2726
exec(vf.read(), {}, version_data)
28-
version = version_data['__version__']
27+
version = version_data["__version__"]
2928
release = version
3029

3130
# The name of the Pygments (syntax highlighting) style to use.
32-
pygments_style = 'sphinx'
31+
pygments_style = "sphinx"
3332

3433
# -- General configuration ---------------------------------------------------
3534

3635
# Add any Sphinx extension module names here, as strings. They can be
3736
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
3837
# ones.
39-
extensions = [
40-
'sphinx.ext.autodoc',
41-
'sphinx.ext.intersphinx'
42-
]
38+
extensions = ["sphinx.ext.autodoc", "sphinx.ext.intersphinx"]
4339

4440
# Add any paths that contain templates here, relative to this directory.
45-
templates_path = ['_templates']
41+
templates_path = ["_templates"]
4642

4743
# List of patterns, relative to source directory, that match files and
4844
# directories to ignore when looking for source files.
@@ -55,12 +51,9 @@
5551
# The theme to use for HTML and HTML Help pages. See the documentation for
5652
# a list of builtin themes.
5753
#
58-
html_theme = 'pydoctheme'
54+
html_theme = "pydoctheme"
5955
html_theme_path = ["."]
60-
html_theme_options = {
61-
'collapsiblesidebar': True,
62-
'googletag': False
63-
}
56+
html_theme_options = {"collapsiblesidebar": True, "googletag": False}
6457

6558
# Add any paths that contain custom static files (such as style sheets) here,
6659
# relative to this directory. They are copied after the builtin static files,
@@ -71,15 +64,15 @@
7164
# or fully qualified paths (eg. https://...)
7265
# Note: html_js_files was added in Sphinx 1.8.
7366
html_js_files = [
74-
'delighted.js',
67+
"delighted.js",
7568
]
7669

7770
# Output file base name for HTML help builder.
78-
htmlhelp_basename = 'PyMongoArrow' + release.replace('.', '_')
71+
htmlhelp_basename = "PyMongoArrow" + release.replace(".", "_")
7972

8073
intersphinx_mapping = {
81-
'pyarrow': ('https://arrow.apache.org/docs/', None),
82-
'pandas': ('https://pandas.pydata.org/docs/', None),
83-
'numpy': ('https://numpy.org/doc/1.20/', None),
84-
'pymongo': ('https://pymongo.readthedocs.io/en/stable/', None)
74+
"pyarrow": ("https://arrow.apache.org/docs/", None),
75+
"pandas": ("https://pandas.pydata.org/docs/", None),
76+
"numpy": ("https://numpy.org/doc/1.20/", None),
77+
"pymongo": ("https://pymongo.readthedocs.io/en/stable/", None),
8578
}

bindings/python/pymongoarrow/__init__.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,12 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
import os
14+
15+
import warnings
16+
1517
# We must import pyarrow before attempting to load the Cython module.
1618
import pyarrow
17-
18-
from pymongoarrow.version import __version__, _MIN_LIBBSON_VERSION
19-
# This line must come second so setuptools can parse the __version__
20-
# above without having a built application.
21-
from pymongoarrow.lib import libbson_version
19+
from pymongoarrow.version import _MIN_LIBBSON_VERSION, __version__
2220

2321
try:
2422
from pkg_resources import parse_version as _parse_version
@@ -28,10 +26,14 @@
2826
def _parse_version(version):
2927
return _LooseVersion(version)
3028

31-
# TODO: PYTHON-2659
32-
on_rtd = os.environ.get('READTHEDOCS') == 'True'
33-
if not on_rtd:
29+
30+
try:
31+
from pymongoarrow.lib import libbson_version
32+
3433
if _parse_version(libbson_version) < _parse_version(_MIN_LIBBSON_VERSION):
3534
raise ImportError(
3635
f"Expected libbson version {_MIN_LIBBSON_VERSION} or greater, "
37-
f"found {libbson_version}")
36+
f"found {libbson_version}"
37+
)
38+
except ImportError:
39+
warnings.warn("This library has not been compiled")

0 commit comments

Comments
 (0)