class Animal:
def speak(self):
raise NotImplementedError()
class Dog(Animal):
def speak(self):
print('Woof!')
class Cat(Animal):
def speak(self):
print('Meow!')
def make_some_noise(animal: Animal):
animal.speak()
make_some_noise(Dog())
make_some_noise(Cat())
class Dog:
def woof(self):
print('Woof!')
class Cat:
def meow(self):
print('Meow!')
def make_some_noise(animal):
if isinstance(animal, Dog):
animal.woof()
elif isinstance(animal, Cat):
animal.meow()
make_some_noise(Dog())
make_some_noise(Cat())
class Bird:
def tweet(self):
print('Tweet!')
# Now go back and add another isinstance check
>>> @naive_dispatch(int, int)
>>> def add(a, b):
... return a + b
...
>>> add(1, 2)
3
>>> class MyInt(int):
... ...
...
>>> add(MyInt(1), 2)
KeyError: Your implementation is naive
from multipledispatch import dispatch
@dispatch(int, int)
@dispatch(float, int)
@dispatch(int, float)
@dispatch(float, float)
def javascript_add(a, b):
return float(a) + float(b)
@dispatch(str, int)
@dispatch(int, str)
def javascript_add(a, b):
return str(a) + str(b)
@dispatch(str, float)
def javascript_add(a, b):
if b.is_integer():
return a + str(int(b))
return a + str(b)
@dispatch(float, str):
def javascript_add(a, b):
if a.is_integer():
return str(int(a)) + b
return str(a) + b
>>> javascript_add(1, 2)
3.0
>>> javascript_add(1.0, 2)
3.0
>>> javascript_add(1, 'a')
'1a'
>>> javascript_add('a', 1.0)
'a1'
from multipledispatch import dispatch
@dispatch(int, int)
@dispatch(float, int)
@dispatch(int, float)
@dispatch(float, float)
def javascript_add(a, b):
return float(a) + float(b)
@dispatch(str, int)
@dispatch(int, str)
def javascript_add(a, b):
return str(a) + str(b)
@dispatch(str, float)
def javascript_add(a, b):
if b.is_integer():
return a + str(int(b))
return a + str(b)
@dispatch(float, str):
def javascript_add(a, b):
if a.is_integer():
return str(int(a)) + b
return str(a) + b
>>> javascript_add(1, 2)
3.0
>>> javascript_add(1.0, 2)
3.0
>>> javascript_add(1, 'a')
'1a'
>>> javascript_add('a', 1.0)
'a1'
@execute_node.register(ops.Cast, type(None), dt.DataType)
def execute_cast_null_to_anything(op, data, type, **kwargs):
return None
@execute_node.register(ops.Cast, datetime.datetime, dt.String)
def execute_cast_datetime_or_timestamp_to_string(op, data, type, **kwargs):
"""Cast timestamps to strings"""
return str(data)
@execute_node.register(ops.Cast, datetime.datetime, dt.Int64)
def execute_cast_datetime_to_integer(op, data, type, **kwargs):
"""Cast datetimes to integers"""
return pd.Timestamp(data).value
@execute_node.register(ops.Cast, pd.Timestamp, dt.Int64)
def execute_cast_timestamp_to_integer(op, data, type, **kwargs):
"""Cast timestamps to integers"""
return data.value