11# -*- coding: utf-8 -*-
2- # Copyright (C) 2008-2010, 2013-2015, 2020-2021 Rocky Bernstein <rocky@gnu.org>
2+ # Copyright (C) 2008-2010, 2013-2015, 2020-2021, 2024
3+ # Rocky Bernstein <rocky@gnu.org>
34#
45# This program is free software: you can redistribute it and/or modify
56# it under the terms of the GNU General Public License as published by
1415# You should have received a copy of the GNU General Public License
1516# along with this program. If not, see <http://www.gnu.org/licenses/>.
1617
18+ import os
19+ from glob import glob
20+ from typing import Any
21+
1722
18- def option_set (options , value , default_options ):
23+ def option_set (options : dict , value , default_options ) -> Any :
24+ """
25+ If ``value`` is found in ``options``, return that, otherwise return the value
26+ from ``defaulit_options``.
27+ """
28+ # Yes, there is probably some fancy dictionary merge operation, that will do this,
29+ # as a one-shot but this code is simple and clear.
1930 if not options or value not in options :
2031 return default_options .get (value )
2132 else :
2233 return options .get (value )
2334 return None # Not reached
2435
2536
26- def bool2YN (b ) -> bool :
37+ def bool2YN (b : bool ) -> str :
38+ """
39+ Turn a bool into the string "Y" or "N".
40+ """
2741 return "Y" if b else "N"
2842
2943
30- def wrapped_lines (msg_part1 , msg_part2 , width ):
44+ def wrapped_lines (msg_part1 : str , msg_part2 : str , width : int ) -> str :
45+ """
46+ if ``msg_part1`` concatenated with ``msg_part2`` is larger than
47+ ``width`` then concatenate the strings with a new line and tab inserted
48+ between the strings. Otherwise, just concatenate with a space between the
49+ two strings.
50+ """
3151 if len (msg_part1 ) + len (msg_part2 ) + 1 > width :
3252 return msg_part1 + "\n \t " + msg_part2
3353 else :
3454 return msg_part1 + " " + msg_part2
35- return # Not reached
3655
3756
3857def pretty_modfunc_name (s ) -> str :
@@ -44,10 +63,6 @@ def pretty_modfunc_name(s) -> str:
4463 return str (s ) + "()"
4564
4665
47- import os
48- from glob import glob
49-
50-
5166def pyfiles (callername , level = 2 ):
5267 "All python files caller's dir without the path and trailing .py"
5368 d = os .path .dirname (callername )
@@ -61,7 +76,10 @@ def pyfiles(callername, level=2):
6176# Demo it
6277if __name__ == "__main__" :
6378 TEST_OPTS = {"a" : True , "b" : 5 , "c" : None }
64- get_option = lambda key : option_set (opts , key , TEST_OPTS )
79+
80+ def get_option (key ):
81+ return option_set (opts , key , TEST_OPTS )
82+
6583 opts = {"d" : 6 , "a" : False }
6684 for opt in ["a" , "b" , "c" , "d" ]:
6785 print (opt , get_option (opt ))
0 commit comments