Skip to content

Commit

Permalink
Micro optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
cclauss committed Apr 3, 2024
1 parent 666a342 commit 4a57e25
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 11 deletions.
2 changes: 1 addition & 1 deletion channels/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def valid_group_name(self, name):
raise TypeError(self.invalid_name_error.format("Group", name))

def valid_channel_names(self, names, receive=False):
_non_empty_list = True if names else False
_non_empty_list = bool(names)
_names_type = isinstance(names, list)
assert _non_empty_list and _names_type, "names must be a non-empty list"

Expand Down
4 changes: 2 additions & 2 deletions channels/security/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@ def get_origin_port(self, origin):
# Return origin.port
return origin.port
# if origin.port doesn`t exists
if origin.scheme == "http" or origin.scheme == "ws":
if origin.scheme in {"http", "ws"}:
# Default port return for http, ws
return 80
elif origin.scheme == "https" or origin.scheme == "wss":
elif origin.scheme in {"https", "wss"}:
# Default port return for https, wss
return 443
else:
Expand Down
1 change: 1 addition & 0 deletions channels/testing/live.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,4 @@ def _is_in_memory_db(self, connection):
"""
if connection.vendor == "sqlite":
return connection.is_in_memory_db()
return None
6 changes: 3 additions & 3 deletions channels/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ async def handle(self):
Listens on all the provided channels and handles the messages.
"""
# For each channel, launch its own listening coroutine
listeners = []
for channel in self.channels:
listeners.append(asyncio.ensure_future(self.listener(channel)))
listeners = [
asyncio.ensure_future(self.listener(channel)) for channel in self.channels
]
# Wait for them all to exit
await asyncio.wait(listeners)
# See if any of the listeners had an error (e.g. channel layer error)
Expand Down
10 changes: 5 additions & 5 deletions tests/test_routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,18 @@ async def test_url_router():
# Valid keyword argument matches
assert await router({"type": "http", "path": "/kwarg/hello/"}, None, None) == 5
assert kwarg_app.call_args[0][0]["url_route"] == {
"args": tuple(),
"args": (),
"kwargs": {"name": "hello"},
}
assert await router({"type": "http", "path": "/kwarg/hellothere/"}, None, None) == 5
assert kwarg_app.call_args[0][0]["url_route"] == {
"args": tuple(),
"args": (),
"kwargs": {"name": "hellothere"},
}
# Valid default keyword arguments
assert await router({"type": "http", "path": "/defaultkwargs/"}, None, None) == 6
assert defaultkwarg_app.call_args[0][0]["url_route"] == {
"args": tuple(),
"args": (),
"kwargs": {"default": 42},
}
# Valid root_path in scope
Expand Down Expand Up @@ -246,13 +246,13 @@ async def test_url_router_path():
await router({"type": "http", "path": "/author/andrewgodwin/"}, None, None) == 3
)
assert kwarg_app.call_args[0][0]["url_route"] == {
"args": tuple(),
"args": (),
"kwargs": {"name": "andrewgodwin"},
}
# Named with typecasting
assert await router({"type": "http", "path": "/year/2012/"}, None, None) == 3
assert kwarg_app.call_args[0][0]["url_route"] == {
"args": tuple(),
"args": (),
"kwargs": {"year": 2012},
}
# Invalid matches
Expand Down

0 comments on commit 4a57e25

Please sign in to comment.