Skip to content

Commit

Permalink
Improve Object and subclasses
Browse files Browse the repository at this point in the history
Add __slots__ to all Objects and subclasses for memory savings and use
__init_subclass__ to automatically populate the TYPES_DICT.
  • Loading branch information
tylertian123 committed Oct 24, 2020
1 parent 4e575ab commit 1997d8a
Showing 1 changed file with 49 additions and 24 deletions.
73 changes: 49 additions & 24 deletions pyryver/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ def get_border_color(self) -> str:
return self._data["colors"]["border"]


#: A dict that maps internal strings for object types to classes.
TYPES_DICT = {}


class Object(ABC):
"""
Base class for all Ryver objects.
Expand All @@ -136,6 +140,8 @@ class Object(ABC):
:param data: The object's data.
"""

__slots__ = ("_ryver", "_data", "_id")

# The _OBJ_TYPE of each class inheriting from Object is used during object creation to determine the type
_OBJ_TYPE = "__object"

Expand All @@ -149,6 +155,11 @@ def __eq__(self, other) -> bool:

def __hash__(self) -> int:
return self.get_id()

def __init_subclass__(cls, **kwargs):
super().__init_subclass__(**kwargs)
# Register types in the dict
TYPES_DICT[cls._OBJ_TYPE] = cls

def get_ryver(self) -> "Ryver":
"""
Expand Down Expand Up @@ -383,6 +394,8 @@ class Message(Object):
Any generic Ryver message, with an author, body, and reactions.
"""

__slots__ = ()

_OBJ_TYPE = "__message"

def get_body(self) -> str:
Expand Down Expand Up @@ -462,6 +475,8 @@ class PostedMessage(Message):
A topic, task, topic reply, etc.
"""

__slots__ = ()

_OBJ_TYPE = "__postedMessage"

async def get_attachments(self) -> typing.List["Storage"]:
Expand Down Expand Up @@ -494,6 +509,8 @@ class PostedComment(PostedMessage):
A topic reply or task comment.
"""

__slots__ = ()

_OBJ_TYPE = "__comment"

# Override as a different field is used
Expand Down Expand Up @@ -548,6 +565,8 @@ class TopicReply(PostedComment):
A reply on a topic.
"""

__slots__ = ()

_OBJ_TYPE = TYPE_TOPIC_REPLY

async def get_topic(self) -> "Topic":
Expand All @@ -564,6 +583,8 @@ class Topic(PostedMessage):
A Ryver topic in a chat.
"""

__slots__ = ()

_OBJ_TYPE = TYPE_TOPIC

def get_subject(self) -> str:
Expand Down Expand Up @@ -720,6 +741,8 @@ class ChatMessage(Message):
:cvar SUBTYPE_TASK_ANNOUNCEMENT: An automatic chat message that announces the creation of a new task.
"""

__slots__ = ()

_OBJ_TYPE = "__chatMessage"

MSG_TYPE_PRIVATE = "chat"
Expand Down Expand Up @@ -976,6 +999,8 @@ class Chat(Object):
E.g. Teams, forums, user DMs, etc.
"""

__slots__ = ()

_OBJ_TYPE = "__chat"

def get_jid(self) -> str:
Expand Down Expand Up @@ -1382,6 +1407,8 @@ class User(Chat):
:cvar USER_TYPE_GUEST: A guest.
"""

__slots__ = ()

_OBJ_TYPE = TYPE_USER

ROLE_USER = "ROLE_USER"
Expand Down Expand Up @@ -1647,6 +1674,8 @@ class GroupChatMember(Object):
:cvar ROLE_ADMIN: Forum/team admin.
"""

__slots__ = ()

_OBJ_TYPE = TYPE_GROUPCHAT_MEMBER

ROLE_MEMBER = "ROLE_TEAM_MEMBER"
Expand Down Expand Up @@ -1709,6 +1738,8 @@ class GroupChat(Chat):
A Ryver team or forum.
"""

__slots__ = ()

_OBJ_TYPE = "__groupChat"

def get_name(self) -> str:
Expand Down Expand Up @@ -1972,6 +2003,8 @@ class Forum(GroupChat):
A Ryver forum.
"""

__slots__ = ()

_OBJ_TYPE = TYPE_FORUM


Expand All @@ -1980,6 +2013,8 @@ class Team(GroupChat):
A Ryver team.
"""

__slots__ = ()

_OBJ_TYPE = TYPE_TEAM


Expand All @@ -1991,6 +2026,8 @@ class TaskBoard(Object):
:cvar BOARD_TYPE_LIST: A task list (i.e. a task board without categories).
"""

__slots__ = ()

_OBJ_TYPE = TYPE_TASK_BOARD

BOARD_TYPE_BOARD = "board"
Expand Down Expand Up @@ -2196,6 +2233,8 @@ class TaskCategory(Object):
:cvar CATEGORY_TYPE_OTHER: Other categories (user-created and not marked as done).
"""

__slots__ = ()

_OBJ_TYPE = TYPE_TASK_CATEGORY

CATEGORY_TYPE_UNCATEGORIZED = "uncategorized"
Expand Down Expand Up @@ -2370,6 +2409,8 @@ class Task(PostedMessage):
A Ryver task.
"""

__slots__ = ()

_OBJ_TYPE = TYPE_TASK

def is_archived(self) -> bool:
Expand Down Expand Up @@ -2811,6 +2852,8 @@ class TaskComment(PostedComment):
A comment on a task.
"""

__slots__ = ()

_OBJ_TYPE = TYPE_TASK_COMMENT

async def get_task(self) -> Task:
Expand All @@ -2832,6 +2875,8 @@ class Notification(Object):
:cvar PREDICATE_TASK_COMPLETED: A task was completed.
"""

__slots__ = ()

_OBJ_TYPE = TYPE_NOTIFICATION

PREDICATE_MENTION = "chat_mention"
Expand Down Expand Up @@ -2999,6 +3044,8 @@ class File(Object):
An uploaded file.
"""

__slots__ = ()

_OBJ_TYPE = TYPE_FILE

def get_title(self) -> str:
Expand Down Expand Up @@ -3079,6 +3126,8 @@ class Storage(Object):
:cvar STORAGE_TYPE_LINK: A link.
"""

__slots__ = ()

_OBJ_TYPE = TYPE_STORAGE

STORAGE_TYPE_FILE = "file"
Expand Down Expand Up @@ -3186,30 +3235,6 @@ async def make_avatar_of(self, chat: Chat) -> None:
await self._ryver._session.post(url, json=data)


TYPES_DICT = {
Object._OBJ_TYPE: Object,
Message._OBJ_TYPE: Message,
PostedMessage._OBJ_TYPE: PostedMessage,
PostedComment._OBJ_TYPE: PostedComment,
TopicReply._OBJ_TYPE: TopicReply,
Topic._OBJ_TYPE: Topic,
ChatMessage._OBJ_TYPE: ChatMessage,
Chat._OBJ_TYPE: Chat,
User._OBJ_TYPE: User,
GroupChatMember._OBJ_TYPE: GroupChatMember,
GroupChat._OBJ_TYPE: GroupChat,
Forum._OBJ_TYPE: Forum,
Team._OBJ_TYPE: Team,
TaskBoard._OBJ_TYPE: TaskBoard,
TaskCategory._OBJ_TYPE: TaskCategory,
Task._OBJ_TYPE: Task,
TaskComment._OBJ_TYPE: TaskComment,
Notification._OBJ_TYPE: Notification,
File._OBJ_TYPE: File,
Storage._OBJ_TYPE: Storage,
}


def get_obj_by_field(objs: typing.Iterable[Object], field: str, value: typing.Any, case_sensitive: str = True) -> Object:
"""
Gets an object from a list of objects by a field.
Expand Down

0 comments on commit 1997d8a

Please sign in to comment.