Skip to content

Commit 4c54f89

Browse files
author
Toto Lin
authored
Merge pull request #64 from Hukeqing/master
根据模式串生成数据(字符串)
2 parents fe4bfe8 + 19f06af commit 4c54f89

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-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: 11 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,12 @@ 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 = int(kwargs.get("limit", default="10"))
105+
if (limit_len <= 1): limit_len = 10
106+
if (list_like(args)):
107+
pattern = random.choice(args)
108+
_x = xeger.Xeger(limit=limit_len)
109+
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)