Split safeprint into separate functions to remove deep indents.
This commit is contained in:
parent
025c8d51a3
commit
6b546b39df
1 changed files with 16 additions and 11 deletions
|
@ -2,17 +2,22 @@
|
||||||
This function is slow and ugly, but I need a way to safely print unicode strings
|
This function is slow and ugly, but I need a way to safely print unicode strings
|
||||||
on systems that don't support it without crippling those who do.
|
on systems that don't support it without crippling those who do.
|
||||||
'''
|
'''
|
||||||
def safeprint(text, file_handle=None, end='\n'):
|
import sys
|
||||||
|
|
||||||
|
def safeprint_handle(text, *, file_handle, end='\n'):
|
||||||
for character in text:
|
for character in text:
|
||||||
try:
|
try:
|
||||||
if file_handle:
|
|
||||||
file_handle.write(character)
|
file_handle.write(character)
|
||||||
else:
|
|
||||||
print(character, end='', flush=False)
|
|
||||||
except UnicodeError:
|
except UnicodeError:
|
||||||
if file_handle:
|
|
||||||
file_handle.write('?')
|
file_handle.write('?')
|
||||||
|
file_handle.write(end)
|
||||||
|
|
||||||
|
def safeprint_stdout(text, *, end='\n'):
|
||||||
|
safeprint_handle(text=text, file_handle=sys.stdout, end=end)
|
||||||
|
sys.stdout.flush()
|
||||||
|
|
||||||
|
def safeprint(text, *, file_handle=None, end='\n'):
|
||||||
|
if file_handle is not None:
|
||||||
|
return safeprint_handle(text=text, file_handle=file_handle, end=end)
|
||||||
else:
|
else:
|
||||||
print('?', end='', flush=False)
|
return safeprint_stdout(text=text, end=end)
|
||||||
if not file_handle:
|
|
||||||
print(end, end='', flush=True)
|
|
||||||
|
|
Loading…
Reference in a new issue