Skip to content

Commit

Permalink
Black formatter integration (#120)
Browse files Browse the repository at this point in the history
* Black formatter integration

* Isort and formatting fixes

* Reformatted for 100 length. Disabled black for 3.5 python

* Fixed conditional step for travis

* Removed yapf references, added black make command
  • Loading branch information
abondar authored Apr 3, 2019
1 parent 9661ae3 commit d4b32c0
Show file tree
Hide file tree
Showing 84 changed files with 2,542 additions and 2,482 deletions.
7 changes: 5 additions & 2 deletions .isort.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
[settings]
line_length=100
multi_line_output=0
known_third_party=aiosqlite,ciso8601
not_skip=__init__.py
multi_line_output=3
include_trailing_comma=True
force_grid_wrap=0
use_parentheses=True
line_length=100
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ services:
cache: pip
install:
- pip install -r requirements-dev.txt
- if [ "$TRAVIS_PYTHON_VERSION" != "3.5" ]; then
pip install black==19.3b0;
fi
script:
- make ci
after_success:
Expand Down
21 changes: 13 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,27 @@ up:

deps:
@pip install -q pip-tools
@pip-sync requirements-dev.txt
@pip install -q -r requirements-dev.txt

check: deps
flake8 $(checkfiles)
ifneq ($(shell which black),)
black --check -l 100 $(checkfiles)
endif
mypy $(mypy_flags) $(checkfiles)
pylint -E $(checkfiles)
bandit -r $(checkfiles)
python setup.py check -mrs

lint: deps
-flake8 $(checkfiles)
-mypy $(mypy_flags) $(checkfiles)
-pylint $(checkfiles)
-bandit -r $(checkfiles)
-python setup.py check -mrs
flake8 $(checkfiles)
ifneq ($(shell which black),)
black --check -l 100 $(checkfiles)
endif
mypy $(mypy_flags) $(checkfiles)
pylint $(checkfiles)
bandit -r $(checkfiles)
python setup.py check -mrs

test: deps
coverage erase
Expand All @@ -57,8 +63,7 @@ docs: deps
python setup.py build_sphinx -E

style: deps
@#yapf -i -r $(checkfiles)
isort -rc $(checkfiles)
black -l 100 $(checkfiles)

publish: deps
rm -fR dist/
Expand Down
2 changes: 1 addition & 1 deletion conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


def pytest_runtest_setup(item):
initializer(['tortoise.tests.testmodels'], db_url='sqlite://:memory:')
initializer(["tortoise.tests.testmodels"], db_url="sqlite://:memory:")


def pytest_runtest_teardown(item, nextitem):
Expand Down
36 changes: 13 additions & 23 deletions examples/aggregation.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ def __str__(self):
class Event(Model):
id = fields.IntField(pk=True)
name = fields.TextField()
tournament = fields.ForeignKeyField('models.Tournament', related_name='events')
tournament = fields.ForeignKeyField("models.Tournament", related_name="events")
participants = fields.ManyToManyField(
'models.Team',
related_name='events',
through='event_team',
"models.Team", related_name="events", through="event_team"
)

def __str__(self):
Expand All @@ -36,39 +34,31 @@ def __str__(self):


async def run():
await Tortoise.init(config_file='config.json')
await Tortoise.init(config_file="config.json")
await Tortoise.generate_schemas()
tournament = await Tournament.create(name='New Tournament')
tournament = await Tournament.create(name="New Tournament")
await tournament.save()
await Tournament.create(name='Second tournament')
await Event(name='Without participants', tournament_id=tournament.id).save()
event = Event(name='Test', tournament_id=tournament.id)
await Tournament.create(name="Second tournament")
await Event(name="Without participants", tournament_id=tournament.id).save()
event = Event(name="Test", tournament_id=tournament.id)
await event.save()
participants = []
for i in range(2):
team = Team(name='Team {}'.format(i + 1))
team = Team(name="Team {}".format(i + 1))
await team.save()
participants.append(team)
await event.participants.add(participants[0], participants[1])
await event.participants.add(participants[0], participants[1])

print(await Tournament.all().annotate(
events_count=Count('events'),
).filter(events_count__gte=1))
print(await Tournament.all().annotate(events_count=Count("events")).filter(events_count__gte=1))

print(await Event.filter(id=event.id).first().annotate(
lowest_team_id=Min('participants__id')
))
print(await Event.filter(id=event.id).first().annotate(lowest_team_id=Min("participants__id")))

print(await Tournament.all().annotate(
events_count=Count('events'),
).order_by('events_count'))
print(await Tournament.all().annotate(events_count=Count("events")).order_by("events_count"))

print(await Event.all().annotate(
tournament_test_id=Sum('tournament__id'),
).first())
print(await Event.all().annotate(tournament_test_id=Sum("tournament__id")).first())


if __name__ == '__main__':
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(run())
18 changes: 9 additions & 9 deletions examples/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,26 @@ class Event(Model):
datetime = fields.DatetimeField(null=True)

class Meta:
table = 'event'
table = "event"

def __str__(self):
return self.name


async def run():
await Tortoise.init(config_file='config.json')
await Tortoise.init(config_file="config.json")
await Tortoise.generate_schemas()

event = await Event.create(name='Test')
await Event.filter(id=event.id).update(name='Updated name')
event = await Event.create(name="Test")
await Event.filter(id=event.id).update(name="Updated name")

print(await Event.filter(name='Updated name').first())
print(await Event.filter(name="Updated name").first())

await Event(name='Test 2').save()
print(await Event.all().values_list('id', flat=True))
print(await Event.all().values('id', 'name'))
await Event(name="Test 2").save()
print(await Event.all().values_list("id", flat=True))
print(await Event.all().values("id", "name"))


if __name__ == '__main__':
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(run())
48 changes: 26 additions & 22 deletions examples/complex_filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ def __str__(self):
class Event(Model):
id = fields.IntField(pk=True)
name = fields.TextField()
tournament = fields.ForeignKeyField('models.Tournament', related_name='events')
tournament = fields.ForeignKeyField("models.Tournament", related_name="events")
participants = fields.ManyToManyField(
'models.Team', related_name='events', through='event_team'
"models.Team", related_name="events", through="event_team"
)

def __str__(self):
Expand All @@ -39,47 +39,51 @@ def __str__(self):


async def run():
await Tortoise.init(config_file='config.json')
await Tortoise.init(config_file="config.json")
await Tortoise.generate_schemas()

tournament = Tournament(name='Tournament')
tournament = Tournament(name="Tournament")
await tournament.save()

second_tournament = Tournament(name='Tournament 2')
second_tournament = Tournament(name="Tournament 2")
await second_tournament.save()

event_first = Event(name='1', tournament=tournament)
event_first = Event(name="1", tournament=tournament)
await event_first.save()
event_second = await Event.create(name='2', tournament=second_tournament)
await Event.create(name='3', tournament=tournament)
await Event.create(name='4', tournament=second_tournament)
event_second = await Event.create(name="2", tournament=second_tournament)
await Event.create(name="3", tournament=tournament)
await Event.create(name="4", tournament=second_tournament)

await Event.filter(tournament=tournament)

team_first = Team(name='First')
team_first = Team(name="First")
await team_first.save()
team_second = Team(name='Second')
team_second = Team(name="Second")
await team_second.save()

await team_first.events.add(event_first)
await event_second.participants.add(team_second)

print(await Event.filter(
Q(id__in=[event_first.id, event_second.id])
| Q(name='3')).filter(
participants__not=team_second.id).order_by('tournament__id').distinct())
print(
await Event.filter(Q(id__in=[event_first.id, event_second.id]) | Q(name="3"))
.filter(participants__not=team_second.id)
.order_by("tournament__id")
.distinct()
)

print(await Team.filter(events__tournament_id=tournament.id).order_by('-events__name'))
print(await Tournament.filter(
events__name__in=['1', '3'],
).order_by('-events__participants__name').distinct())
print(await Team.filter(events__tournament_id=tournament.id).order_by("-events__name"))
print(
await Tournament.filter(events__name__in=["1", "3"])
.order_by("-events__participants__name")
.distinct()
)

print(await Team.filter(name__icontains='CON'))
print(await Team.filter(name__icontains="CON"))

print(await Tournament.filter(events__participants__name__startswith='Fir'))
print(await Tournament.filter(events__participants__name__startswith="Fir"))
print(await Tournament.filter(id__icontains=1).count())


if __name__ == '__main__':
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(run())
24 changes: 13 additions & 11 deletions examples/complex_prefetching.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ def __str__(self):
class Event(Model):
id = fields.IntField(pk=True)
name = fields.TextField()
tournament = fields.ForeignKeyField('models.Tournament', related_name='events')
tournament = fields.ForeignKeyField("models.Tournament", related_name="events")
participants = fields.ManyToManyField(
'models.Team', related_name='events', through='event_team'
"models.Team", related_name="events", through="event_team"
)

def __str__(self):
Expand All @@ -34,19 +34,21 @@ def __str__(self):


async def run():
await Tortoise.init(config_file='config.json')
await Tortoise.init(config_file="config.json")
await Tortoise.generate_schemas()

tournament = await Tournament.create(name='tournament')
await Event.create(name='First', tournament=tournament)
await Event.create(name='Second', tournament=tournament)
tournament_with_filtered = await Tournament.all().prefetch_related(
Prefetch('events', queryset=Event.filter(name='First'))
).first()
tournament = await Tournament.create(name="tournament")
await Event.create(name="First", tournament=tournament)
await Event.create(name="Second", tournament=tournament)
tournament_with_filtered = (
await Tournament.all()
.prefetch_related(Prefetch("events", queryset=Event.filter(name="First")))
.first()
)
print(tournament_with_filtered)
print(await Tournament.first().prefetch_related('events'))
print(await Tournament.first().prefetch_related("events"))


if __name__ == '__main__':
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(run())
40 changes: 18 additions & 22 deletions examples/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,32 @@ def __str__(self):


async def run():
await Tortoise.init({
'connections': {
'default': {
'engine': 'tortoise.backends.asyncpg',
'credentials': {
'host': 'localhost',
'port': '5432',
'user': 'tortoise',
'password': 'qwerty123',
'database': 'test',
await Tortoise.init(
{
"connections": {
"default": {
"engine": "tortoise.backends.asyncpg",
"credentials": {
"host": "localhost",
"port": "5432",
"user": "tortoise",
"password": "qwerty123",
"database": "test",
},
}
}
},
"apps": {"models": {"models": ["__main__"], "default_connection": "default"}},
},
'apps': {
'models': {
'models': ['__main__'],
'default_connection': 'default',
}
}
}, _create_db=True)
_create_db=True,
)
await Tortoise.generate_schemas()

report_data = {
'foo': 'bar',
}
report_data = {"foo": "bar"}
print(await Report.create(content=report_data))
print(await Report.filter(content=report_data).first())
await Tortoise._drop_databases()


if __name__ == '__main__':
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(run())
Loading

0 comments on commit d4b32c0

Please sign in to comment.