Products Resources Support About Us

Python function executes once but only once. Second + times called fails


#1

Hello Again Everyone,

We’ve run into what is seemingly a strange occurrence. A Python module with a function that converts a date from a timezone to UTC will execute the first time; but fails on subsequent calls. Logging out of UV and back in does not change the behavior. I have tested this on my on UVPE 13.1.1 and on Linux (although the Linux one is pointing to a 3.4.4 virtual environment. The routine runs successfully on every subsequent call when running outside of UV.

This is the error message on the second and subsequent calls:
Traceback (most recent call last):
File “pymodules/pyutils.py”, line 64, in
x = convert_dt_2utc(json.dumps(rdl[i]))
File “pymodules/pyutils.py”, line 48, in convert_dt_2utc
local_dt = local_tz.localize(datetime.datetime.strptime(in_date, ‘%Y-%m-%d %H:%M:%S.%f’))
TypeError: attribute of type ‘NoneType’ is not callable

The toolbar upload won’t let me upload the file so I’m pasting it here:

import datetime

import pytz
import json

def convert_dt_2utc(conversion_json):
“”“Return a date converted to UTC.
Keyword arguments:
in_date needs to be in YYYY-mm-dd HH:MM:SS.sss
precision – number of decimal places up to 6 (default 0)
Note: if 0 there won’t be a decimal; if negative, turned positive
timezone if used needs to be an ISO standard timezone desgination (e.g.; Asia/Taipei, Australia/Sydney)
defaults to US/Eastern
“””
conversion_info = json.loads(conversion_json)
print(conversion_info)
in_date = conversion_info[‘in_date’]
precision = conversion_info[‘precision’]
timezone = conversion_info[‘timezone’]

print(precision)
print(timezone)

precision = abs(precision)
precision = min(precision, 6)

local_tz = pytz.timezone(timezone)
local_dt = local_tz.localize(datetime.datetime.strptime(in_date, '%Y-%m-%d %H:%M:%S.%f'))
converted_dt = local_dt.astimezone(pytz.UTC)
if precision == 0:
    return converted_dt.strftime('%Y-%m-%dT%H:%M:%SZ')
else:
    return converted_dt.strftime('%Y-%m-%dT%H:%M:%S.%f')[:precision - 6] + 'Z'

if name == ‘main’:
rdl = []
rdl.append({‘in_date’: ‘2019-03-10 00:00:00.000’, ‘precision’: 3, ‘timezone’: ‘US/Eastern’})
rdl.append({‘in_date’: ‘2019-03-10 01:00:00.000’, ‘precision’: 3, ‘timezone’: ‘US/Eastern’})
rdl.append({‘in_date’: ‘2019-03-10 02:00:00.000’, ‘precision’: 3, ‘timezone’: ‘US/Eastern’})
rdl.append({‘in_date’: ‘2019-03-10 03:00:00.000’, ‘precision’: 3, ‘timezone’: ‘US/Eastern’})

for i in range(len(rdl)):
    x = convert_dt_2utc(json.dumps(rdl[i]))
    print(rdl[i]['in_date'])
    print(x)

As you can see the main sections calls the function four times. The first time the module runs, the four function calls work fine. The second and subsequent times, it bombs.

Please advise as this is very disconcerting not having faith that the simplest of python functions will not run after the first time.

Regards and Thanks,

Dave Howell,
Market America/SHOP.com


#2

Note that this is a known issue related to how some packages interact with the process, due to the nature of the solution, what the python module is doing and the re-initialization of the python environment. This is being addressed in the next release which is currently in Beta.

Mike