Skip to content

Commit

Permalink
using Optional for typing for 3.7 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
rsivilli committed Aug 19, 2023
1 parent 73308c3 commit a2a1ac1
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 7 deletions.
34 changes: 34 additions & 0 deletions newtest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from twitchio.ext import routines, commands
from dotenv import load_dotenv
import os

load_dotenv()
from datetime import datetime, timedelta


THRESHOLD = 0.1


class Bot(commands.Bot):
call_count = 0

def __init__(self):
super().__init__(
token=os.getenv("access_token"),
prefix="#!",
initial_channels=["dreamingofelectricsheep"],
)

async def event_ready(self):
print(f"Logged in as | {self.nick}")
print(f"User id is | {self.user_id}")
bot.basic_scheduled_routine.start()

@routines.routine(time=datetime.now() + timedelta(seconds=5), wait_first=True)
async def basic_scheduled_routine(self):
self.call_count += 1
print(f"{self.call_count} the time is now {datetime.now()}")


bot = Bot()
bot.run()
68 changes: 68 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import asyncio
from twitchio.ext import routines, commands
from dotenv import load_dotenv
import os

load_dotenv()
from datetime import datetime, timedelta


THRESHOLD = 0.1


class Bot(commands.Bot):
call_count = 0

def __init__(self):
super().__init__(
token=os.getenv("access_token"),
prefix="#!",
initial_channels=["dreamingofelectricsheep"],
)

async def event_ready(self):
print(f"Logged in as | {self.nick}")
print(f"User id is | {self.user_id}")

@routines.routine(seconds=1.0, iterations=5)
async def basic_timed_routine(self, arg: str):
print(f"Hello {arg}!")

@routines.routine(seconds=0.1)
async def basic_test(self):
print(self.basic_timed_routine.next_execution_time)
if self.basic_timed_routine.remaining_iterations == 0:
self.basic_timed_routine.start("Restarted")

@routines.routine(seconds=1.0, iterations=5, wait_first=True)
async def basic_timed_wait_routine(self, arg: str):
print(f"Hello {arg}!")

@routines.routine(seconds=0.1)
async def basic_wait_test(self):
print(self.basic_timed_wait_routine.next_execution_time)
if self.basic_timed_wait_routine.remaining_iterations == 0:
self.basic_timed_wait_routine.start("Restarted")

@routines.routine(time=datetime.now() + timedelta(seconds=5), wait_first=True)
async def basic_scheduled_routine(self):
# Note this seems to get called twice in execution. I don't think this is a result of my changes
self.call_count += 1
print(f"{self.call_count} the time is now {datetime.now()}")

@routines.routine(seconds=0.1)
async def basic_schedule_test(self):
print(self.basic_scheduled_routine.next_execution_time)

@routines.routine(seconds=2)
async def restart_schedule_test(self):
pass


bot = Bot()
# bot.basic_timed_wait_routine.start("Test")
# bot.basic_wait_test.start()

bot.basic_scheduled_routine.start()

bot.run()
16 changes: 9 additions & 7 deletions twitchio/ext/routines/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,10 @@ def __init__(

self._instance = None

self._args: tuple | None = None
self._kwargs: dict | None = None
self.next_event_time: datetime.datetime | None = None
self._args: Optional[tuple] = None
self._kwargs: Optional[dict] = None

self.next_event_time: Optional[datetime.datetime] = None

def __get__(self, instance, owner):
if instance is None:
Expand Down Expand Up @@ -332,9 +333,10 @@ def next_execution_time(self) -> datetime.timedelta | None:

if self.next_event_time is None:
return None

return max(self.next_event_time - datetime.datetime.now(self.next_event_time.tzinfo), datetime.timedelta(seconds=0))


return max(
self.next_event_time - datetime.datetime.now(self.next_event_time.tzinfo), datetime.timedelta(seconds=0)
)

@property
def start_time(self) -> Optional[datetime.datetime]:
Expand Down Expand Up @@ -370,7 +372,7 @@ async def _routine(self, *args, **kwargs) -> None:
self.next_event_time = self._time
wait = compute_timedelta(self._time)
await asyncio.sleep(wait)

if self._wait_first and not self._time:
self.next_event_time = self._delta + datetime.datetime.now(datetime.timezone.utc)
await asyncio.sleep(self._delta)
Expand Down

0 comments on commit a2a1ac1

Please sign in to comment.