oTree Forum >

Announcing drypage, a way to establish "general" page behavior while allowing for flexibility

#1 by gr0ssmann

Since the removal of "self" in method signatures, oTree's class methods no longer know to which class/page they belong or, if assigned to multiple pages, which page is currently calling the method. This requires much verbosity and potentially repetition. In the old days, it was easy to write code for a "superpage" that applied to many pages while allowing for some flexibility via class inheritance. One example of this is dropout handling: It might be interesting to "push" dropouts through every page except for some final page, e.g. one that shows the payoff. But then, what if some pages also need custom code for is_displayed or get_timeout_seconds? With the no-self format, one can implement this so that the dropout handler is called in the special page's method(s); the remaining pages either (1) inherit or - more commonly - (2) assign a function that "floats in cyberspace" to each particular Page class. This invites repetition, verbosity and potentially even more complicated issues with inheritance. Clearly, if one has a large number of Pages, this can be cumbersome.

I am announcing drypage, a simple Python library that allows you to specify a "general", "typical" behavior of pages while being able to invoke the specific current page's own methods and read its attributes. This allows, once again, the definition of neat "hook methods" or callbacks that was made impossible by the no-self format. In principle, drypage adds a new argument to any Page method. E.g., in the no-self format, is_displayed had the following signature:

    is_displayed(player: Player) -> bool

drypage allows you to write "superpage" code with the following signature:

    is_displayed(player: Player, page: Page) -> bool

You can use the new "page" argument to call or use any attribute of the actual current running page. drypage magically rewrites your "superpage" methods and transplants them onto the actual pages, allowing for general but dynamic behavior and restoring a useful pattern of the no-self format. drypage constructs the pages using a class decorator, a simple Python metaprogramming technique.

The library's code and an example can be found here: https://gitlab.com/gr0ssmann/drypage

I am looking forward to your feedback.

Write a reply

Set forum username