Created
April 10, 2020 20:33
-
-
Save symbiont-larry-diehl/f798d62c90126bbad4719e5d027a9133 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| JSON = Union[None, bool, str, int, List[JSON], Dict[str, JSON]] | |
| def plenty_o_nuttin(x: bool) -> JSON: | |
| if not x: | |
| return None | |
| else: | |
| return [False, "", 0, {'plenty': ['n', 'u', 't', 't', 'i', 'n']}] |
This file contains hidden or 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
| HasFirstName = Record(first_name: str) | |
| HasLastName = Record(last_name: str) | |
| def formal_name(n: Intersection[HasFirstName, HasLastName]) -> str: | |
| return f"{n.last_name}, {n.first_name}" | |
| person = Person("Mary", None, "Jane") | |
| formal_name(person) # => "Jane, Mary" |
This file contains hidden or 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
| def show_double(x: Optional[int]) -> str: | |
| if isinstance(x, None): | |
| return "" | |
| else: | |
| return str(x * 2) |
This file contains hidden or 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
| def show_double(x: Optional[int]) -> str: | |
| if isinstance(x, int): | |
| return str(x * 2) | |
| else: | |
| return "" |
This file contains hidden or 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
| @dataclass | |
| class Person: | |
| first_name: str | |
| middle_name: Optional[str] | |
| last_name: str | |
| def full_name(self) -> str: | |
| if isinstance(self.middle_name, None): | |
| return f"{self.first_name} {self.last_name}" | |
| else: | |
| return f"{self.first_name} {self.middle_name} {self.last_name}" |
This file contains hidden or 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
| # the Person type | |
| Person = Record( | |
| first_name: str, | |
| middle_name: Optional[str], | |
| last_name: str, | |
| full_name: Callable[[], str] # function w/o args returning a str | |
| ) | |
| # the Person constructor | |
| def Person(first: str, middle: Optional[str], last: str) -> Person: | |
| return < | |
| first_name = first, | |
| middle_name = middle, | |
| last_name = last, | |
| full_name = lambda: f"{first} {last}" if isinstance(middle, None) else f"{first} {middle} {last}" | |
| > |
This file contains hidden or 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
| person = Person("Mary", None, "Jane") | |
| person.first_name # => "Mary" | |
| person.last_name # => "Jane" | |
| person.full_name() # => "Mary Jane" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment