Refactoring: Move

Move text generation funtions into their own module
This commit is contained in:
Matthias Bisping 2023-02-01 17:15:40 +01:00
parent 1de938f2fa
commit a54ccb2fdf
2 changed files with 30 additions and 12 deletions

26
synthesis/text/text.py Normal file
View File

@ -0,0 +1,26 @@
import random
from faker import Faker
from synthesis.random import rnd
def generate_random_words(n_min, n_max):
words = " ".join(Faker().words(rnd.randint(n_min, n_max)))
return words
def generate_random_number():
return str(
round(
random.choice(
[
random.randint(-10000, 10000),
random.uniform(-100, 100),
]
),
random.choice(
[0, 1, 2, 3],
),
)
)

View File

@ -33,6 +33,7 @@ from synthesis.segment.random_content_rectangle import RandomContentRectangle
from synthesis.text.font import pick_random_mono_space_font_available_on_system, pick_random_font_available_on_system
from synthesis.text.line_formatter.identity import IdentityLineFormatter
from synthesis.text.line_formatter.paragraph import ParagraphLineFormatter
from synthesis.text.text import generate_random_words, generate_random_number
logger.remove()
logger.add(sys.stderr, level="INFO")
@ -434,8 +435,8 @@ class RecursiveRandomTable(RandomContentRectangle):
def build_small_cell(self, cell):
content = (possibly() and " ".join(generate_random_words(1, 3))) or (
str(round(generate_random_number(), random.choice([0, 1, 2, 3])))
content = (possibly() and generate_random_words(1, 3)) or (
generate_random_number()
+ ((possibly() and " " + rnd.choice(["$", "£", "%", "EUR", "USD", "CAD", "ADA"])) or "")
)
@ -619,15 +620,6 @@ class Cell(ContentRectangle):
return self
def generate_random_words(n_min, n_max):
column_name = Faker().words(rnd.randint(n_min, n_max))
return column_name
def generate_random_number():
return random.choice([random.randint(-10000, 10000), random.uniform(-100, 100)])
def shrink_rectangle(rectangle: Rectangle, factor: float) -> Rectangle:
x1, y1, x2, y2 = compute_scaled_coordinates(rectangle, (1 - factor))
@ -812,7 +804,7 @@ class RandomPlot(RandomContentRectangle):
plot_fn(x, y, **plot_kwargs)
ax.set_facecolor("none")
probably() and ax.set_title(" ".join(generate_random_words(1, 3)))
probably() and ax.set_title(generate_random_words(1, 3))
# disable axes at random
maybe() and ax.set_xticks([])