https://github.com/jonghwanhyeon/dict-logic
https://github.com/jonghwanhyeon/dict-logic
Last synced: 4 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/jonghwanhyeon/dict-logic
- Owner: jonghwanhyeon
- Created: 2020-12-24T18:30:28.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2020-12-25T08:26:34.000Z (over 4 years ago)
- Last Synced: 2025-02-15T13:42:27.453Z (5 months ago)
- Language: Python
- Size: 3.91 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# dict-logic
## Usage
### Basic
```python
from dictlogic import Logiclogic = Logic()
result = logic.run({
'sequence': [
{'print': 'hello world!'},
{'set': ['a', 10]},
{'print': ['the value of a is', {'get': 'a'}]},
{'select': [
{'>': [{'get': 'a'}, 5]},
{'print': 'this expression will be evaluated'},
{'print': 'this expression will be ignored'},
]},
{'set': ['a', {'*': [{'get': 'a'}, 2]}]},
{'get': 'a'},
],
})
assert(result == 20)
```### With given local variabls
```python
from dictlogic import Logiclogic = Logic()
result = logic.run({
'sequence': [
{'print': ['the value of a is', {'get': 'a'}]},
{'set': ['c', {'+': [{'get': 'a'}, {'get': 'b'}]}]},
{'print': ['the value of c is', {'get': 'c'}]},
],
}, {
'a': 5,
'b': 3,
})
```### With custom functions
```python
from dictlogic import Logiclogic = Logic()
@logic.add('sqrt')
def sqrt(x):
return x() ** 0.5@logic.add('mean')
def power(*args):
args = [arg() for arg in args]
return sum(args) / len(args)logic.run({'print': {'sqrt': 2}})
logic.run({'print': {'mean': [1, 2, 3, 4, 5, 6, 7]}})
```