Skip to content

Commit 2a01cd4

Browse files
committed
make async and rename
1 parent 678eb30 commit 2a01cd4

File tree

11 files changed

+93
-69
lines changed

11 files changed

+93
-69
lines changed

.github/workflows/python.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ jobs:
2424
python -m pip install -r ci_requirements.txt
2525
- name: test
2626
run: |
27-
pytest --cov=covertable
27+
pytest --cov=covertable --cov-report=xml
2828
- name: codecov
2929
uses: codecov/codecov-action@v3
3030
with:
3131
token: ${{ secrets.CODECOV_TOKEN }}
32+
file: ./coverage.xml
3233
verbose: true
3334

3435
release:

python/.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ __pycache__/
33
.tox/
44
venv/
55
.coverage
6+
coverage.xml
67
.junit.xml
78
htmlcov/
89
pip-wheel-metadata
910
*.egg-info
1011
build/
11-
dist/
12+
dist/

python/covertable/criteria/greedy.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33
from itertools import combinations
44

55

6-
def get_num_removable_pairs(indexes, incompleted, length):
6+
def get_num_removable_pairs(indexes, incomplete, length):
77
removing_keys = combinations(indexes, length)
8-
return len(incompleted.intersection(removing_keys))
8+
return len(incomplete.intersection(removing_keys))
99

1010

1111
def extract(
12-
sorted_incompleted, row, parents, length, incompleted, tolerance=0, **kwargs
12+
sorted_incomplete, row, parents, length, incomplete, tolerance=0, **kwargs
1313
):
1414
while True:
1515
max_num_pairs = None
1616
efficient_pair = None
1717

18-
for pair in sorted_incompleted:
18+
for pair in sorted_incomplete:
1919
if not row:
2020
yield pair
2121
continue
@@ -31,7 +31,7 @@ def extract(
3131
continue
3232

3333
num_pairs = get_num_removable_pairs(
34-
sorted({*row.values(), *pair}), incompleted, length
34+
sorted({*row.values(), *pair}), incomplete, length
3535
)
3636
if num_pairs + tolerance > len(row) * storable:
3737
efficient_pair = pair

python/covertable/criteria/simple.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
def extract(sorted_incompleted, row, parents, **kwargs):
2-
for pair in sorted_incompleted:
1+
def extract(sorted_incomplete, row, parents, **kwargs):
2+
for pair in sorted_incomplete:
33
storable = row.storable([(parents[p], p) for p in pair])
44
if storable is None:
55
continue

python/covertable/main.py

Lines changed: 54 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ def convert_factors_to_serials(factors):
2929
return serials, parents
3030

3131

32-
def make_incompleted(serials, length):
33-
incompleted = set()
32+
def make_incomplete(serials, length):
33+
incomplete = set()
3434
for keys in combinations([k for k, _ in get_items(serials)], length):
3535
for pair in product(*[serials[keys[i]] for i in range(length)]):
36-
incompleted.add(pair)
37-
return incompleted
36+
incomplete.add(pair)
37+
return incomplete
3838

3939

4040
class Row(dict):
@@ -65,7 +65,7 @@ def storable(self, candidate=[]):
6565
if self.pre_filter is None:
6666
return num
6767
nxt = self.new({**self, **dict(candidate)})
68-
if not self.pre_filter(nxt.restore()):
68+
if not self.pre_filter(nxt.resolve()):
6969
return None
7070
return num
7171

@@ -79,14 +79,21 @@ def complement(self):
7979
raise InvalidCondition(InvalidCondition.message)
8080
return self
8181

82-
def restore(self):
82+
def resolve(self):
8383
return self.new(
8484
[key, self.factors[key][serial - self.serials[key][0]]]
8585
for key, serial in self.items()
8686
)
8787

88+
def restore(self):
89+
resolved = self.resolve()
90+
if issubclass(self.type, list):
91+
return [r for _, r in sorted(resolved.items())]
92+
if issubclass(self.type, dict):
93+
return dict(resolved)
94+
8895

89-
def make(
96+
def make_async(
9097
factors,
9198
length=2,
9299
progress=False,
@@ -97,55 +104,70 @@ def make(
97104
**params,
98105
):
99106
serials, parents = convert_factors_to_serials(factors)
100-
incompleted = make_incompleted(serials, length)
101-
len_incompleted = float(len(incompleted))
107+
incomplete = make_incomplete(serials, length)
108+
len_incomplete = float(len(incomplete))
102109
md5_cache = {}
103110

104-
rows, row = [], Row(None, factors, serials, pre_filter)
111+
row = Row(None, factors, serials, pre_filter)
105112
# When pre_filter is specified,
106-
# it will be applied to incompleted through `row.storable` beforehand.
107-
for pair in list(filter(lambda _: pre_filter, incompleted)):
113+
# it will be applied to incomplete through `row.storable` beforehand.
114+
for pair in list(filter(lambda _: pre_filter, incomplete)): #########
108115
if not row.storable([(parents[p], p) for p in pair]):
109-
incompleted.discard(pair)
116+
incomplete.discard(pair)
110117

111-
while incompleted:
118+
while incomplete:
112119
if row.filled():
113-
rows.append(row)
120+
if post_filter is None or post_filter(row.resolve()):
121+
yield row.restore()
114122
row = row.new()
115123

116124
common_kwargs = {
117125
**params,
118126
"row": row,
119127
"parents": parents,
120128
"length": length,
121-
"incompleted": incompleted,
129+
"incomplete": incomplete,
122130
"md5_cache": md5_cache,
123131
}
124-
sorted_incompleted = sorter.sort(**common_kwargs)
125-
for pair in criterion.extract(sorted_incompleted, **common_kwargs):
132+
sorted_incomplete = sorter.sort(**common_kwargs)
133+
for pair in criterion.extract(sorted_incomplete, **common_kwargs):
126134
if row.filled():
127135
break
128136
row.update((parents[p], p) for p in pair)
129137
for vs in combinations(sorted(row.values()), length):
130-
incompleted.discard(vs)
138+
incomplete.discard(vs)
131139
else:
132140
if not row.filled():
133141
row.complement()
134142

135143
if progress:
136-
rate = (len_incompleted - len(incompleted)) / len_incompleted
144+
rate = (len_incomplete - len(incomplete)) / len_incomplete
137145
print("{0:.2%}\r".format(rate), end="")
138146

139147
if row:
140-
rows.append(row.complement())
141-
142-
result = []
143-
for row in rows:
144-
restored = row.restore()
145-
if post_filter and not post_filter(restored):
146-
continue
147-
if issubclass(row.type, list):
148-
result.append([r for _, r in sorted(restored.items())])
149-
elif issubclass(row.type, dict):
150-
result.append(dict(restored))
151-
return result
148+
row.complement()
149+
if post_filter is None or post_filter(row.resolve()):
150+
yield row.restore()
151+
152+
153+
def make(
154+
factors,
155+
length=2,
156+
progress=False,
157+
sorter=sorters.hash,
158+
criterion=criteria.greedy,
159+
pre_filter=None,
160+
post_filter=None,
161+
**params,
162+
):
163+
gen = make_async(
164+
factors,
165+
length=length,
166+
progress=progress,
167+
sorter=sorter,
168+
criterion=criterion,
169+
pre_filter=pre_filter,
170+
post_filter=post_filter,
171+
**params,
172+
)
173+
return list(gen)

python/covertable/sorters/hash.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import hashlib
55

66

7-
def sort(incompleted, md5_cache, seed="", use_cache=True, *args, **kwargs):
7+
def sort(incomplete, md5_cache, seed="", use_cache=True, *args, **kwargs):
88
def comparer(v):
99
if use_cache and v in md5_cache:
1010
return md5_cache[v]
@@ -15,4 +15,4 @@ def comparer(v):
1515
md5_cache[v] = value
1616
return value
1717

18-
return sorted(incompleted, key=comparer)
18+
return sorted(incomplete, key=comparer)

python/covertable/sorters/random.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ def random_comparer(_):
88
return random.random()
99

1010

11-
def sort(incompleted, *args, **kwargs):
12-
return sorted(incompleted, key=random_comparer)
11+
def sort(incomplete, *args, **kwargs):
12+
return sorted(incomplete, key=random_comparer)

typescript/src/criteria/greedy.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
import {CriterionArgsType, IncompletedType, PairType} from '../types';
1+
import {CriterionArgsType, IncompleteType, PairType} from '../types';
22
import {getCandidate, combinations, ascOrder, unique} from '../utils';
33

4-
const getNumRemovablePairs = (indexes: Set<number>, incompleted: IncompletedType, length: number) => {
4+
const getNumRemovablePairs = (indexes: Set<number>, incomplete: IncompleteType, length: number) => {
55
let num = 0;
66
const removingKeys = combinations([... indexes], length);
77
for (let pair of removingKeys) {
88
const key = unique(pair);
9-
if (incompleted.has(key)) {
9+
if (incomplete.has(key)) {
1010
num++;
1111
}
1212
}
1313
return num;
1414
};
1515

1616
export default function* (
17-
incompleted: IncompletedType,
17+
incomplete: IncompleteType,
1818
criterionArgs: CriterionArgsType,
1919
): Generator<PairType> {
2020
let {row, parents, length, tolerance} = criterionArgs;
@@ -23,7 +23,7 @@ export default function* (
2323
let maxNumPairs: number | null = null;
2424
let efficientPair: PairType | null = null;
2525

26-
for (let [pairKey, pair] of incompleted.entries()) {
26+
for (let [pairKey, pair] of incomplete.entries()) {
2727
const rowSize = row.size;
2828
if (rowSize === 0) {
2929
yield pair;
@@ -39,12 +39,12 @@ export default function* (
3939
}
4040

4141
if (storable === 0) {
42-
incompleted.delete(pairKey);
42+
incomplete.delete(pairKey);
4343
continue;
4444
}
4545

4646
const numPairs = getNumRemovablePairs(
47-
new Set([... row.values(), ...pair]), incompleted, length
47+
new Set([... row.values(), ...pair]), incomplete, length
4848
);
4949

5050
if (numPairs + tolerance > rowSize * storable) {

typescript/src/criteria/simple.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import {CriterionArgsType, IncompletedType, PairType} from '../types';
1+
import {CriterionArgsType, IncompleteType, PairType} from '../types';
22
import {getCandidate} from '../utils';
33

44
export default function* (
5-
incompleted: IncompletedType,
5+
incomplete: IncompleteType,
66
criterionArgs: CriterionArgsType,
77
): Generator<PairType> {
88
const {row, parents} = criterionArgs;
9-
for (let pair of incompleted.values()) {
9+
for (let pair of incomplete.values()) {
1010
const storable = row.storable(getCandidate(pair, parents));
1111
if (storable === null || storable === 0) {
1212
continue;

typescript/src/index.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
MappingTypes,
2020
Scalar,
2121
Dict,
22-
IncompletedType,
22+
IncompleteType,
2323
ParentsType,
2424
CandidateType,
2525
RowType,
@@ -52,12 +52,12 @@ const serialize = (factors: FactorsType): MappingTypes => {
5252
return { serials, parents, indices };
5353
};
5454

55-
const makeIncompleted = (
55+
const makeIncomplete = (
5656
mappings: MappingTypes,
5757
length: number,
5858
sorter: SorterType,
5959
seed: Scalar
60-
): IncompletedType => {
60+
): IncompleteType => {
6161
const { serials, indices } = mappings;
6262
const pairs: PairType[] = [];
6363
const allKeys = getItems(serials).map(([k, _]) => k);
@@ -68,11 +68,11 @@ const makeIncompleted = (
6868
pairs.push(pair);
6969
}
7070
}
71-
const incompleted: IncompletedType = new Map();
71+
const incomplete: IncompleteType = new Map();
7272
for (let pair of sorter(pairs, { seed, indices })) {
73-
incompleted.set(unique(pair), pair);
73+
incomplete.set(unique(pair), pair);
7474
}
75-
return incompleted;
75+
return incomplete;
7676
};
7777

7878
class Row extends Map<Scalar, number> implements RowType {
@@ -183,24 +183,24 @@ const makeAsync = function* <T extends FactorsType>(
183183
const { preFilter, postFilter } = options;
184184
const mappings = serialize(factors);
185185
const { parents } = mappings;
186-
const incompleted = makeIncompleted(mappings, length, sorter, seed); // {"1,2": [1,2], "3,4": [3,4]}
186+
const incomplete = makeIncomplete(mappings, length, sorter, seed); // {"1,2": [1,2], "3,4": [3,4]}
187187

188188
let row: Row = new Row([], mappings, factors, preFilter);
189189

190-
for (let [pairKey, pair] of incompleted.entries()) {
190+
for (let [pairKey, pair] of incomplete.entries()) {
191191
if (!row.storable(getCandidate(pair, parents))) {
192-
incompleted.delete(pairKey);
192+
incomplete.delete(pairKey);
193193
}
194194
}
195-
while (incompleted.size) {
195+
while (incomplete.size) {
196196
if (row.filled()) {
197197
if (!postFilter || postFilter(row.toObject())) {
198198
yield row.restore() as SuggestRowType<T>;
199199
}
200200
row = row.New([]);
201201
}
202202
let finished = true;
203-
for (let pair of criterion(incompleted, {
203+
for (let pair of criterion(incomplete, {
204204
row,
205205
parents,
206206
length,
@@ -216,7 +216,7 @@ const makeAsync = function* <T extends FactorsType>(
216216
}
217217

218218
for (let p of combinations([...row.values()], length)) {
219-
incompleted.delete(unique(p));
219+
incomplete.delete(unique(p));
220220
}
221221
}
222222
if (finished && !row.filled()) {

0 commit comments

Comments
 (0)