2

This is a 2-part question and the first part is simple. I'm trying to get the current time as a high resolution number, something easily done with the Time::HiRes module in perl. I did find answers this questions elsewhere to use using time.time() in python. The only thing is when I do this all I get as 2 significant digits and am wondering if I need to import something else or set something in my environment or what. Look at this:

>>> import time
>>> print time.time()
1326459121.68

My second question has to do with setitimer, which according to the docs says it will deliver alarms and the time can be specified as a floating point number. Sounds exactly what I want. In fact, according to this - python timer mystery it looks pretty easy to use. But when I make an exact copy of that example, I get this:

[root@poker python]# ./signal.py
Traceback (most recent call last):
  File "./signal.py", line 10, in ?
    signal.setitimer(signal.ITIMER_REAL, 2, 2)
AttributeError: 'module' object has no attribute 'setitimer'

Perhaps part my the problem is that as a new python user I'm still not 'one with python' and perhaps am not understanding what the error message it trying to tell me. Is it complaining that it can't find signal.setitimer OR is there something wrong with the attributes I'm passing? I was wondering if it couldn't resolve signal.ITIMER_REAL and so tried calling it with a 0 just to see what would happen and now it's telling me:

[root@poker python]# ./signal.py
Traceback (most recent call last):
  File "./signal.py", line 10, in ?
    signal.setitimer(0, 2, 2)
AttributeError: 'module' object has no attribute 'setitimer'

so I have to believe it is signal.setitmer itself that is having issues.

Aha! I just figured out the problem by trying this on a VM with a newer version of python and it works just fine. Looks like setitimer is not available with my version of python but how can I tell from the error message? Shouldn't it be saying the module has no method? Calling the missing method an attribute is pretty confusing.

Maybe that's just part of the learning curve. Anyhow I thought I'd keep this second question in here so other might benefit, but I still am still stumped by what I can't get high resultion time from time.time() which still only reports in hundredths of a second even on this newer version of python.

Community
  • 1
  • 1
Mark J Seger
  • 367
  • 2
  • 12
  • 1
    In python, functions are first-class citizens and can be treated as variables. In that sense a method is an attribute and it shouldn't be confusing. – Otto Allmendinger Jan 13 '12 at 13:01
  • 1
    time.time() returns more sigfigs, but 'print' only shows 2 by default. print repr(time.time()) to see all the digits. – AdamKG Jan 13 '12 at 13:11
  • cool! very nice. I guess I was confused because every reference I saw that show printing high res time did so with a simple "print time.time()". does that mean they were being lazy in their postings or just assuming the readers would know what to do? just curious and trying to absorb the python culture – Mark J Seger Jan 13 '12 at 13:56

1 Answers1

4
  • time.time() returns more digits but the repr truncates to 2.

    >>> import time
    >>> time.time()
    1326460396.626451
    >>> print time.time()
    1326460422.68
    
  • The problem with not being able to find setitimer is quite simply because you've named your python script 'signal.py', when you 'import signal' in your script, you're actually importing your script -- not the signal module. Just rename it to something else and it'll work.

synthesizerpatel
  • 27,321
  • 5
  • 74
  • 91
  • for the record, my script named signal works fine with python 2.6.6. I just tried renaming the script to xxx on python 2.4.3 and it still failed with the same error, so what wasn't the problem either. – Mark J Seger Jan 13 '12 at 14:46
  • If there is a file named signal.py in the same directory as 'xxx', that'll cause the same problem. – synthesizerpatel Jan 13 '12 at 14:51