Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-02 07:54:41

0001 import os
0002 from pathlib import Path
0003 from itertools import chain, cycle, dropwhile, starmap, tee
0004 
0005 
0006 def get_cache_dir():
0007     if "XDG_CACHE_HOME" in os.environ:
0008         return os.environ["XDG_CACHE_HOME"] / "epic-capybara"
0009     elif "HOME" in os.environ:
0010         return Path(os.environ["HOME"]) / ".cache" / "epic-capybara"
0011     elif "TMPDIR" in os.environ:
0012         return Path(os.environ["TMPDIR"]) / "epic-capybara"
0013     else:
0014         raise RuntimeError("Unable to fine a suitable cache location")
0015 
0016 
0017 def skip_common_prefix(iters: list):
0018     """Given a list of iterators, skips values until at least one iterator differs from the others. Returns the remaining iterators.
0019 
0020     >>> [list(iter) for iter in skip_common_prefix(["hello"])]
0021     [['h', 'e', 'l', 'l', 'o']]
0022     >>> [list(iter) for iter in skip_common_prefix(["hello", "world!"])]
0023     [['h', 'e', 'l', 'l', 'o'], ['w', 'o', 'r', 'l', 'd', '!']]
0024     >>> [list(iter) for iter in skip_common_prefix([[3, 2, 1], [1, 2, 3, 4]])]
0025     [[3, 2, 1], [1, 2, 3, 4]]
0026     >>> [list(iter) for iter in skip_common_prefix([[1, 2, 3], [1, 2, 3, 4]])]
0027     [[], [4]]
0028     """
0029     # ensure that we have iterators, so that we can chain unconsumed tail in the end
0030     iters = list(map(iter, iters))
0031     if len(iters) == 0:
0032         raise ValueError("iters must not be empty")
0033     elif len(iters) == 1:
0034         tuple_iters = [zip(*iters)]
0035     else:
0036         tuple_iters = tee(
0037             dropwhile(lambda vals: all(val == vals[0] for val in vals), zip(*iters)),
0038             len(iters),
0039         )
0040     return list(starmap(
0041         lambda ix, tuple_iter: chain(map(lambda t: t[ix], tuple_iter), iters[ix]),
0042         enumerate(tuple_iters)
0043     ))