def _generate_grid(max_p,
                   max_d,
                   max_q,
                   max_P,
                   max_D,
                   max_Q,
                   m=0,
                   p=None,
                   d=None,
                   q=None,
                   P=None,
                   D=None,
                   Q=None):
    """Generates a lists of model orders that will be considered.
    """
    def list_range(start, end):
        return list(range(start, end+1))
    ps = list_range(0, max_p)
    ds = list_range(0, max_d)
    qs = list_range(0, max_q)
    Ps = list_range(0, max_P)
    Ds = list_range(0, max_D)
    Qs = list_range(0, max_Q)

    # if some parameter is set explicitely, we want to fix it
    if p:
        ps = [p]
    if d:
        ds = [d]
    if q:
        qs = [q]
    if P:
        Ps = [P]
    if D:
        Ds = [D]
    if Q:
        Qs = [Q]

    if m in [0, 1]:  # non-seasonal models
        Ps = [0]
        Ds = [0]
        Qs = [0]

    return ps, ds, qs, Ps, Ds, Qs