Untitled
unknown
python
a year ago
4.7 kB
12
Indexable
from enum import auto, StrEnum
import warnings
MAX_QUOTE_LENGTH = 50
# The two classes below are available for you to use
# You do not need to implement them
class VariantMode(StrEnum):
NORMAL = auto()
UWU = auto()
PIGLATIN = auto()
class DuplicateError(Exception):
"""Error raised when there is an attempt to add a duplicate entry to a database"""
# Implement the class and function below
class Quote:
def __init__(self, quote: str, mode: "VariantMode" = VariantMode.NORMAL) -> None:
if len(quote) >= MAX_QUOTE_LENGTH:
raise ValueError("Quote is too long")
self.quote = quote
self.mode = mode
self._create_variant()
def __str__(self) -> str:
return self.quote
def li(self) -> str:
return f"- {self.quote}"
def _create_variant(self) -> str:
"""
Transforms the quote to the appropriate variant indicated by `self.mode` and returns the result
"""
if self.mode == VariantMode.NORMAL:
return self.quote
if self.mode == VariantMode.UWU:
nqt = self.quote.replace('L', 'W')
nqt = nqt.replace('l', 'w')
nqt = nqt.replace('R', 'W')
nqt = nqt.replace('r', 'w')
nnqt = nqt.replace(' u', ' u-u')
nnqt = nnqt.replace(' U', ' U-U')
if len(nnqt) >= MAX_QUOTE_LENGTH:
warnings.warn("Quote too long, only partially transformed")
else:
nqt = nnqt
if self.quote == nqt:
raise ValueError("Quote was not modified")
self.quote = nqt
return self.quote
if self.mode == VariantMode.PIGLATIN:
vowels = 'aeiouAEIOU'
words = []
for word in self.quote.split(' '):
if word[0] in vowels:
words.append(word + "way")
continue
cluster = "" # first letters until vowel
for i, c in enumerate(word):
if c in vowels:
words.append(word[i:] + cluster + "ay") # word except cluster + cluster + ay
break
cluster += c
nqt = ' '.join(words)
if len(nqt) >= MAX_QUOTE_LENGTH:
raise ValueError("Quote was not modified")
self.quote = nqt.capitalize()
return self.quote
def run_command(command: str) -> None:
"""
Will be given a command from a user. The command will be parsed and executed appropriately.
Current supported commands:
- `quote` - creates and adds a new quote
- `quote uwu` - uwu-ifys the new quote and then adds it
- `quote piglatin` - piglatin-ifys the new quote and then adds it
- `quote list` - print a formatted string that lists the current
quotes to be displayed in discord flavored markdown
"""
words = command.split(' ')
cmd = words[0]
if not cmd == "quote":
raise ValueError("Invalid command")
subcmd = words[1]
if subcmd[0] in "\"“":
qt = ' '.join(words[1:])[1:-1]
try:
Database.add_quote(Quote(qt))
except DuplicateError:
print("Quote has already been added previously")
elif subcmd == "list":
msg = []
for q in Database.quotes:
msg.append(q.li())
print('\n'.join(msg))
else:
qt = ' '.join(words[2:])[1:-1]
if subcmd == "uwu":
try:
Database.add_quote(Quote(qt, VariantMode.UWU))
except DuplicateError:
print("Quote has already been added previously")
elif subcmd == "piglatin":
try:
Database.add_quote(Quote(qt, VariantMode.PIGLATIN))
except DuplicateError:
print("Quote has already been added previously")
else:
raise ValueError("Invalid command")
# The code below is available for you to use
# You do not need to implement it, you can assume it will work as specified
class Database:
quotes: list["Quote"] = []
@classmethod
def get_quotes(cls) -> list[str]:
"Returns current quotes in a list"
return [str(quote) for quote in cls.quotes]
@classmethod
def add_quote(cls, quote: "Quote") -> None:
"Adds a quote. Will raise a `DuplicateError` if an error occurs."
if str(quote) in [str(quote) for quote in cls.quotes]:
raise DuplicateError
cls.quotes.append(quote)
Editor is loading...
Leave a Comment