From a54ccb2fdf44595720718fef44d5d3b1b8cbfe0a Mon Sep 17 00:00:00 2001 From: Matthias Bisping Date: Wed, 1 Feb 2023 17:15:40 +0100 Subject: [PATCH] Refactoring: Move Move text generation funtions into their own module --- synthesis/text/text.py | 26 ++++++++++++++++++++++++++ test/fixtures/page_generation/page.py | 16 ++++------------ 2 files changed, 30 insertions(+), 12 deletions(-) create mode 100644 synthesis/text/text.py diff --git a/synthesis/text/text.py b/synthesis/text/text.py new file mode 100644 index 0000000..deaedee --- /dev/null +++ b/synthesis/text/text.py @@ -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], + ), + ) + ) diff --git a/test/fixtures/page_generation/page.py b/test/fixtures/page_generation/page.py index 99f913d..c50baf5 100644 --- a/test/fixtures/page_generation/page.py +++ b/test/fixtures/page_generation/page.py @@ -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([])