diff --git a/README.md b/README.md index e38ab24..fafa677 100644 --- a/README.md +++ b/README.md @@ -87,7 +87,7 @@ class Record: class User: - info : str + info: str @property def data(self) -> Dict[str, str]: @@ -186,8 +186,8 @@ Explicit is better than implicit. seq = ("Austin", "New York", "San Francisco") for item in seq: - #do_stuff() - #do_some_other_stuff() + # do_stuff() + # do_some_other_stuff() # Wait, what's `item` again? print(item) @@ -199,8 +199,8 @@ for item in seq: locations = ("Austin", "New York", "San Francisco") for location in locations: - #do_stuff() - #do_some_other_stuff() + # do_stuff() + # do_some_other_stuff() # ... print(location) ``` @@ -290,12 +290,13 @@ class Menu: self.body = config["body"] # ... + menu = Menu( { "title": "My Menu", "body": "Something about my menu", "button_text": "OK", - "cancellable": False + "cancellable": False, } ) ``` @@ -312,6 +313,7 @@ class MenuConfig: button_text: The text for the button label. cancellable: Can it be cancelled? """ + title: str body: str button_text: str @@ -349,6 +351,7 @@ class MenuConfig(NamedTuple): button_text: The text for the button label. cancellable: Can it be cancelled? """ + title: str body: str button_text: str @@ -364,7 +367,7 @@ create_menu( MenuConfig( title="My delicious menu", body="A description of the various items on the menu", - button_text="Order now!" + button_text="Order now!", ) ) ``` @@ -390,6 +393,7 @@ class MenuConfig: button_text: str cancellable: bool = False + def create_menu(config: MenuConfig): title, body, button_text, cancellable = astuple(config) # ... @@ -399,7 +403,7 @@ create_menu( MenuConfig( title="My delicious menu", body="A description of the various items on the menu", - button_text="Order now!" + button_text="Order now!", ) ) ``` @@ -436,7 +440,7 @@ create_menu( title="My delicious menu", body="A description of the various items on the menu", button_text="Order now!", - cancellable=True + cancellable=True, ) ) ``` @@ -464,8 +468,7 @@ def email(client: Client) -> None: def email_clients(clients: List[Client]) -> None: - """Filter active clients and send them an email. - """ + """Filter active clients and send them an email.""" for client in clients: if client.active: email(client) @@ -486,14 +489,12 @@ def email(client: Client) -> None: def get_active_clients(clients: List[Client]) -> List[Client]: - """Filter active clients. - """ + """Filter active clients.""" return [client for client in clients if client.active] def email_clients(clients: List[Client]) -> None: - """Send an email to a given list of clients. - """ + """Send an email to a given list of clients.""" for client in get_active_clients(clients): email(client) ``` @@ -520,8 +521,7 @@ def active_clients(clients: Iterator[Client]) -> Generator[Client, None, None]: def email_client(clients: Iterator[Client]) -> None: - """Send an email to a given list of clients. - """ + """Send an email to a given list of clients.""" for client in active_clients(clients): email(client) ``` @@ -538,6 +538,7 @@ class Email: def handle(self) -> None: pass + message = Email() # What is this supposed to do again? message.handle() @@ -550,6 +551,7 @@ class Email: def send(self) -> None: """Send this message""" + message = Email() message.send() ``` @@ -566,12 +568,13 @@ much. Splitting up functions leads to reusability and easier testing. ```python # type: ignore + def parse_better_js_alternative(code: str) -> None: regexes = [ # ... ] - statements = code.split('\n') + statements = code.split("\n") tokens = [] for regex in regexes: for statement in statements: @@ -592,7 +595,7 @@ from typing import Tuple, List, Dict REGEXES: Tuple = ( - # ... + # ... ) @@ -689,6 +692,7 @@ If you can do this, you will be happier than the vast majority of other programm # However... fullname = "Ryan McDermott" + def split_into_first_and_last_name() -> None: # The use of the global keyword here is changing the meaning of the # the following line. This function is now mutating the module-level @@ -696,6 +700,7 @@ def split_into_first_and_last_name() -> None: global fullname fullname = fullname.split() + split_into_first_and_last_name() # MyPy will spot the problem, complaining about 'Incompatible types in @@ -715,6 +720,7 @@ from typing import List, AnyStr def split_into_first_and_last_name(name: AnyStr) -> List[AnyStr]: return name.split() + fullname = "Ryan McDermott" name, surname = split_into_first_and_last_name(fullname) @@ -792,64 +798,66 @@ updating multiple places any time you want to change one thing. from typing import List, Dict from dataclasses import dataclass + @dataclass class Developer: def __init__(self, experience: float, github_link: str) -> None: self._experience = experience self._github_link = github_link - + @property def experience(self) -> float: return self._experience - + @property def github_link(self) -> str: return self._github_link - + + @dataclass class Manager: def __init__(self, experience: float, github_link: str) -> None: self._experience = experience self._github_link = github_link - + @property def experience(self) -> float: return self._experience - + @property def github_link(self) -> str: return self._github_link - + def get_developer_list(developers: List[Developer]) -> List[Dict]: developers_list = [] for developer in developers: - developers_list.append({ - 'experience' : developer.experience, - 'github_link' : developer.github_link - }) + developers_list.append( + {"experience": developer.experience, "github_link": developer.github_link} + ) return developers_list + def get_manager_list(managers: List[Manager]) -> List[Dict]: managers_list = [] for manager in managers: - managers_list.append({ - 'experience' : manager.experience, - 'github_link' : manager.github_link - }) + managers_list.append( + {"experience": manager.experience, "github_link": manager.github_link} + ) return managers_list + ## create list objects of developers company_developers = [ - Developer(experience=2.5, github_link='https://github.com/1'), - Developer(experience=1.5, github_link='https://github.com/2') + Developer(experience=2.5, github_link="https://github.com/1"), + Developer(experience=1.5, github_link="https://github.com/2"), ] company_developers_list = get_developer_list(developers=company_developers) ## create list objects of managers company_managers = [ - Manager(experience=4.5, github_link='https://github.com/3'), - Manager(experience=5.7, github_link='https://github.com/4') + Manager(experience=4.5, github_link="https://github.com/3"), + Manager(experience=5.7, github_link="https://github.com/4"), ] company_managers_list = get_manager_list(managers=company_managers) ``` @@ -860,42 +868,42 @@ company_managers_list = get_manager_list(managers=company_managers) from typing import List, Dict from dataclasses import dataclass + @dataclass class Employee: def __init__(self, experience: float, github_link: str) -> None: self._experience = experience self._github_link = github_link - + @property def experience(self) -> float: return self._experience - + @property def github_link(self) -> str: return self._github_link - def get_employee_list(employees: List[Employee]) -> List[Dict]: employees_list = [] for employee in employees: - employees_list.append({ - 'experience' : employee.experience, - 'github_link' : employee.github_link - }) + employees_list.append( + {"experience": employee.experience, "github_link": employee.github_link} + ) return employees_list + ## create list objects of developers company_developers = [ - Employee(experience=2.5, github_link='https://github.com/1'), - Employee(experience=1.5, github_link='https://github.com/2') + Employee(experience=2.5, github_link="https://github.com/1"), + Employee(experience=1.5, github_link="https://github.com/2"), ] company_developers_list = get_employee_list(employees=company_developers) ## create list objects of managers company_managers = [ - Employee(experience=4.5, github_link='https://github.com/3'), - Employee(experience=5.7, github_link='https://github.com/4') + Employee(experience=4.5, github_link="https://github.com/3"), + Employee(experience=5.7, github_link="https://github.com/4"), ] company_managers_list = get_employee_list(employees=company_managers) ```