Skip to content

Commit df415fe

Browse files
committed
Apply formatting to all Python modules
1 parent ac471aa commit df415fe

27 files changed

Lines changed: 47 additions & 51 deletions

ultimatepython/advanced/async.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
hit an API endpoint, ask the operating system for resources) so we assume
77
that starting a job has some intrinsic delay.
88
"""
9+
910
import asyncio
1011
from dataclasses import dataclass
1112
from datetime import datetime

ultimatepython/advanced/benchmark.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
and their code intuition to optimize programs further. This module uses
66
cProfile to compare the performance of two functions with each other.
77
"""
8+
89
import cProfile
910
import io
1011
import pstats
@@ -54,8 +55,7 @@ def main():
5455
# large projects. Consider profiling in isolation when analyzing complex
5556
# classes and functions
5657
ps.print_stats()
57-
time_sleep_called = any("60" in line and "time.sleep" in line
58-
for line in buffer.getvalue().split("\n"))
58+
time_sleep_called = any("60" in line and "time.sleep" in line for line in buffer.getvalue().split("\n"))
5959
assert time_sleep_called is True
6060

6161

ultimatepython/advanced/context_manager.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
we simulate how a context manager can handle open and close operations of
66
a file-like object called StringIO.
77
"""
8+
89
from contextlib import contextmanager
910
from io import StringIO
1011

ultimatepython/advanced/data_format.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
converting data to serialized objects for further analysis. This module
44
shows how to parse and process these data formats: JSON, XML and CSV.
55
"""
6+
67
import json
78
from csv import DictReader
89
from dataclasses import dataclass, fields
@@ -59,6 +60,7 @@ class Note:
5960
associated with them. To streamline the creation and comparison of
6061
these records, we define an in-memory model of what it is.
6162
"""
63+
6264
author: str
6365
title: str
6466
body: str
@@ -84,12 +86,7 @@ def main():
8486
# Let's use `ElementTree.parse` to parse note data from a XML file
8587
# https://docs.python.org/3/library/xml.html
8688
tree = ETree.parse(StringIO(_XML_DATA))
87-
xml_notes = [
88-
Note.from_data({
89-
field: note_el.findtext(field)
90-
for field in Note.fields()
91-
}) for note_el in tree.getroot()
92-
]
89+
xml_notes = [Note.from_data({field: note_el.findtext(field) for field in Note.fields()}) for note_el in tree.getroot()]
9390
assert all(isinstance(note, Note) for note in xml_notes)
9491

9592
# Let's use `csv.DictReader` to parse note data from a CSV file

ultimatepython/advanced/date_time.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
UTC timezone and show how it can be used to make the default `datetime`
2323
object more powerful.
2424
"""
25+
2526
from datetime import datetime, timezone
2627

2728

ultimatepython/advanced/decorator.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
be decorated to work with a collection of strings. Note that the decorator
55
handles nested collections with the use of recursion.
66
"""
7+
78
from functools import wraps
89

910
# Module-level constants

ultimatepython/advanced/file_handling.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
builtin 'open' function to open files in different modes like
88
reading ('r'), writing ('w'), and appending ('a').
99
"""
10+
1011
import os
1112

1213
_TARGET_FILE = "sample.txt"

ultimatepython/advanced/meta_class.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
This module shows how a metaclass can add database attributes and tables
44
to "logic-free" model classes for the developer.
55
"""
6+
67
from abc import ABC
78

89

@@ -66,11 +67,7 @@ def __new__(mcs, name, bases, attrs):
6667
kls.model_fields.update(base.model_fields)
6768

6869
# Fill model fields from itself
69-
kls.model_fields.update({
70-
field_name: field_obj
71-
for field_name, field_obj in attrs.items()
72-
if isinstance(field_obj, BaseField)
73-
})
70+
kls.model_fields.update({field_name: field_obj for field_name, field_obj in attrs.items() if isinstance(field_obj, BaseField)})
7471

7572
# Register a real table (a table with valid `model_name`) to
7673
# the metaclass `table` registry. After all the tables are
@@ -121,12 +118,14 @@ class BaseModel(metaclass=ModelMeta):
121118
In short, think of a metaclass as the creator of classes. This is
122119
very similar to how classes are the creator of instances.
123120
"""
121+
124122
__abstract__ = True # This is NOT a real table
125123
row_id = IntegerField()
126124

127125

128126
class UserModel(BaseModel):
129127
"""User model."""
128+
130129
__table_name__ = "user_rocks" # This is a custom table name
131130
username = CharField()
132131
password = CharField()
@@ -136,6 +135,7 @@ class UserModel(BaseModel):
136135

137136
class AddressModel(BaseModel):
138137
"""Address model."""
138+
139139
user_id = IntegerField()
140140
address = CharField()
141141
state = CharField()
@@ -168,8 +168,7 @@ def main():
168168

169169
# Every model is created by `ModelMeta`
170170
assert isinstance(BaseModel, ModelMeta)
171-
assert all(isinstance(model, ModelMeta)
172-
for model in BaseModel.__subclasses__())
171+
assert all(isinstance(model, ModelMeta) for model in BaseModel.__subclasses__())
173172

174173
# And `ModelMeta` is created by `type`
175174
assert isinstance(ModelMeta, type)

ultimatepython/advanced/mixin.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
mixins to illustrate how mixins can make our lives easier when defining
66
concrete classes.
77
"""
8+
89
from abc import ABC, abstractmethod
910
from dataclasses import dataclass
1011

@@ -16,6 +17,7 @@ class Request:
1617
Assumes only GET requests for simplicity so there is no method
1718
attribute associated with this class.
1819
"""
20+
1921
url: str
2022
user: str
2123

@@ -48,6 +50,7 @@ class TemplateHandlerMixin(RequestHandler):
4850
class helps if downstream developers typically implement request
4951
handlers that retrieve template content.
5052
"""
53+
5154
template_suffix = ".template"
5255

5356
def handle(self, request):
@@ -118,8 +121,7 @@ def get_template_name(self, request_url):
118121
return request_url[1:]
119122

120123
def is_valid_template(self, template_name):
121-
return (super().is_valid_template(template_name)
122-
and template_name in self.template_dir)
124+
return super().is_valid_template(template_name) and template_name in self.template_dir
123125

124126
def render_template(self, template_name):
125127
return self.template_dir[template_name]
@@ -143,8 +145,7 @@ def is_valid_user(self, request_user):
143145

144146
def main():
145147
# Handle requests with simple template handler
146-
simple_dir = {"welcome.template": "<p>Hello world</p>",
147-
"about.template": "<p>About me</p>"}
148+
simple_dir = {"welcome.template": "<p>Hello world</p>", "about.template": "<p>About me</p>"}
148149
simple_handler = TemplateFolderHandler(simple_dir)
149150
welcome_from_nobody = Request("/welcome.template", "nobody")
150151
about_from_nobody = Request("/about.template", "nobody")
@@ -155,8 +156,7 @@ def main():
155156

156157
# Handle requests with admin template handler
157158
admin_users = {"john", "jane"}
158-
admin_dir = {"fqdn.template": "<p>server.example.com</p>",
159-
"salary.template": "<p>123456789.00</p>"}
159+
admin_dir = {"fqdn.template": "<p>server.example.com</p>", "salary.template": "<p>123456789.00</p>"}
160160
admin_handler = AdminTemplateHandler(admin_users, admin_dir)
161161
fqdn_from_john = Request("/fqdn.template", "john")
162162
salary_from_jane = Request("/salary.template", "jane")

ultimatepython/advanced/mocking.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
properly. This module shows how to use mocking to modify an application
55
server so that it is easier to test.
66
"""
7+
78
from collections import Counter
89
from unittest.mock import MagicMock, PropertyMock, patch
910

0 commit comments

Comments
 (0)