Skip to content

Commit 77a2f34

Browse files
committed
增加了按照正则表达式生成字符串
1 parent fe4bfe8 commit 77a2f34

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,7 @@ coverage.xml
128128
docs/_build/
129129

130130
# PyBuilder
131-
target/
131+
target/
132+
133+
# Pycharm
134+
venv

cyaron/string.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
from .consts import ALPHABET_SMALL, SENTENCE_SEPARATORS, SENTENCE_TERMINATORS
1+
from .consts import ALPHABET_SMALL, SENTENCE_SEPARATORS, SENTENCE_TERMINATORS, NUMBERS
22
from .utils import *
33
from functools import reduce
44
import random
5+
import xeger
56

67

78
class String:
@@ -97,3 +98,13 @@ def random_paragraph(sentence_count_range, **kwargs):
9798
paragraph = reduce(lambda x, y: x + random.choice(sentence_joiners) + y, sentences)
9899
return paragraph
99100

101+
@staticmethod
102+
def random_regular(*args, **kwargs):
103+
pattern = args
104+
limit_len = kwargs.get("limit", NUMBERS)
105+
if limit_len is None or limit_len == 0:
106+
limit_len = 10
107+
if(list_like(args)):
108+
pattern = random.choice(args)
109+
_x = xeger.Xeger(limit=limit_len)
110+
return _x.xeger(pattern)

cyaron/tests/str_test.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import unittest
22
from cyaron import String
3+
import re
34

45

56
class TestString(unittest.TestCase):
@@ -21,4 +22,9 @@ def test_random_sentence(self):
2122

2223
def test_random_paragraph(self):
2324
# Only test for Errors
24-
String.random_paragraph(10)
25+
String.random_paragraph(10)
26+
27+
def test_random_regular(self):
28+
pattern = r'[0-9]+\w_.{0,9}'
29+
sentence = String.random_regular(pattern, limit=5)
30+
self.assertTrue(re.match(pattern, sentence).group() == sentence)

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
pygraphviz
2+
xeger

0 commit comments

Comments
 (0)