type hints w j zyku python
play

Type hints w jzyku Python Konrad Haas 4Developers 2018 Plan type - PowerPoint PPT Presentation

Type hints w jzyku Python Konrad Haas 4Developers 2018 Plan type hints dlaczego? skadnia narzdzia biblioteki projekty legacy def notify_everyone (items): for item in items: item.owner.notify() def cancel_all (items):


  1. Type hints w języku Python Konrad Hałas 4Developers 2018

  2. Plan type hints dlaczego? składnia narzędzia biblioteki projekty legacy

  3. def notify_everyone (items): for item in items: item.owner.notify()

  4. def cancel_all (items): notify_everyone(items) ...

  5. Rozwiązania dokumentacja zewnętrzna dokumentacja w kodzie (RST, epytext, NumPy, Google) przepisać wszystko do języka statycznie typowanego

  6. def count_unique_words (text, case_sensitive): ...

  7. def count_unique_words (text: str, case_sensitive: bool) ‑> int: ...

  8. def median (values: list) ‑> float: ...

  9. from typing import List def median (values: List[float]) ‑> float: ...

  10. from typing import Dict def count_chars (text: str) ‑> Dict[str, int]: ...

  11. from typing import List class Point : ... def calculate_area (vertices: List[Point]) ‑> float: ...

  12. from typing import Optional def total_amount (order: Order, discount: Optional[Discount]) ‑> Money: ...

  13. from typing import List, Union def send_confirmation (email: Union[str, List[str]]) ‑> None : ...

  14. from typing import Any def get_most_frequent (items: list) ‑> Any: ...

  15. from typing import List, TypeVar T = TypeVar('T') def get_most_frequent (items: List[T]) ‑> T: ...

  16. from typing import List class Item : ... class Basket : def __init__ (self): self.items: List[Item] = [] def add_item (self, item: Item): self.items.append(item)

  17. Narzędzia mypy PyCharm ...

  18. def total_price (items: List[Item], discount: Optional[float] = None) ‑> float: ...

  19. def total_price (items: List[Item], discount: Optional[float] = None) ‑> float: result = 0.0 for item in items: result += item.count * item.product.price result ‑= discount return result

  20. def total_price (items: List[Item], discount: Optional[float] = None) ‑> float: result = 0.0 for item in items: result += item.count * item.product.price result ‑= discount return result $ mypy ‑‑strict example.py example.py:9: error: "Item" has no attribute "count" example.py:10: error: Unsupported operand types for ‑ ("float" and "Optional[float]")

  21. def total_price (items: List[Item], discount: Optional[float] = None) ‑> float: result = 0.0 for item in items: result += item.quantity * item.product.price if discount: result ‑= discount return result $ mypy ‑‑strict example.py

  22. Biblioteki injector dacite ...

  23. def register_user (user_details: UserDetails): ...

  24. from users.repositories import UsersRepository def register_user (user_details: UserDetails): ... repository = UsersRepository() repository.create_user(...)

  25. class UsersService : def __init__ (self, repository: UsersRepository): self.repository = repository def register_user (self, user_details: UserDetails): ... self.repository.create_user(...)

  26. class UsersRepository : def __init__ (self, data_base: DataBase): self.data_base = data_base def create_user (self, user: User): ... self.data_base.insert(...)

  27. class DataBase : def __init__ (self, session: DataBaseSession): self.session = session ...

  28. session = DataBaseSession(...) data_base = DataBase(session) users_repository = UsersRepository(data_base) users_service = UsersService(users_repository)

  29. class DataBaseSession : ... class DataBase : def __init__ (self, session: DataBaseSession): ... class UsersRepository : def __init__ (self, database: DataBase): ... class UsersService : def __init__ (self, repository: UsersRepository): ...

  30. from injector import inject class DataBaseSession : ... class DataBase : @inject def __init__ (self, session: DataBaseSession): ... class UsersRepository : @inject def __init__ (self, database: DataBase): ... class UsersService : @inject def __init__ (self, repository: UsersRepository): ...

  31. from injector import Injector users_service = Injector().get(UsersService)

  32. from dataclasses import dataclass @dataclass class User : name: str age: int is_active: bool user = User(name='John', age=30, is_active= True )

  33. import dacite data = { 'name': 'John', 'age': 30, 'is_active': True , } user = dacite.from_dict(data_class=User, data=data) assert user == User(name='John', age=30, is_active= True )

  34. Type hints w projekcie legacy gradual typing statyczna analiza uruchamiana w ramach CI wymuszanie type annotations dla: kluczowych modułów interfejsów

  35. Podsumowanie zrozumiały kod mniej błędów nowe możliwości

  36. Dziękuję! @konradhalas / konradhalas.pl

  37. Zdjęcia Jacek Kołodziej ‑ http://kolodziejj.info/ Korsan Studio ‑ http://facebook.com/korsanstudio

  38. Narzędzia/biblioteki mypy ‑ http://mypy‑lang.org PyCharm ‑ https://www.jetbrains.com/pycharm dacite ‑ https://github.com/konradhalas/dacite injector ‑ https://github.com/alecthomas/injector

Recommend


More recommend