Like os.walk, but from a Path instance.
We have Path.iterdir, but it's not recursive. Which you use either
os.scandir, or os.walk. In any case, you end up doing:
directory = pathlib.Path(get_dir())
# do things with directory
for root, dirs, files os.walk(directory):
root = Path(root)
for f in files:
f = root / f
# do something with file
Which is not that bad, but you waste a lot of time discovering how to do
that since you look first for something like Path.walk.
Today I got burned because I had code that did this:
When it should have done this:
There's also another timeout error class in asyncio.
Initially I thought, why not make them all use the same exception class?
But Guido objects to that:
I considered this, and decided against unifying the two TimeoutErrors.
First the builtin TimeoutError is specifically a subclass of OSError
representing the case where errno is ETIMEDOUT. But
asyncio.TimeoutError means nothing of the sort.
Second, the precedent is concurrent.futures.TimeoutError. The asyncio
one is used under the same conditions as that one.
I think we should just update the links in the docs to be correct.
So here's my idea: Define a new base class `BaseTimeoutError` and have it
be a base class of all other timeout errors. This way people could do:
And it'll catch all of them.
I thought of doing it myself as an abc, but if I remember correctly the
except logic ignored __subclasshook__ or __instancecheck__ or whatever is
used by abc, so I think this can only work by modifying CPython.
What do you think?
This keeps on coming up in one form or another - either someone
multiplies a list of lists and ends up surprised that they're all the
same, or is frustrated with the verbosity of the alternatives.
Can we use the matmul operator for this?
def __matmul__(self, other):
return [copy.copy(x) for x in self for _ in range(other)]
>>> x = List([*4]) @ 2
[[0, 0, 0, 0], [0, 0, 0, 0]]
>>> x = 1
[[1, 0, 0, 0], [0, 0, 0, 0]]
If this were supported by the built-in list type, it would be either of these:
>>> x = [ * 4] @ 2
>>> x = [ @ 4] @ 4
(identical functionality, as copying an integer has no effect).
The semantics could be either as shown above (copy.copy()), or
something very simple and narrow like "lists get shallow-copied, other
objects get referenced".
> To me, 'pop' implies mutation. Tuples do not have a pop method, and it
> is not obvious to me that either tuples or frozensets should. What are > the use cases that are not better served by converting to list or set?
> Terry Jan Reedy
1) coverting to set or list is O(n) in time
2) if I have to keep the old copy,
standard set solution will be O(n) both in time and space!
1) priority queue:
insert and pop occur
2) share immutable data to difference subroutines:
each one can modify local copy safely and concurrency.