From 41e174f2464cd997ed6a6b71c7749ca0b8c9edec Mon Sep 17 00:00:00 2001 From: Martin Thoma Date: Sat, 12 Feb 2022 09:07:10 +0100 Subject: [PATCH] Apply blacken-docs / the black code formatter Black is becomming the de-facto standard for code formatting in Python. It makes sure code looks similar in many projects and it leads to minimal diffs. --- README.md | 106 +++++++++++++++++++++++++++++------------------------- 1 file changed, 58 insertions(+), 48 deletions(-) diff --git a/README.md b/README.md index 467b695..bfc1a96 100644 --- a/README.md +++ b/README.md @@ -87,7 +87,7 @@ class Record: class User: - info : str + info: str @property def data(self) -> Dict[Text, Text]: @@ -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) ``` @@ -291,12 +291,13 @@ class Menu: self.body = config["body"] # ... + menu = Menu( { "title": "My Menu", "body": "Something about my menu", "button_text": "OK", - "cancellable": False + "cancellable": False, } ) ``` @@ -316,6 +317,7 @@ class MenuConfig: button_text: The text for the button label. cancellable: Can it be cancelled? """ + title: Text body: Text button_text: Text @@ -353,6 +355,7 @@ class MenuConfig(NamedTuple): button_text: The text for the button label. cancellable: Can it be cancelled? """ + title: str body: str button_text: str @@ -368,7 +371,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,11 +393,13 @@ class MenuConfig: button_text: The text for the button label. cancellable: Can it be cancelled? """ + title: Text body: Text button_text: Text cancellable: bool = False + def create_menu(config: MenuConfig): title, body, button_text, cancellable = astuple(config) # ... @@ -404,7 +409,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!", ) ) ``` @@ -424,6 +429,7 @@ class MenuConfig(TypedDict): button_text: The text for the button label. cancellable: Can it be cancelled? """ + title: Text body: Text button_text: Text @@ -441,7 +447,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, ) ) ``` @@ -469,8 +475,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) @@ -491,14 +496,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) ``` @@ -525,8 +528,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) ``` @@ -543,6 +545,7 @@ class Email: def handle(self) -> None: pass + message = Email() # What is this supposed to do again? message.handle() @@ -555,6 +558,7 @@ class Email: def send(self) -> None: """Send this message""" + message = Email() message.send() ``` @@ -571,12 +575,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: @@ -597,7 +602,7 @@ from typing import Tuple, List, Text, Dict REGEXES: Tuple = ( - # ... + # ... ) @@ -696,6 +701,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 @@ -703,6 +709,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 @@ -722,6 +729,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) @@ -800,64 +808,66 @@ updating multiple places any time you want to change one thing. from typing import List, Text, Dict from dataclasses import dataclass + @dataclass class Developer: def __init__(self, experience: float, github_link: Text) -> None: self._experience = experience self._github_link = github_link - + @property def experience(self) -> float: return self._experience - + @property def github_link(self) -> Text: return self._github_link - + + @dataclass class Manager: def __init__(self, experience: float, github_link: Text) -> None: self._experience = experience self._github_link = github_link - + @property def experience(self) -> float: return self._experience - + @property def github_link(self) -> Text: 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) ``` @@ -868,42 +878,42 @@ company_managers_list = get_manager_list(managers=company_managers) from typing import List, Text, Dict from dataclasses import dataclass + @dataclass class Employee: def __init__(self, experience: float, github_link: Text) -> None: self._experience = experience self._github_link = github_link - + @property def experience(self) -> float: return self._experience - + @property def github_link(self) -> Text: 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) ```