Browse Source

Struct

pull/36/head
Jure Šorn 5 years ago
parent
commit
1d75ae3697
2 changed files with 17 additions and 10 deletions
  1. 13
      README.md
  2. 14
      index.html

13
README.md

@ -1246,6 +1246,7 @@ print(<el_1>, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
```
* **Use `'file=sys.stderr'` for errors.**
* **Use `'flush=True'` to forcibly flush the stream.**
### Pretty Print
```python
@ -1592,12 +1593,12 @@ def write_bytes(filename, bytes_obj):
Struct
------
* **Module that performs conversions between Python values and a C struct, represented as a Python bytes object.**
* **Module that performs conversions between a sequence of numbers and a C struct, represented as a Python bytes object.**
* **Machine’s native type sizes and byte order are used by default.**
```python
from struct import pack, unpack, iter_unpack, calcsize
<bytes> = pack('<format>', <value_1> [, <value_2>, ...])
<bytes> = pack('<format>', <num_1> [, <num_2>, ...])
<tuple> = unpack('<format>', <bytes>)
<tuples> = iter_unpack('<format>', <bytes>)
```
@ -1618,20 +1619,22 @@ b'\x00\x01\x00\x02\x00\x00\x00\x03'
* **`'<'` - little-endian**
* **`'>'` - big-endian**
#### Use capital letter for unsigned type. Standard sizes are in brackets:
#### Integer types. Use capital letter for unsigned type. Standard sizes are in brackets:
* **`'x'` - pad byte**
* **`'b'` - char (1)**
* **`'h'` - short (2)**
* **`'i'` - int (4)**
* **`'l'` - long (4)**
* **`'q'` - long long (8)**
#### Floating point types:
* **`'f'` - float (4)**
* **`'d'` - double (8)**
Array
-----
**List that can hold only elements of predefined type. Available types and their sizes are listed above.**
**List that can only hold numbers of predefined type. Available types and their sizes in bytes are listed above.**
```python
from array import array
@ -1671,7 +1674,7 @@ from collections import deque
[2, 3, 4]
>>> a.appendleft(5)
[5, 2, 3]
>>> a.insert(6, 1)
>>> a.insert(1, 6)
IndexError: deque already at its maximum size
```

14
index.html

@ -1139,6 +1139,7 @@ KeyboardInterrupt
</code></pre>
<ul>
<li><strong>Use <code class="python hljs"><span class="hljs-string">'file=sys.stderr'</span></code> for errors.</strong></li>
<li><strong>Use <code class="python hljs"><span class="hljs-string">'flush=True'</span></code> to forcibly flush the stream.</strong></li>
</ul>
<h3 id="prettyprint">Pretty Print</h3>
<pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> pprint <span class="hljs-keyword">import</span> pprint
@ -1395,11 +1396,11 @@ cursor.execute(<span class="hljs-string">'&lt;query&gt;'</span>, &lt;dict/namedt
</code></pre>
<h2 id="struct"><a href="#struct" name="struct">#</a>Struct</h2>
<ul>
<li><strong>Module that performs conversions between Python values and a C struct, represented as a Python bytes object.</strong></li>
<li><strong>Module that performs conversions between a sequence of numbers and a C struct, represented as a Python bytes object.</strong></li>
<li><strong>Machine’s native type sizes and byte order are used by default.</strong></li>
</ul>
<pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> struct <span class="hljs-keyword">import</span> pack, unpack, iter_unpack, calcsize
&lt;bytes&gt; = pack(<span class="hljs-string">'&lt;format&gt;'</span>, &lt;value_1&gt; [, &lt;value_2&gt;, ...])
&lt;bytes&gt; = pack(<span class="hljs-string">'&lt;format&gt;'</span>, &lt;num_1&gt; [, &lt;num_2&gt;, ...])
&lt;tuple&gt; = unpack(<span class="hljs-string">'&lt;format&gt;'</span>, &lt;bytes&gt;)
&lt;tuples&gt; = iter_unpack(<span class="hljs-string">'&lt;format&gt;'</span>, &lt;bytes&gt;)
</code></pre>
@ -1418,7 +1419,7 @@ cursor.execute(<span class="hljs-string">'&lt;query&gt;'</span>, &lt;dict/namedt
<li><strong><code class="python hljs"><span class="hljs-string">'&lt;'</span></code> - little-endian</strong></li>
<li><strong><code class="python hljs"><span class="hljs-string">'&gt;'</span></code> - big-endian</strong></li>
</ul>
<h4 id="usecapitalletterforunsignedtypestandardsizesareinbrackets">Use capital letter for unsigned type. Standard sizes are in brackets:</h4>
<h4 id="integertypesusecapitalletterforunsignedtypestandardsizesareinbrackets">Integer types. Use capital letter for unsigned type. Standard sizes are in brackets:</h4>
<ul>
<li><strong><code class="python hljs"><span class="hljs-string">'x'</span></code> - pad byte</strong></li>
<li><strong><code class="python hljs"><span class="hljs-string">'b'</span></code> - char (1)</strong></li>
@ -1426,11 +1427,14 @@ cursor.execute(<span class="hljs-string">'&lt;query&gt;'</span>, &lt;dict/namedt
<li><strong><code class="python hljs"><span class="hljs-string">'i'</span></code> - int (4)</strong></li>
<li><strong><code class="python hljs"><span class="hljs-string">'l'</span></code> - long (4)</strong></li>
<li><strong><code class="python hljs"><span class="hljs-string">'q'</span></code> - long long (8)</strong></li>
</ul>
<h4 id="floatingpointtypes">Floating point types:</h4>
<ul>
<li><strong><code class="python hljs"><span class="hljs-string">'f'</span></code> - float (4)</strong></li>
<li><strong><code class="python hljs"><span class="hljs-string">'d'</span></code> - double (8)</strong></li>
</ul>
<h2 id="array"><a href="#array" name="array">#</a>Array</h2>
<p><strong>List that can hold only elements of predefined type. Available types and their sizes are listed above.</strong></p>
<p><strong>List that can only hold numbers of predefined type. Available types and their sizes in bytes are listed above.</strong></p>
<pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> array <span class="hljs-keyword">import</span> array
&lt;array&gt; = array(<span class="hljs-string">'&lt;typecode&gt;'</span> [, &lt;collection&gt;])
</code></pre>
@ -1454,7 +1458,7 @@ cursor.execute(<span class="hljs-string">'&lt;query&gt;'</span>, &lt;dict/namedt
[<span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>]
<span class="hljs-meta">&gt;&gt;&gt; </span>a.appendleft(<span class="hljs-number">5</span>)
[<span class="hljs-number">5</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]
<span class="hljs-meta">&gt;&gt;&gt; </span>a.insert(<span class="hljs-number">6</span>, <span class="hljs-number">1</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>a.insert(<span class="hljs-number">1</span>, <span class="hljs-number">6</span>)
IndexError: deque already at its maximum size
</code></pre>
<h2 id="threading"><a href="#threading" name="threading">#</a>Threading</h2>

Loading…
Cancel
Save