Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Classy Abstractions @ Python Web Conf

Classy Abstractions @ Python Web Conf

Hynek Schlawack

June 18, 2020
Tweet

More Decks by Hynek Schlawack

Other Decks in Programming

Transcript

  1. –me, today “You’re wasting precious resources if you use a

    high-level language but only use low-level primitives.”
  2. PATHLIB >>> p = Path() / "foo" / "bar" >>>

    p.absolute() PosixPath('/Users/hynek/foo/bar') >>> p.absolute() WindowsPath('C:/Users/Hynek Schlawack/foo/bar')
  3. PATHLIB >>> p = Path() / "foo" / "bar" >>>

    p.absolute() PosixPath('/Users/hynek/foo/bar') >>> p.absolute() WindowsPath('C:/Users/Hynek Schlawack/foo/bar') >>> p.name 'bar'
  4. PATHLIB >>> p = Path() / "foo" / "bar" >>>

    p.absolute() PosixPath('/Users/hynek/foo/bar') >>> p.absolute() WindowsPath('C:/Users/Hynek Schlawack/foo/bar') >>> p.name 'bar' >>> Path("README.md").read_text() ...
  5. YARL >>> u = yarl.URL("https://hynek.me/articles/") >>> u.host 'hynek.me' >>> u.path

    '/articles/' >>> u / "speaking/" URL('https://hynek.me/articles/speaking/')
  6. YARL >>> u = yarl.URL("https://hynek.me/articles/") >>> u.host 'hynek.me' >>> u.path

    '/articles/' >>> u / "speaking/" URL('https://hynek.me/articles/speaking/') >>> u.with_path("about/") URL('https://hynek.me/about/')
  7. >>> u = yarl.URL.build( scheme="redis", user="scott", password="tiger", host="redis.local", port=369, path="/1",

    query={"socket_timeout": 42}) >>> u URL('redis://scott:[email protected]:369/1?socket_timeout=42') >>> redis.ConnectionPool.from_url(str(u))
  8. ENUM from enum import Enum class What(Enum): A = "a"

    B = "b" C = "c" def f(what: What): ... f(What.D) f(What.A)
  9. ENUM from enum import Enum class What(Enum): A = "a"

    B = "b" C = "c" def f(what: What): ... f(What.D) f(What.A) error: "Type[What]" has no attribute "D"
  10. @app.route("/") def view(): return { "data": { "id": "some-id", "type":

    "example", "attributes": { "foo": "bar" } } "links": "http://localhost/", }
  11. @bp.route("/<int:id>", methods=["GET"]) def get_dns_record(id: int) -> APIResult: try: return APIResult(

    get_uow().dns_records.get(id) ) except UnknownDNSRecordError: raise_not_found("DNS record", id)
  12. @bp.route("/<int:id>", methods=["GET"]) def get_dns_record(id: int) -> APIResult: try: return APIResult(

    get_uow().dns_records.get(id) ) except UnknownDNSRecordError: raise_not_found("DNS record", id)
  13. @bp.route("/", methods=["POST"]) @validated_body def create_dns_record(ndr: CreateDNSRecord) -> APIResult: try: dr_id

    = svc.create_record( get_uow(), ndr.domain, # ... ) except PlanError as e: raise APIError( id=error_ids.PLAN_NOT_SUFFICIENT, status="402", title=e.args[0], source={"pointer": "/data/attributes/domain"}, ) # ...
  14. @bp.route("/", methods=["POST"]) @validated_body def create_dns_record(ndr: CreateDNSRecord) -> APIResult: try: dr_id

    = svc.create_record( get_uow(), ndr.domain, # ... ) except PlanError as e: raise APIError( id=error_ids.PLAN_NOT_SUFFICIENT, status="402", title=e.args[0], source={"pointer": "/data/attributes/domain"}, ) # ...
  15. @bp.route("/", methods=["POST"]) @validated_body def create_dns_record(ndr: CreateDNSRecord) -> APIResult: try: dr_id

    = svc.create_record( get_uow(), ndr.domain, # ... ) except PlanError as e: raise APIError( id=error_ids.PLAN_NOT_SUFFICIENT, status="402", title=e.args[0], source={"pointer": "/data/attributes/domain"}, ) # ...
  16. @bp.route("/", methods=["POST"]) @validated_body def create_dns_record(ndr: CreateDNSRecord) -> APIResult: try: dr_id

    = svc.create_record( get_uow(), ndr.domain, # ... ) except PlanError as e: raise APIError( id=error_ids.PLAN_NOT_SUFFICIENT, status="402", title=e.args[0], source={"pointer": "/data/attributes/domain"}, ) # ...
  17. class Point(namedtuple('Point', ['x', 'y'])): __slots__ = () @property def hypot(self):

    return (self.x ** 2 + self.y ** 2) ** 0.5 def __str__(self): return 'Point: x=%6.3f y=%6.3f hypot=%6.3f' % ( self.x, self.y, self.hypot)
  18. class Point(namedtuple('Point', ['x', 'y'])): __slots__ = () @property def hypot(self):

    return (self.x ** 2 + self.y ** 2) ** 0.5 def __str__(self): return 'Point: x=%6.3f y=%6.3f hypot=%6.3f' % ( self.x, self.y, self.hypot) >>> Point.__mro__ (<class '__main__.Point'>, <class '__main__.Point'>, <class 'tuple'>, <class 'object'>)
  19. @attr.s class Point: x = attr.ib() y = attr.ib() @property

    def hypot(self): return (self.x ** 2 + self.y ** 2) ** 0.5 def __str__(self): return 'Point: x=%6.3f y=%6.3f hypot=%6.3f' % ( self.x, self.y, self.hypot)
  20. from attrs import define, field @define class Point3D: x: int

    y: int z: int = field( validator=positive )