-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Claudia Ng
authored and
Claudia Ng
committed
Oct 2, 2023
1 parent
0e63bbc
commit be759a2
Showing
6 changed files
with
17 additions
and
0 deletions.
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
python-type-hints-multiple-types/01_type_hints_for_one_piece_of_data.py
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
2 changes: 2 additions & 0 deletions
2
python-type-hints-multiple-types/02_type_hints_for_multiple_pieces_of_data.py
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
3 changes: 3 additions & 0 deletions
3
python-type-hints-multiple-types/03_type_hints_for_callback.py
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 |
---|---|---|
@@ -1,15 +1,18 @@ | ||
from collections.abc import Callable | ||
from typing import Any | ||
|
||
|
||
def apply_func( | ||
func: Callable[..., Any], *args: Any | ||
) -> Any: | ||
return func(*args) | ||
|
||
|
||
def parse_email(email_address: str) -> tuple[str, str] | None: | ||
if "@" in email_address: | ||
username, domain = email_address.split("@") | ||
return username, domain | ||
return None | ||
|
||
|
||
apply_funct(parse_email, "[email protected]") |
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 |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
from collections.abc import Callable | ||
from typing import Any | ||
|
||
|
||
def timeit(function: Callable[..., Any]) -> Callable[..., Any]: | ||
@functools.wraps(function) | ||
def wrapper(*args, **kwargs): | ||
|
@@ -14,12 +15,14 @@ def wrapper(*args, **kwargs): | |
|
||
return wrapper | ||
|
||
|
||
@timeit | ||
def parse_email(email_address: str) -> tuple[str, str] | None: | ||
if "@" in email_address: | ||
username, domain = email_address.split("@") | ||
return username, domain | ||
return None | ||
|
||
|
||
username, domain = parse_email("[email protected]") | ||
print(username, domain) |
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
from collections.abc import Generator, Iterable, Iterator | ||
|
||
|
||
def parse_email() -> Generator[tuple[str, str], str, str]: | ||
sent = yield "", "" | ||
while sent != "": | ||
|
@@ -10,6 +11,7 @@ def parse_email() -> Generator[tuple[str, str], str, str]: | |
sent = yield "invalid email" | ||
return "Done" | ||
|
||
|
||
generator = parse_email() | ||
next(generator) | ||
generator.send("[email protected]") | ||
|
@@ -19,12 +21,14 @@ def parse_email() -> Generator[tuple[str, str], str, str]: | |
except StopIteration as ex: | ||
print(ex.value) | ||
|
||
|
||
def parse_emails(emails: list[str]) -> Iterator[tuple[str, str]]: | ||
for email in emails: | ||
if "@" in email: | ||
username, domain = email.split("@") | ||
yield username, domain | ||
|
||
|
||
def parse_emails(emails: Iterable[str]) -> Iterable[tuple[str, str]]: | ||
for email in emails: | ||
if "@" in email: | ||
|
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 |
---|---|---|
@@ -1,12 +1,15 @@ | ||
from typing import TypeAlias | ||
|
||
|
||
EmailComponents = tuple[str, str] | None | ||
|
||
|
||
def parse_email(email_address: str) -> EmailComponents: | ||
if "@" in email_address: | ||
username, domain = email_address.split("@") | ||
return username, domain | ||
return None | ||
|
||
|
||
EmailComponents: TypeAlias = tuple[str, str] | None | ||
type EmailComponents = tuple[str, str] | None |