Fallbacks

We can declare parameters to act in place of others if they are not specified. For example we can specify a circle either by its radius, diameter or circumference:

from hanna import Configurable, Number

class Circle(Configurable):
    radius = Number().fallback(Number('diameter') >> (lambda x: x/2))
    
c = Circle({'radius': 5})
c.radius  # 5.0

c = Circle({'diameter': 10})
c.radius  # 5.0

The fallback parameter must be declared with the appropriate transformation in order to provide the original expected value (the radius in the example above).

Syntactic sugar

Syntactic sugar for declaring fallbacks is available:

class Circle(Configurable):
    radius = Number() | Number('diameter') >> (lambda x: x/2)

Last updated