|
1 | 1 | # Apache Fory™ Python |
2 | 2 |
|
3 | | -Fory is a blazingly-fast multi-language serialization framework powered by just-in-time compilation and zero-copy. |
| 3 | +[](https://github.com/apache/fory/actions/workflows/ci.yml) |
| 4 | +[](https://pypi.org/project/pyfory/) |
| 5 | +[](https://join.slack.com/t/fory-project/shared_invite/zt-36g0qouzm-kcQSvV_dtfbtBKHRwT5gsw) |
| 6 | +[](https://x.com/ApacheFory) |
4 | 7 |
|
5 | | -## Build Fory Python |
| 8 | +**Apache Fory** (formerly _Fury_) is a blazing fast multi-language serialization framework powered by **JIT** (just-in-time compilation) and **zero-copy**, providing up to 170x performance and ease of use. |
| 9 | + |
| 10 | +This package provides the Python bindings for Apache Fory. |
| 11 | + |
| 12 | +## Installation |
| 13 | + |
| 14 | +You can install `pyfory` using pip: |
6 | 15 |
|
7 | 16 | ```bash |
8 | | -cd python |
9 | | -# Uninstall numpy first so that when we install pyarrow, it will install the correct numpy version automatically. |
10 | | -# For Python versions less than 3.13, numpy 2 is not currently supported. |
11 | | -pip uninstall -y numpy |
12 | | -# Install necessary environment for Python < 3.13. |
13 | | -pip install pyarrow==15.0.0 Cython wheel pytest |
14 | | -# For Python 3.13, pyarrow 18.0.0 is available and requires numpy version greater than 2. |
15 | | -# pip install pyarrow==18.0.0 Cython wheel pytest |
16 | | -pip install -v -e . |
| 17 | +pip install pyfory |
17 | 18 | ``` |
18 | 19 |
|
19 | | -If the last steps fails with an error like `libarrow_python.dylib: No such file or directory`, |
20 | | -you are probably suffering from bazel's aggressive caching; the sought library is longer at the |
21 | | -temporary directory it was the last time bazel ran. To remedy this run |
22 | | - |
23 | | -> bazel clean --expunge |
| 20 | +## Quickstart |
24 | 21 |
|
25 | | -In this situation, you might also find it fruitful to run bazel yourself before pip: |
| 22 | +Here are a few examples of how to use `pyfory` for serialization. |
26 | 23 |
|
27 | | -> bazel build -s //:cp_fory_so |
| 24 | +### Basic Serialization |
28 | 25 |
|
29 | | -### Environment Requirements |
| 26 | +This example shows how to serialize and deserialize a simple Python object. |
30 | 27 |
|
31 | | -- python 3.8+ |
| 28 | +```python |
| 29 | +from typing import Dict |
| 30 | +import pyfory |
32 | 31 |
|
33 | | -## Testing |
| 32 | +class SomeClass: |
| 33 | + f1: "SomeClass" |
| 34 | + f2: Dict[str, str] |
| 35 | + f3: Dict[str, str] |
34 | 36 |
|
35 | | -```bash |
36 | | -cd python |
37 | | -pytest -v -s . |
| 37 | +fory = pyfory.Fory(ref_tracking=True) |
| 38 | +fory.register_type(SomeClass, typename="example.SomeClass") |
| 39 | +obj = SomeClass() |
| 40 | +obj.f2 = {"k1": "v1", "k2": "v2"} |
| 41 | +obj.f1, obj.f3 = obj, obj.f2 |
| 42 | +data = fory.serialize(obj) |
| 43 | +# bytes can be data serialized by other languages. |
| 44 | +print(fory.deserialize(data)) |
38 | 45 | ``` |
39 | 46 |
|
40 | | -## Code Style |
| 47 | +### Cross-language Serialization |
41 | 48 |
|
42 | | -```bash |
43 | | -cd python |
44 | | -pip install ruff |
45 | | -ruff format python |
46 | | -``` |
| 49 | +Fory excels at cross-language serialization. You can serialize data in Python and deserialize it in another language like Java or Go, and vice-versa. |
47 | 50 |
|
48 | | -## Debug |
| 51 | +Here's an example of how to serialize an object in Python and deserialize it in Java: |
49 | 52 |
|
50 | | -```bash |
51 | | -cd python |
52 | | -python setup.py develop |
53 | | -``` |
| 53 | +**Python** |
54 | 54 |
|
55 | | -- Use `cython --cplus -a pyfory/_serialization.pyx` to produce an annotated HTML file of the source code. Then you can |
56 | | - analyze interaction between Python objects and Python's C API. |
57 | | -- Read more: <https://cython.readthedocs.io/en/latest/src/userguide/debugging.html> |
| 55 | +```python |
| 56 | +from typing import Dict |
| 57 | +import pyfory |
58 | 58 |
|
59 | | -```bash |
60 | | -FORY_DEBUG=true python setup.py build_ext --inplace |
61 | | -# For linux |
62 | | -cygdb build |
63 | | -``` |
| 59 | +class SomeClass: |
| 60 | + f1: "SomeClass" |
| 61 | + f2: Dict[str, str] |
| 62 | + f3: Dict[str, str] |
64 | 63 |
|
65 | | -## Debug with lldb |
| 64 | +fory = pyfory.Fory(ref_tracking=True) |
| 65 | +fory.register_type(SomeClass, typename="example.SomeClass") |
| 66 | +obj = SomeClass() |
| 67 | +obj.f2 = {"k1": "v1", "k2": "v2"} |
| 68 | +obj.f1, obj.f3 = obj, obj.f2 |
| 69 | +data = fory.serialize(obj) |
| 70 | +# `data` can now be sent to a Java application |
| 71 | +``` |
66 | 72 |
|
67 | | -```bash |
68 | | -lldb |
69 | | -(lldb) target create -- python |
70 | | -(lldb) settings set -- target.run-args "-c" "from pyfory.tests.test_serializer import test_enum; test_enum()" |
71 | | -(lldb) run |
72 | | -(lldb) bt |
| 73 | +**Java** |
| 74 | + |
| 75 | +```java |
| 76 | +import org.apache.fory.*; |
| 77 | +import org.apache.fory.config.*; |
| 78 | +import java.util.*; |
| 79 | + |
| 80 | +public class ReferenceExample { |
| 81 | + public static class SomeClass { |
| 82 | + SomeClass f1; |
| 83 | + Map<String, String> f2; |
| 84 | + Map<String, String> f3; |
| 85 | + } |
| 86 | + |
| 87 | + public static void main(String[] args) { |
| 88 | + Fory fory = Fory.builder().withLanguage(Language.XLANG) |
| 89 | + .withRefTracking(true).build(); |
| 90 | + fory.register(SomeClass.class, "example.SomeClass"); |
| 91 | + // `bytes` would be the data received from the Python application |
| 92 | + byte[] bytes = ... |
| 93 | + System.out.println(fory.deserialize(bytes)); |
| 94 | + } |
| 95 | +} |
73 | 96 | ``` |
| 97 | + |
| 98 | +## Useful Links |
| 99 | + |
| 100 | +- **[Project Website](https://fory.apache.org)** |
| 101 | +- **[Documentation](https://fory.apache.org/docs/latest/python_guide/)** |
| 102 | +- **[GitHub Repository](https://github.com/apache/fory)** |
| 103 | +- **[Issue Tracker](https://github.com/apache/fory/issues)** |
| 104 | +- **[Slack Channel](https://join.slack.com/t/fory-project/shared_invite/zt-36g0qouzm-kcQSvV_dtfbtBKHRwT5gsw)** |
0 commit comments