Simplify some conditions (#632)

This commit is contained in:
Almaz 2021-07-18 14:20:07 +03:00 committed by GitHub
parent 04a64fbfc5
commit 70aa80abf1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 38 additions and 55 deletions

View file

@ -330,9 +330,8 @@ class Dispatcher(DataMixin, ContextInstanceMixin):
def _loop_create_task(self, coro):
if self._main_loop is None:
return asyncio.create_task(coro)
else:
_ensure_loop(self._main_loop)
return self._main_loop.create_task(coro)
_ensure_loop(self._main_loop)
return self._main_loop.create_task(coro)
async def start_polling(self,
timeout=20,
@ -1394,29 +1393,26 @@ class Dispatcher(DataMixin, ContextInstanceMixin):
no_error=True)
if is_not_throttled:
return await func(*args, **kwargs)
else:
kwargs.update(
{
'rate': rate,
'key': key,
'user_id': user_id,
'chat_id': chat_id
}
) # update kwargs with parameters which were given to throttled
kwargs.update(
{
'rate': rate,
'key': key,
'user_id': user_id,
'chat_id': chat_id,
}
) # update kwargs with parameters which were given to throttled
if on_throttled:
if asyncio.iscoroutinefunction(on_throttled):
await on_throttled(*args, **kwargs)
else:
kwargs.update(
{
'loop': asyncio.get_running_loop()
}
)
partial_func = functools.partial(on_throttled, *args, **kwargs)
asyncio.get_running_loop().run_in_executor(None,
partial_func
)
if on_throttled:
if asyncio.iscoroutinefunction(on_throttled):
await on_throttled(*args, **kwargs)
else:
kwargs.update({'loop': asyncio.get_running_loop()})
partial_func = functools.partial(
on_throttled, *args, **kwargs
)
asyncio.get_running_loop().run_in_executor(
None, partial_func
)
return wrapped
return decorator

View file

@ -279,9 +279,10 @@ class Text(Filter):
elif check == 0:
raise ValueError(f"No one mode is specified!")
equals, contains, endswith, startswith = map(lambda e: [e] if isinstance(e, str) or isinstance(e, LazyProxy)
else e,
(equals, contains, endswith, startswith))
equals, contains, endswith, startswith = map(
lambda e: [e] if isinstance(e, (str, LazyProxy)) else e,
(equals, contains, endswith, startswith),
)
self.equals = equals
self.contains = contains
self.endswith = endswith

View file

@ -188,10 +188,9 @@ class WebhookRequestHandler(web.View):
if fut.done():
return fut.result()
else:
# context.set_value(WEBHOOK_CONNECTION, False)
fut.remove_done_callback(cb)
fut.add_done_callback(self.respond_via_request)
# context.set_value(WEBHOOK_CONNECTION, False)
fut.remove_done_callback(cb)
fut.add_done_callback(self.respond_via_request)
finally:
timeout_handle.cancel()

View file

@ -50,7 +50,7 @@ class InlineKeyboardMarkup(base.TelegramObject):
if index % self.row_width == 0:
self.inline_keyboard.append(row)
row = []
if len(row) > 0:
if row:
self.inline_keyboard.append(row)
return self

View file

@ -41,13 +41,9 @@ class InputFile(base.TelegramObject):
self._path = path_or_bytesio
if filename is None:
filename = os.path.split(path_or_bytesio)[-1]
elif isinstance(path_or_bytesio, io.IOBase):
elif isinstance(path_or_bytesio, (io.IOBase, _WebPipe)):
self._path = None
self._file = path_or_bytesio
elif isinstance(path_or_bytesio, _WebPipe):
self._path = None
self._file = path_or_bytesio
elif isinstance(path_or_bytesio, Path):
self._file = path_or_bytesio.open("rb")
self._path = path_or_bytesio.resolve()
@ -174,10 +170,7 @@ class _WebPipe:
def name(self):
if not self._name:
*_, part = self.url.rpartition('/')
if part:
self._name = part
else:
self._name = secrets.token_urlsafe(24)
self._name = part or secrets.token_urlsafe(24)
return self._name
async def open(self):

View file

@ -50,11 +50,9 @@ class MessageEntity(base.TelegramObject):
if sys.maxunicode == 0xFFFF:
return text[self.offset : self.offset + self.length]
if not isinstance(text, bytes):
entity_text = text.encode("utf-16-le")
else:
entity_text = text
entity_text = (
text.encode("utf-16-le") if not isinstance(text, bytes) else text
)
entity_text = entity_text[self.offset * 2 : (self.offset + self.length) * 2]
return entity_text.decode("utf-16-le")

View file

@ -67,7 +67,7 @@ class ReplyKeyboardMarkup(base.TelegramObject):
if index % self.row_width == 0:
self.keyboard.append(row)
row = []
if len(row) > 0:
if row:
self.keyboard.append(row)
return self

View file

@ -130,7 +130,6 @@ class CallbackDataFilter(Filter):
if isinstance(value, (list, tuple, set, frozenset)):
if data.get(key) not in value:
return False
else:
if data.get(key) != value:
return False
elif data.get(key) != value:
return False
return {'callback_data': data}

View file

@ -103,10 +103,7 @@ class HelperMode(Helper):
if symbol == '_' and pos > 0:
need_upper = True
else:
if need_upper:
result += symbol.upper()
else:
result += symbol.lower()
result += symbol.upper() if need_upper else symbol.lower()
need_upper = False
if first_upper:
result = result[0].upper() + result[1:]