Open mkoeppe opened 3 years ago
I'll change my mind about this if it lets us delete a good bit of custom code, but looking at the given examples:
uniq()
is trivial and could be deleted anyway. Either list(set(x))
or sorted(set(x))
are idiomatic python._stable_uniq
seems to be that it returns an iterator, but the only two uses of it in the tree immediately convert the result to a list. Can they be replaced by list(set(x))
?Replying to @orlitzky:
I'll change my mind about this if it lets us delete a good bit of custom code
I agree, there needs to be a demonstrated substantial use before we add another dependency
One example is slicing a list into sublists of size "ncpu" to feed a parallel function.
For that, we can each time define a custom "list of lists" function, for instance
lol = lambda lst, sz: [lst[i:i+sz] for i in range(0, len(lst), sz)]
as found 17 times in sage/manifolds
and sage/tensor
(try git grep 'lol ='
).
We could instead use chunked
, ichunked
,
sliced
or isliced
from more-itertools.
lol = lambda lst, sz: [lst[i:i+sz] for i in range(0, len(lst), sz)]
Changing that line on its own won't eliminate much code, though. The pattern
lol = lambda lst, sz: [lst[i:i+sz] for i in range(0, len(lst), sz)]
ind_list = [ind for ind in other._comp] # only this line changes
ind_step = max(1, int(len(ind_list)/nproc/2))
local_list = lol(ind_list, ind_step)
is repeated over and over again. If the whole thing were factored out into a helper function, then we'd be left with changing
lol = lambda lst, sz: [lst[i:i+sz] for i in range(0, len(lst), sz)]
into
lol = lambda lst, sz: chunked(lst,sz)
which only saves us a few characters.
Description changed:
---
+++
@@ -3,4 +3,7 @@
Then get rid of our homegrown iteration tools:
- from `sage.misc.misc`: `uniq`, `_stable_uniq`, `subsets`/`powerset`
+Useful:
+- [SequenceView](https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.SequenceView) - e.g. gives an idiom for taking the `len` of an iterable
+
Description changed:
---
+++
@@ -6,4 +6,5 @@
Useful:
- [SequenceView](https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.SequenceView) - e.g. gives an idiom for taking the `len` of an iterable
+- `only`/`strictly_n`/`take` - [#34509 comment:52](https://github.com/sagemath/sage/issues/34509#comment:52)
https://pypi.org/project/more-itertools/
Then get rid of our homegrown iteration tools:
sage.misc.misc
:uniq
,_stable_uniq
,subsets
/powerset
Useful:
SequenceView - e.g. gives an idiom for taking the
len
of an iterableonly
/strictly_n
/take
- #34509 comment:52CC: @tscrim @orlitzky @slel @yyyyx4
Component: refactoring
Issue created by migration from https://trac.sagemath.org/ticket/32100