<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://debianws.lexgopc.com/wiki143/index.php?action=history&amp;feed=atom&amp;title=Construct_%28Python_library%29</id>
	<title>Construct (Python library) - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://debianws.lexgopc.com/wiki143/index.php?action=history&amp;feed=atom&amp;title=Construct_%28Python_library%29"/>
	<link rel="alternate" type="text/html" href="http://debianws.lexgopc.com/wiki143/index.php?title=Construct_(Python_library)&amp;action=history"/>
	<updated>2026-05-05T21:02:04Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.1</generator>
	<entry>
		<id>http://debianws.lexgopc.com/wiki143/index.php?title=Construct_(Python_library)&amp;diff=3974076&amp;oldid=prev</id>
		<title>imported&gt;Nagaralage at 02:59, 22 November 2024</title>
		<link rel="alternate" type="text/html" href="http://debianws.lexgopc.com/wiki143/index.php?title=Construct_(Python_library)&amp;diff=3974076&amp;oldid=prev"/>
		<updated>2024-11-22T02:59:11Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&amp;#039;&amp;#039;&amp;#039;Construct&amp;#039;&amp;#039;&amp;#039; is a [[Python (programming_language)|Python]] library for the construction and deconstruction of [[data structures]] in a [[Declarative programming|declarative]] fashion. In this context, construction, or building, refers to the process of converting ([[Serialization|serializing]]) a programmatic object into a binary representation. &lt;br /&gt;
&lt;br /&gt;
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 [[Endianness|byte-ordering]].&lt;br /&gt;
&lt;br /&gt;
==Benefits==&lt;br /&gt;
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.{{cn|date=August 2023}} If one is familiar with the [[C (programming language)]], one can think of constructs as [[Type conversion|casting]] from &amp;lt;code&amp;gt;char *&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;struct foo *&amp;lt;/code&amp;gt; and vice versa, rather than writing code that unpacks the data.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
The 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.&lt;br /&gt;
&lt;br /&gt;
First, the [[Ethernet]] [[Ethernet_frame#Header|header]] (layer 2):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
 ethernet = Struct(&lt;br /&gt;
    &amp;quot;destination&amp;quot; / Bytes(6),&lt;br /&gt;
    &amp;quot;source&amp;quot; / Bytes(6),&lt;br /&gt;
    &amp;quot;type&amp;quot; / Enum(Int16ub,&lt;br /&gt;
        IPv4=0x0800,&lt;br /&gt;
        ARP=0x0806,&lt;br /&gt;
        RARP=0x8035,&lt;br /&gt;
        X25=0x0805,&lt;br /&gt;
        IPX=0x8137,&lt;br /&gt;
        IPv6=0x86DD,&lt;br /&gt;
    ),&lt;br /&gt;
 )&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next, the [[Internet_Protocol|IP]] header (layer 3):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
 ip = Struct(&lt;br /&gt;
    &amp;quot;header&amp;quot; / BitStruct(&lt;br /&gt;
        &amp;quot;version&amp;quot; / Const(Nibble, 4),&lt;br /&gt;
        &amp;quot;header_length&amp;quot; / Nibble,&lt;br /&gt;
    ),&lt;br /&gt;
    &amp;quot;tos&amp;quot; / BitStruct(&lt;br /&gt;
        &amp;quot;precedence&amp;quot; / Bytes(3),&lt;br /&gt;
        &amp;quot;minimize_delay&amp;quot; / Flag,&lt;br /&gt;
        &amp;quot;high_throuput&amp;quot; / Flag,&lt;br /&gt;
        &amp;quot;high_reliability&amp;quot; / Flag,&lt;br /&gt;
        &amp;quot;minimize_cost&amp;quot; / Flag,&lt;br /&gt;
        Padding(1),&lt;br /&gt;
    ),&lt;br /&gt;
    &amp;quot;total_length&amp;quot; / Int16ub,&lt;br /&gt;
    # ...&lt;br /&gt;
 )&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
And finally, the [[Transmission Control Protocol|TCP]] header (layer 4):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
 tcp = Struct(&lt;br /&gt;
    &amp;quot;source&amp;quot; / Int16ub,&lt;br /&gt;
    &amp;quot;destination&amp;quot; / Int16ub,&lt;br /&gt;
    &amp;quot;seq&amp;quot; / Int32ub,&lt;br /&gt;
    &amp;quot;ack&amp;quot; / Int32ub,&lt;br /&gt;
    # ...&lt;br /&gt;
 )&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now define the hierarchy of the protocol stack. The following code &amp;quot;binds&amp;quot; each pair of adjacent protocols into a separate unit. Each such unit will &amp;quot;select&amp;quot; the proper next layer based on its contained protocol.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
 layer4tcp = Struct(&lt;br /&gt;
    tcp,&lt;br /&gt;
    # ... payload&lt;br /&gt;
 )&lt;br /&gt;
&lt;br /&gt;
 layer3ip = Struct(&lt;br /&gt;
    ip,&lt;br /&gt;
    &amp;quot;next&amp;quot; / Switch(this.protocol,&lt;br /&gt;
        {&lt;br /&gt;
            &amp;quot;TCP&amp;quot; : layer4tcp,&lt;br /&gt;
        }&lt;br /&gt;
    ),&lt;br /&gt;
 )&lt;br /&gt;
&lt;br /&gt;
 layer2ethernet = Struct(&lt;br /&gt;
    ethernet,&lt;br /&gt;
    &amp;quot;next&amp;quot; / Switch(this.type,&lt;br /&gt;
        {&lt;br /&gt;
            &amp;quot;IP&amp;quot; : layer3ip,&lt;br /&gt;
        }&lt;br /&gt;
    ),&lt;br /&gt;
 )&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
At this point, the code can parse captured TCP/IP frames into &amp;quot;packet&amp;quot; objects and build these packet objects back into binary representation.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
 tcpip_stack = layer2ethernet&lt;br /&gt;
 packet = tcpip_stack.parse(b&amp;quot;...raw captured packet...&amp;quot;)&lt;br /&gt;
 raw_data = tcpip_stack.build(packet)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Ports and spin-offs==&lt;br /&gt;
&lt;br /&gt;
===Perl===&lt;br /&gt;
Data::ParseBinary&amp;lt;ref&amp;gt;{{Cite web |last= |first= |title=Data::ParseBinary - Yet Another parser for binary structures - metacpan.org |url=https://metacpan.org/pod/Data::ParseBinary |access-date=2023-08-29 |website=metacpan.org}}&amp;lt;/ref&amp;gt; is a [[CPAN]] module that originated as a port of Construct to the [[Perl|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.&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
A port to Java is available on GitHub.&amp;lt;ref&amp;gt;{{Citation |last=Ziglioli |first=Emanuele |title=ZiglioUK/construct |date=2022-12-29 |url=https://github.com/ZiglioUK/construct |access-date=2023-08-29}}&amp;lt;/ref&amp;gt; Examples in Java, the [[Ethernet]] header (layer 2):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
 Construct ethernet_header = Struct(&amp;quot;ethernet_header&amp;quot;,&lt;br /&gt;
      MacAddress(&amp;quot;destination&amp;quot;),&lt;br /&gt;
      MacAddress(&amp;quot;source&amp;quot;),&lt;br /&gt;
      Enum(UBInt16(&amp;quot;type&amp;quot;),&lt;br /&gt;
          &amp;quot;IPv4&amp;quot;,  0x0800,&lt;br /&gt;
          &amp;quot;ARP&amp;quot;,   0x0806,&lt;br /&gt;
          &amp;quot;RARP&amp;quot;,  0x8035,&lt;br /&gt;
          &amp;quot;X25&amp;quot;,   0x0805,&lt;br /&gt;
          &amp;quot;IPX&amp;quot;,   0x8137,&lt;br /&gt;
          &amp;quot;IPv6&amp;quot;,  0x86DD,&lt;br /&gt;
          &amp;quot;_default_&amp;quot;,  Pass&lt;br /&gt;
   ));&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
* [[Natural Language Toolkit]]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==External links==&lt;br /&gt;
*[https://construct.readthedocs.org/en/latest/ Construct&amp;#039;s Documentation]&lt;br /&gt;
*[https://github.com/construct/construct Construct&amp;#039;s Repository]&lt;br /&gt;
*[https://datasciencepartners.nl/ Python Training]&lt;br /&gt;
&lt;br /&gt;
[[Category:Python (programming language) libraries]]&lt;br /&gt;
[[Category:Parser generators]]&lt;/div&gt;</summary>
		<author><name>imported&gt;Nagaralage</name></author>
	</entry>
</feed>