Skip to content

Funcs

ph_from_json

ph_from_json(data)

Decode a JSON string or a Python dict using the Haystack JSON encoding defined here to a PhKind (phable's Python representation of a Project Haystack kind).

Example:

from phable import ph_from_json

data = {"equip": {"_kind": "marker"}}
ph_from_json(data)
# {"equip": Marker()}

Parameters:

Name Type Description Default
data str | dict[str, typing.Any]

A JSON string or a Python dict using the Haystack JSON encoding.

required

Returns:

Type Description
phable.kinds.PhKind

A PhKind.

Source code in src/phable/io/ph_json.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
def ph_from_json(data: str | dict[str, Any]) -> PhKind:
    """Decode a JSON string or a Python dict using the Haystack JSON encoding defined
    [here](https://project-haystack.org/doc/docHaystack/Json) to a `PhKind` (phable's
    Python representation of a Project Haystack kind).

    **Example:**
    ```python
    from phable import ph_from_json

    data = {"equip": {"_kind": "marker"}}
    ph_from_json(data)
    # {"equip": Marker()}
    ```

    Parameters:
        data: A JSON string or a Python dict using the Haystack JSON encoding.

    Returns:
        A `PhKind`.
    """

    if isinstance(data, str):
        data = json.loads(data)
    return _parse_val(data)

ph_to_json_str

ph_to_json_str(data)

Encode a PhKind (phable's Python representation of a Project Haystack kind) to a JSON string using the Haystack JSON encoding defined here.

Example:

from phable import Marker, ph_to_json_str

data = {"equip": Marker()}
ph_to_json_str(data)
# '{"equip": {"_kind": "marker"}}'

Parameters:

Name Type Description Default
data phable.kinds.PhKind

A PhKind to encode.

required

Returns:

Type Description
str

A JSON string representation of the Haystack JSON encoding.

Source code in src/phable/io/ph_json.py
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
def ph_to_json_str(data: PhKind) -> str:
    """Encode a `PhKind` (phable's Python representation of a Project Haystack kind)
    to a JSON string using the Haystack JSON encoding defined
    [here](https://project-haystack.org/doc/docHaystack/Json).

    **Example:**
    ```python
    from phable import Marker, ph_to_json_str

    data = {"equip": Marker()}
    ph_to_json_str(data)
    # '{"equip": {"_kind": "marker"}}'
    ```

    Parameters:
        data: A `PhKind` to encode.

    Returns:
        A JSON string representation of the Haystack JSON encoding.
    """

    return json.dumps(_kind_to_json(data))

ph_from_zinc

ph_from_zinc(data)

Decode a Zinc string using the Haystack Zinc encoding defined here to a PhKind (phable's Python representation of a Project Haystack kind).

Example:

from phable import ph_from_zinc

ph_from_zinc('{equip}')
# {"equip": Marker()}

Parameters:

Name Type Description Default
data str

A Zinc string to decode.

required

Returns:

Type Description
phable.kinds.PhKind

A PhKind.

Source code in src/phable/io/ph_zinc.py
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
def ph_from_zinc(data: str) -> PhKind:
    """Decode a Zinc string using the Haystack Zinc encoding defined
    [here](https://project-haystack.org/doc/docHaystack/Zinc) to a `PhKind` (phable's
    Python representation of a Project Haystack kind).

    **Example:**
    ```python
    from phable import ph_from_zinc

    ph_from_zinc('{equip}')
    # {"equip": Marker()}
    ```

    Parameters:
        data: A Zinc string to decode.

    Returns:
        A `PhKind`.
    """

    return _ZincParser(data).read()

ph_to_zinc

ph_to_zinc(data)

Encode a PhKind (phable's Python representation of a Project Haystack kind) to a Zinc string using the Haystack Zinc encoding defined here.

Example:

from phable import Marker, ph_to_zinc

data = {"equip": Marker()}
ph_to_zinc(data)
# '{equip}'

Parameters:

Name Type Description Default
data phable.kinds.PhKind

A PhKind to encode.

required

Returns:

Type Description
str

A Zinc string representation of the Haystack Zinc encoding.

Source code in src/phable/io/ph_zinc.py
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
def ph_to_zinc(data: PhKind) -> str:
    """Encode a `PhKind` (phable's Python representation of a Project Haystack kind)
    to a Zinc string using the Haystack Zinc encoding defined
    [here](https://project-haystack.org/doc/docHaystack/Zinc).

    **Example:**
    ```python
    from phable import Marker, ph_to_zinc

    data = {"equip": Marker()}
    ph_to_zinc(data)
    # '{equip}'
    ```

    Parameters:
        data: A `PhKind` to encode.

    Returns:
        A Zinc string representation of the Haystack Zinc encoding.
    """

    out = StringIO()
    if isinstance(data, Grid):
        _write_grid(out, data)
    else:
        _write_val(out, data)
    return out.getvalue()