0

My requirement is to have a function that works exactly like print but adds a timestamp at top. Currently I use something like:

def tprint(var):
    print(str(datetime.now().strftime('%Y-%m-%d %H:%M:%S'))+" :: "+str(var))

Though it gives me all required output for a specific set – e.g.,

tprint("dummy"+" print")
2017-11-09 19:38:42 :: dummy print

I am not able to completely morph it for print statement. For example, tprint("hi","hello") and tprint("a =", a, sep='0', end='') fail.

My requirement is not to ideally make these two statements work. But to write an alternative function for print that works for all print arguments but gives an additional timestamp along with it. I am sure this may not be a straight forward solution. But do not want to miss if someone has already figured out any similar approach.

2
  • as i mentioned , my idea is to have a complete alternative to print statement. for eg , the above one does not work for tprint("a =", a, sep='0', end='') , but is supported for print("a =", a, sep='0', end='') . The idea is to take argument as it is and then substitute it for original print and then get the result back by adding a timestamp. I hope you got it? Think of simply replacing the whole print statement(with all sorts of arguments) in a huge code with tprint Commented Nov 9, 2017 at 14:25
  • See my updated post. Apologize if there is a syntax error, but my Python is on another system, so no copy paste available. Commented Nov 9, 2017 at 14:43

1 Answer 1

3

Edit:

After reviewing what you wanted, why not just pass your desired values to a function that uses the built-in print()? Like so:

def tprint(*args, **kwargs):
    stamp = str(datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
    print(stamp + ' :: ', *args, sep = kwargs['sep'], end = kwargs['end'])

a = 'Hello World'
tprint("a =", a, sep='0000', end='')

>>> [whatever the timestamp is] :: 0000a =0000Hello World

In order to provide a better response, I would really need to know what your expected output would be given an example. So far you have only said what does or does not work, but not why or how it should look.

Original Response:

Use the *args parameter in your function definition. It lets you supply an optional (unspecified) number of arguments in your function call and collects them into a list.

By definition, keyword arguments must come after all *args parameter. **kwargs packs them into a dictionary to iterate over. More information is available in this and that on SO.

So you can do something like this:

def tprint(*args, **kwargs):
    tempa = ' '.join(str(a) for a in args)
    tempk = ' '.join([str(kwargs[k]) for k in kwargs])
    temp = tempa + ' ' + tempk # puts a space between the two for clean output
    print(str(datetime.now().strftime('%Y-%m-%d %H:%M:%S')) + " :: " + temp)


a = 'Hello World!'

tprint("a =", a, sep='0', end='')

>>> 2017-11-09 09:35:37.148780 :: a = Hello World! 0 # I used datetime.datetime.now()
Sign up to request clarification or add additional context in comments.

8 Comments

@Carcigenicate I was (am) working on that still. At work, gotta balance.
Thanks for the effort. This solves the comma issue. but as i mentioned , my idea is to have a complete alternative to print statement. for eg , the above one does not work for tprint("a =", a, sep='0', end='') , but is supported for print("a =", a, sep='0', end='') . The idea is to take argument as it is and then substitute it for original print and then get the result back by adding a timestamp. I hope you got it? Think of simply replacing the whole print statement(with all sorts of arguments) in a huge code with tprint .
If you need to use keyword arguments, you can use the **kwargs parameter as well.
print("a =", a, sep='0000', end='') and tprint("a =", a, sep='0000', end='') , gives different output
@niths4u Ah, I see you wanted to overload the print statement. I'll have to look at this later. What would your expected output be from tprint("a =", a, sep='0000', end='')?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.