Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
fisher60 committed Apr 6, 2021
0 parents commit cf41070
Show file tree
Hide file tree
Showing 13 changed files with 303 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/uwu_api.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM python:3.9-slim

RUN pip install pipenv

COPY . /app/

WORKDIR /app/

RUN pipenv install --system --deploy

ENTRYPOINT ["pipenv"]
CMD ["run", "start"]
16 changes: 16 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]

[packages]
fastapi = "*"
uvicorn = "*"

[requires]
python_version = "3.9"

[scripts]
start = "uvicorn api.main:app --host 0.0.0.0 --port 8005 --reload"
97 changes: 97 additions & 0 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file added api/__init__.py
Empty file.
14 changes: 14 additions & 0 deletions api/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from fastapi import FastAPI
from .utils import uwu

app = FastAPI()


@app.get("/")
async def root():
return {"message": "Welcome"}


@app.get("/uwu/{string}")
async def translate_to_uwu(string: str) -> dict:
return {"message": uwu.to_uwu(string)}
92 changes: 92 additions & 0 deletions api/utils/uwu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import functools
import re
import string

UWU_WORDS = {
"fi": "fwi",
"l": "w",
"r": "w",
"some": "sum",
"th": "d",
"thing": "fing",
"tho": "fo",
"you're": "yuw'we",
"your": "yur",
"you": "yuw",
}


def _replace_many(
sentence: str,
replacements: dict,
*,
ignore_case: bool = False,
match_case: bool = False,
) -> str:
"""
Replaces multiple substrings in a string given a mapping of strings.
By default replaces long strings before short strings, and lowercase before uppercase.
Example:
var = replace_many("This is a sentence", {"is": "was", "This": "That"})
assert var == "That was a sentence"
If `ignore_case` is given, does a case insensitive match.
Example:
var = replace_many("THIS is a sentence", {"IS": "was", "tHiS": "That"}, ignore_case=True)
assert var == "That was a sentence"
If `match_case` is given, matches the case of the replacement with the replaced word.
Example:
var = replace_many(
"This IS a sentence", {"is": "was", "this": "that"}, ignore_case=True, match_case=True
)
assert var == "That WAS a sentence"
This is shamelessly stolen from Python Discord's seasonalbot
https://github.com/CharlieADavies/seasonalbot/blob/master/bot/utils/__init__.py
"""
if ignore_case:
replacements = dict(
(word.lower(), replacement) for word, replacement in replacements.items()
)

words_to_replace = sorted(replacements, key=lambda s: (-len(s), s))

# Join and compile words to replace into a regex
pattern = "|".join(re.escape(word) for word in words_to_replace)
regex = re.compile(pattern, re.I if ignore_case else 0)

def _repl(match: re.Match) -> str:
"""Returns replacement depending on `ignore_case` and `match_case`."""
word = match.group(0)
replacement = replacements[word.lower() if ignore_case else word]

if not match_case:
return replacement

# Clean punctuation from word so string methods work
cleaned_word = word.translate(str.maketrans("", "", string.punctuation))
if cleaned_word.isupper():
return replacement.upper()

elif cleaned_word[0].isupper():
return replacement.capitalize()

else:
return replacement.lower()

return regex.sub(_repl, sentence)


def to_uwu(text: str) -> str:
"""
Converts a given `text` into it's uwu equivalent.
This is shamelessly stolen from Python Discord's seasonalbot.
https://github.com/python-discord/seasonalbot/blob/master/bot/exts/evergreen/fun.py
"""
conversion_func = functools.partial(
_replace_many, replacements=UWU_WORDS, ignore_case=True, match_case=True
)
converted_text = conversion_func(text)

# Don't put >>> if only embed present
if converted_text:
converted_text = f"{converted_text.lstrip('> ')}"
return converted_text
9 changes: 9 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: "3.7"

services:
api:
build:
context: .
dockerfile: Dockerfile
ports:
- 8005:8005

0 comments on commit cf41070

Please sign in to comment.