4
4
# utils.py
5
5
"""
6
6
General utility functions
7
+
8
+ .. versionchanged:: 0.8.0
9
+
10
+ ``tuple2str`` and ``list2string`` are deprecated and will be removed in 1.0.0.
11
+ Use :func:`domdf_python_tools.utils.list2str` instead.
7
12
"""
8
13
#
9
14
# Copyright © 2018-2020 Dominic Davis-Foster <[email protected] >
48
53
import itertools
49
54
import sys
50
55
from typing import Any , Callable , Dict , Generator , Iterable , List , Optional , Sequence , Tuple , Union
56
+ from domdf_python_tools import __version__
57
+ import deprecation # type: ignore
58
+ import domdf_python_tools .words
59
+
51
60
52
61
__all__ = [
53
62
"pyversion" ,
54
63
"SPACE_PLACEHOLDER" ,
55
- "as_text" ,
56
64
"check_dependencies" ,
57
65
"chunks" ,
58
66
"cmp" ,
59
67
"list2str" ,
60
- "tuple2str" ,
61
68
"permutations" ,
62
69
"printr" ,
63
70
"printt" ,
71
78
"Len" ,
72
79
"double_chain" ,
73
80
"posargs2kwargs" ,
74
- "word_join" ,
75
81
"convert_indents" ,
76
82
]
77
83
82
88
SPACE_PLACEHOLDER = '␣'
83
89
84
90
85
- def as_text (value : Any ) -> str :
86
- """
87
- Convert the given value to a string. ``None`` is converted to ``''``.
88
-
89
- :param value: Value to convert to a string
90
- """
91
-
92
- if value is None :
93
- return ''
94
-
95
- return str (value )
96
-
97
-
98
91
def check_dependencies (dependencies : Iterable [str ], prt : bool = True ) -> List [str ]:
99
92
"""
100
93
Check whether one or more dependencies are available to be imported.
101
94
102
95
:param dependencies: The list of dependencies to check the availability of.
103
96
:param prt: Whether the status should be printed to the terminal. Default :py:obj:`True`.
104
- :type prt: bool, optional
105
97
106
98
:return: A list of any missing modules
107
99
"""
@@ -134,7 +126,6 @@ def chunks(l: Sequence[Any], n: int) -> Generator[Any, None, None]:
134
126
135
127
:param l: The objects to yield chunks from
136
128
:param n: The size of the chunks
137
- :type n: int
138
129
"""
139
130
140
131
for i in range (0 , len (l ), n ):
@@ -166,6 +157,7 @@ def list2str(the_list: Iterable[Any], sep: str = ',') -> str:
166
157
return sep .join ([str (x ) for x in the_list ])
167
158
168
159
160
+ # Will be removed in 1.0.0
169
161
tuple2str = list2string = list2str
170
162
171
163
@@ -225,9 +217,7 @@ def split_len(string: str, n: int) -> List[str]:
225
217
Split a string every ``n`` characters.
226
218
227
219
:param string: The string to split
228
- :type string: str
229
220
:param n: The number of characters to split after
230
- :type n: int
231
221
232
222
:return: The split string
233
223
"""
@@ -249,9 +239,7 @@ def str2tuple(input_string: str, sep: str = ',') -> Tuple[int, ...]:
249
239
.. TODO:: Allow custom types, not just ``int`` (making ``int`` the default)
250
240
251
241
:param input_string: The string to be converted into a tuple
252
- :type input_string: str
253
242
:param sep: The separator in the string. Default `,`
254
- :type sep: str
255
243
"""
256
244
257
245
return tuple (int (x ) for x in input_string .split (sep ))
@@ -284,7 +272,7 @@ def strtobool(val: Union[str, bool]) -> bool:
284
272
285
273
def enquote_value (value : Any ) -> Union [str , bool , float ]:
286
274
"""
287
- Adds quotes (``'``) to the given value, suitable for use in a templating system such as Jinja2.
275
+ Adds single quotes (``'``) to the given value, suitable for use in a templating system such as Jinja2.
288
276
289
277
:class:`Floats <float>`, :class:`integers <int>`, :class:`booleans <bool>`, :py:obj:`None`,
290
278
and the strings ``'True'``, ``'False'`` and ``'None'`` are returned as-is.
@@ -342,7 +330,7 @@ def double_chain(iterable: Iterable[Iterable]):
342
330
[1, 2, 3, 4, 5, 6, 7, 8]
343
331
344
332
345
- :param iterable: The iterable to
333
+ :param iterable: The iterable to chain.
346
334
:return:
347
335
348
336
.. versionadded:: 0.4.7
@@ -381,41 +369,13 @@ def posargs2kwargs(
381
369
return kwargs
382
370
383
371
384
- def word_join (iterable : Iterable [str ], use_repr : bool = False , oxford : bool = False ) -> str :
385
- """
386
- Join the given list of strings in a natural manner, with 'and' to join the last two elements.
387
-
388
- :param iterable:
389
- :param use_repr: Whether to join the ``repr`` of each object.
390
- :param oxford: Whether to use an oxford comma when joining the last two elements.
391
- Always :py:obj:`False` if there are less than three elements.
392
- """
393
-
394
- if use_repr :
395
- words = [repr (w ) for w in iterable ]
396
- else :
397
- words = list (iterable )
398
-
399
- if len (words ) == 0 :
400
- return ''
401
- elif len (words ) == 1 :
402
- return words [0 ]
403
- elif len (words ) == 2 :
404
- return " and " .join (words )
405
- else :
406
- if oxford :
407
- return ", " .join (words [:- 1 ]) + f", and { words [- 1 ]} "
408
- else :
409
- return ", " .join (words [:- 1 ]) + f" and { words [- 1 ]} "
410
-
411
-
412
372
def convert_indents (text : str , tab_width : int = 4 , from_ : str = "\t " , to : str = " " ) -> str :
413
- """
373
+ r """
414
374
Convert indentation at the start of lines in ``text`` from tabs to spaces.
415
375
416
376
:param text: The text to convert indents in.
417
377
:param tab_width: The number of spaces per tab.
418
- :param from_ : The indent to convert from.
378
+ :param from\_ : The indent to convert from.
419
379
:param to: The indent to convert to.
420
380
"""
421
381
@@ -425,10 +385,24 @@ def convert_indents(text: str, tab_width: int = 4, from_: str = "\t", to: str =
425
385
426
386
for line in text .splitlines ():
427
387
indent_count = 0
388
+
428
389
while line .startswith (from_ ):
429
390
indent_count += 1
430
- print (indent_count )
431
391
line = line [from_size :]
392
+
432
393
output .append (f"{ tab * indent_count } { line } " )
433
394
434
395
return "\n " .join (output )
396
+
397
+
398
+ as_text = deprecation .deprecated (
399
+ deprecated_in = "0.8.0" , removed_in = "1.0.0" ,
400
+ current_version = __version__ ,
401
+ details = "Import from 'domdf_python_tools.words' instead." ,
402
+ )(domdf_python_tools .words .as_text )
403
+
404
+ word_join = deprecation .deprecated (
405
+ deprecated_in = "0.8.0" , removed_in = "1.0.0" ,
406
+ current_version = __version__ ,
407
+ details = "Import from 'domdf_python_tools.words' instead." ,
408
+ )(domdf_python_tools .words .word_join )
0 commit comments