Skip to content

Commit 3891aeb

Browse files
committed
Add "double_repr_string" function.
1 parent 3cf18bb commit 3891aeb

File tree

2 files changed

+84
-40
lines changed

2 files changed

+84
-40
lines changed

domdf_python_tools/utils.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262

6363
# stdlib
6464
import inspect
65+
import json
6566
import sys
6667
from math import log10
6768
from pprint import pformat
@@ -96,6 +97,7 @@
9697
"head",
9798
"magnitude",
9899
"trim_precision",
100+
"double_repr_string",
99101
]
100102

101103
#: The current major python version.
@@ -377,3 +379,17 @@ def trim_precision(value: float, precision: int = 4) -> float:
377379
"""
378380

379381
return float(format(value, f"0.{precision}f"))
382+
383+
384+
def double_repr_string(string: str):
385+
"""
386+
Like :func:`repr(str) <repr>`, but tries to use double quotes instead.
387+
388+
.. versionadded:: 2.5.0
389+
"""
390+
391+
# figure out which quote to use; double is preferred
392+
if '"' in string and "'" not in string:
393+
return repr(string)
394+
else:
395+
return json.dumps(string, ensure_ascii=False)

tests/test_utils.py

Lines changed: 68 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,28 @@
1818
import pytest
1919

2020
# this package
21-
from domdf_python_tools import utils
2221
from domdf_python_tools.testing import testing_boolean_values
2322
from domdf_python_tools.typing import HasHead
24-
from domdf_python_tools.utils import head, trim_precision
23+
from domdf_python_tools.utils import (
24+
cmp,
25+
convert_indents,
26+
double_repr_string,
27+
enquote_value,
28+
head,
29+
list2str,
30+
posargs2kwargs,
31+
printr,
32+
printt,
33+
pyversion,
34+
stderr_writer,
35+
str2tuple,
36+
strtobool,
37+
trim_precision
38+
)
2539

2640

2741
def test_pyversion():
28-
assert isinstance(utils.pyversion, int)
42+
assert isinstance(pyversion, int)
2943

3044

3145
class TestList2Str:
@@ -40,7 +54,7 @@ class TestList2Str:
4054
],
4155
)
4256
def test_list2str(self, value, expects):
43-
str_representation = utils.list2str(value)
57+
str_representation = list2str(value)
4458
assert isinstance(str_representation, str)
4559
assert str_representation == expects
4660

@@ -54,7 +68,7 @@ def test_list2str(self, value, expects):
5468
],
5569
)
5670
def test_list2str_semicolon(self, value, expects):
57-
str_representation = utils.list2str(value, sep=';')
71+
str_representation = list2str(value, sep=';')
5872
assert isinstance(str_representation, str)
5973
assert str_representation == expects
6074

@@ -96,7 +110,7 @@ def get_mem_addr(obj):
96110
],
97111
)
98112
def test_printr(obj, expects, capsys):
99-
utils.printr(obj)
113+
printr(obj)
100114

101115
captured = capsys.readouterr()
102116
stdout = captured.out.split('\n')
@@ -115,7 +129,7 @@ def test_printr(obj, expects, capsys):
115129
],
116130
)
117131
def test_printt(obj, expects, capsys):
118-
utils.printt(obj)
132+
printt(obj)
119133

120134
captured = capsys.readouterr()
121135
stdout = captured.out.split('\n')
@@ -134,7 +148,7 @@ def test_printt(obj, expects, capsys):
134148
],
135149
)
136150
def test_stderr_writer(obj, expects, capsys):
137-
utils.stderr_writer(obj)
151+
stderr_writer(obj)
138152

139153
captured = capsys.readouterr()
140154
stderr = captured.err.split('\n')
@@ -151,8 +165,8 @@ class TestStr2Tuple:
151165
],
152166
)
153167
def test_str2tuple(self, value, expects):
154-
assert isinstance(utils.str2tuple(value), tuple)
155-
assert utils.str2tuple(value) == expects
168+
assert isinstance(str2tuple(value), tuple)
169+
assert str2tuple(value) == expects
156170

157171
@pytest.mark.parametrize(
158172
"value, expects",
@@ -162,15 +176,15 @@ def test_str2tuple(self, value, expects):
162176
],
163177
)
164178
def test_str2tuple_semicolon(self, value, expects):
165-
assert isinstance(utils.str2tuple(value, sep=';'), tuple)
166-
assert utils.str2tuple(value, sep=';') == expects
179+
assert isinstance(str2tuple(value, sep=';'), tuple)
180+
assert str2tuple(value, sep=';') == expects
167181

168182

169183
class TestStrToBool:
170184

171185
@testing_boolean_values(extra_truthy=[50, -1])
172186
def test_strtobool(self, boolean_string, expected_boolean):
173-
assert utils.strtobool(boolean_string) == expected_boolean
187+
assert strtobool(boolean_string) == expected_boolean
174188

175189
@pytest.mark.parametrize(
176190
"obj, expects",
@@ -185,7 +199,7 @@ def test_strtobool(self, boolean_string, expected_boolean):
185199
)
186200
def test_strtobool_errors(self, obj, expects):
187201
with pytest.raises(expects):
188-
utils.strtobool(obj)
202+
strtobool(obj)
189203

190204

191205
@pytest.mark.parametrize(
@@ -210,7 +224,7 @@ def test_strtobool_errors(self, obj, expects):
210224
],
211225
)
212226
def test_enquote_value(obj, expects):
213-
assert utils.enquote_value(obj) == expects
227+
assert enquote_value(obj) == expects
214228

215229

216230
#
@@ -225,20 +239,20 @@ def test_enquote_value(obj, expects):
225239
# ])
226240
# def test_enquote_value_errors(obj, expects):
227241
# with pytest.raises(expects):
228-
# utils.enquote_value(obj)
242+
# enquote_value(obj)
229243

230244

231245
def test_cmp():
232-
assert isinstance(utils.cmp(5, 20), int)
233-
assert utils.cmp(5, 20) < 0
234-
assert utils.cmp(5, 20) == -1
246+
assert isinstance(cmp(5, 20), int)
247+
assert cmp(5, 20) < 0
248+
assert cmp(5, 20) == -1
235249

236-
assert isinstance(utils.cmp(20, 5), int)
237-
assert utils.cmp(20, 5) > 0
238-
assert utils.cmp(20, 5) == 1
250+
assert isinstance(cmp(20, 5), int)
251+
assert cmp(20, 5) > 0
252+
assert cmp(20, 5) == 1
239253

240-
assert isinstance(utils.cmp(20, 20), int)
241-
assert utils.cmp(20, 20) == 0
254+
assert isinstance(cmp(20, 20), int)
255+
assert cmp(20, 20) == 0
242256

243257

244258
def demo_function(arg1, arg2, arg3):
@@ -259,31 +273,31 @@ def demo_function(arg1, arg2, arg3):
259273
]
260274
)
261275
def test_posargs2kwargs(args, posarg_names, kwargs, expects):
262-
assert utils.posargs2kwargs(args, posarg_names, kwargs) == expects
276+
assert posargs2kwargs(args, posarg_names, kwargs) == expects
263277

264278

265279
def test_convert_indents():
266280

267281
# TODO: test 'to'
268282

269-
assert utils.convert_indents("hello world") == "hello world"
270-
assert utils.convert_indents("\thello world") == " hello world"
271-
assert utils.convert_indents("\t\thello world") == " hello world"
272-
assert utils.convert_indents("\t hello world") == " hello world"
283+
assert convert_indents("hello world") == "hello world"
284+
assert convert_indents("\thello world") == " hello world"
285+
assert convert_indents("\t\thello world") == " hello world"
286+
assert convert_indents("\t hello world") == " hello world"
273287

274-
assert utils.convert_indents("hello world", tab_width=2) == "hello world"
275-
assert utils.convert_indents("\thello world", tab_width=2) == " hello world"
276-
assert utils.convert_indents("\t\thello world", tab_width=2) == " hello world"
277-
assert utils.convert_indents("\t hello world", tab_width=2) == " hello world"
288+
assert convert_indents("hello world", tab_width=2) == "hello world"
289+
assert convert_indents("\thello world", tab_width=2) == " hello world"
290+
assert convert_indents("\t\thello world", tab_width=2) == " hello world"
291+
assert convert_indents("\t hello world", tab_width=2) == " hello world"
278292

279-
assert utils.convert_indents("hello world", from_=" ") == "hello world"
280-
assert utils.convert_indents(" hello world", from_=" ") == " hello world"
281-
assert utils.convert_indents(" hello world", from_=" ") == " hello world"
282-
assert utils.convert_indents(" hello world", from_=" ") == " hello world"
293+
assert convert_indents("hello world", from_=" ") == "hello world"
294+
assert convert_indents(" hello world", from_=" ") == " hello world"
295+
assert convert_indents(" hello world", from_=" ") == " hello world"
296+
assert convert_indents(" hello world", from_=" ") == " hello world"
283297

284-
assert utils.convert_indents("hello world", tab_width=2, from_=" ") == "hello world"
285-
assert utils.convert_indents(" hello world", tab_width=2, from_=" ") == " hello world"
286-
assert utils.convert_indents(" hello world", tab_width=2, from_=" ") == " hello world"
298+
assert convert_indents("hello world", tab_width=2, from_=" ") == "hello world"
299+
assert convert_indents(" hello world", tab_width=2, from_=" ") == " hello world"
300+
assert convert_indents(" hello world", tab_width=2, from_=" ") == " hello world"
287301

288302

289303
class TestHead:
@@ -396,3 +410,17 @@ def test_trim_precision():
396410
assert trim_precision(170.15800000000002, 4) == 170.158
397411
assert trim_precision(170.15800000000002, 5) == 170.158
398412
assert trim_precision(170.15800000000002) == 170.158
413+
414+
415+
@pytest.mark.parametrize(
416+
"value, expects",
417+
[
418+
("foo", '"foo"'),
419+
("'foo'", "\"'foo'\""),
420+
("don't", "\"don't\""),
421+
("Here's a single quote \"", "\"Here's a single quote \\\"\""),
422+
(enquote_value('☃'), "\"'☃'\""),
423+
]
424+
)
425+
def test_double_repr_string(value: str, expects: str):
426+
assert double_repr_string(value) == expects

0 commit comments

Comments
 (0)