6
6
import binascii
7
7
8
8
9
- __version__ = "5.2.1 "
9
+ __version__ = "6.0.0 "
10
10
11
11
12
12
class Error (Exception ):
@@ -246,27 +246,15 @@ def _unpack_bytearray(size, bits):
246
246
return binascii .unhexlify (hex (int ('10000000' + bits , 2 ))[4 :].rstrip ('L' ))
247
247
248
248
249
- class CompiledFormat (object ):
250
- """A compiled format string that can be used to pack and/or unpack
251
- data multiple times.
252
-
253
- Instances of this class are created by the factory function
254
- :func:`~bitstruct.compile()`.
255
-
256
- """
249
+ class _CompiledFormat (object ):
257
250
258
251
def __init__ (self , fmt , names = None ):
259
252
infos , byte_order = _parse_format (fmt , names )
260
253
self ._infos = infos
261
254
self ._byte_order = byte_order
262
255
self ._number_of_bits_to_unpack = sum ([info .size for info in infos ])
263
- self ._number_of_arguments = 0
264
-
265
- for info in infos :
266
- if not isinstance (info , _Padding ):
267
- self ._number_of_arguments += 1
268
256
269
- def _pack_value (self , info , value , bits ):
257
+ def pack_value (self , info , value , bits ):
270
258
value_bits = info .pack (value )
271
259
272
260
# Reverse the bit order in little endian values.
@@ -288,14 +276,14 @@ def _pack_value(self, info, value, bits):
288
276
289
277
return bits
290
278
291
- def _pack (self , values ):
279
+ def pack_any (self , values ):
292
280
bits = ''
293
281
294
282
for info in self ._infos :
295
283
if isinstance (info , _Padding ):
296
284
bits += info .pack ()
297
285
else :
298
- bits = self ._pack_value (info , values [info .name ], bits )
286
+ bits = self .pack_value (info , values [info .name ], bits )
299
287
300
288
# Padding of last byte.
301
289
tail = len (bits ) % 8
@@ -305,7 +293,7 @@ def _pack(self, values):
305
293
306
294
return bytes (_unpack_bytearray (len (bits ), bits ))
307
295
308
- def _unpack_from (self , data , offset ):
296
+ def unpack_from_any (self , data , offset ):
309
297
bits = bin (int (b'01' + binascii .hexlify (bytearray (data )), 16 ))[3 + offset :]
310
298
311
299
# Sanity check.
@@ -345,7 +333,7 @@ def _unpack_from(self, data, offset):
345
333
346
334
offset += info .size
347
335
348
- def _pack_into (self , buf , offset , data , ** kwargs ):
336
+ def pack_into_any (self , buf , offset , data , ** kwargs ):
349
337
fill_padding = kwargs .get ('fill_padding' , True )
350
338
buf_bits = _pack_bytearray (8 * len (buf ), buf )
351
339
bits = buf_bits [0 :offset ]
@@ -358,7 +346,7 @@ def _pack_into(self, buf, offset, data, **kwargs):
358
346
else :
359
347
bits += buf_bits [len (bits ):len (bits ) + info .size ]
360
348
else :
361
- bits = self ._pack_value (info , data [info .name ], bits )
349
+ bits = self .pack_value (info , data [info .name ], bits )
362
350
i += 1
363
351
364
352
bits += buf_bits [len (bits ):]
@@ -370,8 +358,33 @@ def _pack_into(self, buf, offset, data, **kwargs):
370
358
371
359
buf [:] = _unpack_bytearray (len (bits ), bits )
372
360
361
+ def calcsize (self ):
362
+ """Return the number of bits in the compiled format string.
363
+
364
+ """
365
+
366
+ return self ._number_of_bits_to_unpack
367
+
368
+
369
+ class CompiledFormat (_CompiledFormat ):
370
+ """A compiled format string that can be used to pack and/or unpack
371
+ data multiple times.
372
+
373
+ Instances of this class are created by the factory function
374
+ :func:`~bitstruct.compile()`.
375
+
376
+ """
377
+
378
+ def __init__ (self , fmt ):
379
+ super (CompiledFormat , self ).__init__ (fmt , None )
380
+ self ._number_of_arguments = 0
381
+
382
+ for info in self ._infos :
383
+ if not isinstance (info , _Padding ):
384
+ self ._number_of_arguments += 1
385
+
373
386
def pack (self , * args ):
374
- """See :func:`pack()`.
387
+ """See :func:`~bitstruct. pack()`.
375
388
376
389
"""
377
390
@@ -382,17 +395,17 @@ def pack(self, *args):
382
395
self ._number_of_arguments ,
383
396
len (args )))
384
397
385
- return self ._pack (args )
398
+ return self .pack_any (args )
386
399
387
400
def unpack (self , data ):
388
- """See :func:`unpack()`.
401
+ """See :func:`~bitstruct. unpack()`.
389
402
390
403
"""
391
404
392
405
return self .unpack_from (data )
393
406
394
407
def pack_into (self , buf , offset , * args , ** kwargs ):
395
- """See :func:`pack_into()`.
408
+ """See :func:`~bitstruct. pack_into()`.
396
409
397
410
"""
398
411
@@ -403,55 +416,54 @@ def pack_into(self, buf, offset, *args, **kwargs):
403
416
self ._number_of_arguments ,
404
417
len (args )))
405
418
406
- self ._pack_into (buf , offset , args , ** kwargs )
419
+ self .pack_into_any (buf , offset , args , ** kwargs )
407
420
408
421
def unpack_from (self , data , offset = 0 ):
409
- """See :func:`unpack_from()`.
422
+ """See :func:`~bitstruct. unpack_from()`.
410
423
411
424
"""
412
425
413
- return tuple ([v [1 ] for v in self ._unpack_from (data , offset )])
426
+ return tuple ([v [1 ] for v in self .unpack_from_any (data , offset )])
427
+
428
+
429
+ class CompiledFormatDict (_CompiledFormat ):
430
+ """See :class:`~bitstruct.CompiledFormat`.
431
+
432
+ """
414
433
415
- def pack_dict (self , data ):
416
- """See :func:`pack_dict()`.
434
+ def pack (self , data ):
435
+ """See :func:`~bitstruct. pack_dict()`.
417
436
418
437
"""
419
438
420
439
try :
421
- return self ._pack (data )
440
+ return self .pack_any (data )
422
441
except KeyError as e :
423
442
raise Error ('{} not found in data dictionary' .format (str (e )))
424
443
425
- def unpack_dict (self , data ):
426
- """See :func:`unpack_dict()`.
444
+ def unpack (self , data ):
445
+ """See :func:`~bitstruct. unpack_dict()`.
427
446
428
447
"""
429
448
430
- return self .unpack_from_dict (data )
449
+ return self .unpack_from (data )
431
450
432
- def pack_into_dict (self , buf , offset , data , ** kwargs ):
433
- """See :func:`pack_into_dict()`.
451
+ def pack_into (self , buf , offset , data , ** kwargs ):
452
+ """See :func:`~bitstruct. pack_into_dict()`.
434
453
435
454
"""
436
455
437
456
try :
438
- self ._pack_into (buf , offset , data , ** kwargs )
457
+ self .pack_into_any (buf , offset , data , ** kwargs )
439
458
except KeyError as e :
440
459
raise Error ('{} not found in data dictionary' .format (str (e )))
441
460
442
- def unpack_from_dict (self , data , offset = 0 ):
443
- """See :func:`unpack_from_dict()`.
444
-
445
- """
446
-
447
- return {info .name : v for info , v in self ._unpack_from (data , offset )}
448
-
449
- def calcsize (self ):
450
- """Return the number of bits in the compiled format string.
461
+ def unpack_from (self , data , offset = 0 ):
462
+ """See :func:`~bitstruct.unpack_from_dict()`.
451
463
452
464
"""
453
465
454
- return self ._number_of_bits_to_unpack
466
+ return { info . name : v for info , v in self .unpack_from_any ( data , offset )}
455
467
456
468
457
469
def pack (fmt , * args ):
@@ -542,7 +554,8 @@ def unpack_from(fmt, data, offset=0):
542
554
543
555
544
556
def pack_dict (fmt , names , data ):
545
- """Same as :func:`pack()`, but data is read from a dictionary.
557
+ """Same as :func:`~bitstruct.pack()`, but data is read from a
558
+ dictionary.
546
559
547
560
The names list `names` contains the format group names, used as
548
561
keys in the dictionary.
@@ -552,43 +565,45 @@ def pack_dict(fmt, names, data):
552
565
553
566
"""
554
567
555
- return CompiledFormat (fmt , names ).pack_dict (data )
568
+ return CompiledFormatDict (fmt , names ).pack (data )
556
569
557
570
558
571
def unpack_dict (fmt , names , data ):
559
- """Same as :func:`unpack()`, but returns a dictionary.
572
+ """Same as :func:`~bitstruct. unpack()`, but returns a dictionary.
560
573
561
- See :func:`pack_dict()` for details on `names`.
574
+ See :func:`~bitstruct. pack_dict()` for details on `names`.
562
575
563
576
>>> unpack_dict('u4u4', ['foo', 'bar'], b'\\ x12')
564
577
{'foo': 1, 'bar': 2}
565
578
566
579
"""
567
580
568
- return CompiledFormat (fmt , names ).unpack_dict (data )
581
+ return CompiledFormatDict (fmt , names ).unpack (data )
569
582
570
583
571
584
def pack_into_dict (fmt , names , buf , offset , data , ** kwargs ):
572
- """Same as :func:`pack_into()`, but data is read from a dictionary.
585
+ """Same as :func:`~bitstruct.pack_into()`, but data is read from a
586
+ dictionary.
573
587
574
- See :func:`pack_dict()` for details on `names`.
588
+ See :func:`~bitstruct. pack_dict()` for details on `names`.
575
589
576
590
"""
577
591
578
- return CompiledFormat (fmt , names ).pack_into_dict (buf ,
579
- offset ,
580
- data ,
581
- ** kwargs )
592
+ return CompiledFormatDict (fmt , names ).pack_into (buf ,
593
+ offset ,
594
+ data ,
595
+ ** kwargs )
582
596
583
597
584
598
def unpack_from_dict (fmt , names , data , offset = 0 ):
585
- """Same as :func:`unpack_from_dict()`, but returns a dictionary.
599
+ """Same as :func:`~bitstruct.unpack_from_dict()`, but returns a
600
+ dictionary.
586
601
587
- See :func:`pack_dict()` for details on `names`.
602
+ See :func:`~bitstruct. pack_dict()` for details on `names`.
588
603
589
604
"""
590
605
591
- return CompiledFormat (fmt , names ).unpack_from_dict (data , offset )
606
+ return CompiledFormatDict (fmt , names ).unpack_from (data , offset )
592
607
593
608
594
609
def calcsize (fmt ):
@@ -623,12 +638,18 @@ def byteswap(fmt, data, offset=0):
623
638
624
639
625
640
def compile (fmt , names = None ):
626
- """Compile given format string `fmt` and return a
627
- :class:`~bitstruct.CompiledFormat` object that can be used to pack
628
- and/or unpack data multiple times.
641
+ """Compile given format string `fmt` and return a compiled format
642
+ object that can be used to pack and/or unpack data multiple times.
643
+
644
+ Returns a :class:`~bitstruct.CompiledFormat` object if `names` is
645
+ ``None``, and otherwise a :class:`~bitstruct.CompiledFormatDict`
646
+ object.
629
647
630
- See :func:`pack_dict()` for details on `names`.
648
+ See :func:`~bitstruct. pack_dict()` for details on `names`.
631
649
632
650
"""
633
651
634
- return CompiledFormat (fmt , names )
652
+ if names is None :
653
+ return CompiledFormat (fmt )
654
+ else :
655
+ return CompiledFormatDict (fmt , names )
0 commit comments