Skip to content

Commit

Permalink
Update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
maartenuni committed Oct 11, 2024
1 parent 80ede0d commit 2081e42
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
8 changes: 6 additions & 2 deletions examples/python/loop-child.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ def on_leave_loop(loop, tp, mainloop):


class Trial(Psy.Trial):
def do_enter(self, tp):
print(Trial.do_enter)
def __init__(self, **kwargs):
super().__init__(**kwargs)

def do_on_enter(self, tp):
"""Chain up the virtual function, further this function does nothing"""
Psy.Trial.do_on_enter(self, tp)

def do_activate(self, tp):
loop = self.props.parent
Expand Down
43 changes: 43 additions & 0 deletions examples/python/timers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import gi
import os
import time as t

gi.require_version("Psy", "0.1")
gi.require_version("GLib", "2.0")

# The next two line may generate warnings, ignore them ;-)
from gi.repository import Psy, GLib
import psy_operators

wait_dur = Psy.Duration.new_ms(100)
clock = Psy.Clock()


class MyTimer(Psy.Timer):
def __init__(self, loop: GLib.MainLoop, Max: int, **kwargs):
super().__init__(**kwargs)
self.connect("fired", self.on_fired)
self.loop = loop
self.max = Max
self.n = 0
self.old_now = t.time()

def on_fired(self, _self, tp):
"""Callback registered in init"""
assert self is _self
now = t.time()
print(f"{self.n}: dur = {now - self.old_now} seconds")
self.old_now = now
self.props.fire_time = tp + wait_dur
self.n += 1
if self.n >= self.max:
self.loop.quit()


event_loop = GLib.MainLoop()

now = clock.now()
mt = MyTimer(event_loop, 10)
mt.props.fire_time = now + wait_dur

event_loop.run()

0 comments on commit 2081e42

Please sign in to comment.