コード例 #1
0
def create_model(model, val):
    """
    Creates a new model given a reference to it's class and a list of
    the values of it's variables.

    :param DjangoModel model:
        A reference to the class of the model that will be created.
    :param val: A list of pairs having the format(field name, field value).
    :type val: tuple(pair(str, .))
    :returns: A model with the values given.
    """
    vals_dictionary = dict(val)
    have_many_to_many_relation = any(x for x in list_of_fields(model)
                                     if (is_related(x) and
                                         'ManyToMany' in relation_type(x)))
    if not have_many_to_many_relation:
        mdl = model(**vals_dictionary)
        mdl.save()
        return mdl
    else:
        mdl = model()
        flds = list_of_fields(model)
        dict_T = {}
        for field in flds:
            dict_T[field.name] = relation_type(field)
        for key, val in vals_dictionary.items():
            if 'ManyToMany' not in dict_T[key]:
                setattr(mdl, key, val)
        mdl.save()
        for key, val in vals_dictionary.items():
            if 'ManyToMany' in dict_T[key]:
                for x in val:
                    getattr(mdl, key).add(x)
        mdl.save()
        return mdl
コード例 #2
0
def generate_model(model, size, shuffle=None):
    """ Generate model

    Generate 'size' sample models given a model and stores them in a temporary
    data base.

    Args :
        model : A reference to the class of the given model.
        size : An integer of the size of the sample models to be generated.
        shuffle : An optional boolean variable that will determine if
                  the sample input will be shuffled or not.

    Returns :
        A tuple that contains a reference to the class of the given model,
        and list of field that's not computed.
    """
    unique_fields = [(field.name,) for field in list_of_fields(model)
                     if (hasattr(field, 'unique') and field.unique
                         and not is_auto_field(field))]
    unique_together = []
    if hasattr(model._meta, 'unique_together'):
        unique_together = list(model._meta.unique_together)
    unique = unique_together + unique_fields
    unique = sort_unique_tuples(unique, model)
    unique_constraints = [unique_items(un_tuple) for un_tuple in unique]
    constraints = []
    if hasattr(model, 'Constraints'):
        constraints = model.Constraints.constraints
    constraints += unique_constraints
    if shuffle is None:
        shuffle = True
    to_be_computed = []
    dfs.size = size
    dfs([], 0, to_be_computed, constraints, model, shuffle)
    return model, to_be_computed
コード例 #3
0
def dependencies(model):
    """
    Retrieves the models the must be generated before a given model.

    :param DjangoModel model: A reference to the class of the given model.

    :rtype: List
    :returns: list of references to the classes of the models.
    """
    fields = list_of_fields(model)
    return [field.rel.to for field in fields
            if (is_required(field) and (is_related(field)
                and 'ManyToMany' not in relation_type(field)))]
コード例 #4
0
ファイル: utility.py プロジェクト: mostafa-mahmoud/djenerator
def sort_unique_tuple(var_tuple, model):
    """
    Sorts a tuple of names of a fields for a given model, in the order of
    which field comes first.

    :param tuple var_tuple:
        A tuple of strings of the names of some fields in the given model.
    :param DjangoModel model: A reference to the class of the given model.
    :rtype: tuple(str)
    :returns: A tuple of the names of the fields.
    """
    result = []
    fields = list_of_fields(model)
    for field in fields:
        if field.name in var_tuple:
            result.append(field.name)
    return tuple(result)
コード例 #5
0
def dfs(instances, cur_tuple, index, to_be_computed, constraints,
        model, to_be_shuffled):
    """
    Value generator for the fields of a given model by simulating
    a depth first search. The model will be saved in a (temporary) database.

    The interface of the predicate should be:
        boolean predicate(cur_tuple, model, field)
         - cur_tuple: List of tuples of the filled values of the field being
                      filled, in the format (str:field_name , field_value).
         - model: A reference to the class of the given model.
         - field: A reference to the class of the field being generated

         The function should handle that the given tuple might be not full,
         and it should depend that the previously generated models are stored
         in the temporary database, and it should return a boolean value that's
         true only if the required constraint is satisfied.

    :param int instances:
        The target number of generated instances of the model.
    :param cur_tuple:
        A list of pairs str:field_name, field_value of the values of
        the filled fields.
    :type cur_tuple: List(pair(str, .))
    :param int index:
        The index of the field being filled in the list of fields.
    :param List to_be_computed:
        A list used for accumulation of the ignored fields.
    :param List constraints:
        A list of predicate functions that will constraint the output.
    :param DjangoModel model: A reference to the class of the given model.
    :param boolean to_be_shuffled:
        A boolean variable that will determine if the sample data
        will be shuffled or not.
    :rtype: None
    """
    fields = list_of_fields(model)
    if index >= len(fields):
        dfs.total += 1
        create_model(model, cur_tuple)
        return 1
    else:
        list_field_values = field_sample_values(fields[index])
        if not list_field_values:
            many_to_many_related = (is_related(fields[index]) and 'ManyToMany'
                                    in relation_type(fields[index]))
            optional_field = not is_required(fields[index])
            auto_fld = is_auto_field(fields[index])
            if many_to_many_related or optional_field or auto_fld:
                if not is_auto_field(fields[index]):
                    to_be_computed.append(fields[index])
                return dfs(instances, cur_tuple, index + 1, to_be_computed,
                           constraints, model, to_be_shuffled)
        else:
            if to_be_shuffled:
                random.shuffle(list_field_values)
            instances_so_far = 0
            for field_id, nxt_field in enumerate(list_field_values):
                new_tuple = cur_tuple[:]
                new_tuple.append((fields[index].name, nxt_field))
                are_constraints_satisfied = True
                for cons in constraints:
                    if not cons(new_tuple, model, fields[index]):
                        are_constraints_satisfied = False
                        break
                if are_constraints_satisfied:
                    instances_remaining = instances - instances_so_far
                    remaining_values = len(list_field_values) - field_id
                    value_instances = ((instances_remaining - 1 +
                                       remaining_values) / remaining_values)
                    new_instances = dfs(value_instances, new_tuple, index + 1,
                                        to_be_computed, constraints, model,
                                        to_be_shuffled)
                    instances_so_far += new_instances
                    if instances_so_far >= instances or dfs.total >= dfs.size:
                        return instances_so_far
            return instances_so_far
コード例 #6
0
def dfs(cur_tuple, index, to_be_computed, constraints, model, to_be_shuffled):
    """ Depth first search

    Generates values for the fields of a given model by simulating
    a depth first search.

    Args :
        cur_tuple : current tuple, a tuple of the values of the filled fields.
        index : the index of the field being filled in the list of fields.
        to_be_computed : A list used for accumulation of the ignored fields.
        constraints : a list of utility, that will constraint the output.
        model : a reference to the class of the given model.
        to_be_shuffled : A boolean variable that will determine if the sample
                         data will be shuffled or not.

    Returns:
        None

    The model will be saved in a temporary database.

    The interface of the predicate should be :
        predicate(cur_tuple, model, field)
            where:
                - cur_tuple : list of tuples of the filled values of the field
                              being filled, in the
                              format (field name , field value).

                - model : a reference to the class of the given model.

                - field : A reference to the class of the field being generated

         The function should handle that the given tuple might be not full,
         and it should depend that the previously generated models are stored
         in the temporary database, and it should return a boolean value that's
         true only if the required constraint is satisfied.

    """
    fields = list_of_fields(model)
    if dfs.size <= 0:
        return True
    if index >= len(fields):
        dfs.size -= 1
        create_model(model, cur_tuple)
    else:
        list_field_values = field_sample_values(fields[index])
        if not list_field_values:
            many_to_many_related = (is_related(fields[index]) and 'ManyToMany'
                                    in relation_type(fields[index]))
            optional_field = not is_required(fields[index])
            auto_fld = is_auto_field(fields[index])
            if many_to_many_related or optional_field or auto_fld:
                if not is_auto_field(fields[index]):
                    to_be_computed.append(fields[index])
                return dfs(cur_tuple, index + 1, to_be_computed,
                           constraints, model, to_be_shuffled)
        else:
            if to_be_shuffled:
                random.shuffle(list_field_values)
            for nxt_field in list_field_values:
                new_tuple = cur_tuple[:]
                new_tuple.append((fields[index].name, nxt_field))
                are_constraints_satisfied = True
                for cons in constraints:
                    if not cons(new_tuple, model, fields[index]):
                        are_constraints_satisfied = False
                        break
                if are_constraints_satisfied:
                    is_done = dfs(new_tuple, index + 1, to_be_computed,
                                  constraints, model, to_be_shuffled)
                    if is_done:
                        return True