Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upGitHub is where the world builds software
Millions of developers and companies build, ship, and maintain their software on GitHub — the largest and most advanced development platform in the world.
Add full_name property in User class #335
Conversation
|
Some thoughts on this PR. Overall good idea. As from the first comment I'd suggest a different syntax to prevent potential errors. |
| full_name=(user.first_name + " " + | ||
| user.last_name if user.last_name else None), |
ColinTheShark
Nov 6, 2019
Member
That handling of full_name would likely result in a TypeError because of invalid operand + between NoneType and str as well as Python not being able to concatenate NoneType and str.
This might be an ugly workaround, but it's a working one.
"{} {}".format(user.first_name, user.last_name) if user.last_name else user.first_name
That above line would always only return a string without trailing whitespace. Alternatively we could do the following to avoid the use of .format():
user.first_name + " " + user.last_name if user.last_name else user.first_name
That handling of full_name would likely result in a TypeError because of invalid operand + between NoneType and str as well as Python not being able to concatenate NoneType and str.
This might be an ugly workaround, but it's a working one.
"{} {}".format(user.first_name, user.last_name) if user.last_name else user.first_name
That above line would always only return a string without trailing whitespace. Alternatively we could do the following to avoid the use of .format():
user.first_name + " " + user.last_name if user.last_name else user.first_name
JosXa
Dec 22, 2019
Contributor
I don't see a reason why .format() should be avoided (not sure about f-strings and pyro compatibility, would have to check...)
1st option LGTM.
I don't see a reason why .format() should be avoided (not sure about f-strings and pyro compatibility, would have to check...)
1st option LGTM.
tasi788
Mar 16, 2020
Author
I've change the code, please have review.
I've change the code, please have review.
| photo=ChatPhoto._parse(client, user.photo, user.id, user.access_hash), | ||
| restrictions=pyrogram.List([Restriction._parse(r) for r in user.restriction_reason]) or None, | ||
| photo=ChatPhoto._parse( | ||
| client, user.photo, user.id, user.access_hash), | ||
| restrictions=pyrogram.List( | ||
| [Restriction._parse(r) for r in user.restriction_reason]) or None, |
ColinTheShark
Nov 6, 2019
Member
These changes don't change the code itself, but only the layout/format of it. I guess you've used a formatter and I suggest changing the maximum amount of characters per line on your formatter, as Pyrogram doesn't adhere to PEP8's 80 char limit, but rather it's own 120 (if I remember correctly). Your formatter creates unnecessary diffs.
These changes don't change the code itself, but only the layout/format of it. I guess you've used a formatter and I suggest changing the maximum amount of characters per line on your formatter, as Pyrogram doesn't adhere to PEP8's 80 char limit, but rather it's own 120 (if I remember correctly). Your formatter creates unnecessary diffs.
tasi788
Mar 16, 2020
Author
I didn't notice that my IDE do autopep8 automatically, now should be good.
I didn't notice that my IDE do autopep8 automatically, now should be good.
using format instead
|
This should be a property, by the way. |
|
I also experimented with custom formats in the past, which would allow things like However, this only works for string interpolations and having |
|
I have a constant need for def displayname(entity: Any) -> str:
if hasattr(entity, "title"):
return entity.title
elif hasattr(entity, "first_name"):
if entity.last_name:
return f"{entity.first_name} {entity.last_name}"
return entity.first_name
raise ValueError("This entity does not seem to have a display name.")
def user_or_displayname(entity: Any) -> str:
if entity.username:
return f"@{entity.username}"
return displayname(entity) |
|
Maybe also related? from enum import IntEnum
from pyrogram import Client
from pyrogram.raw.types import Channel
from pyrogram.types import Message, User, Chat
from typing import Optional, Union, cast, Dict
_links_cache: Dict[int, str] = {}
class Platform(IntEnum):
android = 1
ios = 2
web = 3
desktop = 4
async def direct_link_to_message(
reference: Message, platform: Optional[Platform] = Platform.android
) -> str:
entity_link = await direct_link(reference._client, reference.chat, platform)
return f"{entity_link}/{reference.message_id}"
async def direct_link(
client: Client,
peer: Union[User, Chat, Channel],
platform: Optional[Platform] = Platform.android,
) -> str:
if peer.username:
return f"https://t.me/{peer.username}"
else:
if not isinstance(peer, User):
peer_id = peer.id
if isinstance(peer, Channel):
return f"https://t.me/c/{peer_id}"
invite_link = _links_cache.get(peer_id, None)
if not invite_link:
invite_link = (await client.get_chat(peer_id)).invite_link
_links_cache[peer_id] = invite_link
if invite_link:
return cast(str, invite_link)
if platform == Platform.android:
return f"tg://openmessage?user_id={peer.id}"
elif platform == Platform.ios:
return f"t.me/@{peer.id}"
elif platform == Platform.web:
# TODO: maybe incorrect, test!
return f"https://web.Telegram.org/#/im?p=u{peer.id}"
else:
raise ValueError(
f"User has no username, creating direct link on platform {platform} not possible."
) |

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.

No description provided.