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.

feetwet
  • 3,248
  • 7
  • 46
  • 84
niths4u
  • 440
  • 3
  • 13
  • 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 – niths4u Nov 09 '17 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. – pstatix Nov 09 '17 at 14:43

1 Answers1

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()
mareoraft
  • 3,474
  • 4
  • 26
  • 62
pstatix
  • 3,611
  • 4
  • 18
  • 40
  • @Carcigenicate I was (am) working on that still. At work, gotta balance. – pstatix Nov 09 '17 at 14:22
  • 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 . – niths4u Nov 09 '17 at 14:23
  • 1
    If you need to use keyword arguments, you can use the `**kwargs` parameter as well. – pstatix Nov 09 '17 at 14:26
  • print("a =", a, sep='0000', end='') and tprint("a =", a, sep='0000', end='') , gives different output – niths4u Nov 09 '17 at 15:00
  • @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='')`? – pstatix Nov 09 '17 at 15:12
  • though i should thank you for the effort , i cannot accept it as the answer. As stated , my problem is not to rewrite all print supported args , but to get a function that takes the args and sends it to print statement as it is and then return back with a date added. for eg tprint fails for tprint('{0:2d} {1:3d} {2:4d}'.format(x, x*x, x*x*x)) , whereas print works fine tprint('{0:2d} {1:3d} {2:4d}'.format(x, x*x, x*x*x)) ( where x=10 as an example) – niths4u Nov 13 '17 at 17:07
  • @niths4u You've still not updated your post with what you would expect to be an output given an input. Your example should be visual so that we can assess it further. – pstatix Nov 13 '17 at 17:49
  • I like this answer even if it not fulfilling the question. But it has a BUG: if not supplying a 'sep' or 'end' arguments to tprint, it will fail for a missing keyword. Need to add: 'if 'sep' in kwargs: else:. same for 'end'. I don't need 'sep' so did it just for 'end' – ishahak Mar 22 '18 at 10:22