Skip to content

Commit

Permalink
增加了按照正则表达式生成字符串
Browse files Browse the repository at this point in the history
  • Loading branch information
Hukeqing committed Jul 26, 2019
1 parent fe4bfe8 commit 77a2f34
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Expand Up @@ -128,4 +128,7 @@ coverage.xml
docs/_build/

# PyBuilder
target/
target/

# Pycharm
venv
13 changes: 12 additions & 1 deletion cyaron/string.py
@@ -1,7 +1,8 @@
from .consts import ALPHABET_SMALL, SENTENCE_SEPARATORS, SENTENCE_TERMINATORS
from .consts import ALPHABET_SMALL, SENTENCE_SEPARATORS, SENTENCE_TERMINATORS, NUMBERS
from .utils import *
from functools import reduce
import random
import xeger


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

@staticmethod
def random_regular(*args, **kwargs):
pattern = args
limit_len = kwargs.get("limit", NUMBERS)
if limit_len is None or limit_len == 0:
limit_len = 10
if(list_like(args)):
pattern = random.choice(args)
_x = xeger.Xeger(limit=limit_len)
return _x.xeger(pattern)
8 changes: 7 additions & 1 deletion cyaron/tests/str_test.py
@@ -1,5 +1,6 @@
import unittest
from cyaron import String
import re


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

def test_random_paragraph(self):
# Only test for Errors
String.random_paragraph(10)
String.random_paragraph(10)

def test_random_regular(self):
pattern = r'[0-9]+\w_.{0,9}'
sentence = String.random_regular(pattern, limit=5)
self.assertTrue(re.match(pattern, sentence).group() == sentence)
1 change: 1 addition & 0 deletions requirements.txt
@@ -1 +1,2 @@
pygraphviz
xeger

0 comments on commit 77a2f34

Please sign in to comment.