Package: prawvents
PRAWvents, Events for PRAWA simple wrapper to write event-based bots withPRAW.ScopeYou can register event handlers for everything thats based on the prawstream_generatorAny other functionality is offered as-is, since this subclasses the main PRAWRedditinstance.A async version of this should be possible, but is not yet planned.QuickstartThis is a simple bot that will print out the subreddit and the submission title for all posts in the subreddits AskReddit and pics, while skipping the existing posts in AskReddit.
This example assumes the presence of apraw.iniin your working directory.fromprawventsimportEventRedditfromprawimportredditr=EventReddit(user_agent=f"ExampleBot for prawvents version (0.0.1) by /u/laundmo")# change the description and username!sub1=r.subreddit("AskReddit")sub2=r.subreddit("pics")defhandle_exception(e):# very dumb exception handlerprint(e)@r.register_event(sub1.stream.submissions,err_handler=handle_exception,skip_existing=True)@r.register_event(sub2.stream.submissions,err_handler=handle_exception)defhandle(submission:reddit.Submission):print(submission.subreddit,submission.title)r.run_loop()DocsRedditEventDecorator ObjectsclassRedditEventDecorator()Decorator class for event handlers.__init__|__init__(reddit:praw.Reddit,stream:RStream,err_handler:Callable)Initialise RedditEventDecorator.Arguments:redditEventReddit- TheEventRedditinstancestreamRStream- The stream to which the event responds.err_handlerCallable- A function thats called with the exception as a argument.__call__|__call__(f:Callable)->CallableSet the event handler.Arguments:fCallable- The event handler function.Returns:Callable- The function.EventReddit ObjectsclassEventReddit(praw.Reddit)Main Reddit instance, subclass ofpraw.Reddit.Arguments:prawpraw.Reddit- Praw Reddit superclass.__init__|__init__(*args,**kwargs)Initialise EventReddit. All arguments are passed through topraw.Redditregister_event|register_event(stream:RStream,err_handler:Callable=None,**kwargs)->RedditEventDecoratorRegister a event, should generally be used as a decorator like this:@r.register_event(subreddit.stream.submissions,err_handler=handle_exception)defevent_handler(submission):passArguments:streamRStream- The stream to which the event responds.err_handlerCallable, optional- The error handler for this event. Defaults to None.Returns:RedditEventDecorator- The decorator instance.handle_exception|handle_exception(f:Callable,e:Exception)Handle a Exception happening in a function fArguments:fCallable- The function which threw the exception.eException- The exception which was thrown.Raises:e- The Exception that was thrown.run_stream_till_none|run_stream_till_none(stream:RStream,funcs:Iterable[Callable])->NoneRuns a stream until none is returnedArguments:streamRStream- The finalized stream to run.funcsIterable[Callable]- The functions which handle this stream.run_loop|run_loop(interweave=True)->NoneRun the event loop. If interweave is Truthy, events from multiple streams will be mixed to ensure a single high-traffic stream cant take up the entire event loop. This is highly
recommended.Arguments:interweavebool, optional- Whether to interweave streams to ensure fair distribution. Defaults to True.