I’ve got a custom log function in Python and I’d like to have a Pythonic way to split the input in multiple lines. The input could be a string, a multi-line-string or a list.
This works well for strings and multi-line-strings, however it doesn’t handle lists:
def log( text, indent = 0):
indent_level = 4
current_time = str( datetime.datetime.now().time())[0:-3]
for line in text.splitlines():
log_line = current_time + ' | ' + indent_level * indent * ' ' + str( line)
print( log_line)
What do you suggest to also be able to handle lists without complex
if( type( string) == list):
tests all over the place?
Use duck typing to normalize the input once, at the beginning of the method.