Datetime.strptime - NonType error on second use
#1
I'm having an odd issue when converting date formats using datetime, I'm running on Windows the Oct. 17th build, but happens on any other pre-Eden nightly I've tested on

In Dharma I believe it is running Python 2.4 so is not affected as the strptime attribute is in the 'time' library, did not get moved to datetime until 2.5

Basically I have the following quick function to convert a date to a different format, and I'm executing it in a small tester video addon:

Code:
from datetime import datetime
import time
..
..
    def convert_date(self, string, in_format, out_format):
        ''' Helper method to convert a string date in format dd MMM YYYY to YYYY-MM-DD '''
        
        #Legacy check, Python 2.4 does not have strptime attribute, instroduced in 2.5
        if hasattr(datetime, 'strptime'):
            strptime = datetime.strptime
        else:
            strptime = lambda date_string, format: datetime(*(time.strptime(date_string, format)[0:6]))
        
        try:
            a = strptime(string, in_format).strftime(out_format)
        except Exception, e:
            print 'Date conversion failed: %s' % e
            return None
        return a

I have the legacy check in for Dharma users, I can assure you that piece of code is not causing any issues

Doing a quick call to this method to return just the year from a given date works fine:

Code:
year = convert_date('2011-01-01', '%Y-%m-%d', '%Y')

But if I run the addon a 2nd time (the only thing it does is executes this code) I get the following error:

Code:
attribute of type 'NoneType' is not callable

I need to shut down xbmc and restart it for it to work again, then on the 2nd run it fails with this error.. I can reproduce this time and time again. I can call this method a hundred times over on the first run and will work fine. It's always on the 2nd time launching the addon

If I revert back to Dharma 10.1 it will use time.strptime and works perfect

Any ideas? This code works perfectly fine in Python Idle 2.7 no matter how many times I run it..
Reply
#2
I'm having similar issues with the strptime function, except i cant even run it a first time. Im using linux with system python version 2.7.1. Its very odd because it works fine in the interpreter, but when trying to run it from xbmc its fails with the same message: "attribute of type 'NoneType' is not callable".
Reply
#3
I'm glad I'm not the only one! Smile

Can anyone else test this on pre-Eden?

Here's a more simpler piece to test with:

Code:
from datetime import datetime
a = datetime.strptime('2011-01-01', '%Y-%m-%d')
print a

First run:
Code:
NOTICE: 2011-01-01 00:00:00

Second run:
Code:
ERROR: Traceback (most recent call last):
                                              File "C:\Users\M33282\AppData\Roaming\XBMC\addons\plugin.video.tester\default.py", line 17, in <module>
                                                a = datetime.strptime('2011-01-01', '%Y-%m-%d')
                                            TypeError: attribute of type 'NoneType' is not callable
Reply
#4
@ Eldorado,

Have you come across any more on this issue?

Having the same issue: works on first try after starting but fails on repeated attempts. Works fine when I try in IDLE.

Running Python 2.7.2 and Pre-Eden (PVR-Margo compiled on 2011-12-04) on Win7 64bit
Reply
#5
Yep I was able to fix it by using the new python, only works with pre-Eden

In your addon.xml, set python to 2.0

<import addon="xbmc.python" version="2.0" />
Reply
#6
Eldorado Wrote:I'm glad I'm not the only one! Smile

Can anyone else test this on pre-Eden?

Here's a more simpler piece to test with:

Code:
from datetime import datetime
a = datetime.strptime('2011-01-01', '%Y-%m-%d')
print a

First run:
Code:
NOTICE: 2011-01-01 00:00:00

Second run:
Code:
ERROR: Traceback (most recent call last):
                                              File "C:\Users\M33282\AppData\Roaming\XBMC\addons\plugin.video.tester\default.py", line 17, in <module>
                                                a = datetime.strptime('2011-01-01', '%Y-%m-%d')
                                            TypeError: attribute of type 'NoneType' is not callable


Hey Eldorado
I found this below and it got me to thinking.

Quote:classmethod datetime.strptime(date_string, format)

Return a datetime corresponding to date_string, parsed according to format. This is equivalent to datetime(*(time.strptime(date_string, format)[0:6])). ValueError is raised if the date_string and format can’t be parsed by time.strptime() or if it returns a value which isn’t a time tuple.

On first run you get a=2011-01-01 00:00:00 but time is nothing

In VB i define what a variable will be ie Dim a as Date or String
What if you put a='' to force it into a string at the begining of your routine or format it as a DateTime variable

It could be that 00:00:00 is causing an issue.

OR i could just be wrong. ..... nevermind

also seen this. maybe a clue ie the %H:%M:%S

from datetime import datetime
dt = '16-MAR-2010 03:37:04'
datetime.strptime(dt, " %d-%b-%Y %H:%M:%S ")
datetime.datetime(2010, 3, 16, 3, 37, 4)
Reply
#7
(2011-12-20, 18:25)Eldorado Wrote: Yep I was able to fix it by using the new python, only works with pre-Eden

In your addon.xml, set python to 2.0

<import addon="xbmc.python" version="2.0" />

I've got exactly the same problem in Eden and the latest nightlies. I tried this solution but it didn't help. Have you come across this again since?
Leopold's Repository: Home of LibreELEC Dev Updater ...
Reply
#8
as far as i understand it's a known issue with python.
if you google for a bit, you'll find several reports like this one:
https://mail.python.org/pipermail/python...40636.html

the workaround seems to be:
Code:
import datetime
import _strptime

edit: http://bugs.python.org/issue27400
Do not PM or e-mail Team-Kodi members directly asking for support.
Always read the Forum rules, Kodi online-manual, FAQ, Help and Search the forum before posting.
Reply
#9
Here is how I worked around the issue in one add-on using time module -

Code:
try:
        start_date = datetime.strptime(url, "%B %d, %Y - %A")
    except TypeError:
        start_date = datetime.fromtimestamp(time.mktime(time.strptime(url, "%B %d, %Y - %A")))
Reply
#10
(2012-10-12, 17:17)ronie Wrote: as far as i understand it's a known issue with python.
if you google for a bit, you'll find several reports like this one:
http://mail.python.org/pipermail/python-...40579.html

the workaround seems to be:
Code:
import datetime
import _strptime

Thanks but this didn't work. I think it might fix a slightly different problem to mine.
(2012-10-12, 18:28)divingmule Wrote: Here is how I worked around the issue in one add-on using time module -

Code:
try:
        start_date = datetime.strptime(url, "%B %d, %Y - %A")
    except TypeError:
        start_date = datetime.fromtimestamp(time.mktime(time.strptime(url, "%B %d, %Y - %A")))

Thanks, this works, but I ended up taking a simpler example from the Python doc.

Code:
try:
    datetime.strptime(date_string, format)
except TypeError:
    datetime(*(time.strptime(date_string, format)[0:6]))

I was hoping there wouldn't be a need to work around the problem but at least it's working now.
Leopold's Repository: Home of LibreELEC Dev Updater ...
Reply
#11
Just thankyou - helped me solve a very confusing problem!!

Addons I wrote &/or maintain:
OzWeather (Australian BOM weather) | Check Previous Episode | Playback Resumer | Unpause Jumpback | XSqueezeDisplay | (Legacy - XSqueeze & XZen)
Sorry, no help w/out a *full debug log*.
Reply
#12
Code:
try:
      self.date = datetime.strptime(date, '%Y-%m-%d %I:%M %p')
    except TypeError:
      self.date = datetime.fromtimestamp(time.mktime(time.strptime(date, "%Y-%m-%d %I:%M %p")))
    # Convert date to local time
    self.date_est = self.date.replace(tzinfo=timezone('US/Eastern'))
    self.date_local = self.date_est.astimezone(tz.tzlocal())

I have this code above but for some reason its not displaying the time correctly. That above shows 1 hour ahead of what the current time actually is. If I set it to US/Pacific it puts it in eastern time... Any ideas how I can get this to the current PST time? or at least fix the daylight savings issue I assume it is?
Image
Reply
#13
Wow.. I'm totally going to necro this thread!

I running into this exact problem, years later on a Helix platform with Kodi 2.7. The first run is acceptable, subsequent runs throw
TypeError: attribute of type 'NoneType' is not callable


Strange, indeed.
Reply
#14
I also bumped into this on a Jarvis platform just now. Workaround that also worked for me:


Code:
Code:
try:
    datetime.strptime(date_string, format)
except TypeError:
    datetime(*(time.strptime(date_string, format)[0:6]))
Reply
#15
(2016-07-02, 21:29)tmelin Wrote: I also bumped into this on a Jarvis platform just now. Workaround that also worked for me:


Code:
Code:
try:
    datetime.strptime(date_string, format)
except TypeError:
    datetime(*(time.strptime(date_string, format)[0:6]))

The same problem. Can confirm workaround. Laugh
Big thanks !!!
Reply

Logout Mark Read Team Forum Stats Members Help
Datetime.strptime - NonType error on second use1