Skip to content

Commit

Permalink
explicitly identified async functions and brought back old ones
Browse files Browse the repository at this point in the history
  • Loading branch information
hitblast committed Dec 9, 2024
1 parent a1e1348 commit 97bf8b0
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 63 deletions.
58 changes: 38 additions & 20 deletions src/avro/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,10 +318,12 @@ def _reverse_backend_ext(text: str, remap_words: bool) -> str:
# ---


async def parse(
async def parse_async(
*texts: str, bijoy: bool = False, remap_words: bool = True
) -> Union[str, list[str]]:
"""Parses input text, matches and replaces using the Avro Dictionary.
"""Asynchronous version of parse() function.
Parses input text, matches and replaces using the Avro Dictionary.
If a valid replacement is found, then it returns the replaced string.
If no replacement is found, then it instead returns the input text.
Expand Down Expand Up @@ -350,15 +352,18 @@ async def parse(

# If the `bijoy` parameter is set to `True`, then convert the output to Bijoy Keyboard format.
if bijoy:
return await to_bijoy(*output)
return await to_bijoy_async(*output)
else:
return output[0] if len(output) == 1 else output


def parse_sync(
def parse(
*texts: str, bijoy: bool = False, remap_words: bool = True
) -> Union[str, list[str]]:
"""Synchronous version of parse() function using multithreading.
"""Parses input text, matches and replaces using the Avro Dictionary.
If a valid replacement is found, then it returns the replaced string.
If no replacement is found, then it instead returns the input text.
Parameters:
-----------
Expand All @@ -383,13 +388,15 @@ def parse_sync(
)

if bijoy:
return to_bijoy_sync(*output)
return to_bijoy(*output)
else:
return output[0] if len(output) == 1 else output


async def to_bijoy(*texts: str) -> Union[str, list[str]]:
"""Converts input text (Avro, Unicode) to Bijoy Keyboard format (ASCII).
async def to_bijoy_async(*texts: str) -> Union[str, list[str]]:
"""Asynchronous version of to_bijoy() function.
Converts input text (Avro, Unicode) to Bijoy Keyboard format (ASCII).
If a valid conversion is found, then it returns the converted string.
Expand All @@ -410,8 +417,10 @@ async def to_bijoy(*texts: str) -> Union[str, list[str]]:
return output[0] if len(output) == 1 else output


def to_bijoy_sync(*texts: str) -> Union[str, list[str]]:
"""Synchronous version of to_bijoy() function using multithreading.
def to_bijoy(*texts: str) -> Union[str, list[str]]:
"""Converts input text (Avro, Unicode) to Bijoy Keyboard format (ASCII).
If a valid conversion is found, then it returns the converted string.
Parameters:
-----------
Expand All @@ -430,8 +439,10 @@ def to_bijoy_sync(*texts: str) -> Union[str, list[str]]:
return output[0] if len(output) == 1 else output


async def to_unicode(*texts: str) -> Union[str, list[str]]:
"""Converts input text (Bijoy Keyboard, ASCII) to Unicode (Avro Keyboard format).
async def to_unicode_async(*texts: str) -> Union[str, list[str]]:
"""Asynchronous version of to_unicode() function.
Converts input text (Bijoy Keyboard, ASCII) to Unicode (Avro Keyboard format).
If a valid conversion is found, then it returns the converted string.
Expand All @@ -450,8 +461,10 @@ async def to_unicode(*texts: str) -> Union[str, list[str]]:
return output[0] if len(output) == 1 else output


def to_unicode_sync(*texts: str) -> Union[str, list[str]]:
"""Synchronous version of to_unicode() function using multithreading.
def to_unicode(*texts: str) -> Union[str, list[str]]:
"""Converts input text (Bijoy Keyboard, ASCII) to Unicode (Avro Keyboard format).
If a valid conversion is found, then it returns the converted string.
Parameters:
-----------
Expand All @@ -468,10 +481,12 @@ def to_unicode_sync(*texts: str) -> Union[str, list[str]]:
return output[0] if len(output) == 1 else output


async def reverse(
async def reverse_async(
*texts: str, from_bijoy: bool = False, remap_words: bool = True
) -> Union[str, list[str]]:
"""Reverses input text to Roman script typed in English.
"""Asynchronous version of reverse() function.
Reverses input text to Roman script typed in English.
If a valid replacement is found, then it returns the replaced string.
If no replacement is found, then it instead returns the input text.
Expand All @@ -497,7 +512,7 @@ async def reverse(

# Convert from Bijoy to Unicode if from_bijoy is True
if from_bijoy:
converted_texts = await to_unicode(*texts)
converted_texts = await to_unicode_async(*texts)
if isinstance(converted_texts, str):
texts = (converted_texts,)

Expand All @@ -507,10 +522,13 @@ async def reverse(
return output[0] if len(output) == 1 else output


def reverse_sync(
def reverse(
*texts: str, from_bijoy: bool = False, remap_words: bool = True
) -> Union[str, list[str]]:
"""Synchronous version of reverse() function using multithreading.
"""Reverses input text to Roman script typed in English.
If a valid replacement is found, then it returns the replaced string.
If no replacement is found, then it instead returns the input text.
Parameters:
-----------
Expand All @@ -533,7 +551,7 @@ def reverse_sync(

# Convert from Bijoy to Unicode if from_bijoy is True
if from_bijoy:
converted_texts = to_unicode_sync(*texts)
converted_texts = to_unicode(*texts)
if isinstance(converted_texts, str):
texts = (converted_texts,)

Expand Down
88 changes: 45 additions & 43 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ async def test_patterns_without_rules_from_config() -> NoReturn:
if "rules" not in pattern and pattern.get("find", None):
assert (
pattern["replace"]
== await avro.parse(pattern["find"])
== avro.parse_sync(pattern["find"])
== await avro.parse_async(pattern["find"])
== avro.parse(pattern["find"])
)


Expand All @@ -41,7 +41,7 @@ async def test_patterns_without_rules_not_from_config() -> NoReturn:
This test is done in addition to
test_patterns_without_rules_from_config() to ensure that text
passed manually to avro.parse are properly parsed when they
passed manually to avro.parse_async are properly parsed when they
don't exact match a pattern that has no rules specified.
"""

Expand All @@ -53,7 +53,7 @@ async def test_patterns_without_rules_not_from_config() -> NoReturn:
}

for key, value in conjunctions.items():
assert key == await avro.parse(value) == avro.parse_sync(value)
assert key == await avro.parse_async(value) == avro.parse(value)


@pytest.mark.asyncio
Expand All @@ -77,7 +77,7 @@ async def test_patterns_numbers() -> NoReturn:
}

for key, value in numbers.items():
assert key == await avro.parse(value) == avro.parse_sync(value)
assert key == await avro.parse_async(value) == avro.parse(value)


@pytest.mark.asyncio
Expand All @@ -93,8 +93,8 @@ async def test_patterns_punctuations() -> NoReturn:
}

for key, value in punctuations.items():
assert key == await avro.parse(value) == avro.parse_sync(value)
assert value == await avro.reverse(key) == avro.reverse_sync(key)
assert key == await avro.parse_async(value) == avro.parse(value)
assert value == await avro.reverse_async(key) == avro.reverse(key)


@pytest.mark.asyncio
Expand All @@ -118,7 +118,7 @@ async def test_patterns_with_rules_svaravarna() -> NoReturn:
}

for key, value in svaravarna.items():
assert key == await avro.parse(value) == avro.parse_sync(value)
assert key == await avro.parse_async(value) == avro.parse(value)


@pytest.mark.asyncio
Expand All @@ -137,7 +137,7 @@ async def test_non_ascii() -> NoReturn:
}

for key, value in non_ascii.items():
assert key == await avro.parse(value) == avro.parse_sync(value)
assert key == await avro.parse_async(value) == avro.parse(value)


@pytest.mark.asyncio
Expand All @@ -149,13 +149,15 @@ async def test_ascii() -> NoReturn:

assert (
"Avwg evsjvi gan MvB|"
== await avro.reverse("Avwg evsjvi গান MvB|")
== avro.reverse_sync("Avwg evsjvi গান MvB|")
== await avro.reverse_async("Avwg evsjvi গান MvB|")
== avro.reverse("Avwg evsjvi গান MvB|")
)
assert (
"Avwg amar Avwg‡K wPiw`b GB banglay Lyu‡R cvB!"
== await avro.reverse("Avwg আমার Avwg‡K wPiw`b GB বাংলায় Lyu‡R cvB!")
== avro.reverse_sync("Avwg আমার Avwg‡K wPiw`b GB বাংলায় Lyu‡R cvB!")
== await avro.reverse_async(
"Avwg আমার Avwg‡K wPiw`b GB বাংলায় Lyu‡R cvB!"
)
== avro.reverse("Avwg আমার Avwg‡K wPiw`b GB বাংলায় Lyu‡R cvB!")
)


Expand All @@ -173,9 +175,9 @@ async def test_words_with_punctuations() -> NoReturn:
}

for key, value in test_words.items():
assert key == await avro.parse(value) == avro.parse_sync(value)
assert key == await avro.parse_async(value) == avro.parse(value)
assert (
value.lower() == await avro.reverse(key) == avro.reverse_sync(key)
value.lower() == await avro.reverse_async(key) == avro.reverse(key)
)


Expand All @@ -187,13 +189,13 @@ async def test_exceptions() -> NoReturn:

assert (
"আমি উইকিপিডিয়া আর ফেসবুক চালাই।"
== await avro.parse("ami Wikipedia ar Facebook calai.")
== avro.parse_sync("ami Wikipedia ar Facebook calai.")
== await avro.parse_async("ami Wikipedia ar Facebook calai.")
== avro.parse("ami Wikipedia ar Facebook calai.")
)
assert (
"ami Wikipedia ar Facebook chalai."
== await avro.reverse("আমি উইকিপিডিয়া আর ফেসবুক চালাই।")
== avro.reverse_sync("আমি উইকিপিডিয়া আর ফেসবুক চালাই।")
== await avro.reverse_async("আমি উইকিপিডিয়া আর ফেসবুক চালাই।")
== avro.reverse("আমি উইকিপিডিয়া আর ফেসবুক চালাই।")
)


Expand All @@ -206,27 +208,27 @@ async def test_conversion_bijoy_func() -> NoReturn:
# Regular Conversion.
assert (
"Avwg evsjvq Mvb MvB;"
== await avro.to_bijoy("আমি বাংলায় গান গাই;")
== avro.to_bijoy_sync("আমি বাংলায় গান গাই;")
== await avro.to_bijoy_async("আমি বাংলায় গান গাই;")
== avro.to_bijoy("আমি বাংলায় গান গাই;")
)
assert (
[
"Avwg evsjvi Mvb MvB|",
"Avwg Avgvi Avwg‡K wPiw`b GB evsjvq Lyu‡R cvB!",
]
== await avro.to_bijoy(
== await avro.to_bijoy_async(
"আমি বাংলার গান গাই।", "আমি আমার আমিকে চিরদিন এই বাংলায় খুঁজে পাই!"
)
== avro.to_bijoy_sync(
== avro.to_bijoy(
"আমি বাংলার গান গাই।", "আমি আমার আমিকে চিরদিন এই বাংলায় খুঁজে পাই!"
)
)

# Fail-safe Conversion.
assert (
"Hello, World!"
== await avro.to_bijoy("Hello, World!")
== avro.to_bijoy_sync("Hello, World!")
== await avro.to_bijoy_async("Hello, World!")
== avro.to_bijoy("Hello, World!")
)


Expand All @@ -239,19 +241,19 @@ async def test_conversion_unicode_func() -> NoReturn:
# Regular Conversion.
assert (
"আমি বাংলায় গান গাই;"
== await avro.to_unicode("Avwg evsjvh় Mvb MvB;")
== avro.to_unicode_sync("Avwg evsjvh় Mvb MvB;")
== await avro.to_unicode_async("Avwg evsjvh় Mvb MvB;")
== avro.to_unicode("Avwg evsjvh় Mvb MvB;")
)
assert (
[
"আমি বাংলার গান গাই।",
"আমি আমার আমিকে চিরদিন এই বাংলায় খুঁজে পাই!",
]
== await avro.to_unicode(
== await avro.to_unicode_async(
"Avwg evsjvi Mvb MvB|",
"Avwg Avgvi Avwg‡K wPiw`b GB evsjvq Lyu‡R cvB!",
)
== avro.to_unicode_sync(
== avro.to_unicode(
"Avwg evsjvi Mvb MvB|",
"Avwg Avgvi Avwg‡K wPiw`b GB evsjvq Lyu‡R cvB!",
)
Expand All @@ -267,19 +269,19 @@ async def test_parse_sentences() -> NoReturn:
# Default parsing.
assert (
"আমি বাংলায় গান গাই;"
== await avro.parse("ami banglay gan gai;")
== avro.parse_sync("ami banglay gan gai;")
== await avro.parse_async("ami banglay gan gai;")
== avro.parse("ami banglay gan gai;")
)
assert (
[
"আমি বাংলার গান গাই।",
"আমি আমার আমিকে চিরদিন এই বাংলায় খুঁজে পাই।",
]
== await avro.parse(
== await avro.parse_async(
"ami banglar gan gai.",
"ami amar amike cirodin ei banglay khu^je pai.",
)
== avro.parse_sync(
== avro.parse(
"ami banglar gan gai.",
"ami amar amike cirodin ei banglay khu^je pai.",
)
Expand All @@ -288,20 +290,20 @@ async def test_parse_sentences() -> NoReturn:
# Bijoy parsing.
assert (
"Avwg evsjvq Mvb MvB;"
== await avro.parse("ami banglay gan gai;", bijoy=True)
== avro.parse_sync("ami banglay gan gai;", bijoy=True)
== await avro.parse_async("ami banglay gan gai;", bijoy=True)
== avro.parse("ami banglay gan gai;", bijoy=True)
)
assert (
[
"Avwg evsjvi Mvb MvB|",
"Avwg Avgvi Avwg‡K wPiw`b GB evsjvq Lyu‡R cvB!",
]
== await avro.parse(
== await avro.parse_async(
"ami banglar gan gai.",
"ami amar amike cirodin ei banglay khu^je pai!",
bijoy=True,
)
== avro.parse_sync(
== avro.parse(
"ami banglar gan gai.",
"ami amar amike cirodin ei banglay khu^je pai!",
bijoy=True,
Expand All @@ -318,25 +320,25 @@ async def test_reverse_sentences() -> NoReturn:
# Default reversing.
assert (
"ami banglay gan gai."
== await avro.reverse("আমি বাংলায় গান গাই।")
== avro.reverse_sync("আমি বাংলায় গান গাই।")
== await avro.reverse_async("আমি বাংলায় গান গাই।")
== avro.reverse("আমি বাংলায় গান গাই।")
)
assert (
[
"rohim, tomake korim dakche. ekhon ki rowna debe?",
"rowna dile amake bole zew.",
]
== await avro.reverse(
== await avro.reverse_async(
"রহিম, তোমাকে করিম ডাকছে। এখন কি রওনা দেবে?", "রওনা দিলে আমাকে বলে যেও।"
)
== avro.reverse_sync(
== avro.reverse(
"রহিম, তোমাকে করিম ডাকছে। এখন কি রওনা দেবে?", "রওনা দিলে আমাকে বলে যেও।"
)
)

# Bijoy reversing.
assert (
"ami banglar gan gai."
== await avro.reverse("Avwg evsjvi Mvb MvB|", from_bijoy=True)
== avro.reverse_sync("Avwg evsjvi Mvb MvB|", from_bijoy=True)
== await avro.reverse_async("Avwg evsjvi Mvb MvB|", from_bijoy=True)
== avro.reverse("Avwg evsjvi Mvb MvB|", from_bijoy=True)
)

0 comments on commit 97bf8b0

Please sign in to comment.