Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type info updated #1

Merged
merged 6 commits into from
Apr 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions docs/devel/example_closure.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
# Python closure example

def OuterCount(start):
counter = [start] # counter is 1-element array
print "passei por fora"
def InnerCount():
from typing import List, Callable

def OuterCount(start: int) -> Callable[[], int]:
counter: List[int] = [start] # counter is 1-element array
print("passei por fora")
def InnerCount() -> int:
counter[0] = counter[0] + 1
print "passei por dentro"
print("passei por dentro")
return counter[0]
return InnerCount

print "Init counter at 5"
count = OuterCount(5)
print "\nRun counter 3 times"
print count()
print count()
print count()
print("Init counter at 5")
count: Callable[[], int] = OuterCount(5)
print("\nRun counter 3 times")
print(count())
print(count())
print(count())

print "**********"
print("**********")
count = OuterCount(0)
print count()
print(count())
27 changes: 14 additions & 13 deletions docs/devel/example_pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
# http://wiki.wxpython.org/ModelViewController
# http://wiki.wxpython.org/PubSub
from invesalius.pubsub import pub as Publisher
from typing import Dict, Any

# The maintainer of Pubsub module is Oliver Schoenborn.
# Since the end of 2006 Pubsub is now maintained separately on SourceForge at:
# http://pubsub.sourceforge.net/


class Student:
def __init__(self, name):
def __init__(self, name: str):
self.name = name
self.mood = ":|"
self.__bind_events()
Expand All @@ -20,39 +21,39 @@ def __bind_events(self):
Publisher.subscribe(self.ReceiveProject, "Set Student Project")
Publisher.subscribe(self.ReceiveGrade, "Set Student Grade")

def ReceiveProject(self, pubsub_evt):
projects_dict = pubsub_evt.data
def ReceiveProject(self, pubsub_evt: Any):
projects_dict: Dict[str, str] = pubsub_evt.data
self.project = projects_dict[self.name]
print "%s: I've received the project %s" % (self.name, self.project)
print(f"{self.name}: I've received the project {self.project}")

def ReceiveGrade(self, pubsub_evt):
grades_dict = pubsub_evt.data
def ReceiveGrade(self, pubsub_evt: Any):
grades_dict: Dict[str, float] = pubsub_evt.data
self.grade = grades_dict[self.name]
if self.grade > 6:
self.mood = ":)"
else:
self.mood = ":("
print "%s: I've received the grade %d %s" % (self.name, self.grade, self.mood)
print(f"{self.name}: I've received the grade {self.grade} {self.mood}")


class Teacher:
def __init__(self, name, course):
def __init__(self, name: str, course: Any):
self.name = name
self.course = course

def SendMessage(self):
print "%s: Telling students the projects" % (self.name)
print(f"{self.name}: Telling students the projects")
Publisher.sendMessage("Set Student Project", self.course.projects_dict)

print "\n%s: Telling students the grades" % (self.name)
print(f"\n{self.name}: Telling students the grades")
Publisher.sendMessage("Set Student Grade", self.course.grades_dict)


class Course:
def __init__(self, subject):
def __init__(self, subject: str):
self.subject = subject
self.grades_dict = {}
self.projects_dict = {}
self.grades_dict: Dict[str, float] = {}
self.projects_dict: Dict[str, str] = {}


# Create students:
Expand Down
68 changes: 34 additions & 34 deletions docs/devel/example_singleton.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,48 @@
class Singleton(type):
# This is a Gary Robinson implementation:
# http://www.garyrobinson.net/2004/03/python_singleto.html
def __init__(cls,name,bases,dic):
super(Singleton,cls).__init__(name,bases,dic)
cls.instance=None
def __call__(cls,*args,**kw):
def __init__(cls, name: str, bases: tuple, dic: dict) -> None:
super().__init__(name, bases, dic)
cls.instance = None

def __call__(cls, *args: tuple, **kw: dict) -> object:
if cls.instance is None:
cls.instance=super(Singleton,cls).__call__(*args,**kw)
cls.instance = super().__call__(*args, **kw)
return cls.instance

class Bone(object):
# Only one project will be initialized per time. Therefore, we use
# Singleton design pattern for implementing it
__metaclass__= Singleton

def __init__(self):
self.size = 100

def RemovePart(self, part_size):
self.size -= part_size # self.size = self.size - part_size

class Dog():
def __init__(self, name):
self.name = name
self.bone = Bone()

def EatBonePart(self, part_size):
self.bone.RemovePart(part_size)

print "Initial state:"
d1 = Dog("Nina")
d2 = Dog("Tang")
print "Bone size of %s: %d"%(d1.name, d1.bone.size)
print "Bone size of %s: %d\n"%(d2.name, d2.bone.size)

print "Only Nina eats:"
d1.EatBonePart(5)
print "Bone size of %s: %d"%(d1.name, d1.bone.size)
print "Bone size of %s: %d\n"%(d2.name, d2.bone.size)
def __init__(self) -> None:
self.size: int = 100

print "Tang eats after Nina:"
d2.EatBonePart(20)
print "Bone size of %s: %d"%(d1.name, d1.bone.size)
print "Bone size of %s: %d"%(d2.name, d2.bone.size)
def RemovePart(self, part_size: int) -> None:
self.size -= part_size


class Dog:
def __init__(self, name: str) -> None:
self.name: str = name
self.bone: Bone = Bone()

def EatBonePart(self, part_size: int) -> None:
self.bone.RemovePart(part_size)


print("Initial state:")
d1: Dog = Dog("Nina")
d2: Dog = Dog("Tang")
print(f"Bone size of {d1.name}: {d1.bone.size}")
print(f"Bone size of {d2.name}: {d2.bone.size}\n")

print("Only Nina eats:")
d1.EatBonePart(5)
print(f"Bone size of {d1.name}: {d1.bone.size}")
print(f"Bone size of {d2.name}: {d2.bone.size}\n")

print("Tang eats after Nina:")
d2.EatBonePart(20)
print(f"Bone size of {d1.name}: {d1.bone.size}")
print(f"Bone size of {d2.name}: {d2.bone.size}")
53 changes: 26 additions & 27 deletions docs/devel/example_singleton_pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ class Singleton(type):
# This is a Gary Robinson implementation:
# http://www.garyrobinson.net/2004/03/python_singleto.html

def __init__(cls,name,bases,dic):
super(Singleton,cls).__init__(name,bases,dic)
cls.instance=None
def __call__(cls,*args,**kw):
def __init__(cls, name: str, bases: tuple, dic: dict):
super(Singleton, cls).__init__(name, bases, dic)
cls.instance = None

def __call__(cls, *args, **kw):
if cls.instance is None:
cls.instance=super(Singleton,cls).__call__(*args,**kw)
cls.instance = super(Singleton, cls).__call__(*args, **kw)
return cls.instance

class Pizza(object):
Expand All @@ -21,42 +21,41 @@ class Pizza(object):
__metaclass__= Singleton

def __init__(self):
self.npieces = 8
self.npieces: int = 8
self.__bind_events()

def __bind_events(self):
ps.Publisher().subscribe(self.RemovePiece,
'Eat piece of pizza')
Publisher.subscribe(self.RemovePiece, 'Eat piece of pizza')

def RemovePiece(self, pubsub_evt):
person = pubsub_evt.data
def RemovePiece(self, pubsub_evt: Publisher):
person: Person = pubsub_evt.data
if self.npieces:
self.npieces -= 1
print "%s ate pizza!"%(person.name)
print(f"{person.name} ate pizza!")
else:
print "%s is hungry!"%(person.name)
print(f"{person.name} is hungry!")


class Person():
def __init__(self, name):
self.name = name
self.pizza = Pizza()
def __init__(self, name: str):
self.name: str = name
self.pizza: Pizza = Pizza()

def EatPieceOfPizza(self):
ps.Publisher.sendMessage('Eat piece of pizza',(self))
Publisher.sendMessage('Eat piece of pizza', (self,))


print "Initial state:"
p1 = Person("Paulo ")
p2 = Person("Thiago")
p3 = Person("Andre ")
people = [p1, p2, p3]
print("Initial state:")
p1: Person = Person("Paulo ")
p2: Person = Person("Thiago")
p3: Person = Person("Andre ")
people: list = [p1, p2, p3]

print "Everyone eats 2 pieces:"
print("Everyone eats 2 pieces:")
for i in range(2):
for person in people:
person.EatPieceOfPizza()

print "Everyone tries to eat another piece:"
print("Everyone tries to eat another piece:")
for person in people:
person.EatPieceOfPizza()

person.EatPieceOfPizza()
Loading