Format the codebase using Black (#2109)
This commit: 1. Formats all of our existing code using `black`. 2. Adds a note about using `black` to `CONTRIBUTING.rst`. 3. Runs `black --check` as part of CI (failing builds that aren't properly formatted).
This commit is contained in:
@@ -1,11 +1,18 @@
|
||||
from timeit import repeat
|
||||
|
||||
import mongoengine
|
||||
from mongoengine import (BooleanField, Document, EmailField, EmbeddedDocument,
|
||||
EmbeddedDocumentField, IntField, ListField,
|
||||
StringField)
|
||||
from mongoengine import (
|
||||
BooleanField,
|
||||
Document,
|
||||
EmailField,
|
||||
EmbeddedDocument,
|
||||
EmbeddedDocumentField,
|
||||
IntField,
|
||||
ListField,
|
||||
StringField,
|
||||
)
|
||||
|
||||
mongoengine.connect(db='mongoengine_benchmark_test')
|
||||
mongoengine.connect(db="mongoengine_benchmark_test")
|
||||
|
||||
|
||||
def timeit(f, n=10000):
|
||||
@@ -24,46 +31,41 @@ def test_basic():
|
||||
|
||||
def init_book():
|
||||
return Book(
|
||||
name='Always be closing',
|
||||
name="Always be closing",
|
||||
pages=100,
|
||||
tags=['self-help', 'sales'],
|
||||
tags=["self-help", "sales"],
|
||||
is_published=True,
|
||||
author_email='alec@example.com',
|
||||
author_email="alec@example.com",
|
||||
)
|
||||
|
||||
print('Doc initialization: %.3fus' % (timeit(init_book, 1000) * 10**6))
|
||||
print("Doc initialization: %.3fus" % (timeit(init_book, 1000) * 10 ** 6))
|
||||
|
||||
b = init_book()
|
||||
print('Doc getattr: %.3fus' % (timeit(lambda: b.name, 10000) * 10**6))
|
||||
print("Doc getattr: %.3fus" % (timeit(lambda: b.name, 10000) * 10 ** 6))
|
||||
|
||||
print(
|
||||
'Doc setattr: %.3fus' % (
|
||||
timeit(lambda: setattr(b, 'name', 'New name'), 10000) * 10**6
|
||||
)
|
||||
"Doc setattr: %.3fus"
|
||||
% (timeit(lambda: setattr(b, "name", "New name"), 10000) * 10 ** 6)
|
||||
)
|
||||
|
||||
print('Doc to mongo: %.3fus' % (timeit(b.to_mongo, 1000) * 10**6))
|
||||
print("Doc to mongo: %.3fus" % (timeit(b.to_mongo, 1000) * 10 ** 6))
|
||||
|
||||
print('Doc validation: %.3fus' % (timeit(b.validate, 1000) * 10**6))
|
||||
print("Doc validation: %.3fus" % (timeit(b.validate, 1000) * 10 ** 6))
|
||||
|
||||
def save_book():
|
||||
b._mark_as_changed('name')
|
||||
b._mark_as_changed('tags')
|
||||
b._mark_as_changed("name")
|
||||
b._mark_as_changed("tags")
|
||||
b.save()
|
||||
|
||||
print('Save to database: %.3fus' % (timeit(save_book, 100) * 10**6))
|
||||
print("Save to database: %.3fus" % (timeit(save_book, 100) * 10 ** 6))
|
||||
|
||||
son = b.to_mongo()
|
||||
print(
|
||||
'Load from SON: %.3fus' % (
|
||||
timeit(lambda: Book._from_son(son), 1000) * 10**6
|
||||
)
|
||||
"Load from SON: %.3fus" % (timeit(lambda: Book._from_son(son), 1000) * 10 ** 6)
|
||||
)
|
||||
|
||||
print(
|
||||
'Load from database: %.3fus' % (
|
||||
timeit(lambda: Book.objects[0], 100) * 10**6
|
||||
)
|
||||
"Load from database: %.3fus" % (timeit(lambda: Book.objects[0], 100) * 10 ** 6)
|
||||
)
|
||||
|
||||
def create_and_delete_book():
|
||||
@@ -72,9 +74,8 @@ def test_basic():
|
||||
b.delete()
|
||||
|
||||
print(
|
||||
'Init + save to database + delete: %.3fms' % (
|
||||
timeit(create_and_delete_book, 10) * 10**3
|
||||
)
|
||||
"Init + save to database + delete: %.3fms"
|
||||
% (timeit(create_and_delete_book, 10) * 10 ** 3)
|
||||
)
|
||||
|
||||
|
||||
@@ -92,42 +93,36 @@ def test_big_doc():
|
||||
|
||||
def init_company():
|
||||
return Company(
|
||||
name='MongoDB, Inc.',
|
||||
name="MongoDB, Inc.",
|
||||
contacts=[
|
||||
Contact(
|
||||
name='Contact %d' % x,
|
||||
title='CEO',
|
||||
address='Address %d' % x,
|
||||
)
|
||||
Contact(name="Contact %d" % x, title="CEO", address="Address %d" % x)
|
||||
for x in range(1000)
|
||||
]
|
||||
],
|
||||
)
|
||||
|
||||
company = init_company()
|
||||
print('Big doc to mongo: %.3fms' % (timeit(company.to_mongo, 100) * 10**3))
|
||||
print("Big doc to mongo: %.3fms" % (timeit(company.to_mongo, 100) * 10 ** 3))
|
||||
|
||||
print('Big doc validation: %.3fms' % (timeit(company.validate, 1000) * 10**3))
|
||||
print("Big doc validation: %.3fms" % (timeit(company.validate, 1000) * 10 ** 3))
|
||||
|
||||
company.save()
|
||||
|
||||
def save_company():
|
||||
company._mark_as_changed('name')
|
||||
company._mark_as_changed('contacts')
|
||||
company._mark_as_changed("name")
|
||||
company._mark_as_changed("contacts")
|
||||
company.save()
|
||||
|
||||
print('Save to database: %.3fms' % (timeit(save_company, 100) * 10**3))
|
||||
print("Save to database: %.3fms" % (timeit(save_company, 100) * 10 ** 3))
|
||||
|
||||
son = company.to_mongo()
|
||||
print(
|
||||
'Load from SON: %.3fms' % (
|
||||
timeit(lambda: Company._from_son(son), 100) * 10**3
|
||||
)
|
||||
"Load from SON: %.3fms"
|
||||
% (timeit(lambda: Company._from_son(son), 100) * 10 ** 3)
|
||||
)
|
||||
|
||||
print(
|
||||
'Load from database: %.3fms' % (
|
||||
timeit(lambda: Company.objects[0], 100) * 10**3
|
||||
)
|
||||
"Load from database: %.3fms"
|
||||
% (timeit(lambda: Company.objects[0], 100) * 10 ** 3)
|
||||
)
|
||||
|
||||
def create_and_delete_company():
|
||||
@@ -136,13 +131,12 @@ def test_big_doc():
|
||||
c.delete()
|
||||
|
||||
print(
|
||||
'Init + save to database + delete: %.3fms' % (
|
||||
timeit(create_and_delete_company, 10) * 10**3
|
||||
)
|
||||
"Init + save to database + delete: %.3fms"
|
||||
% (timeit(create_and_delete_company, 10) * 10 ** 3)
|
||||
)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
test_basic()
|
||||
print('-' * 100)
|
||||
print("-" * 100)
|
||||
test_big_doc()
|
||||
|
||||
@@ -26,10 +26,10 @@ myNoddys = noddy.find()
|
||||
[n for n in myNoddys] # iterate
|
||||
"""
|
||||
|
||||
print('-' * 100)
|
||||
print('PyMongo: Creating 10000 dictionaries.')
|
||||
print("-" * 100)
|
||||
print("PyMongo: Creating 10000 dictionaries.")
|
||||
t = timeit.Timer(stmt=stmt, setup=setup)
|
||||
print('{}s'.format(t.timeit(1)))
|
||||
print("{}s".format(t.timeit(1)))
|
||||
|
||||
stmt = """
|
||||
from pymongo import MongoClient, WriteConcern
|
||||
@@ -49,10 +49,10 @@ myNoddys = noddy.find()
|
||||
[n for n in myNoddys] # iterate
|
||||
"""
|
||||
|
||||
print('-' * 100)
|
||||
print("-" * 100)
|
||||
print('PyMongo: Creating 10000 dictionaries (write_concern={"w": 0}).')
|
||||
t = timeit.Timer(stmt=stmt, setup=setup)
|
||||
print('{}s'.format(t.timeit(1)))
|
||||
print("{}s".format(t.timeit(1)))
|
||||
|
||||
setup = """
|
||||
from pymongo import MongoClient
|
||||
@@ -78,10 +78,10 @@ myNoddys = Noddy.objects()
|
||||
[n for n in myNoddys] # iterate
|
||||
"""
|
||||
|
||||
print('-' * 100)
|
||||
print('MongoEngine: Creating 10000 dictionaries.')
|
||||
print("-" * 100)
|
||||
print("MongoEngine: Creating 10000 dictionaries.")
|
||||
t = timeit.Timer(stmt=stmt, setup=setup)
|
||||
print('{}s'.format(t.timeit(1)))
|
||||
print("{}s".format(t.timeit(1)))
|
||||
|
||||
stmt = """
|
||||
for i in range(10000):
|
||||
@@ -96,10 +96,10 @@ myNoddys = Noddy.objects()
|
||||
[n for n in myNoddys] # iterate
|
||||
"""
|
||||
|
||||
print('-' * 100)
|
||||
print('MongoEngine: Creating 10000 dictionaries (using a single field assignment).')
|
||||
print("-" * 100)
|
||||
print("MongoEngine: Creating 10000 dictionaries (using a single field assignment).")
|
||||
t = timeit.Timer(stmt=stmt, setup=setup)
|
||||
print('{}s'.format(t.timeit(1)))
|
||||
print("{}s".format(t.timeit(1)))
|
||||
|
||||
stmt = """
|
||||
for i in range(10000):
|
||||
@@ -112,10 +112,10 @@ myNoddys = Noddy.objects()
|
||||
[n for n in myNoddys] # iterate
|
||||
"""
|
||||
|
||||
print('-' * 100)
|
||||
print("-" * 100)
|
||||
print('MongoEngine: Creating 10000 dictionaries (write_concern={"w": 0}).')
|
||||
t = timeit.Timer(stmt=stmt, setup=setup)
|
||||
print('{}s'.format(t.timeit(1)))
|
||||
print("{}s".format(t.timeit(1)))
|
||||
|
||||
stmt = """
|
||||
for i in range(10000):
|
||||
@@ -128,10 +128,12 @@ myNoddys = Noddy.objects()
|
||||
[n for n in myNoddys] # iterate
|
||||
"""
|
||||
|
||||
print('-' * 100)
|
||||
print('MongoEngine: Creating 10000 dictionaries (write_concern={"w": 0}, validate=False).')
|
||||
print("-" * 100)
|
||||
print(
|
||||
'MongoEngine: Creating 10000 dictionaries (write_concern={"w": 0}, validate=False).'
|
||||
)
|
||||
t = timeit.Timer(stmt=stmt, setup=setup)
|
||||
print('{}s'.format(t.timeit(1)))
|
||||
print("{}s".format(t.timeit(1)))
|
||||
|
||||
stmt = """
|
||||
for i in range(10000):
|
||||
@@ -144,10 +146,12 @@ myNoddys = Noddy.objects()
|
||||
[n for n in myNoddys] # iterate
|
||||
"""
|
||||
|
||||
print('-' * 100)
|
||||
print('MongoEngine: Creating 10000 dictionaries (force_insert=True, write_concern={"w": 0}, validate=False).')
|
||||
print("-" * 100)
|
||||
print(
|
||||
'MongoEngine: Creating 10000 dictionaries (force_insert=True, write_concern={"w": 0}, validate=False).'
|
||||
)
|
||||
t = timeit.Timer(stmt=stmt, setup=setup)
|
||||
print('{}s'.format(t.timeit(1)))
|
||||
print("{}s".format(t.timeit(1)))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user