"return Date1 > Date2" works but "if Date1 > Date2" does not

Here’s my script:

	import re

	operators = {
			">" : (lambda x,y: x>y),
			">=" : (lambda x,y: x>=y),
			"<" : (lambda x,y: x<y),
			"<=" : (lambda x,y: x<=y),
			"==" : (lambda x,y: x==y),
			"<>" : (lambda x,y: x<>y)
	}

	output = []
	Dataset = system.dataset.toPyDataSet(self.custom.Dataset)
	ColumnHeaders = system.dataset.getColumnHeaders(self.custom.Dataset)
	if len(value) == 0:
		output = shared.Data.datasetToJSON(self.custom.Dataset)
	else:
		for row in Dataset:
			AddRow = True
			for filter in value:
				if filter["Expression"] in [">",">=","<","<=","==","<>"]:
#					if not operators[filter["Expression"]] (row[filter["Column"]], filter["Value1"]):
					if not row[filter["Column"]] > filter["Value1"]:
						AddRow = False	
				elif filter["Expression"] == "between":
					if not eval(str(row[filter["Column"]]) >= str(filter["Value1"]) and str(row[filter["Column"]]) <= str(filter["Value2"])):
						AddRow = False
				elif filter["Expression"] == "in":
					pass
				elif filter["Expression"] == "contains":
					if not re.search(str(filter["Value"]), str(row[filter["Column"]])):
						AddRow = False
			if AddRow:
				row_object = {}
				row_value = {}
				row_style = {}
				for col in ColumnHeaders:
					row_value[col] = row[col]
				row_object["value"] = row_value
				row_object["style"] = row_style
				output.append(row_object)
	return output

this results in the following error:

if I try to return row[filter[“Column”]] > filter[“Value1”], it works:

I’m not sure why it can evaluate the expression when I’m returning it, but it can’t evaluate in the context of an if statement…

Any help would be greatly appreciated.

EDIT:
Wanted to show what the values look like if returned on their own:


this looks like a complicate approach.
Anyways, try to use:

if not system.date.parse(str(row[filter["Column"])],'yyyy-MM-dd hh:mm:ss') > system.date.parse(str(filter["Value1"]),'yyyy-MM-dd hh:mm:ss')

The return is terminating the for loops (and the entire script) the first time it enters that first if statement. Looks like you have a NULL in your data somewhere that subsequent iterations through the for loop(s) hits.

2 Likes

This was it! Thank you for the quick response.

You may be able to trim some of your code by leaning on the builtin operator module.

1 Like

It looks like the operator is specified by the user at runtime… So there would need to be an operation check anyway, as far as I understand - I could be wrong.