[Python-modules-commits] [bitstruct] 01/12: import bitstruct_2.0.2.orig.tar.gz

Brian May bam at moszumanska.debian.org
Sat Mar 26 04:40:10 UTC 2016


This is an automated email from the git hooks/post-receive script.

bam pushed a commit to branch master
in repository bitstruct.

commit 7809db74eaef6a7b5fe0dbd142d7a4e678abefaa
Author: Brian May <brian at linuxpenguins.xyz>
Date:   Thu Mar 24 13:29:18 2016 +1100

    import bitstruct_2.0.2.orig.tar.gz
---
 PKG-INFO                                | 105 ++++++++++++++++
 README.rst                              |  91 ++++++++++++++
 bitstruct.egg-info/PKG-INFO             | 105 ++++++++++++++++
 bitstruct.egg-info/SOURCES.txt          |   7 ++
 bitstruct.egg-info/dependency_links.txt |   1 +
 bitstruct.egg-info/top_level.txt        |   1 +
 bitstruct.py                            | 205 ++++++++++++++++++++++++++++++++
 setup.cfg                               |   5 +
 setup.py                                |  22 ++++
 9 files changed, 542 insertions(+)

diff --git a/PKG-INFO b/PKG-INFO
new file mode 100644
index 0000000..3b40873
--- /dev/null
+++ b/PKG-INFO
@@ -0,0 +1,105 @@
+Metadata-Version: 1.1
+Name: bitstruct
+Version: 2.0.2
+Summary: This module performs conversions between Python values and C bit field structs represented as Python bytearrays.
+Home-page: https://github.com/eerimoq/bitstruct
+Author: Erik Moqvist, Ilya Petukhov
+Author-email: erik.moqvist at gmail.com
+License: MIT
+Description: |buildstatus|_
+        
+        About
+        =====
+        
+        This module is intended to have a similar interface as the python
+        struct module, but working on bits instead of primitive data types
+        (char, int, ...).
+        
+        Documentation: http://bitstruct.readthedocs.org/en/latest
+        
+        
+        Installation
+        ============
+        
+        .. code-block:: python
+        
+            pip install bitstruct
+        
+        
+        Example usage
+        =============
+        
+        See the test suite: https://github.com/eerimoq/bitstruct/blob/master/tests/test_bitstruct.py
+        
+        A basic example of packing/unpacking four integers:
+        
+        .. code-block:: python
+        
+            >>> from bitstruct import *
+            >>> pack('u1u3u4s16', 1, 2, 3, -4)
+            bytearray(b'\xa3\xff\xfc')
+            >>> unpack('u1u3u4s16', bytearray(b'\xa3\xff\xfc'))
+            (1, 2, 3, -4)
+            >>> calcsize('u1u3u4s16')
+            24
+        
+        The unpacked fields can be named by assigning them to variables or by
+        wrapping the result in a named tuple:
+        
+        .. code-block:: python
+        
+            >>> from bitstruct import *
+            >>> from collections import namedtuple
+            >>> MyName = namedtuple('myname', [ 'a', 'b', 'c', 'd' ])
+            >>> unpacked = unpack('u1u3u4s16', bytearray(b'\xa3\xff\xfc'))
+            >>> myname = MyName(*unpacked)
+            >>> myname
+            myname(a=1, b=2, c=3, d=-4)
+            >>> myname.c
+            3
+        
+        An example of packing/unpacking a unsinged integer, a signed integer,
+        a float, a boolean and a bytearray:
+        
+        .. code-block:: python
+        
+            >>> from bitstruct import *
+            >>> pack('u5s5f32b1r13', 1, -1, 3.75, True, bytearray(b'\xff\xff'))
+            bytearray(b'\x0f\xd0\x1c\x00\x00?\xff')
+            >>> unpack('u5s5f32b1r13', bytearray(b'\x0f\xd0\x1c\x00\x00?\xff'))
+            (1, -1, 3.75, True, bytearray(b'\xff\xf8'))
+            >>> calcsize('u5s5f32b1r13')
+            56
+        
+        An example of unpacking values from a hexstring and a binary file:
+        
+        .. code-block:: python
+        
+            >>> from bitstruct import *
+            >>> from binascii import *
+            >>> unpack('s17s13r24', bytearray(unhexlify('0123456789abcdef')))
+            (582, -3751, bytearray(b'\xe2j\xf3'))
+            >>> with open("test.bin", "rb") as fin:
+            ...     unpack('s17s13r24', bytearray(fin.read(8)))
+            ...     
+            ... 
+            (582, -3751, bytearray(b'\xe2j\xf3'))
+        
+        Change endianness of the data with byteswap(), and then unpack the
+        values:
+        
+        .. code-block:: python
+        
+            >>> from bitstruct import *
+            >>> packed = pack('u1u3u4s16', 1, 2, 3, 1)
+            >>> unpack('u1u3u4s16', byteswap('12', packed))
+            (1, 2, 3, 256)
+        
+        .. |buildstatus| image:: https://travis-ci.org/eerimoq/bitstruct.svg
+        .. _buildstatus: https://travis-ci.org/eerimoq/bitstruct
+        
+Keywords: bit field,bit parsing,bit unpack,bit pack
+Platform: UNKNOWN
+Classifier: License :: OSI Approved :: MIT License
+Classifier: Programming Language :: Python :: 2
+Classifier: Programming Language :: Python :: 3
diff --git a/README.rst b/README.rst
new file mode 100644
index 0000000..7194e4b
--- /dev/null
+++ b/README.rst
@@ -0,0 +1,91 @@
+|buildstatus|_
+
+About
+=====
+
+This module is intended to have a similar interface as the python
+struct module, but working on bits instead of primitive data types
+(char, int, ...).
+
+Documentation: http://bitstruct.readthedocs.org/en/latest
+
+
+Installation
+============
+
+.. code-block:: python
+
+    pip install bitstruct
+
+
+Example usage
+=============
+
+See the test suite: https://github.com/eerimoq/bitstruct/blob/master/tests/test_bitstruct.py
+
+A basic example of packing/unpacking four integers:
+
+.. code-block:: python
+
+    >>> from bitstruct import *
+    >>> pack('u1u3u4s16', 1, 2, 3, -4)
+    bytearray(b'\xa3\xff\xfc')
+    >>> unpack('u1u3u4s16', bytearray(b'\xa3\xff\xfc'))
+    (1, 2, 3, -4)
+    >>> calcsize('u1u3u4s16')
+    24
+
+The unpacked fields can be named by assigning them to variables or by
+wrapping the result in a named tuple:
+
+.. code-block:: python
+
+    >>> from bitstruct import *
+    >>> from collections import namedtuple
+    >>> MyName = namedtuple('myname', [ 'a', 'b', 'c', 'd' ])
+    >>> unpacked = unpack('u1u3u4s16', bytearray(b'\xa3\xff\xfc'))
+    >>> myname = MyName(*unpacked)
+    >>> myname
+    myname(a=1, b=2, c=3, d=-4)
+    >>> myname.c
+    3
+
+An example of packing/unpacking a unsinged integer, a signed integer,
+a float, a boolean and a bytearray:
+
+.. code-block:: python
+
+    >>> from bitstruct import *
+    >>> pack('u5s5f32b1r13', 1, -1, 3.75, True, bytearray(b'\xff\xff'))
+    bytearray(b'\x0f\xd0\x1c\x00\x00?\xff')
+    >>> unpack('u5s5f32b1r13', bytearray(b'\x0f\xd0\x1c\x00\x00?\xff'))
+    (1, -1, 3.75, True, bytearray(b'\xff\xf8'))
+    >>> calcsize('u5s5f32b1r13')
+    56
+
+An example of unpacking values from a hexstring and a binary file:
+
+.. code-block:: python
+
+    >>> from bitstruct import *
+    >>> from binascii import *
+    >>> unpack('s17s13r24', bytearray(unhexlify('0123456789abcdef')))
+    (582, -3751, bytearray(b'\xe2j\xf3'))
+    >>> with open("test.bin", "rb") as fin:
+    ...     unpack('s17s13r24', bytearray(fin.read(8)))
+    ...     
+    ... 
+    (582, -3751, bytearray(b'\xe2j\xf3'))
+
+Change endianness of the data with byteswap(), and then unpack the
+values:
+
+.. code-block:: python
+
+    >>> from bitstruct import *
+    >>> packed = pack('u1u3u4s16', 1, 2, 3, 1)
+    >>> unpack('u1u3u4s16', byteswap('12', packed))
+    (1, 2, 3, 256)
+
+.. |buildstatus| image:: https://travis-ci.org/eerimoq/bitstruct.svg
+.. _buildstatus: https://travis-ci.org/eerimoq/bitstruct
diff --git a/bitstruct.egg-info/PKG-INFO b/bitstruct.egg-info/PKG-INFO
new file mode 100644
index 0000000..3b40873
--- /dev/null
+++ b/bitstruct.egg-info/PKG-INFO
@@ -0,0 +1,105 @@
+Metadata-Version: 1.1
+Name: bitstruct
+Version: 2.0.2
+Summary: This module performs conversions between Python values and C bit field structs represented as Python bytearrays.
+Home-page: https://github.com/eerimoq/bitstruct
+Author: Erik Moqvist, Ilya Petukhov
+Author-email: erik.moqvist at gmail.com
+License: MIT
+Description: |buildstatus|_
+        
+        About
+        =====
+        
+        This module is intended to have a similar interface as the python
+        struct module, but working on bits instead of primitive data types
+        (char, int, ...).
+        
+        Documentation: http://bitstruct.readthedocs.org/en/latest
+        
+        
+        Installation
+        ============
+        
+        .. code-block:: python
+        
+            pip install bitstruct
+        
+        
+        Example usage
+        =============
+        
+        See the test suite: https://github.com/eerimoq/bitstruct/blob/master/tests/test_bitstruct.py
+        
+        A basic example of packing/unpacking four integers:
+        
+        .. code-block:: python
+        
+            >>> from bitstruct import *
+            >>> pack('u1u3u4s16', 1, 2, 3, -4)
+            bytearray(b'\xa3\xff\xfc')
+            >>> unpack('u1u3u4s16', bytearray(b'\xa3\xff\xfc'))
+            (1, 2, 3, -4)
+            >>> calcsize('u1u3u4s16')
+            24
+        
+        The unpacked fields can be named by assigning them to variables or by
+        wrapping the result in a named tuple:
+        
+        .. code-block:: python
+        
+            >>> from bitstruct import *
+            >>> from collections import namedtuple
+            >>> MyName = namedtuple('myname', [ 'a', 'b', 'c', 'd' ])
+            >>> unpacked = unpack('u1u3u4s16', bytearray(b'\xa3\xff\xfc'))
+            >>> myname = MyName(*unpacked)
+            >>> myname
+            myname(a=1, b=2, c=3, d=-4)
+            >>> myname.c
+            3
+        
+        An example of packing/unpacking a unsinged integer, a signed integer,
+        a float, a boolean and a bytearray:
+        
+        .. code-block:: python
+        
+            >>> from bitstruct import *
+            >>> pack('u5s5f32b1r13', 1, -1, 3.75, True, bytearray(b'\xff\xff'))
+            bytearray(b'\x0f\xd0\x1c\x00\x00?\xff')
+            >>> unpack('u5s5f32b1r13', bytearray(b'\x0f\xd0\x1c\x00\x00?\xff'))
+            (1, -1, 3.75, True, bytearray(b'\xff\xf8'))
+            >>> calcsize('u5s5f32b1r13')
+            56
+        
+        An example of unpacking values from a hexstring and a binary file:
+        
+        .. code-block:: python
+        
+            >>> from bitstruct import *
+            >>> from binascii import *
+            >>> unpack('s17s13r24', bytearray(unhexlify('0123456789abcdef')))
+            (582, -3751, bytearray(b'\xe2j\xf3'))
+            >>> with open("test.bin", "rb") as fin:
+            ...     unpack('s17s13r24', bytearray(fin.read(8)))
+            ...     
+            ... 
+            (582, -3751, bytearray(b'\xe2j\xf3'))
+        
+        Change endianness of the data with byteswap(), and then unpack the
+        values:
+        
+        .. code-block:: python
+        
+            >>> from bitstruct import *
+            >>> packed = pack('u1u3u4s16', 1, 2, 3, 1)
+            >>> unpack('u1u3u4s16', byteswap('12', packed))
+            (1, 2, 3, 256)
+        
+        .. |buildstatus| image:: https://travis-ci.org/eerimoq/bitstruct.svg
+        .. _buildstatus: https://travis-ci.org/eerimoq/bitstruct
+        
+Keywords: bit field,bit parsing,bit unpack,bit pack
+Platform: UNKNOWN
+Classifier: License :: OSI Approved :: MIT License
+Classifier: Programming Language :: Python :: 2
+Classifier: Programming Language :: Python :: 3
diff --git a/bitstruct.egg-info/SOURCES.txt b/bitstruct.egg-info/SOURCES.txt
new file mode 100644
index 0000000..fbfd09b
--- /dev/null
+++ b/bitstruct.egg-info/SOURCES.txt
@@ -0,0 +1,7 @@
+README.rst
+bitstruct.py
+setup.py
+bitstruct.egg-info/PKG-INFO
+bitstruct.egg-info/SOURCES.txt
+bitstruct.egg-info/dependency_links.txt
+bitstruct.egg-info/top_level.txt
\ No newline at end of file
diff --git a/bitstruct.egg-info/dependency_links.txt b/bitstruct.egg-info/dependency_links.txt
new file mode 100644
index 0000000..8b13789
--- /dev/null
+++ b/bitstruct.egg-info/dependency_links.txt
@@ -0,0 +1 @@
+
diff --git a/bitstruct.egg-info/top_level.txt b/bitstruct.egg-info/top_level.txt
new file mode 100644
index 0000000..2426106
--- /dev/null
+++ b/bitstruct.egg-info/top_level.txt
@@ -0,0 +1 @@
+bitstruct
diff --git a/bitstruct.py b/bitstruct.py
new file mode 100644
index 0000000..a288b2a
--- /dev/null
+++ b/bitstruct.py
@@ -0,0 +1,205 @@
+import re
+import struct
+
+
+def _parse_format(fmt):
+    types = re.findall(r'[a-zA-Z]+', fmt)
+    sizes = map(lambda size: int(size),
+                re.findall(r'\d+', fmt))
+
+    return zip(types, sizes)
+
+
+def _pack_integer(size, arg):
+    if arg < 0:
+        arg = ((1 << size) + arg)
+
+    return '{{:0{}b}}'.format(size).format(arg)
+
+
+def _pack_boolean(size, arg):
+    return _pack_integer(size, int(arg))
+
+
+def _pack_float(size, arg):
+    if size == 32:
+        value = struct.pack('>f', arg)
+    elif size == 64:
+        value = struct.pack('>d', arg)
+    else:
+        raise ValueError('Bad float size {}. Must be 32 or 64 bits.'.format(size))
+    return ''.join('{:08b}'.format(b)
+                   for b in bytearray(value))
+
+
+def _pack_bytearray(size, arg):
+    bits = ''.join('{:08b}'.format(b)
+                   for b in arg)
+
+    return bits[0:size]
+
+
+def _unpack_integer(type, bits):
+    value = int(bits, 2)
+
+    if type == 's':
+        if bits[0] == '1':
+            value -= (1 << len(bits))
+
+    return value
+
+
+def _unpack_boolean(bits):
+    value = _unpack_integer('u', bits)
+
+    return bool(value)
+
+
+def _unpack_float(size, bits):
+    packed = _unpack_bytearray(size, bits)
+
+    if size == 32:
+        value = struct.unpack('>f', packed)[0]
+    elif size == 64:
+        value = struct.unpack('>d', packed)[0]
+    else:
+        raise ValueError('Bad float size {}. Must be 32 or 64 bits.'.format(size))
+
+    return value
+
+def _unpack_bytearray(size, bits):
+    value = bytearray()
+    for i in range(size // 8):
+        value.append(int(bits[8*i:8*i+8], 2))
+    rest = size % 8
+    if rest > 0:
+        value.append(int(bits[size-rest:], 2) << (8-rest))
+    return value
+
+
+def pack(fmt, *args):
+    """Return a bytearray containing the values v1, v2, ... packed
+    according to the given format. If the total number of bits are not
+    a multiple of 8, padding will be added at the end of the last
+    byte.
+
+    :param fmt: Bitstruct format string.
+    :param args: Variable argument list of values to pack.
+    :returns: Bytearray of packed values.
+    
+    `fmt` is a string of type-length pairs. There are six types; 'u',
+    's', 'f', 'b', 'r' and 'p'. Length is the number of bits to pack
+    the value into.
+    
+    - 'u' -- unsigned integer
+    - 's' -- signed integer
+    - 'f' -- floating point number of 32 or 64 bits
+    - 'b' -- boolean
+    - 'r' -- raw, bytearray
+    - 'p' -- padding, ignore
+
+    Example format string: 'u1u3p7s16'
+
+    """
+
+    bits = ''
+    infos = _parse_format(fmt)
+    i = 0
+
+    for type, size in infos:
+        if type == 'p':
+            bits += size * '0'
+        else:
+            if type in 'us':
+                bits += _pack_integer(size, args[i])
+            elif type == 'f':
+                bits += _pack_float(size, args[i])
+            elif type == 'b':
+                bits += _pack_boolean(size, args[i])
+            elif type == 'r':
+                bits += _pack_bytearray(size, args[i])
+            else:
+                raise ValueError("bad type '{}' in format".format(type))
+            i += 1
+
+    # padding of last byte
+    tail = len(bits) % 8
+    if tail != 0:
+        bits += (8 - tail) * '0'
+
+    return bytearray([int(''.join(bits[i:i+8]), 2)
+                      for i in range(0, len(bits), 8)])
+
+
+def unpack(fmt, data):
+    """Unpack the bytearray (presumably packed by pack(fmt, ...))
+    according to the given format. The result is a tuple even if it
+    contains exactly one item.
+
+    :param fmt: Bitstruct format string. See pack() for details.
+    :param data: Bytearray of values to unpack.
+    :returns: Tuple of unpacked values.
+
+    """
+
+    bits = ''.join(['{:08b}'.format(b) for b in data])
+    infos = _parse_format(fmt)
+    res = []
+    i = 0
+
+    for type, size in infos:
+        if type == 'p':
+            pass
+        else:
+            if type in 'us':
+                value = _unpack_integer(type, bits[i:i+size])
+            elif type == 'f':
+                value = _unpack_float(size, bits[i:i+size])
+            elif type == 'b':
+                value = _unpack_boolean(bits[i:i+size])
+            elif type == 'r':
+                value = _unpack_bytearray(size, bits[i:i+size])
+            else:
+                raise ValueError("bad type '{}' in format".format(type))
+            res.append(value)
+        i += size
+
+    return tuple(res)
+
+
+def calcsize(fmt):
+    """Return the size of the bitstruct (and hence of the bytearray)
+    corresponding to the given format.
+
+    :param fmt: Bitstruct format string.
+    :returns: Number of bits in format string.
+
+    """
+
+    return sum([size for _, size in _parse_format(fmt)])
+
+
+def byteswap(fmt, data, offset = 0):
+    """In place swap bytes in `data` according to `fmt`, starting at byte
+    `offset`. `fmt` must be an iterable, iterating over number of
+    bytes to swap. For example, the format string "24" applied to the
+    bytearray "\x00\x11\x22\x33\x44\x55" will produce the result
+    "\x11\x00\x55\x44\x33\x22".
+
+    :param fmt: Swap format string.
+    :param data: Bytearray of data to swap.
+    :param offset: Start offset into `data`.
+    :returns: Bytearray of swapped bytes.
+
+    """
+
+    i = offset
+
+    for f in fmt:
+        length = int(f)
+        value = data[i:i+length]
+        value.reverse()
+        data[i:i+length] = value
+        i += length
+
+    return data
diff --git a/setup.cfg b/setup.cfg
new file mode 100644
index 0000000..861a9f5
--- /dev/null
+++ b/setup.cfg
@@ -0,0 +1,5 @@
+[egg_info]
+tag_build = 
+tag_date = 0
+tag_svn_revision = 0
+
diff --git a/setup.py b/setup.py
new file mode 100755
index 0000000..1ea2ba8
--- /dev/null
+++ b/setup.py
@@ -0,0 +1,22 @@
+#!/usr/bin/env python
+
+from setuptools import setup
+
+setup(name='bitstruct',
+      version='2.0.2',
+      description=('This module performs conversions between Python values '
+                   'and C bit field structs represented as Python '
+                   'bytearrays.'),
+      long_description=open('README.rst', 'r').read(),
+      author='Erik Moqvist, Ilya Petukhov',
+      author_email='erik.moqvist at gmail.com',
+      license='MIT',
+      classifiers=[
+          'License :: OSI Approved :: MIT License',
+          'Programming Language :: Python :: 2',
+          'Programming Language :: Python :: 3',
+      ],
+      keywords=['bit field', 'bit parsing', 'bit unpack', 'bit pack'],
+      url='https://github.com/eerimoq/bitstruct',
+      py_modules=['bitstruct'],
+      test_suite="tests")

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/bitstruct.git



More information about the Python-modules-commits mailing list