Skip to content

Commit 1c235f8

Browse files
committed
v0.4.13
1 parent f9f2573 commit 1c235f8

9 files changed

+134
-5
lines changed

README.md

+21
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,8 @@ Length of all numbers: 3
498498

499499
### 魔术方法
500500

501+
#### 字典
502+
501503
```python
502504
from pnlp import MagicDict
503505

@@ -522,6 +524,14 @@ print(pmag.MagicDict.reverse(dx))
522524
"""
523525
```
524526

527+
#### 获取唯一文件名
528+
529+
```python
530+
from pnlp import get_unique_fn
531+
532+
get_unique_fn("a/b/c.md") == "a_b_c.md"
533+
```
534+
525535
### 并行处理
526536

527537
支持四种并行处理方式:
@@ -561,6 +571,17 @@ def get_primes(lst):
561571

562572
`concurring` 装饰器让你的迭代函数并行。
563573

574+
### 后台处理
575+
576+
```python
577+
from pnlp import run_in_new_thread
578+
579+
def func(file, a, b, c):
580+
background_task()
581+
582+
run_in_new_thread(func, file, 1, 2, 3)
583+
```
584+
564585
## 测试
565586

566587
Clone 仓库后执行:

README_EN.md

+25
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,8 @@ Length of all numbers: 3
493493

494494
### Magic
495495

496+
#### MagicDict
497+
496498
```python
497499
from pnlp import MagicDict
498500

@@ -517,6 +519,14 @@ print(pmag.MagicDict.reverse(dx))
517519
"""
518520
```
519521

522+
#### GetUniqueFileName
523+
524+
```python
525+
from pnlp import get_unique_fn
526+
527+
get_unique_fn("a/b/c.md") == "a_b_c.md"
528+
```
529+
520530
### Concurring
521531

522532
Support 4 types of concurring:
@@ -556,6 +566,17 @@ def get_primes(lst):
556566

557567
`concurring` wrapper just make your original function concurring.
558568

569+
### Background
570+
571+
```python
572+
from pnlp import run_in_new_thread
573+
574+
def func(file, a, b, c):
575+
background_task()
576+
577+
run_in_new_thread(func, file, 1, 2, 3)
578+
```
579+
559580
## Test
560581

561582
Clone the repo run:
@@ -566,6 +587,10 @@ $ python -m pytest
566587

567588
## ChangeLog
568589

590+
**v0.4.13**
591+
592+
Feat: Background task and magic get unique file name from file path.
593+
569594
**v0.4.12**
570595

571596
Feat: Subsentence cut and combine with a given threshold.

pnlp/__init__.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
from pnlp.pnorm import NumNorm
99
from pnlp.penh import TokenLevelSampler, SentenceLevelSampler
1010
from pnlp.ptrans import pick_entity_from_bio_labels, generate_uuid
11-
from pnlp.pmag import MagicDict
11+
from pnlp.pmag import MagicDict, get_unique_fn
1212
from pnlp.stopwords import StopWords
1313
from pnlp.stopwords import chinese_stopwords, english_stopwords
1414

15-
from pnlp.utils import pstr, concurring, divide2int
15+
from pnlp.utils import pstr, concurring, divide2int, run_in_new_thread
1616
from pnlp.utils import generate_batches_by_num, generate_batches_by_size
1717

1818

@@ -24,7 +24,7 @@
2424

2525

2626
__title__ = "pnlp"
27-
__version__ = "0.4.12"
27+
__version__ = "0.4.13"
2828
__author__ = "Yam"
2929
__license__ = "Apache-2.0"
3030
__copyright__ = "Copyright 2019, 2020, 2021, 2022, 2023 Yam"

pnlp/piop.py

+1
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ def write_json(fpath: str, data, **kwargs) -> None:
212212
fout.close()
213213

214214

215+
215216
def write_file(fpath: str, data, **kwargs) -> None:
216217
with open(fpath, "w", **kwargs) as fout:
217218
for line in data:

pnlp/pmag.py

+18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from collections import Counter
22
from addict import Dict
3+
from pathlib import Path
34

45

56
class MagicDict(Dict):
@@ -46,3 +47,20 @@ def reverse(dic):
4647
else (x, d2[x])
4748
for x in d1.keys()])
4849
return reversdict
50+
51+
52+
def get_unique_fn(file_path: str, level=0):
53+
fp = Path(file_path)
54+
fn = fp.name
55+
56+
file_path = str(file_path).strip("/")
57+
tmp = file_path.split("/")[:-1]
58+
length = len(tmp)
59+
if level > length:
60+
level = length
61+
62+
if length == 0:
63+
return fn
64+
65+
path = "_".join(tmp[-level:])
66+
return "_".join([path, fn])

pnlp/utils.py

+9
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,12 @@ def wrapper(lst: List[Any], *args, **kwargs):
145145
raise ValueError(err_info)
146146

147147
return wrapper
148+
149+
150+
def run_in_new_thread(
151+
func: Callable, *args, **kwargs
152+
):
153+
if kwargs:
154+
func = partial(func, **kwargs)
155+
t = Thread(target=func, name="BackgroundRun", args=args)
156+
t.start()

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name="pnlp",
8-
version="0.4.12",
8+
version="0.4.13",
99
author="Yam",
1010
author_email="[email protected]",
1111
description="A pre/post-processing tool for NLP.",

tests/test_pmag.py

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from pnlp.pmag import MagicDict
1+
import pytest
2+
from pnlp.pmag import MagicDict, get_unique_fn
23

34

45
def test_magic():
@@ -16,3 +17,23 @@ def test_magic_set_get():
1617
def test_magic_reverse():
1718
dx = {1: "a", 2: "a", 3: "a", 4: "b"}
1819
assert MagicDict.reverse(dx) == {"a": [1, 2, 3], "b": 4}
20+
21+
22+
23+
@pytest.mark.parametrize("inp,oup,level", [
24+
("a.md", "a.md", 0),
25+
("a.md", "a.md", 1),
26+
("a.md", "a.md", 10),
27+
("a/b.md", "a_b.md", 0),
28+
("a/b.md", "a_b.md", 1),
29+
("a/b.md", "a_b.md", 10),
30+
("a/b/c.md", "a_b_c.md", 0),
31+
("a/b/c.md", "b_c.md", 1),
32+
("a/b/c.md", "a_b_c.md", 10),
33+
("/a/b/c.md", "a_b_c.md", 0),
34+
("/a/b/c.md", "b_c.md", 1),
35+
("/a/b/c.md", "a_b_c.md", 10),
36+
])
37+
def test_get_unique_fn(inp, level, oup):
38+
res = get_unique_fn(inp, level)
39+
assert res == oup

tests/test_utils.py

+34
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import re
2+
import os
3+
from functools import partial
24
import math
35
import itertools
46
import pytest
57
import multiprocessing as mp
68

79
from pnlp.utils import pstr, concurring, generate_batches_by_num
10+
from pnlp.utils import run_in_new_thread
11+
from pnlp.piop import read_file, write_file
812

913

1014
def test_pstr1():
@@ -119,3 +123,33 @@ def get_primes(lst):
119123
return res
120124
except Exception as err:
121125
assert "0" in str(err)
126+
127+
128+
129+
def test_run_in_thread():
130+
file = "run_in_new_thread.txt"
131+
132+
def func(file, a, b, c):
133+
write_file(file, list(map(str, [a, b, c])))
134+
135+
run_in_new_thread(func, file, 1, 2, 3)
136+
import time
137+
time.sleep(1)
138+
139+
assert os.path.exists(file)
140+
os.remove(file)
141+
142+
143+
def test_run_in_thread_kwargs():
144+
kwargs = {
145+
"b": 2,
146+
"c": 3,
147+
}
148+
149+
def func(a, b, c):
150+
return a + b + c
151+
152+
func = partial(func, **kwargs)
153+
154+
assert 6 == func(1)
155+

0 commit comments

Comments
 (0)