-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(optimization): update assert_values on Result construction to s…
…peed up the object init (#65)
- Loading branch information
1 parent
db952bb
commit cdee17e
Showing
3 changed files
with
97 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
from __future__ import annotations | ||
|
||
import pyperf | ||
|
||
from meiga import Error, Failure, Result, Success | ||
|
||
|
||
# Without meiga | ||
class NoSuchKeyException(Exception): | ||
... | ||
|
||
|
||
class TypeMismatchException(Exception): | ||
... | ||
|
||
|
||
def string_from_key(dictionary: dict, key: str) -> str: | ||
if key not in dictionary.keys(): | ||
raise NoSuchKeyException() | ||
|
||
value = dictionary[key] | ||
if not isinstance(value, str): | ||
raise TypeMismatchException() | ||
|
||
return value | ||
|
||
|
||
def string_from_key_without_meiga(dictionary: dict, key: str) -> str: | ||
try: | ||
return string_from_key(dictionary, key) | ||
except Exception: # noqa | ||
pass | ||
|
||
|
||
# With meiga | ||
class NoSuchKey(Error): | ||
... | ||
|
||
|
||
class TypeMismatch(Error): | ||
... | ||
|
||
|
||
def string_from_key_with_meiga( | ||
dictionary: dict, key: str | ||
) -> Result[str, NoSuchKey | TypeMismatch]: | ||
if key not in dictionary.keys(): | ||
return Failure(NoSuchKey()) | ||
|
||
value = dictionary[key] | ||
if not isinstance(value, str): | ||
return Failure(TypeMismatch()) | ||
|
||
return Success(value) | ||
|
||
|
||
dictionary = { | ||
"key1": "value", | ||
"key2": 2, | ||
"key3": "value", | ||
"key4": 2, | ||
"key5": "value", | ||
"key6": 2, | ||
"key7": "value", | ||
"key8": 2, | ||
"key9": "value", | ||
"key10": 2, | ||
"key11": "value", | ||
"key12": 2, | ||
} | ||
|
||
|
||
def meiga(): | ||
string_from_key_with_meiga(dictionary=dictionary, key="invalid_key") | ||
|
||
|
||
def without_meiga(): | ||
string_from_key_without_meiga(dictionary=dictionary, key="invalid_key") | ||
|
||
|
||
runner = pyperf.Runner() | ||
runner.bench_func("meiga", meiga) | ||
runner.bench_func("without meiga", without_meiga) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters