Coverage for src/lsqfitgp/_utils.py: 100%
23 statements
« prev ^ index » next coverage.py v7.6.3, created at 2024-10-15 19:54 +0000
« prev ^ index » next coverage.py v7.6.3, created at 2024-10-15 19:54 +0000
1# lsqfitgp/_utils.py
2#
3# Copyright (c) 2023, 2024, Giacomo Petrillo
4#
5# This file is part of lsqfitgp.
6#
7# lsqfitgp is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# lsqfitgp is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with lsqfitgp. If not, see <http://www.gnu.org/licenses/>.
20import textwrap 1fabcde
22def append_to_docstring(docs, doctail, front=False): 1fabcde
23 doctail = textwrap.dedent(doctail) 1fabcde
24 dedocs = textwrap.dedent(docs) 1fabcde
25 lineend = docs.find('\n') 1fabcde
26 indented_lineend = dedocs.find('\n') 1fabcde
27 indent = docs[:indented_lineend - lineend] 1fabcde
28 if front: 1fabcde
29 newdocs = doctail + dedocs 1bcde
30 else:
31 newdocs = dedocs + doctail 1fabcde
32 return textwrap.indent(newdocs, indent) 1fabcde
34def top_bottom_rule(title, body): 1fabcde
35 body = textwrap.dedent(body) 1a
36 body_lines = list(map(str.rstrip, body.split('\n'))) 1a
37 body = '\n'.join(body_lines) 1a
38 body_width = max(map(len, body_lines)) 1a
39 title_width = 2 + len(title) 1a
40 width = max(body_width, title_width + 4) 1a
41 pre_length = (width - title_width) // 2 1a
42 post_length = width - title_width - pre_length 1a
43 toprule = '=' * pre_length + ' ' + title + ' ' + '=' * post_length 1a
44 bottomrule = '=' * width 1a
45 return '\n'.join([toprule, body, bottomrule]) 1a