Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Micro-optimizations #2088

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
6 changes: 3 additions & 3 deletions tests/test_generic_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ async def receive(self, text_data=None, bytes_data=None):
# Test that the specific channel layer is retrieved
assert channel_layer is not None

channel_name = list(channel_layer.channels.keys())[0]
channel_name = next(iter(channel_layer.channels))
message = {"type": "websocket.receive", "text": "hello"}
await channel_layer.send(channel_name, message)
response = await communicator.receive_from()
Expand Down Expand Up @@ -386,7 +386,7 @@ async def _my_private_handler(self, _):
# Test that the specific channel layer is retrieved
assert channel_layer is not None

channel_name = list(channel_layer.channels.keys())[0]
channel_name = next(iter(channel_layer.channels))
# Should block call to private functions handler and raise ValueError
message = {"type": "_my_private_handler", "text": "hello"}
await channel_layer.send(channel_name, message)
Expand Down Expand Up @@ -422,7 +422,7 @@ async def _my_private_handler(self, _):
# Test that the specific channel layer is retrieved
assert channel_layer is not None

channel_name = list(channel_layer.channels.keys())[0]
channel_name = next(iter(channel_layer.channels))
# Should not replace dot by underscore and call private function (see
# issue: #1430)
message = {"type": ".my_private_handler", "text": "hello"}
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
Loading