词条 | Construct (python library) |
释义 |
Construct is a python library for the construction and deconstruction of data structures in a declarative fashion. In this context, construction, or building, refers to the process of converting (serializing) a programmatic object into a binary representation. Deconstruction, or parsing, refers to the opposite process of converting (deserializing) binary data into a programmatic object. Being declarative means that user code defines the data structure, instead of the convention of writing procedural code to accomplish the goal. Construct can work seamlessly with bit- and byte-level data granularity and various byte-ordering. Using declarative code has many benefits. For example, the same code that can parse can also build (symmetrical), debugging and testing are much simpler (provable to some extent), creating new constructs is easy (wrapping components), and many more. If one is familiar with the C (programming language), one can think of constructs as casting from ExampleThe following example show how a TCP/IP protocol stack might be defined using Construct; some code is omitted for brevity and simplicity. Also note that the following code is just python code that creates objects. First, the ethernet header (layer 2): Next, the IP header (layer 3): And finally, the TCP header (layer 4): Now define the hierarchy of the protocol stack. The following code "binds" each pair of adjacent protocols into a separate unit. Each such unit will "select" the proper next layer based on its contained protocol. At this point, the code can parse captured TCP/IP frames into "packet" objects and build these packet objects back into binary representation. Ports and spin-offsPerl[https://metacpan.org/module/Data::ParseBinary Data::ParseBinary] is a CPAN module that originated as a port of Construct to the Perl programming language. (see [https://metacpan.org/module/Data::ParseBinary its main POD document] for its inspiration). Since the initial version, some parts of the original API have been deprecated. JavaA port to Java is available on [https://github.com/ZiglioUK/construct GitHub]. Examples in Java, the ethernet header (layer 2): External links
2 : Python libraries|Parser generators |
随便看 |
|
开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。