Giter VIP home page Giter VIP logo

json-logic-py's People

Contributors

nadirizr avatar rczajka avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

json-logic-py's Issues

Python 3.6 issue with dict_keys

>>> 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

check if key exists and has any value

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?

Include in comparation to work with list

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

#13

Evaluation of both "then" and "else" paths in "if" statements

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.

tests without urlopen

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.

Incorrect "==" (soft_equals) implementation

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

def soft_equals(a, b):
"""Implements the '==' operator, which does type JS-style coertion."""
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

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.

Custom Return Values

If possible we can add a custom return value (optional) on successful execution that can replicate a switch case using ifelsifelse

There is possibility change json logic to values using this module

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

Error while running

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

Consider updating package on pypi

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?

pip out of date

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.

Incorrect "==" (soft_equals) implementation for None value

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.