File: utils.py

package info (click to toggle)
python-cutadapt 4.7-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,992 kB
  • sloc: python: 9,695; ansic: 177; makefile: 159
file content (53 lines) | stat: -rw-r--r-- 1,403 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import sys
import os.path
import subprocess
from pathlib import Path


def datapath(path):
    return os.path.join(os.path.dirname(__file__), "data", path)


def cutpath(path):
    return os.path.join(os.path.dirname(__file__), "cut", path)


class FilesDifferent(Exception):
    pass


def assert_files_equal(path1, path2, ignore_trailing_space: bool = False):
    if not Path(path1).exists():
        raise FileNotFoundError(path1)
    if not Path(path2).exists():
        raise FileNotFoundError(path2)
    cmd = ["diff", "-u"]
    if sys.platform == "win32":
        cmd.append("--strip-trailing-cr")
    if ignore_trailing_space:
        if sys.platform == "darwin":
            # Ignores too much, but macOS doesn’t have the option below
            cmd.append("-b")
        else:
            cmd.append("--ignore-trailing-space")
    if sys.platform == "win32":
        path1, path2 = os.fspath(path1), os.fspath(path2)
    try:
        subprocess.check_output(cmd + [path1, path2], stderr=subprocess.STDOUT)
    except subprocess.CalledProcessError as e:
        raise FilesDifferent("\n" + e.output.decode()) from None


def binomial(n, k):
    """
    Return binomial coefficient ('n choose k').
    This implementation does not use factorials.
    """
    k = min(k, n - k)
    if k < 0:
        return 0
    r = 1
    for j in range(k):
        r *= n - j
        r //= j + 1
    return r