Skip to content

Commit e588141

Browse files
added use of ABC to mark TextGenerator as abstract
1 parent aae8bde commit e588141

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

dbldatagen/text_generators.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import random
1111

1212
import logging
13+
from abc import ABC, abstractmethod
1314
import numpy as np
1415
import pandas as pd
1516

@@ -61,7 +62,7 @@
6162
'LABORUM']
6263

6364

64-
class TextGenerator(object):
65+
class TextGenerator(ABC):
6566
""" Base class for text generation classes
6667
6768
"""
@@ -161,6 +162,10 @@ def getAsTupleOrElse(v, defaultValue, valueName):
161162

162163
return defaultValue
163164

165+
@abstractmethod
166+
def pandasGenerateText(self, v):
167+
raise NotImplementedError("Subclasses should implement unique versions of `pandasGenerateText`")
168+
164169

165170
class TemplateGenerator(TextGenerator): # lgtm [py/missing-equals]
166171
"""This class handles the generation of text from templates

tests/test_text_generation.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import re
2-
import pytest
3-
import pandas as pd
4-
import numpy as np
52

3+
import numpy as np
4+
import pandas as pd
65
import pyspark.sql.functions as F
6+
import pytest
77
from pyspark.sql.types import BooleanType, DateType
88
from pyspark.sql.types import StructType, StructField, IntegerType, StringType, TimestampType
99

@@ -44,9 +44,13 @@ class TestTextGeneration:
4444
row_count = 100000
4545
partitions_requested = 4
4646

47+
class TestTextGenerator(TextGenerator):
48+
def pandasGenerateText(self, v): # pylint: disable=useless-parent-delegation
49+
return super().pandasGenerateText(v)
50+
4751
def test_text_generator_basics(self):
4852
# test the random humber generator
49-
tg1 = TextGenerator()
53+
tg1 = self.TestTextGenerator()
5054

5155
# test the repr
5256
desc = repr(tg1)

tests/test_text_generator_basic.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import re
2-
import pytest
2+
33
import numpy as np
44
import pandas as pd
5+
import pytest
56

67
from dbldatagen import TextGenerator, TemplateGenerator
78

@@ -12,10 +13,14 @@ class TestTextGeneratorBasic:
1213
row_count = 100000
1314
partitions_requested = 4
1415

16+
class TestTextGenerator(TextGenerator):
17+
def pandasGenerateText(self, v): # pylint: disable=useless-parent-delegation
18+
return super().pandasGenerateText(v)
19+
1520
@pytest.mark.parametrize("randomSeed", [None, 0, -1, 2112, 42])
1621
def test_text_generator_basic(self, randomSeed):
17-
text_gen1 = TextGenerator()
18-
text_gen2 = TextGenerator()
22+
text_gen1 = self.TestTextGenerator()
23+
text_gen2 = self.TestTextGenerator()
1924

2025
if randomSeed is not None:
2126
text_gen1 = text_gen1.withRandomSeed(randomSeed)
@@ -29,14 +34,19 @@ def test_text_generator_basic(self, randomSeed):
2934

3035
assert text_gen1 == text_gen2
3136

37+
def test_base_textgenerator_raises_error(self):
38+
with pytest.raises(NotImplementedError):
39+
text_gen1 = self.TestTextGenerator()
40+
text_gen1.pandasGenerateText(None)
41+
3242
@pytest.mark.parametrize("randomSeed, forceNewInstance", [(None, True), (None, False),
3343
(0, True), (0, False),
3444
(-1, True), (-1, False),
3545
(2112, True), (2112, False),
3646
(42, True), (42, False)])
3747
def test_text_generator_rng(self, randomSeed, forceNewInstance):
38-
text_gen1 = TextGenerator()
39-
text_gen2 = TextGenerator()
48+
text_gen1 = self.TestTextGenerator()
49+
text_gen2 = self.TestTextGenerator()
4050

4151
if randomSeed is not None:
4252
text_gen1 = text_gen1.withRandomSeed(randomSeed)
@@ -71,7 +81,7 @@ def test_text_generator_rng(self, randomSeed, forceNewInstance):
7181
(np.array([1, 40000.4, 3]), np.uint16)
7282
])
7383
def test_text_generator_compact_types(self, values, expectedType):
74-
text_gen1 = TextGenerator()
84+
text_gen1 = self.TestTextGenerator()
7585

7686
np_type = text_gen1.compactNumpyTypeForValues(values)
7787
assert np_type == expectedType

0 commit comments

Comments
 (0)