-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
67 lines (48 loc) · 1.04 KB
/
example.py
File metadata and controls
67 lines (48 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import enum
import typing
import partialdispatch
_use_rich = None
try:
import rich
except ModuleNotFoundError:
pass
_use_rich = False
else:
_use_rich = True
print = rich.print
class Pet(enum.Enum):
Cat: str = "🐱"
Dog: str = "🐕"
Shark: str = "🦈"
@partialdispatch.singledispatch_literal
def func(a, b):
return f"default: {a=}, {b=}"
@func.register
def _(a: int, b):
return f"int: {a=}, {b=}"
@func.register
def _(a: typing.Literal[49], b):
return f"The meaning of life {a=}, {b=}"
@func.register
def _(a: Pet.Cat, b):
return f"meow! {a=}, {b=}"
@func.register(Pet.Dog, literal=True)
def _(a, b):
return f"Good boy! {a=}, {b=}"
@func.register(int, literal=True)
def _(a, b):
return f"Literally, int! {a=}, {b=}"
vals = [
123,
"some string",
49,
Pet.Cat,
Pet.Dog,
Pet.Shark,
int,
None,
]
for v in vals:
output = func(v, None)
fmt = ("[yellow]", "[/yellow]") if _use_rich else ("", "")
print(f"Calling func with {v}: {fmt[0]}{output}{fmt[1]}")