-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvanced.py
75 lines (60 loc) · 1.79 KB
/
advanced.py
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
68
69
70
71
72
73
from typing import Any
from warnings import warn
# function annotations
def name(fname:str, lname: str)->str:
#walrus operator , assignment operator which evaluates as an expression
# x = 0 is assigned and evaluated to 0
if x:=0:
print("x=0")
else:
print(x)
if y:=1:
print("y=1")
print(y)
print(name.__annotations__)
return f"{fname}-{lname}"
# positional only arguments
# arg before / is positional and after * is keyword
def concatinate(first :str, second:str,/,*,delim:str)->str:
return delim.join([first,second])
def newConcatinate(*items, delim: str)->str:
return delim.join(items)
# union type annotations
#use the word Unioin or|
def key_lookup(d: dict[str|bytes, Any ],key :str|bytes )->Any:
for k,v in d.items():
if key.lower()==k.lower():
print("found")
break
else:
print("not found")
## supoose key look up has been updated and the old name was checkkey
def __getattr__(name:str):
if name=="checkkey":
print("deprecated")
warn(f"{name} is deprecated",DeprecationWarning)
return key_lookup
raise AttributeError(f"module {__name__} has no attribute {name}")
# pattern matching
def matchMe(string :str)->bool:
match (string):
case("aman"):
print("pandey")
return True
case("pandey"):
print("aman")
return True
case("nothing"):
print("nothing")
return True
case _:
print("defaut")
return False
matchMe("aman")
matchMe("pandey")
matchMe("nothing")
matchMe("pytohn3")
key_lookup({"k1":2132_213213_312313,"k2":13123},"k1")
# concatinate("aman","pandey","*")
print(concatinate("aman","pandey",delim="$"))
name("aman", "pandey")