nadirizr / json-logic-py Goto Github PK
View Code? Open in Web Editor NEWBuild complex rules, serialize them as JSON, and execute them in Python
License: MIT License
Build complex rules, serialize them as JSON, and execute them in Python
License: MIT License
>>> from json_logic import jsonLogic
>>> jsonLogic( { "==" : [1, 1] } )
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/anaconda/lib/python3.6/site-packages/json_logic/__init__.py", line 13, in jsonLogic
op = tests.keys()[0]
TypeError: 'dict_keys' object does not support indexing
I want to test if a key exist and have any value.
Much like the pythonic if variable:
statement.
example:
from json_logic import jsonLogic
c = {'abc': 'foo'}
How do I test if key def
exists and has any value?
It would be a good improvement, to contemplate the in operation to work with lists and tuples.
Example:
rule = {"in" : [ { "var" : "temp" }, 110 ]}
data = { "temp" : [99, 100, 101]}
jsonLogic(rules, data)
# True
I noticed this issue while running the following code:
from json_logic import jsonLogic
data={"var1":17,"var2":0}
rule={
"if": [
{
">": [
{
"var": "var2"
},
"0"
]
},
{
"if": [
{
">=": [
{
"/": [
{
"var": "var1"
},
{
"var": "var2"
}
]
},
"16"
]
},
"g",
"b"
]
},
"b"
]
}
print(jsonLogic(rule,data))
running above code gives a ZeroDivisionError.
It looks like the "some", "all", "none" operators are not implemented.
Anyone planning to add them?
Is there a way to add a new customer operator (as it is in javascript version)?
Thanks
I think it would be a better idea to not use urlopen
to fetch tests from the website. There are many good reasons, but I will mention just usability, when you're behind proxies.
The type coercion is not done right. In particular the type coercion to double precision floating point numbers is missing. Also boolean handling is wrong.
>>> soft_equals("01",1)
False
>>> soft_equals("1e1",10)
False
>>> soft_equals("1",True)
False
>>> soft_equals(" 0.0e1 ",False)
False
>>> soft_equals(2,True)
True
> "01"==1
true
> "1e1"==10
true
> "1"==true
true
> " 0.0e1 "==false
true
> 2==true
false
json-logic-py/json_logic/__init__.py
Lines 31 to 37 in 5fda912
Yes, the logic behind JavaScript's ==
is brain dead. It would probably have been a good idea to only have ===
(or ==
with the semantics of JavaScript's ===
) in JsonLogic. But now that it is specified like JavaScript's ==
operator it is what it is.
If possible we can add a custom return value (optional) on successful execution that can replicate a switch case using ifelsifelse
Hi there is possibility to change
{
"DATABASE": {
"ENGINE": "pyodbc",
"NAME": "ssssss",
"USER": "xxxxxxxxxxx",
"PASSWORD": "xxxxxxxxxxxxxxxx",
"HOST": "tttttttttt"
},
"CONSOLE_CONFIG_DATE_FORMAT": "%Y/%m/%d %H:%M",
"CC_PATH": "R:\\",
"TIM_PATH": "Z:\\",
"CC_LOCAL_STORAGE_PATH": "C:\\Projects\\tmp",
"TIM_LOCAL_STORAGE_PATH": "C:\\Projects\\tmp2",
"MONITOR_JOB_PERIOD_S": {"*": [1, 60]},
"MONITOR_JOB_TC_TIMEOUT_S": {"*": [4, {"var": "MONITOR_JOB_PERIOD_S"}]},
"EMAIL_ACCOUNT": "[email protected]",
"EMAIL_PASSWORD": "******",
"EMAIL_MAIN_MAINTAINER": "[email protected]",
"TIM_CLIENT_EMAILS": {
}
}
to
{
"DATABASE": {
"ENGINE": "pyodbc",
"NAME": "ssssss",
"USER": "xxxxxxxxxxx",
"PASSWORD": "xxxxxxxxxxxxxxxx",
"HOST": "tttttttttt"
},
"CONSOLE_CONFIG_DATE_FORMAT": "%Y/%m/%d %H:%M",
"CC_PATH": "R:\\",
"TIM_PATH": "Z:\\",
"CC_LOCAL_STORAGE_PATH": "C:\\Projects\\tmp",
"TIM_LOCAL_STORAGE_PATH": "C:\\Projects\\tmp2",
"MONITOR_JOB_PERIOD_S": 60,
"MONITOR_JOB_TC_TIMEOUT_S": 240,
"EMAIL_ACCOUNT": "[email protected]",
"EMAIL_PASSWORD": "******",
"EMAIL_MAIN_MAINTAINER": "[email protected]",
"TIM_CLIENT_EMAILS": {
}
}
As you can see the json logic was replaced by values. This is possible to do it in easy way?
Best regards.
Draqun
rules = { "and" : [
{"<" : [ { "var" : "temp" }, 110 ]},
{"==" : [ { "var" : "pie.filling" }, "apple" ] }
] }
data = { "temp" : 100, "pie" : { "filling" : "apple" } }
jsonLogic(rules, data)
I get this error:
{TypeError}'dict_keys' object is not subscriptable
I have this issure :
op = tests.keys()[0] TypeError: 'dict_keys' object is not subscriptable
So i change init.py at line 14.
op = tests.keys()[0]
with :
for key in tests:
op = key
break
Is there a better way to avoid this error?
regards
Was pulling my hair out trying to figure out why a certain json logic expression wasn't working, but then I realized the version of the code I got from pip install json-logic
is older and less complete than what's on master.
Considering publishing the latest version?
I just ran pip install json-logic
and it installed version 0.6.3 . However, this version is out of date from master. The json_logic.py on PIP contains:
def jsonLogic(tests, data=None):
# You've recursed to a primitive, stop!
if tests is None or type(tests) != dict:
return tests
data = data or {}
op = tests.keys()[0]
which causes an exception under Python 3.6, because dict_keys are no longer indexable,.
Looking at the code that's downloaded via pip, it's just very out of date. Can you publish the latest changes to PIP?
Thanks.
There is a problem when one of operand is string with value "None" and the other operand is None. Then the method call will be True witch is incorrect.
soft_equals("None",None)
True
soft_equals(None,"None")
True
The suggested change should be like:
def soft_equals(a, b):
"""Implements the '==' operator, which does type JS-style coertion."""
if (not a and b) or (
a and not b
): # we want to skip false positive like str("None")==str(None)
return False
if isinstance(a, str) or isinstance(b, str):
return str(a) == str(b)
if isinstance(a, bool) or isinstance(b, bool):
return bool(a) is bool(b)
return a == b
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.