Skip to content

Commit

Permalink
Do minor refactor (#171)
Browse files Browse the repository at this point in the history
- Replace `getattr` with `enum[...]` for cleaner code
  • Loading branch information
vsakkas authored May 12, 2024
1 parent 2bbf6b0 commit 385802c
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions sydney/sydney.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,11 @@ def __init__(
"""
self.bing_cookies = bing_cookies if bing_cookies else getenv("BING_COOKIES")
self.use_proxy = use_proxy
self.conversation_style: ConversationStyle = getattr(
ConversationStyle, style.upper()
self.conversation_style: ConversationStyle = ConversationStyle[style.upper()]
self.conversation_style_option_sets: ConversationStyleOptionSets = (
ConversationStyleOptionSets[style.upper()]
)
self.conversation_style_option_sets: ConversationStyleOptionSets = getattr(
ConversationStyleOptionSets, style.upper()
)
self.persona: GPTPersonaID = getattr(GPTPersonaID, persona.upper())
self.persona: GPTPersonaID = GPTPersonaID[persona.upper()]
self.conversation_signature: str | None = None
self.encrypted_conversation_signature: str | None = None
self.conversation_id: str | None = None
Expand Down Expand Up @@ -665,8 +663,8 @@ async def compose(
"""
# Get the enum values corresponding to the given tone, format, and length.
compose_tone = getattr(ComposeTone, tone.upper(), CustomComposeTone(tone))
compose_format = getattr(ComposeFormat, format.upper())
compose_length = getattr(ComposeLength, length.upper())
compose_format = ComposeFormat[format.upper()]
compose_length = ComposeLength[length.upper()]

async for response, suggested_responses in self._ask(
prompt,
Expand Down Expand Up @@ -731,8 +729,8 @@ async def compose_stream(
"""
# Get the enum values corresponding to the given tone, format, and length.
compose_tone = getattr(ComposeTone, tone.upper(), CustomComposeTone(tone))
compose_format = getattr(ComposeFormat, format.upper())
compose_length = getattr(ComposeLength, length.upper())
compose_format = ComposeFormat[format.upper()]
compose_length = ComposeLength[length.upper()]

previous_response: str | dict = ""
async for response, suggested_responses in self._ask(
Expand Down Expand Up @@ -777,9 +775,9 @@ async def reset_conversation(self, style: str | None = None) -> None:
"""
await self.close_conversation()
if style:
self.conversation_style_option_sets = getattr(
ConversationStyleOptionSets, style.upper()
)
self.conversation_style_option_sets = ConversationStyleOptionSets[
style.upper()
]
await self.start_conversation()

async def close_conversation(self) -> None:
Expand Down

0 comments on commit 385802c

Please sign in to comment.