|
| 1 | +{############################################################################### |
| 2 | +## Replacements for modules.itertools (deprecated in dbt-core >= 1.9) |
| 3 | +###############################################################################} |
| 4 | + |
| 5 | +{# itertools.combinations() #} |
| 6 | +{% macro _combinations(iterable, r) %} |
| 7 | + {% set result = [] %} |
| 8 | + {% set n = iterable | length %} |
| 9 | + {% if r > n or r < 0 %} |
| 10 | + {{ return(result) }} |
| 11 | + {% endif %} |
| 12 | + {% if r == 0 %} |
| 13 | + {% do result.append(()) %} |
| 14 | + {{ return(result) }} |
| 15 | + {% endif %} |
| 16 | + {% if r == 1 %} |
| 17 | + {% for item in iterable %} |
| 18 | + {% do result.append((item,)) %} |
| 19 | + {% endfor %} |
| 20 | + {{ return(result) }} |
| 21 | + {% endif %} |
| 22 | + {% if r == 2 %} |
| 23 | + {% for i in range(n) %} |
| 24 | + {% for j in range(i + 1, n) %} |
| 25 | + {% do result.append((iterable[i], iterable[j])) %} |
| 26 | + {% endfor %} |
| 27 | + {% endfor %} |
| 28 | + {{ return(result) }} |
| 29 | + {% endif %} |
| 30 | + {# recursive for r >= 3 #} |
| 31 | + {% for i in range(n - r + 1) %} |
| 32 | + {% set rest = dbt_linreg._combinations(iterable[i + 1:], r - 1) %} |
| 33 | + {% for combo in rest %} |
| 34 | + {% do result.append((iterable[i],) + combo) %} |
| 35 | + {% endfor %} |
| 36 | + {% endfor %} |
| 37 | + {{ return(result) }} |
| 38 | +{% endmacro %} |
| 39 | + |
| 40 | +{# itertools.combinations_with_replacement() #} |
| 41 | +{% macro _combinations_with_replacement(iterable, r) %} |
| 42 | + {% set result = [] %} |
| 43 | + {% set n = iterable | length %} |
| 44 | + {% if n == 0 and r > 0 %} |
| 45 | + {{ return(result) }} |
| 46 | + {% endif %} |
| 47 | + {% if r == 0 %} |
| 48 | + {% do result.append(()) %} |
| 49 | + {{ return(result) }} |
| 50 | + {% endif %} |
| 51 | + {% if r == 1 %} |
| 52 | + {% for item in iterable %} |
| 53 | + {% do result.append((item,)) %} |
| 54 | + {% endfor %} |
| 55 | + {{ return(result) }} |
| 56 | + {% endif %} |
| 57 | + {% if r == 2 %} |
| 58 | + {% for i in range(n) %} |
| 59 | + {% for j in range(i, n) %} |
| 60 | + {% do result.append((iterable[i], iterable[j])) %} |
| 61 | + {% endfor %} |
| 62 | + {% endfor %} |
| 63 | + {{ return(result) }} |
| 64 | + {% endif %} |
| 65 | + {# recursive for r >= 3 #} |
| 66 | + {% for i in range(n) %} |
| 67 | + {% set rest = dbt_linreg._combinations_with_replacement(iterable[i:], r - 1) %} |
| 68 | + {% for combo in rest %} |
| 69 | + {% do result.append((iterable[i],) + combo) %} |
| 70 | + {% endfor %} |
| 71 | + {% endfor %} |
| 72 | + {{ return(result) }} |
| 73 | +{% endmacro %} |
| 74 | + |
1 | 75 | {############################################################################### |
2 | 76 | ## Simple univariate regression. |
3 | 77 | ###############################################################################} |
|
0 commit comments