Refactoring: Move
Move text generation funtions into their own module
This commit is contained in:
parent
1de938f2fa
commit
a54ccb2fdf
26
synthesis/text/text.py
Normal file
26
synthesis/text/text.py
Normal 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],
|
||||||
|
),
|
||||||
|
)
|
||||||
|
)
|
||||||
16
test/fixtures/page_generation/page.py
vendored
16
test/fixtures/page_generation/page.py
vendored
@ -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.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.identity import IdentityLineFormatter
|
||||||
from synthesis.text.line_formatter.paragraph import ParagraphLineFormatter
|
from synthesis.text.line_formatter.paragraph import ParagraphLineFormatter
|
||||||
|
from synthesis.text.text import generate_random_words, generate_random_number
|
||||||
|
|
||||||
logger.remove()
|
logger.remove()
|
||||||
logger.add(sys.stderr, level="INFO")
|
logger.add(sys.stderr, level="INFO")
|
||||||
@ -434,8 +435,8 @@ class RecursiveRandomTable(RandomContentRectangle):
|
|||||||
|
|
||||||
def build_small_cell(self, cell):
|
def build_small_cell(self, cell):
|
||||||
|
|
||||||
content = (possibly() and " ".join(generate_random_words(1, 3))) or (
|
content = (possibly() and generate_random_words(1, 3)) or (
|
||||||
str(round(generate_random_number(), random.choice([0, 1, 2, 3])))
|
generate_random_number()
|
||||||
+ ((possibly() and " " + rnd.choice(["$", "£", "%", "EUR", "USD", "CAD", "ADA"])) or "")
|
+ ((possibly() and " " + rnd.choice(["$", "£", "%", "EUR", "USD", "CAD", "ADA"])) or "")
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -619,15 +620,6 @@ class Cell(ContentRectangle):
|
|||||||
return self
|
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:
|
def shrink_rectangle(rectangle: Rectangle, factor: float) -> Rectangle:
|
||||||
x1, y1, x2, y2 = compute_scaled_coordinates(rectangle, (1 - factor))
|
x1, y1, x2, y2 = compute_scaled_coordinates(rectangle, (1 - factor))
|
||||||
|
|
||||||
@ -812,7 +804,7 @@ class RandomPlot(RandomContentRectangle):
|
|||||||
plot_fn(x, y, **plot_kwargs)
|
plot_fn(x, y, **plot_kwargs)
|
||||||
ax.set_facecolor("none")
|
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
|
# disable axes at random
|
||||||
maybe() and ax.set_xticks([])
|
maybe() and ax.set_xticks([])
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user