Skip to content

Commit

Permalink
reformat all code with black
Browse files Browse the repository at this point in the history
  • Loading branch information
Gsllchb committed Oct 10, 2018
1 parent 6226dcd commit 8f0d628
Show file tree
Hide file tree
Showing 13 changed files with 533 additions and 282 deletions.
101 changes: 60 additions & 41 deletions pylf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@
_DEFAULT_PERTURB_THETA_SIGMA = 0.07


def handwrite(text: str,
template: Mapping[str, Any],
*,
worker: Optional[int] = None,
seed: Optional[Hashable] = None) -> List[PIL.Image.Image]:
def handwrite(
text: str,
template: Mapping[str, Any],
*,
worker: Optional[int] = None,
seed: Optional[Hashable] = None
) -> List[PIL.Image.Image]:
"""Handwrite the text with the parameters in the template.
Args:
Expand Down Expand Up @@ -134,11 +136,13 @@ def handwrite(text: str,
return handwrite2(text, template2, worker=worker, seed=seed)


def handwrite2(text: str,
template2: Mapping[str, Any],
*,
worker: Optional[int] = None,
seed: Optional[Hashable] = None) -> List[PIL.Image.Image]:
def handwrite2(
text: str,
template2: Mapping[str, Any],
*,
worker: Optional[int] = None,
seed: Optional[Hashable] = None
) -> List[PIL.Image.Image]:
"""The 'periodic' version of handwrite. See also handwrite().
The parameters of handwrite2() and handwrite() are similar. The difference is that some of the parameters in the
template of handwrite() are replaced with their plural form in template2. These 'plural' parameters become a
Expand Down Expand Up @@ -183,45 +187,60 @@ def handwrite2(text: str,

font_sizes = template2["font_sizes"]

word_spacings = template2.get("word_spacings", tuple(_DEFAULT_WORD_SPACING for _ in font_sizes))
word_spacings = template2.get(
"word_spacings", tuple(_DEFAULT_WORD_SPACING for _ in font_sizes)
)

line_spacing_sigmas = template2.get("line_spacing_sigmas", tuple(i / 32 for i in font_sizes))
font_size_sigmas = template2.get("font_size_sigmas", tuple(i / 64 for i in font_sizes))
word_spacing_sigmas = template2.get("word_spacing_sigmas", tuple(i / 32 for i in font_sizes))
line_spacing_sigmas = template2.get(
"line_spacing_sigmas", tuple(i / 32 for i in font_sizes)
)
font_size_sigmas = template2.get(
"font_size_sigmas", tuple(i / 64 for i in font_sizes)
)
word_spacing_sigmas = template2.get(
"word_spacing_sigmas", tuple(i / 32 for i in font_sizes)
)

color = template2.get("color", _DEFAULT_COLOR)

is_half_char_fn = template2.get("is_half_char_fn", _DEFAULT_IS_HALF_CHAR_FN)
is_end_char_fn = template2.get("is_end_char_fn", _DEFAULT_IS_END_CHAR_FN)

perturb_x_sigmas = template2.get("perturb_x_sigmas", tuple(i / 32 for i in font_sizes))
perturb_y_sigmas = template2.get("perturb_y_sigmas", tuple(i / 32 for i in font_sizes))
perturb_theta_sigmas = template2.get("perturb_theta_sigmas",
tuple(_DEFAULT_PERTURB_THETA_SIGMA for _ in font_sizes))
perturb_x_sigmas = template2.get(
"perturb_x_sigmas", tuple(i / 32 for i in font_sizes)
)
perturb_y_sigmas = template2.get(
"perturb_y_sigmas", tuple(i / 32 for i in font_sizes)
)
perturb_theta_sigmas = template2.get(
"perturb_theta_sigmas", tuple(_DEFAULT_PERTURB_THETA_SIGMA for _ in font_sizes)
)

if worker is None:
worker = multiprocessing.cpu_count()

return _core.handwrite(text=text,
# If template2["backgrounds"] is already a tuple, CPython will share it instead of creating
# a new copy of it.
backgrounds=tuple(template2["backgrounds"]),
top_margins=tuple(m["top"] for m in template2["margins"]),
bottom_margins=tuple(m["bottom"] for m in template2["margins"]),
left_margins=tuple(m["left"] for m in template2["margins"]),
right_margins=tuple(m["right"] for m in template2["margins"]),
line_spacings=tuple(template2["line_spacings"]),
font_sizes=tuple(font_sizes),
word_spacings=tuple(word_spacings),
line_spacing_sigmas=tuple(line_spacing_sigmas),
font_size_sigmas=tuple(font_size_sigmas),
word_spacing_sigmas=tuple(word_spacing_sigmas),
font=template2["font"],
color=color,
is_half_char_fn=is_half_char_fn,
is_end_char_fn=is_end_char_fn,
perturb_x_sigmas=tuple(perturb_x_sigmas),
perturb_y_sigmas=tuple(perturb_y_sigmas),
perturb_theta_sigmas=tuple(perturb_theta_sigmas),
worker=worker,
seed=seed)
return _core.handwrite(
text=text,
# If template2["backgrounds"] is already a tuple, CPython will share it instead of creating
# a new copy of it.
backgrounds=tuple(template2["backgrounds"]),
top_margins=tuple(m["top"] for m in template2["margins"]),
bottom_margins=tuple(m["bottom"] for m in template2["margins"]),
left_margins=tuple(m["left"] for m in template2["margins"]),
right_margins=tuple(m["right"] for m in template2["margins"]),
line_spacings=tuple(template2["line_spacings"]),
font_sizes=tuple(font_sizes),
word_spacings=tuple(word_spacings),
line_spacing_sigmas=tuple(line_spacing_sigmas),
font_size_sigmas=tuple(font_size_sigmas),
word_spacing_sigmas=tuple(word_spacing_sigmas),
font=template2["font"],
color=color,
is_half_char_fn=is_half_char_fn,
is_end_char_fn=is_end_char_fn,
perturb_x_sigmas=tuple(perturb_x_sigmas),
perturb_y_sigmas=tuple(perturb_y_sigmas),
perturb_theta_sigmas=tuple(perturb_theta_sigmas),
worker=worker,
seed=seed,
)
78 changes: 59 additions & 19 deletions pylf/_check_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,27 @@ def _check_template2(template2) -> None:
if length <= 0:
raise ValueError("The length of 'backgrounds' must be at least 1")

if not (length == len(template2["margins"]) == len(template2["line_spacings"]) == len(template2["font_sizes"])):
raise ValueError("'backgrounds', 'margins', 'line_spacings' and 'font_sizes' must have the same length")
if not (
length
== len(template2["margins"])
== len(template2["line_spacings"])
== len(template2["font_sizes"])
):
raise ValueError(
"'backgrounds', 'margins', 'line_spacings' and 'font_sizes' must have the same length"
)

# check backgrounds
if not all(isinstance(b, PIL.Image.Image) for b in template2["backgrounds"]):
raise TypeError("'background' must be Pillow's Image")
if not all(b.width <= _MAX_IMAGE_SIDE_LENGTH for b in template2["backgrounds"]):
raise ValueError("The width of background cannot exceed {}".format(_MAX_IMAGE_SIDE_LENGTH))
raise ValueError(
"The width of background cannot exceed {}".format(_MAX_IMAGE_SIDE_LENGTH)
)
if not all(b.height <= _MAX_IMAGE_SIDE_LENGTH for b in template2["backgrounds"]):
raise ValueError("The height of background cannot exceed {}".format(_MAX_IMAGE_SIDE_LENGTH))
raise ValueError(
"The height of background cannot exceed {}".format(_MAX_IMAGE_SIDE_LENGTH)
)

# check margins
for m in template2["margins"]:
Expand All @@ -52,31 +63,52 @@ def _check_template2(template2) -> None:
raise TypeError("'line_spacing' must be Integral")
if not all(ls >= 1 for ls in template2["line_spacings"]):
raise ValueError("'line_spacing' must be at least 1")
if not all(b.height >= m["top"] + ls + m["bottom"] for b, m, ls
in zip(template2["backgrounds"], template2["margins"], template2["line_spacings"])):
raise ValueError("'margin[\"top\"] + line_spacing + margin[\"bottom\"]'"
" can not be greater than background's height")
if not all(
b.height >= m["top"] + ls + m["bottom"]
for b, m, ls in zip(
template2["backgrounds"], template2["margins"], template2["line_spacings"]
)
):
raise ValueError(
'\'margin["top"] + line_spacing + margin["bottom"]\''
" can not be greater than background's height"
)

# check font_sizes
if not all(isinstance(fs, numbers.Integral) for fs in template2["font_sizes"]):
raise TypeError("'line_spacing' must be Integral")
if not all(fs >= 1 for fs in template2["font_sizes"]):
raise ValueError("'font_size' must be at least 1")
if not all(fs <= ls for fs, ls in zip(template2["font_sizes"], template2["line_spacings"])):
if not all(
fs <= ls for fs, ls in zip(template2["font_sizes"], template2["line_spacings"])
):
raise ValueError("'font_size' can not be greater than 'line_spacing'")

if not all(b.width >= m["left"] + fs + m["right"] for b, m, fs
in zip(template2["backgrounds"], template2["margins"], template2["font_sizes"])):
raise ValueError("'margin[\"left\"] + font_size + margin[\"right\"]'"
" can not be greater than background's width")
if not all(
b.width >= m["left"] + fs + m["right"]
for b, m, fs in zip(
template2["backgrounds"], template2["margins"], template2["font_sizes"]
)
):
raise ValueError(
'\'margin["left"] + font_size + margin["right"]\''
" can not be greater than background's width"
)

# check word_spacings
if "word_spacings" in template2:
if len(template2["word_spacings"]) != length:
raise ValueError("'word_spacings' and 'backgrounds' must have the same length")
if not all(isinstance(ws, numbers.Integral) for ws in template2["word_spacings"]):
raise ValueError(
"'word_spacings' and 'backgrounds' must have the same length"
)
if not all(
isinstance(ws, numbers.Integral) for ws in template2["word_spacings"]
):
raise TypeError("'word_spacing' must be Integral")
if not all(ws > -fs // 2 for ws, fs in zip(template2["word_spacings"], template2["font_sizes"])):
if not all(
ws > -fs // 2
for ws, fs in zip(template2["word_spacings"], template2["font_sizes"])
):
raise ValueError("'word_spacing' must be greater than (-font_size // 2)")

# TODO: check font
Expand All @@ -86,11 +118,19 @@ def _check_template2(template2) -> None:
raise TypeError("'color' must be str")

# check *_sigmas
for sigmas in ("line_spacing_sigmas", "font_size_sigmas", "word_spacing_sigmas", "perturb_x_sigmas",
"perturb_y_sigmas", "perturb_theta_sigmas"):
for sigmas in (
"line_spacing_sigmas",
"font_size_sigmas",
"word_spacing_sigmas",
"perturb_x_sigmas",
"perturb_y_sigmas",
"perturb_theta_sigmas",
):
if sigmas in template2:
if len(template2[sigmas]) != length:
raise ValueError("'{}' and 'backgrounds' must have the same length".format(sigmas))
raise ValueError(
"'{}' and 'backgrounds' must have the same length".format(sigmas)
)
if not all(isinstance(s, numbers.Real) for s in template2[sigmas]):
raise TypeError("'{}' must be Real".format(sigmas[:-1]))
if not all(s >= 0.0 for s in template2[sigmas]):
Expand Down
Loading

0 comments on commit 8f0d628

Please sign in to comment.