|
1 | 1 | # Python Generator for Zserio
|
2 | 2 |
|
3 |
| -Zserio extension which generates Python serialization API from the Zserio schema. |
| 3 | +Zserio extension generates Python [serialization API](#serialization-api) from the Zserio schema together |
| 4 | +with [additional API](#additional-api). |
| 5 | + |
| 6 | +The generated code must be always used with |
| 7 | +[Python Runtime Library](https://zserio.org/doc/runtime/latest/python) which provides functionality common for |
| 8 | +all generated code. |
4 | 9 |
|
5 | 10 | For a **quick start** see the [Python Tutorial](https://github.com/ndsev/zserio-tutorial-python#zserio-python-quick-start-tutorial).
|
6 | 11 |
|
7 |
| -For an **API documentation** see the [Python Runtime Library](https://zserio.org/doc/runtime/latest/python). |
| 12 | +## Content |
| 13 | + |
| 14 | +[Supported Python Versions](#supported-python-versions) |
| 15 | + |
| 16 | +[Serialization API](#serialization-api) |
| 17 | + |
| 18 | + [Auto-generated API helper](#auto-generated-api-helper) |
| 19 | + |
| 20 | + [PEP-8 compliant API](#pep-8-compliant-api) |
| 21 | + |
| 22 | + [Possible clashing](#possible-clashing) |
| 23 | + |
| 24 | +[Additional API](#additional-api) |
| 25 | + |
| 26 | + [Range Check](#range-check) |
8 | 27 |
|
9 |
| -## Supported Python Version |
| 28 | + [Code Comments](#code-comments) |
10 | 29 |
|
11 |
| -Zserio Python generator supports the Python 3.8, 3.9, 3.10 and 3.11. |
| 30 | + [Type Information](#type-information) |
| 31 | + |
| 32 | + [JSON Debug String](#json-debug-string) |
| 33 | + |
| 34 | +[Compatibility Check](#compatibility-check) |
| 35 | + |
| 36 | +## Supported Python Versions |
| 37 | + |
| 38 | +Zserio Python generator supports the Python 3.8, 3.9, 3.10, 3.11 and 3.12. |
12 | 39 |
|
13 | 40 | Although newer Python versions are not tested, they should work as well as long as they are backward compatible.
|
14 | 41 |
|
15 |
| -## Auto-generated API helper |
| 42 | +## Serialization API |
| 43 | + |
| 44 | +The serialization API provides the following features for all Zserio structures, choices and unions: |
| 45 | + |
| 46 | +- Serialization of all Zserio objects to the bit stream |
| 47 | + (method [`zserio.serialization.serialize()`](https://zserio.org/doc/runtime/latest/python/zserio.serialization.html#module-zserio.serialization)). |
| 48 | +- Deserialization of all Zserio objects from the bit stream |
| 49 | + (method [`zserio.serialization.deserialize()`](https://zserio.org/doc/runtime/latest/python/zserio.serialization.html#module-zserio.serialization)). |
| 50 | +- Properties for all fields to get and set values |
| 51 | +- Method `bitsizeof()` which calculates a number of bits needed for serialization of the Zserio object. |
| 52 | +- Method `__eq__()` which compares two Zserio objects field by field. |
| 53 | +- Method `__hash__()` which calculates a hash code of the Zserio object. |
| 54 | + |
| 55 | +### Auto-generated API helper |
16 | 56 |
|
17 | 57 | Python Generator generates each package symbol in its own Python module (i.e. file). It's necessary in order
|
18 | 58 | to prevent problems with Python symbols dependencies. Zserio allows dependencies which are defined after the
|
@@ -40,42 +80,87 @@ testStructure = my_package.sub_package.TestStructure()
|
40 | 80 | testUnion = my_package.other_package.TestUnion()
|
41 | 81 | ```
|
42 | 82 |
|
43 |
| -## PEP-8 compliant API |
| 83 | +### PEP-8 compliant API |
44 | 84 |
|
45 | 85 | Python generator generates the serialization API according to the
|
46 | 86 | [PEP-8](https://www.python.org/dev/peps/pep-0008/). It means that the generator must rename symbols defined
|
47 | 87 | in the Zserio schema to conform PEP-8 style guide. However such renaming can introduce symbol clashing which
|
48 | 88 | could not be detected by the Zserio core. Therefore the Python generator must do its own checks and can cause
|
49 | 89 | generation failure in case that it detects some clashing.
|
50 | 90 |
|
51 |
| -### Possible clashing |
| 91 | +#### Possible clashing |
52 | 92 |
|
53 | 93 | 1. Names of generated files for package symbols (structures, choices, constants, etc.) -
|
54 | 94 | i.e. Python module names:
|
55 |
| - * may clash with names of generated Python packages defined in the Zserio schema which causes problems to |
56 |
| - Python import system which cannot distinguish easily between module and package with the same name |
57 |
| - * may clash with other modules generated for another package symbols (both `SomeStruct` and `Some_Struct` |
58 |
| - are generated like `some_struct.py` module) |
59 |
| - * may clash with auto-generated `api.py` |
| 95 | + - may clash with names of generated Python packages defined in the Zserio schema which causes problems to |
| 96 | + Python import system which cannot distinguish easily between module and package with the same name |
| 97 | + - may clash with other modules generated for another package symbols (both `SomeStruct` and `Some_Struct` |
| 98 | + are generated like `some_struct.py` module) |
| 99 | + - may clash with auto-generated `api.py` |
60 | 100 |
|
61 | 101 | 2. Names of symbols in a scope (field names, enum item names, function names, etc.):
|
62 |
| - * may clash between each other (both `some_field` and `someField` are generated like `some_field` Python |
63 |
| - property) |
64 |
| - * may clash with some of the generated API methods - e.g. `read`, `write`, `bitsizeof`, etc. |
65 |
| - * may clash with some private member or reserved symbol - for simplicity, Python generator forbids |
| 102 | + - may clash between each other (both `some_field` and `someField` are generated like `some_field` Python |
| 103 | + property) |
| 104 | + - may clash with some of the generated API methods - e.g. `read`, `write`, `bitsizeof`, etc. |
| 105 | + - may clash with some private member or reserved symbol - for simplicity, Python generator forbids |
66 | 106 | identifiers starting with underscore (`_`)
|
67 | 107 |
|
68 | 108 | 3. Top level package name
|
69 |
| - * top level package name should be carefully chosen since Python import system is not much robust - |
70 |
| - e.g. all the packages and modules which are imported during Python startup are set in `sys.modules` and |
71 |
| - it isn't possible to import another package with the same name, at least not using classic import syntax |
72 |
| - * since it's not clear which packages/modules are imported during Python startup |
73 |
| - (and moreover the list probably isn't fixed across Python versions), the Python Generator doesn't try |
74 |
| - to detect such clashes and it's up to user to choose a safe name |
75 |
| - * in case that the top level package name cannot be change, user can use `-setTopLevelPackage` Zserio option |
76 |
| - which allows to set an additional top level package |
77 |
| - * Python Generator only checks for possible clashes with packages/modules which the generated serialization |
78 |
| - API uses, which is currently only `zserio` and `typing` |
| 109 | + - top level package name should be carefully chosen since Python import system is not much robust - |
| 110 | + e.g. all the packages and modules which are imported during Python startup are set in `sys.modules` and |
| 111 | + it isn't possible to import another package with the same name, at least not using classic import syntax |
| 112 | + - since it's not clear which packages/modules are imported during Python startup |
| 113 | + (and moreover the list probably isn't fixed across Python versions), the Python Generator doesn't try |
| 114 | + to detect such clashes and it's up to user to choose a safe name |
| 115 | + - in case that the top level package name cannot be change, user can use `-setTopLevelPackage` Zserio option |
| 116 | + which allows to set an additional top level package |
| 117 | + - Python Generator only checks for possible clashes with packages/modules which the generated serialization |
| 118 | + API uses, which is currently only `zserio` and `typing` |
| 119 | + |
| 120 | +## Additional API |
| 121 | + |
| 122 | +The following additional API features which are disabled by default, are available for users: |
| 123 | + |
| 124 | +- [Range Check](#range-check) - Generation of code for the range checking for fields and parameters (integer types only). |
| 125 | +- [Code Comments](#code-comments) - Generation of Javadoc comments in code. |
| 126 | +- [Type Information](#type-information) - Generation of static information about Zserio objects like schema names, types, etc. |
| 127 | +- [JSON Debug String](#json-debug-string) - Supports export/import of all Zserio objects to/from the JSON file. |
| 128 | + |
| 129 | +All of these features can be enabled using command line options which are described in the |
| 130 | +[Zserio User Guide](../../../doc/ZserioUserGuide.md#zserio-command-line-interface) document. |
| 131 | + |
| 132 | +### Range Check |
| 133 | + |
| 134 | +Because not all Zserio integer types can be directly mapped to the Python types (e.g. `bit:4` is mapped to |
| 135 | +`int`), it can be helpful to explicitly check values stored in Java types for the correct ranges |
| 136 | +(e.g to check if `int` value which holds `bit:4`, is from range `<0, 15>`). Such explicit checks allow |
| 137 | +throwing exception with the detailed description of the Zserio field with wrong value. |
| 138 | + |
| 139 | +The range check code is generated only in the `write()` method directly before the field is written to the |
| 140 | +bit stream. |
| 141 | + |
| 142 | +### Code Comments |
| 143 | + |
| 144 | +The code comments generate Sphinx comments for all generated Zserio objects. Some comments available |
| 145 | +in Zserio schema are used as well. |
| 146 | + |
| 147 | +### Type Information |
| 148 | + |
| 149 | +The type information generates static method `type_info()` in all generated Zserio types (except of Zserio |
| 150 | +subtypes). This method returns all static information of Zserio type which is available in the Zserio schema |
| 151 | +(e.g. schema name, if field is optional, if field is array, etc...). |
| 152 | + |
| 153 | +### JSON Debug String |
| 154 | + |
| 155 | +JSON debug string feature provides export and import to/from JSON string for all Zserio structures, |
| 156 | +choices and unions: |
| 157 | + |
| 158 | +- Export to the JSON string |
| 159 | + (method [`zserio.debugstring.to_json_string()`](https://zserio.org/doc/runtime/latest/python/zserio.debugstring.html#module-zserio.debugstring)). |
| 160 | +- Import from the JSON string |
| 161 | + (method [`zserio.debugstring.from_json_string()`](https://zserio.org/doc/runtime/latest/python/zserio.debugstring.html#module-zserio.debugstring)). |
| 162 | + |
| 163 | +> Note that this feature is available only if type information is enabled! |
79 | 164 |
|
80 | 165 | ## Compatibility check
|
81 | 166 |
|
|
0 commit comments