Selections, Overrides, and Final Variables

Selections, Overrides, and Final Variables#

Final Variables#

The final_vars parameter is a list that defines which variables within the functions should be returned. If left empty (or if an empty list is provided), all objects will be returned.

Example:

import hypster
from hypster import HP


@hypster.config
def my_config(hp: HP):
    var1 = hp.select(["a", "b"], default="b")
    var2 = hp.select({"c": 5, "d": 7}, default="d")
    var3 = hp.text("hello")
    var4 = hp.number(10)


my_config(final_vars=["var2", "var3"])
# {'var2': 7, 'var3': 'hello'}
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[1], line 1
----> 1 import hypster
      2 from hypster import HP
      5 @hypster.config
      6 def my_config(hp: HP):

ModuleNotFoundError: No module named 'hypster'

Selections#

  • Selections only work with hp.select and need to be one of the keys for the options.

  • For dictionaries, the keys are used, and for lists, the values themselves are used as keys.

  • If there’s a selection, it takes precedence over the default value.

  • If a selection is not part of the options keys, it will raise an error.

Example:#

import hypster
from hypster import HP


@hypster.config
def my_config(hp: HP):
    var1 = hp.select(["a", "b"], default="b")
    var2 = hp.select({"c": 5, "d": 7}, default="d")
    var3 = hp.text("hello")
    var4 = hp.number(10)


my_config(selections={"var1": "a", "var2": "d"})
# {'var1': 'a', 'var2': 7, 'var3': 'hello', 'var4': 10}
{'var1': 'a', 'var2': 7, 'var3': 'hello', 'var4': 10}

Overrides#

  • Overrides work on both hp.select, text & number methods.

  • For hp.select, if the override is a key in the options, it will output the value associated with that key.

  • If it’s not in the option keys or if it’s selected for a parameter that uses text or number, it will output that value directly.

The precedence order is: overrides > selections > defaults

Warning

Currently, Hypster doesn’t support type-checking. This feature will be added in the future.

Example:#

import hypster
from hypster import HP


@hypster.config
def my_config(hp: HP):
    var1 = hp.select(["a", "b"], default="b")
    var2 = hp.select({"c": 5, "d": 7}, default="d")
    var3 = hp.text("hello")
    var4 = hp.number(10)


my_config(overrides={"var1": "hey there", "var4": 5})
# {'var1': 'hey there', 'var2': 7, 'var3': 'hello', 'var4': 5}
{'var1': 'hey there', 'var2': 7, 'var3': 'hello', 'var4': 5}
my_config(selections={"var1": "a"}, overrides={"var1": "hey there", "var4": 5})
# {'var1': 'hey there', 'var2': 7, 'var3': 'hello', 'var4': 5}
{'var1': 'hey there', 'var2': 7, 'var3': 'hello', 'var4': 5}

Note how the override takes precedence in the second example.

Defaults#

  • In hp.select, you need to specify the defaults explicitly.

  • For text and number methods, the value itself serves as the default.

Common Use Case: Empty Call#

Here’s a common use case demonstrating how defaults work with an empty call:

import hypster
from hypster import HP


@hypster.config
def my_config(hp: HP):
    var1 = hp.select(["a", "b"], default="b")
    var2 = hp.select({"c": 5, "d": 7}, default="d")
    var3 = hp.text("hello")
    var4 = hp.number(10)


my_config()
{"var1": "b", "var2": 7, "var3": "hello", "var4": 10}
{'var1': 'b', 'var2': 7, 'var3': 'hello', 'var4': 10}
  • If no final_vars are defined (empty list), it will output all the variables in the function.

  • If no selections and overrides are defined, it will output the default values.

  • If there are no defaults specified, it will raise an error.