You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
1.0 KiB
Python
66 lines
1.0 KiB
Python
from typing import Protocol, runtime_checkable
|
|
|
|
from pytraits import trait, impl
|
|
from traits import ShortUrl
|
|
|
|
|
|
class MyUrl:
|
|
def __init__(self, url):
|
|
self.url = url
|
|
|
|
def url(self):
|
|
return self.url
|
|
|
|
|
|
@trait
|
|
class ShortUrlForMyUrl(MyUrl, ShortUrl):
|
|
|
|
def short_url(self):
|
|
return self.url[0]
|
|
|
|
|
|
class Ab:
|
|
def __init__(self, msg: str):
|
|
self.msg = msg
|
|
self.m0 = msg[0]
|
|
|
|
def foo(self):
|
|
print('foo', self.msg)
|
|
|
|
|
|
class Abc(Ab):
|
|
pass
|
|
|
|
|
|
@runtime_checkable
|
|
class BProtocol(Protocol):
|
|
def print_m0(self):
|
|
pass
|
|
|
|
|
|
@runtime_checkable
|
|
class DProtocol(Protocol):
|
|
def print_1(self):
|
|
pass
|
|
|
|
|
|
@impl(BProtocol)
|
|
class C(Abc):
|
|
|
|
def print_m0(self):
|
|
print('show', self.m0)
|
|
|
|
|
|
@trait
|
|
class D(Abc, DProtocol):
|
|
def print_1(self):
|
|
print('show', self.m0)
|
|
self.foo()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
a: Ab | DProtocol = Abc('acb')
|
|
a.print_1()
|
|
my_url: MyUrl | ShortUrl = MyUrl('http://acb.com')
|
|
print(my_url.short_url())
|