跳至主要内容

Workaround a jython runtime bug by interop with Java java.time

TypeError: unsupported operand type(s) for +: 'java.sql.Timestamp' and 'timedelta'

I was working on a NIFI script recently. As the following code snip shows, I was trying to construct a series of timestamped string with Python’s datetime.

import os.path
from datetime import datetime, time, timedelta
import subprocess

now = datetime.now()

dirs = [ (now + timedelta(milliseconds=(-100 * x))).strftime('%Y%m%d%H/%M/%S/%f')[0:-5] for x in range(0,100)]

fullpaths = [ '/workspace/nifi_output/{}'.format(d) for d in dirs]

existspaths = [fp for fp in fullpaths if os.path.exists(fp)]

flowFile = session.get() 
if (flowFile == None):
    flowFile = session.create()

ep_length = len(existspaths)

if ep_length > 1:
    session.putAttribute(flowFile, 'batch_path', existspaths[1])
    session.transfer(flowFile, REL_SUCCESS)
elif ep_length == 1:
    session.putAttribute(flowFile, 'batch_path', existspaths[0])
    session.transfer(flowFile, REL_SUCCESS)
else:
    session.transfer(flowFile, REL_FAILURE)

While the datetime package works perfect in Python2, It refuse to run in Nifi bundled jython runtime. I got a exception:

TypeError: unsupported operand type(s) for +: 'java.sql.Timestamp' and 'timedelta'

It feels like the underlying jython implementation wrapped java.sql.Timestamp to emulate pyhton datetime package. And the type system was confused during the excution of translated bytecode or JIT code.

WorkAround

The workaround: Avoid use of python datatime package whenever your code is targeting a jython runtime.

import os.path
import subprocess
from java.time import LocalDateTime
from java.time.format import DateTimeFormatterBuilder
from java.time.temporal import ChronoUnit

# We use java LocalDateTime instead of datetime.datetime datetime.timedelta
# becasue of Jython bug 
# 
# TypeError: unsupported operand type(s) for +: 'java.sql.Timestamp' and 'timedelta' in <script> at line number 8
#
# ref: https://community.openhab.org/t/using-python-datetime/114917
# ref: https://issues.streamsets.com/browse/SDC-3429
# 
now = LocalDateTime.now()
formatter = DateTimeFormatterBuilder().appendPattern("yyyyMMddHH/mm/ss/S").toFormatter()

localdatetimes = (now.minus(x*100, ChronoUnit.MILLIS) for x in range(100, 0, -1))
timestamps = (ldt.format(formatter) for ldt in localdatetimes)

fullpaths =  ('/opt/ericsson/workspace/nifi_output/{}'.format(ts) for ts in timestamps)

existspaths = [fp for fp in fullpaths if os.path.exists(fp)]


flowFile = session.get() 
if (flowFile == None):
    flowFile = session.create()

ep_length = len(existspaths)

if ep_length > 1:
    session.putAttribute(flowFile, 'batch_path', existspaths[1])
    session.transfer(flowFile, REL_SUCCESS)
elif ep_length == 1:
    session.putAttribute(flowFile, 'batch_path', existspaths[0])
    session.transfer(flowFile, REL_SUCCESS)
else:
    session.transfer(flowFile, REL_FAILURE)

Note that, the DateTimeFormatterBuilder().appendPattern("yyyyMMddHH/mm/ss/S").toFormatter() may be simplified to DateTimeFormatter.ofPattern("yyyyMMddHH/mm/ss/S")

评论

此博客中的热门博文

Use MobaDiff with git difftool

Recently there's an activity in IT that forces the deletion of all unauthorized softwares from all work machines. Unfortunately, kdiff3 is one in the list. As it is generally okay to use vimdiff as an alternative for kdiff3, A gui tool is better suited for desktop workflows. Known that MobaXterm is shipping a gui diff tool named MobaDiff. But it only appears in the windows right click context menu. Find the real application name takes me some time to search in the windows registry. "MobaRTE.exe", which is the one invoked by HKCR\*\shell\MobaDiff. And it was invoked with "-contextdiff" switch to show MobaDiff UI, while when the switch is "-contextedit" it shows MobaTextEditor. Too bad that the "-contextdiff" switch do not support pre-image post-image as other diff tool did, which effectively made it unable to be used as a command line diff utility. Also MobaTech did not mention anything in their document of this Mob...

Emacs 26.2 windows Hunspell setup

 Hunspell failed with default setup With my shining new emacs26.2 setup, I am happy until trying to `M-$’ on a word. I got following error message, it used to be work by just set ispell program to hunspell. Starting new Ispell process hunspell with default dictionary... split-string: Wrong type argument: stringp, nil After some check in my customization setting. nothing found. install a copy of hunspell dictionary At first I think it was caused by no hunspell dictionary. I downloaded en US and en CA hunspell dictionary from wordlist.sf.net project. After put the .aff and .dic files into C:directory I’ve also made a symbolic link from en US ones by running the `mklink’ tool: mklink default.aff en_US.aff mklink default.dic en_US.dic Err… hunspell does not work, still. Time to read code Dig into ispell.el I suprisingly find there’re lots of hunspell specific logic. Originally I was thinking hunspell support involves no elisp work. Acturally ispell did many work to de...

XEmacs 21.5 beta 35 "kohlrabi" has been released.

If you are an old XEmacs user, you may feel happy to see this from https://www.xemacs.org/.    After ten years, XEmacs released a new version 21.5. So there's still many people cares about XEmacs. The XEmacs' source repo have been moved from altassian Bitbucket to https://heptapod.net/. As Bitbucket have been dropped Mercurial support many years ago.