def preference_form_builder(form_base_class, preferences=[], **kwargs):
    registry = form_base_class.registry
    preferences_obj = []
    if len(preferences) > 0:
        # Preferences have been selected explicitly
        for pref in preferences:
            if isinstance(pref, string_types):
                preferences_obj.append(registry.get(name=pref))
            elif type(pref) == tuple:
                preferences_obj.append(
                    registry.get(name=pref[0], section=pref[1]))
            else:
                raise NotImplementedError(
                    "The data you provide can't be converted to a Preference object")
    elif kwargs.get('section', None):
        # Try to use section param
        preferences_obj = registry.preferences(
            section=kwargs.get('section', None))

    else:
        # display all preferences in the form
        preferences_obj = registry.preferences()

    fields = OrderedDict()
    instances = []
    model_kwargs = kwargs.get('model', {})
    manager = registry.manager(**model_kwargs)

    for preference in preferences_obj:
        f = preference.field
        instance = manager.get_db_pref(
            section=preference.section.name, name=preference.name)
        f.initial = instance.value
        fields[preference.identifier()] = f
        instances.append(instance)

    form_class = type(
        'Custom' + form_base_class.__name__, (form_base_class,), {})
    form_class.base_fields = fields
    form_class.preferences = preferences_obj
    form_class.instances = instances
    return form_class
This function runs on kernels to check the correctness of their
dataset/ETHPy150Open EliotBerriot/django-dynamic-preferences/dynamic_preferences/forms.py/preference_form_builder--crossover with--dataset/ETHPy150Open SheffieldML/GPy/GPy/testing/kernel_tests.py/check_kernel_gradient_functions