API
binapy
¶
Top-level package for BinaPy.
BinaPy
¶
Bases: bytes
A helper class for binary data manipulation.
This subclass of bytes
exposes various binary data manipulation methods. Since this is
a bytes
subclass, you can use instances of BinaPy
anywhere you can use bytes
. BinaPy
allows (re)encoding of data using encode_to(<format>)
, decoding using
decode_from(<format>)
, parsing using parse_from(<format>)
, and serialisation using
serialize_to(<format>)
.
Actual transformations into formats such as Base64, JSON, etc. are implemented using
Extensions. Those extensions are registered using the decorators binapy_encoder
,
binapy_decoder
, binapy_checker
, binapy_serializer
, and binapy_parser
.
Source code in binapy/binapy.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 |
|
extensions: dict[str, dict[str, Callable[..., Any]]] = {}
class-attribute
¶
Extension registry.
from_int(i, *, length=None, byteorder='big', signed=False)
classmethod
¶
Convert an int
to a BinaPy
.
This is a wrapper around int.to_bytes() and takes the same parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
i |
int
|
the integer to convert to BinaPy |
required |
length |
int | None
|
the length of the integer, in bytes. If omitted,default to the minimal length that fits the given integer. |
None
|
byteorder |
Literal['little', 'big']
|
"little" or "big" (defaults to "big") |
'big'
|
signed |
bool
|
determines whether two's complement is used to represent the integer. |
False
|
Returns:
Type | Description |
---|---|
BinaPy
|
a BinaPy with the binary representation of the given integer |
Source code in binapy/binapy.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
|
ascii()
¶
Decode this BinaPy to a str.
This makes sure that only ascii characters are part of the result.
Returns:
Type | Description |
---|---|
str
|
a str with only ASCII chars |
Source code in binapy/binapy.py
94 95 96 97 98 99 100 101 102 103 |
|
re_match(pattern, encoding='ascii')
¶
Decode this binary value to a string, then match it to a regex.
Decoding is done using the encoding defined in encoding
(default to "ascii").
The decoded value is then matched with the regular expression pattern
.
If the match is successful, return the decoded string. Raise a ValueError
otherwise.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pattern |
str
|
the regular expression pattern to match |
required |
encoding |
str
|
the encoding to use to decode the binary value to a string |
'ascii'
|
Returns:
Type | Description |
---|---|
str
|
the decoded, matching |
Raises:
Type | Description |
---|---|
ValueError
|
if the decoded str does not match |
Source code in binapy/binapy.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
|
text(encoding='ascii')
¶
Decode this BinaPy to a str, and check that the result is printable.
Printable characters are characters from the range [a-zA-Z0-9!"#$%&\'()*+,-./:;<=>?@[\\]^_
~ ]`.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
encoding |
str
|
the encoding to use to decode the binary data |
'ascii'
|
Returns:
Type | Description |
---|---|
str
|
the decoded text |
Source code in binapy/binapy.py
130 131 132 133 134 135 136 137 138 139 140 141 142 |
|
urlsafe()
¶
Convert this BinaPy to a str, and check that it contains only url-safe characters.
Url-safe characters are [A-Za-z0-9_.\\-~]
.
Returns:
Type | Description |
---|---|
str
|
a str with only URL-safe chars |
Source code in binapy/binapy.py
144 145 146 147 148 149 150 151 152 153 |
|
alphanumeric()
¶
Check that this BinaPy contains only alphanumeric characters.
Returns:
Type | Description |
---|---|
str
|
a str with only alphanumeric chars |
Source code in binapy/binapy.py
155 156 157 158 159 160 161 162 |
|
to_int(*, byteorder='big', signed=False)
¶
Convert this BinaPy to an int
.
This is a wrapper around int.from_bytes() and takes the same parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
byteorder |
Literal['little', 'big']
|
"little" or "big" (defaults to "big") |
'big'
|
signed |
bool
|
determines whether two's complement is used to represent the integer. Default to False. |
False
|
Returns:
Type | Description |
---|---|
int
|
an integer based on this BinaPy binary value |
Source code in binapy/binapy.py
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
|
from_binary_string(s, *, byteorder='big', signed=False)
classmethod
¶
Initialize a BinaPy based on a binary string (containing only 0 and 1).
The string may contain spaces or tabs characters which will be ignored.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
s |
str
|
a binary string |
required |
byteorder |
Literal['little', 'big']
|
byte order to use |
'big'
|
signed |
bool
|
True if 2 complement is used to represent negative values |
False
|
Returns:
Type | Description |
---|---|
BinaPy
|
a BinaPy |
Source code in binapy/binapy.py
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
|
to_binary_string(*, byteorder='big', signed=False, pad=True)
¶
Return a string containing this BinaPy value in binary representation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
byteorder |
Literal['little', 'big']
|
byte order to use |
'big'
|
signed |
bool
|
|
False
|
pad |
bool
|
if |
True
|
Returns:
Type | Description |
---|---|
str
|
a string with containing only 0 and 1 |
Source code in binapy/binapy.py
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
|
random(length)
classmethod
¶
Return a BinaPy containing length
random bytes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
length |
int
|
number of bytes to generate |
required |
Returns:
Type | Description |
---|---|
BinaPy
|
a BinaPy with randomly generated data |
Source code in binapy/binapy.py
223 224 225 226 227 228 229 230 231 232 233 234 |
|
random_bits(length)
classmethod
¶
Return a BinaPy containing length
random bits. Same as random(length//8).
Length must be a multiple of 8.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
length |
int
|
number of bits to randomly generate |
required |
Returns:
Type | Description |
---|---|
BinaPy
|
a BinaPy with randomly generated data |
Source code in binapy/binapy.py
236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
|
char_at(index)
¶
Return the character at the given index.
Slicing a standard bytes returns an int. Sometimes what you really want is a single char string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
index |
int
|
the index of the desired character |
required |
Returns:
Type | Description |
---|---|
str
|
the single character at the given index |
Source code in binapy/binapy.py
273 274 275 276 277 278 279 280 281 282 283 284 285 |
|
split(sep=None, maxsplit=-1)
¶
Override base method to return a BinaPy instead of bytes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sep |
bytes | None
|
a separator |
None
|
maxsplit |
int
|
the maximum number of splits |
-1
|
Returns:
Type | Description |
---|---|
list[BinaPy]
|
a BinaPy |
Source code in binapy/binapy.py
314 315 316 317 318 319 320 321 322 323 324 325 |
|
split_at(*pos)
¶
Split this BinaPy at one or more integer positions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*pos |
int
|
indexes where to cut the BinaPy |
()
|
Returns:
Type | Description |
---|---|
tuple[BinaPy, ...]
|
a tuple of |
Source code in binapy/binapy.py
327 328 329 330 331 332 333 334 335 336 337 338 |
|
encode_to(name, *args, **kwargs)
¶
Encode data from this BinaPy according to the format name
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
format to use |
required |
*args |
Any
|
additional position parameters for the extension encoder method |
()
|
**kwargs |
Any
|
additional keyword parameters for the extension encoder method |
{}
|
Returns:
Type | Description |
---|---|
BinaPy
|
the resulting data |
Source code in binapy/binapy.py
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 |
|
to(name, *args, **kwargs)
¶
Alias for encode_to()
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
same as |
required |
*args |
Any
|
same as |
()
|
**kwargs |
Any
|
same as |
{}
|
Returns:
Type | Description |
---|---|
BinaPy
|
same as |
Source code in binapy/binapy.py
414 415 416 417 418 419 420 421 422 423 424 425 426 |
|
decode_from(name, *args, **kwargs)
¶
Decode data from this BinaPy according to the format name
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
format name to use |
required |
*args |
Any
|
additional position parameters for the extension decoder method |
()
|
**kwargs |
Any
|
additional keyword parameters for the extension decoder method |
{}
|
Returns:
Type | Description |
---|---|
BinaPy
|
the resulting data |
Source code in binapy/binapy.py
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 |
|
check(name, *, decode=False, raise_on_error=False)
¶
Check that this BinaPy conforms to a given format extension.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
the name of the extension to check |
required |
decode |
bool
|
if |
False
|
raise_on_error |
bool
|
if |
False
|
Returns:
Type | Description |
---|---|
bool
|
a boolean, that is True if this BinaPy conforms to the given extension format, False otherwise. |
Source code in binapy/binapy.py
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 |
|
check_all(*, decode=False)
¶
Check if this BinaPy conforms to any of the registered format extensions.
Returns:
Type | Description |
---|---|
list[str]
|
a list of format extensions that this BinaPy can be decoded from. |
Parameters:
Name | Type | Description | Default |
---|---|---|---|
decode |
bool
|
if |
False
|
Source code in binapy/binapy.py
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 |
|
parse_from(name, *args, **kwargs)
¶
Parse data from this BinaPy, based on a given format extension.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
name of the extension to use |
required |
*args |
Any
|
additional position parameters for the extension decoder method |
()
|
**kwargs |
Any
|
additional keyword parameters for the extension decoder method |
{}
|
Returns:
Type | Description |
---|---|
Any
|
the result from parsing this BinaPy |
Source code in binapy/binapy.py
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 |
|
serialize_to(name, *args, **kwargs)
classmethod
¶
Serialize (dump) data to a BinaPy, based on a given extension format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
name of the extension to use |
required |
*args |
Any
|
additional position parameters for the extension decoder method (which includes the data to serialize) |
()
|
**kwargs |
Any
|
additional keyword parameters for the extension decoder method |
{}
|
Returns:
Type | Description |
---|---|
BinaPy
|
a BinaPy, resulting from serialization of the data |
Source code in binapy/binapy.py
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 |
|
register_extension(name, feature, func)
classmethod
¶
Register a new feature for the given extension name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
the extension name |
required |
feature |
str
|
name of the feature to register ("encode", "decode", etc.) |
required |
func |
Callable[..., Any]
|
the method implementing the feature |
required |
Source code in binapy/binapy.py
542 543 544 545 546 547 548 549 550 551 552 553 |
|
InvalidExtensionMethodError
¶
Bases: ValueError
Raised when an extension method returns invalid data.
Source code in binapy/binapy.py
559 560 |
|
binapy_checker(name)
¶
Declare a new checker for BinaPy.
This is a decorator. Checker checks that some data is valid for a given format/extension.
1 |
|
1 |
|
Usage
The matching checker for the special "double-encoding" we created with binapy_encoder()
:
1 2 3 4 5 6 7 8 9 |
|
Source code in binapy/binapy.py
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 |
|
binapy_decoder(name)
¶
Declare a new decoder for BinaPy.
This is a method decorator. Decoders do convert BinaPy data from a given format into another BinaPy.
1 |
|
1 |
|
Usage
The matching decoder for the special "double-encoding" we created with binapy_encoder()
:
1 2 3 4 5 6 7 8 9 10 11 |
|
Source code in binapy/binapy.py
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 |
|
binapy_encoder(name)
¶
Declare new encoders for BinaPy.
This is a method decorator. Encoders do convert a BinaPy into another BinaPy using a given format/extension.
1 |
|
1 |
|
Usage
To declare a special "double-encoding" which just copy the data twice:
1 2 3 4 5 6 7 8 9 |
|
Source code in binapy/binapy.py
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 |
|
binapy_parser(name)
¶
Declare a new parser for BinaPy.
This is a method decorator. Parsers do convert BinaPy into Python objects. For example, a JSON parser will convert a BinaPy into a dict, by parsing the JSON formatted string from a BinaPy.
1 |
|
1 |
|
Source code in binapy/binapy.py
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 |
|
binapy_serializer(name)
¶
Declare a new serializer for BinaPy.
This is a method decorator. Serializers do convert Python objects into BinaPy.
For example, a JSON serializer will convert a Python dict
into a serialized JSON value.
1 |
|
1 |
|
Source code in binapy/binapy.py
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 |
|