-
Notifications
You must be signed in to change notification settings - Fork 0
Parameters
Data tests provide a way for you to validate your assumptions about your data. However these assumptions may change over time and still be completely valid. For example you might assume that the weekly sales number is between $1000 and $10000. However, if this number is now consistently $20000, it doesn't necessarily mean that your data is wrong. It may mean that your business has grown or changed. In this case, you would need to update your data tests to reflect the new reality. Parameters provides a simple mechanism to do just that.
Assume that you have a data test set up. It might look like this:
from panda_patrol.patrols import patrol_group
with patrol_group("Sales") as patrol:
@patrol("Weekly Sales Within Range")
def weekly_sales_within_range(patrol_id):
weekly_sales = get_weekly_sales()
if weekly_sales < 1000 or weekly_sales > 10000:
raise Exception("Weekly sales is not within range")
return weekly_salesThis test may have been valid at one point in time. However, if the business has grown, then this test may no longer be valid. In this case, you would need to update the test to reflect the new reality. You can go back into the code base and change the number. Alternatively, you can also use parameters and modify its value from the frontend.
Adjustable parameters are parameters that can be modified from the frontend. This allows you to update your data tests without having to go back into the code base. To use adjustable parameters, you define it with the adjustable_parameter method. This method takes in the following parameters:
-
parameter_id: str- The id of the parameter. This should be unique to the patrol. -
patrol_id: str- The id of the patrol that this parameter is associated with. This id (patrol_id) is passed into the decorated data test method. -
value: Any- The value of the parameter. This can be any Python object as long as it can be converted to a string.
This method stores the value as a string in the database and returns the current value of the parameter as a string. If the parameter already exists, then the value is updated. Otherwise, a new parameter is created. Here is an example of how you would use it for the above example:
from panda_patrol.patrols import patrol_group
+ from panda_patrol.parameters import adjustable_parameter
with patrol_group("Sales") as patrol:
@patrol("Weekly Sales Within Range")
def weekly_sales_within_range(patrol_id):
weekly_sales = get_weekly_sales()
+ if weekly_sales < int(
+ adjustable_parameter("min_weekly_sales", patrol_id, 1000)
+ ) or weekly_sales > int(
+ adjustable_parameter("max_weekly_sales", patrol_id, 10000)
+ ):
raise Exception("Weekly sales is not within range")
return weekly_salesThese parameters can then be updated from the frontend in the future.
Static parameters are parameters that cannot be modified from the frontend. This is useful for parameters that you want to be able to show on the frontend but do not want to be able to modify. To use static parameters, you define it with the static_parameter method. The parameters for this method are the same as the adjustable_parameter method and returns a string representation of the value. Here is an example of how you would use it for the above example:
from panda_patrol.patrols import patrol_group
+ from panda_patrol.parameters import static_parameter
with patrol_group("Sales") as patrol:
@patrol("Weekly Sales Within Range")
def weekly_sales_within_range(patrol_id):
weekly_sales = 20000
+ if weekly_sales < int(
+ static_parameter("min_weekly_sales", patrol_id, 1000)
+ ) or weekly_sales > int(
+ static_parameter("max_weekly_sales", patrol_id, 10000)
+ ):
raise Exception("Weekly sales is not within range")
return weekly_sales❗IMPORTANT
Note that these parameter methods returns a string representation of the value. This means that you will need to convert it to the appropriate type before using it. In the above example, we convert it to an integer before using it in the data test.
Documentation