Metadata-Version: 2.4
Name: simple-json-func
Version: 0.1.0
Summary: a simple way to create json configuration for functions
License-Expression: MIT
License-File: LICENSE
Author: GGN_2015
Author-email: neko@jlulug.org
Requires-Python: >=3.6
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.6
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Description-Content-Type: text/markdown

# simple_json_func

`simple_json_func` is a Python library for evaluating expressions stored as JSON.
Register Python callables by name, then use JSON objects to call them, pass
arguments, and compose nested expressions. The library has no third-party
runtime dependencies.

## Installation

Use a standard Python installation with `pip`. From the repository root, run:

```sh
python -m pip install .
```

## Quick start

```python
import json
from operator import add

from simple_json_func import JsonFunc

evaluator = JsonFunc(is_root=True, can_echo=False)
evaluator.bind("add", add)

expression = json.loads('''
{
  "_f": "add",
  "_c": [2, {"_f": "add", "_c": [3, 4]}]
}
''')
print(evaluator.calc(expression))  # 9
```

`calc()` takes a decoded Python value. Use `json.loads()` for JSON text or
`json.load()` for an open JSON file. Use trusted configurations: an expression
can invoke any callable registered on its evaluator.

## Expression format

An object with a non-null `_f` refers to a registered function. Its `_c` field
controls how the function is used:

| `_c` value | Default behavior |
| --- | --- |
| Array | Unpack it as positional arguments; use `[]` for a call with no arguments. |
| Object | Unpack it as keyword arguments. |
| Other non-null value | Pass it as one positional argument. |
| Omitted or `null` | Return the registered callable without invoking it. |

Arrays and ordinary objects (those without a non-null `_f`) are evaluated
recursively. Scalar values pass through unchanged. A reference to an unregistered
function raises `KeyError`.

## Built-in functions

Set `is_root=True` to register these functions. With `is_root=False`, the
evaluator starts with no registered functions.

| Function | Behavior |
| --- | --- |
| `@calc` | Evaluates the raw `_c` value as an expression. |
| `@if` | Takes the raw arguments `[cond, vt, vf]` and evaluates only the selected branch. It tests `cond` using Python truthiness without evaluating it as an expression. |
| `@map` | Takes a callable `func` and a list `lis`, then returns a list of results, one per item. |
| `@static` | Returns the entire expression object unchanged, including the `@static` wrapper. |
| `@json` | Evaluates `_c`, then serializes the result to a JSON string with `json.dumps()`. |
| `@echo` | Evaluates `_c`, records its string representation, and returns `None`. With `can_echo=True`, it also sends the text to `print_hook` (which defaults to `print`). |

For example, pass a callable reference to `@map` and use `@if` to skip an unused
branch. This example reuses `evaluator` from the quick start:

```python
evaluator.bind("double", lambda value: value * 2)

result = evaluator.calc({
    "_f": "@if",
    "_c": [
        True,
        {"_f": "@map", "_c": [{"_f": "double"}, [1, 2, 3]]},
        {"_f": "not_registered", "_c": []},
    ],
})
print(result)  # [2, 4, 6]
```

## Registration and output

Use `bind(name, func, no_fill=False, late=False)` to register a callable. By
default, `calc()` evaluates `_c` before calling the function and returns the
function's result as-is.

- `no_fill=True` passes `_c` as a single argument, even when it is an array or an
  object.
- `late=True` passes raw arguments and evaluates the returned value. With both
  flags enabled, the callable receives the raw `_c` as one argument and its
  return value passes through unchanged.
- `late(name)` and `unlate(name)` toggle late evaluation for a name. `all_f()`
  lists registered names, and `all_late()` lists registered late-evaluation names.
- `merge(other, prefix)` imports bindings from another `JsonFunc` instance,
  prepending `prefix` to each name. Use `""` to leave the names unchanged.
- `get_echo_history()` returns the recorded `@echo` entries, even when immediate
  output is disabled. `flush_echo_history(show=True)` sends the joined entries to
  `print_hook` and clears them. Pass `show=False` to clear them without printing.

## License

[MIT](LICENSE)

