Pynotify makes you wonder why?

Problem

This code

#!/usr/bin/env python
import pynotify
import time
import datetime

c='5/1/12 1:15 PM'
print c
dt = time.strptime(c, "%d/%m/%y %H:%M %p")

produces

5/1/12 1:15 PM
Traceback (most recent call last):
  File "tmp.py", line 9, in <module>
    dt = time.strptime(c, "%d/%m/%y %H:%M %p")
  File "/usr/lib/python2.7/_strptime.py", line 454, in _strptime_time
    return _strptime(data_string, format)[0]
  File "/usr/lib/python2.7/_strptime.py", line 328, in _strptime
    data_string[found.end():])
ValueError: unconverted data remains: PM

Removing import pynotify,

#!/usr/bin/env python
import time
import datetime

c='5/1/12 1:15 PM'
print c
dt = time.strptime(c, "%d/%m/%y %H:%M %p")

Deletes an error.

5/1/12 1:15 PM

WHY?!!?!

Python version

Python 2.7.2+ (default, October 4, 2011 8:06:09 PM) [GCC 4.6.1] on linux2

. pynotify file

I added print calls for pynotify.__file__anddatetime.__file__

/usr/lib/python2.7/lib-dynload/datetime.so
/usr/lib/python2.7/dist-packages/gtk-2.0/pynotify/__init__.pyc
5/1/12 1:15 PM
Traceback (most recent call last):
  File "a.py", line 11, in <module>
    dt = time.strptime(c, "%d/%m/%y %H:%M %p")
  File "/usr/lib/python2.7/_strptime.py", line 454, in _strptime_time
    return _strptime(data_string, format)[0]
  File "/usr/lib/python2.7/_strptime.py", line 328, in _strptime
    data_string[found.end():])
ValueError: unconverted data remains: PM

PDB

5/1/12 1:15 PM
> /usr/lib/python2.7/_strptime.py(324)_strptime()
-> found = format_regex.match(data_string)
(Pdb) format
'%d/%m/%y %H:%M %p'
(Pdb) continue
> /usr/lib/python2.7/_strptime.py(329)_strptime()
-> if len(data_string) != found.end():
(Pdb) continue
> /usr/lib/python2.7/_strptime.py(331)_strptime()
-> raise ValueError("unconverted data remains: %s" %
(Pdb) len(data_string)
14
(Pdb) found.end()
12
(Pdb) found.group(0)
'5/1/12 1:15 '

It '%d/%m/%y %H:%M %p'doesn't seem to capture ALL of the '5/1/12 1:15 PM'

+5
source share
1 answer

This is a fun problem. I would bet what happens when pynotify changes your locale settings, which breaks the strptimeinterpretation of your timestamp string.

Here is your code with several debugging print operations that illustrate the theory:

#!/usr/bin/env python

import time
import datetime
import locale

print locale.getlocale()
import pynotify
print locale.getlocale()
c='5/1/12 1:15 PM'
print c
dt = time.strptime(c, "%d/%m/%y %H:%M %p")

On my system, I get the following:

(None, None)
('en_US', 'UTF8')
5/1/12 1:15 PM

, , pynotify , strptime.

, , strptime ( , , pynotify), - , - .

+2

All Articles