diff options
author | Martin Czygan <martin.czygan@gmail.com> | 2020-10-21 10:57:55 +0200 |
---|---|---|
committer | Martin Czygan <martin.czygan@gmail.com> | 2020-10-21 10:57:55 +0200 |
commit | f9f28047f1f30f75afdce91e3a18adb3eb7f5dfb (patch) | |
tree | 22e9fd0cb9ba0a8fc6b9c62ab52f880e1e367662 | |
parent | 0cc68900cc160dc934233825d02ee7deaff3b439 (diff) | |
download | fuzzycat-f9f28047f1f30f75afdce91e3a18adb3eb7f5dfb.tar.gz fuzzycat-f9f28047f1f30f75afdce91e3a18adb3eb7f5dfb.zip |
use custom tmp prefix
-rw-r--r-- | fuzzycat/cluster.py | 13 |
1 files changed, 7 insertions, 6 deletions
diff --git a/fuzzycat/cluster.py b/fuzzycat/cluster.py index 9b97509..32faec6 100644 --- a/fuzzycat/cluster.py +++ b/fuzzycat/cluster.py @@ -24,13 +24,14 @@ import orjson as json import fuzzy DEFAULT_CACHE_DIR = os.path.join(os.path.expanduser("~"), ".cache", "fuzzycat") +TMP_PREFIX = 'fuzzycat-' -def sort_by_column(filename, mode="w", opts="-k 2", fast=True): +def sort_by_column(filename, mode="w", opts="-k 2", fast=True, prefix=TMP_PREFIX): """ Sort tabular file with sort(1), returns the filename of the sorted file. """ - with tempfile.NamedTemporaryFile(delete=False, mode=mode) as tf: + with tempfile.NamedTemporaryFile(delete=False, mode=mode, prefix=prefix) as tf: env = os.environ.copy() if fast: env["LC_ALL"] = "C" @@ -60,12 +61,12 @@ def cut(f=0, sep='\t'): """ return lambda v: v.split(sep)[f] -def cluster_by_title(args): +def cluster_by_title(args, prefix=TMP_PREFIX): """ Basic example for a three stage process: extract, sort, group. Speed is about: 20K/s (json roundtrip, sorting, grouping). """ - with tempfile.NamedTemporaryFile(delete=False, mode="w") as tf: + with tempfile.NamedTemporaryFile(delete=False, mode="w", prefix=prefix) as tf: for line in fileinput.input(files=args.files if len(args.files) > 0 else ('-', )): doc = json.loads(line) try: @@ -91,7 +92,7 @@ def cluster_by_title_normalized(args): Normalize title, e.g. analysisofheritability. 17k/s. """ pattern = re.compile('[\W_]+', re.UNICODE) - with tempfile.NamedTemporaryFile(delete=False, mode="w") as tf: + with tempfile.NamedTemporaryFile(delete=False, mode="w", prefix=prefix) as tf: for line in fileinput.input(files=args.files if len(args.files) > 0 else ('-', )): doc = json.loads(line) try: @@ -117,7 +118,7 @@ def cluster_by_title_nysiis(args): """ Soundex on title. """ - with tempfile.NamedTemporaryFile(delete=False, mode="w") as tf: + with tempfile.NamedTemporaryFile(delete=False, mode="w", prefix=prefix) as tf: for line in fileinput.input(files=args.files if len(args.files) > 0 else ('-', )): doc = json.loads(line) try: |