Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
szabgab committed Dec 14, 2024
1 parent a3f3206 commit ca5896c
Show file tree
Hide file tree
Showing 10 changed files with 228 additions and 82 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
ReturnType = dict[str, int]
result: ReturnType = {
"blue": 1,
"green": 0,
}
#result: ReturnType = {
# "blue": "ok",
# "green": 0,
#}

# ------------------------------------------

UnionReturnType = dict[str, str | int]
ret: UnionReturnType = {
"blue": "ok",
"green": 0,
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
LabelType = str

txt = "hello"
label: LabelType = txt

print(label)


23 changes: 23 additions & 0 deletions python/examples/python-types-at-pyweb-2024-12/defined_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from typing import Literal
LevelType = Literal["debug", "info", "warning"]

size: LevelType = "debug"
#size = "error"

# ------------------------------------------

from datetime import datetime
from typing import TypedDict
HistoryType = TypedDict('HistoryType', {
"date" : datetime,
"level": LevelType,
"text": str,
})

event: HistoryType = {
"date": datetime.now(),
"level": "debug",
"text": "Demo typing",
}


22 changes: 10 additions & 12 deletions python/examples/python-types-at-pyweb-2024-12/generics.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
from typing import TypeVar
from typing import Protocol, Self

from abc import abstractmethod

class SupportsAdd(Protocol):
@abstractmethod
def __add__(self, other: Self) -> Self:
pass


# def add[T](x: T, y: T) -> T:
# return x + y
T = TypeVar('T', bound=SupportsAdd)

#T = TypeVar('T', int, str)
# T = TypeVar('T', bound=SupportsAdd)
# def add(x: T, y: T) -> T:
# return x + y
# -------------------------------
def add(x: T, y: T) -> T:
return x + y

z = add(2, 3)
print(z)

def add[T: SupportsAdd](x: T, y: T) -> T:
# -------------------------------
def adder[T: SupportsAdd](x: T, y: T) -> T:
return x + y

z = add(2, 3)
print(z + 2)
#print(z + "2")
z = adder(3, 4)
print(z)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def add[T](x: T, y: T) -> T:
return x + y

add(2, 3)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from typing import TypeVar

T = TypeVar('T', int, str)

def add(x: T, y: T) -> T:
return x + y

add(2, 3)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def do_something[T](x: T) -> T:
return x

y = do_something(23)
print(y + 19)
#print(y + "19")

15 changes: 15 additions & 0 deletions python/examples/python-types-at-pyweb-2024-12/this_or_none.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
text = "a,b,c"

print(text.split())
print(text.split(None))
print(text.split(","))




def split(text, sep: str | None=None):
return text.split(sep)

print(split(text))
print(split(text, None))
print(split(text, ","))
14 changes: 14 additions & 0 deletions python/examples/python-types-at-pyweb-2024-12/type_annotation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
def add(x: int, y: int) -> int:
return x + y


def multiply(x, y):
res: int = x * y
return res


a: int = 23

z: int = add(a, 19)

result = multiply(a, 19)
Loading

0 comments on commit ca5896c

Please sign in to comment.