Skip to content

Commit 530adc3

Browse files
committed
Micro optimizations
1 parent 666a342 commit 530adc3

File tree

6 files changed

+15
-14
lines changed

6 files changed

+15
-14
lines changed

channels/layers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def valid_group_name(self, name):
166166
raise TypeError(self.invalid_name_error.format("Group", name))
167167

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

channels/security/websocket.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,10 @@ def get_origin_port(self, origin):
123123
# Return origin.port
124124
return origin.port
125125
# if origin.port doesn`t exists
126-
if origin.scheme == "http" or origin.scheme == "ws":
126+
if origin.scheme in {"http", "ws"}:
127127
# Default port return for http, ws
128128
return 80
129-
elif origin.scheme == "https" or origin.scheme == "wss":
129+
elif origin.scheme in {"https", "wss"}:
130130
# Default port return for https, wss
131131
return 443
132132
else:

channels/testing/live.py

+1
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,4 @@ def _is_in_memory_db(self, connection):
7474
"""
7575
if connection.vendor == "sqlite":
7676
return connection.is_in_memory_db()
77+
return None

channels/worker.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ async def handle(self):
2121
Listens on all the provided channels and handles the messages.
2222
"""
2323
# For each channel, launch its own listening coroutine
24-
listeners = []
25-
for channel in self.channels:
26-
listeners.append(asyncio.ensure_future(self.listener(channel)))
24+
listeners = [
25+
asyncio.ensure_future(self.listener(channel)) for channel in self.channels
26+
]
2727
# Wait for them all to exit
2828
await asyncio.wait(listeners)
2929
# See if any of the listeners had an error (e.g. channel layer error)

tests/test_generic_websocket.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ async def receive(self, text_data=None, bytes_data=None):
280280
# Test that the specific channel layer is retrieved
281281
assert channel_layer is not None
282282

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

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

418-
channel_name = list(channel_layer.channels.keys())[0]
418+
channel_name = next(iter(channel_layer.channels))
419419
# Should not replace dot by underscore and call private function (see
420420
# issue: #1430)
421421
message = {"type": ".my_private_handler", "text": "hello"}

tests/test_routing.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -93,18 +93,18 @@ async def test_url_router():
9393
# Valid keyword argument matches
9494
assert await router({"type": "http", "path": "/kwarg/hello/"}, None, None) == 5
9595
assert kwarg_app.call_args[0][0]["url_route"] == {
96-
"args": tuple(),
96+
"args": (),
9797
"kwargs": {"name": "hello"},
9898
}
9999
assert await router({"type": "http", "path": "/kwarg/hellothere/"}, None, None) == 5
100100
assert kwarg_app.call_args[0][0]["url_route"] == {
101-
"args": tuple(),
101+
"args": (),
102102
"kwargs": {"name": "hellothere"},
103103
}
104104
# Valid default keyword arguments
105105
assert await router({"type": "http", "path": "/defaultkwargs/"}, None, None) == 6
106106
assert defaultkwarg_app.call_args[0][0]["url_route"] == {
107-
"args": tuple(),
107+
"args": (),
108108
"kwargs": {"default": 42},
109109
}
110110
# Valid root_path in scope
@@ -246,13 +246,13 @@ async def test_url_router_path():
246246
await router({"type": "http", "path": "/author/andrewgodwin/"}, None, None) == 3
247247
)
248248
assert kwarg_app.call_args[0][0]["url_route"] == {
249-
"args": tuple(),
249+
"args": (),
250250
"kwargs": {"name": "andrewgodwin"},
251251
}
252252
# Named with typecasting
253253
assert await router({"type": "http", "path": "/year/2012/"}, None, None) == 3
254254
assert kwarg_app.call_args[0][0]["url_route"] == {
255-
"args": tuple(),
255+
"args": (),
256256
"kwargs": {"year": 2012},
257257
}
258258
# Invalid matches

0 commit comments

Comments
 (0)