Python Forum
How to timestamp a Python program, your thoughts?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to timestamp a Python program, your thoughts?
#1
I want my Python programs to display a timestamp when they run. It doesn't have to be precise, but it should reflect the date (at least) when the program was declared to be ready for use. It must be set automatically, because if it has to be done manually, a user can never be sure whether it's reliable (whether the developer remembered to set it) or not.

This is easy with a compiled language like C, at least in principle: insert the time into the object code when the program is linked. In Python it's more elusive. The time when when the program is compiled to .pyc files is meaningless, because every user who installs the program "compiles" it the first time they run it.

An obvious solution would be to use the .py files' timestamps; the latest one among the program's source files is the program's timestamp. But this won't work because our source files are distributed through a source control system, and when a user fetches a new version of a file, the local copy is timestamped with the fetch time. (The s.c.s. can be configured to preserve the original timestamps, but for most other types of files the fetch time is right, so we're not likely to change it.)

A secondary problem is that a Python "program" really doesn't exist as a distinct entity. There's no one file we can point to and say, "this file's timestamp is the program's timestamp." The best we can do is look at every script the program executes and take the latest of their dates. But we want to know the program's timestamp at the start of execution, and we can't easily tell what scripts it executes until it's done. To do that we'd have to perform a static analysis of the program's call tree, which sounds far more technically challenging than anything I can justify doing.

Does anyone have thoughts on a good way to solve this problem?
Reply
#2
CVS and SVN both support "keyword substitution", where you put a special string (for instance $version$) in your source and its is replaced by the CVS/SVN version number of the file when you extract it. Such substitutions can happen in string literals and can be used to set module variables. This is harder to do with Git but not impossible.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#3
That's an interesting possibility. Our SCS is Perforce; I researched it and found that it has a similar feature. So the timestamping part of the problem is solved.

But it's not clear how to deal with the part of the problem that I described in the next-to-last paragraph above. I don't know how to build a call tree for a program written in Python; offhand I'm not even sure how to go about learning. And now that I think about it, a call tree wouldn't do what I need, because it would include shared modules that are not part of the program for this purpose, and it's not clear how I could exclude them.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python wheel packages - how to add timestamp to the wheel name? vcankovic 0 938 Jul-06-2022, 12:04 PM
Last Post: vcankovic
  error in timestamp Led_Zeppelin 3 3,106 Jun-15-2022, 08:28 PM
Last Post: deanhystad
  error in timestamp Led_Zeppelin 0 980 Jun-10-2022, 07:59 PM
Last Post: Led_Zeppelin
  Thoughts on interfacing with a QR code reader that outputs keystrokes? wrybread 1 1,449 Oct-08-2021, 03:44 PM
Last Post: bowlofred
  Newbie have thoughts about logic sdd 32 8,766 Sep-17-2021, 10:13 AM
Last Post: Serafim
  Timestamp is undefined ErnestTBass 7 7,840 Feb-16-2019, 08:27 PM
Last Post: snippsat
  timestamp not updating bowen73 3 7,124 Aug-20-2017, 11:13 PM
Last Post: bowen73
  matplotlib timestamp zero_shubh0 2 6,763 Dec-02-2016, 02:12 PM
Last Post: zero_shubh0

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020