Version-independent bytes-chains
Project description
- Name:
byt
- Website:
- Author:
Guillaume Schworer
- Version:
1.1
Although the new python3 strings/bytes-chains are arguably neater than that of python2, writing code using them, and which is compatible with both versions, is nearly a nightmare. This package attempts to gap the major discontinuity in the management of strings and bytes-chains between the two major python versions, 2.7+ and 3.5+.
Byt is a package subclassing str in python2 and bytes in python3. Its main design focus is to behaves exactly the same way, no matter the python version used. To achieve such behaviour, byt is extremely conservative: Byt objects will only work with other Byt objects (concatenate, find, replace, etc). This was (unfortunately?) necessary to ensure the somewhat lax python2 strings and bytes-chains mix-up work the same way as those of python3.
This packages also fixes some strange behaviors of python3 bytes (try b'test'[0:1] == b'test'[0]) and provides new convenient methods or properties.
It is built by Guillaume Schworer and licensed under the GNU General Public License v3 or later (GPLv3+) license (see LICENSE).
Installation
Just run
pip install byt
to get the most recent stable version.
Usage
The two entry points are the byt.Byt and byt.DByt classes which automatically load depending on the python version you’re running.
The only difference between Byt and DByt is that str(Byt) will print the ASCII representation of the string while str(DByt) – D like display – will print its corresponding hexadecimal values. These classes are full inter-operable.
You’ll just use it like this:
from byt import Byt >> b = Byt('hello world!') >> print(b) Byt('hello world!') >> (Byt(1,2) + Byt("\x01\x02") + Byt([1,2])).hex() '01 02 01 02 01 02' >> print(b.hex()) 68 65 6c 6c 6f 20 77 6f 72 6c 64 21 >> eval(repr(b)) == b True >> print(Byt('str1') + 'str2') TypeError: can't concat Byt to str >> byt.Byt('str1')[2:].ints() [114, 49] from byt import DByt >> b = DByt('hello world!') >> print(b) 68 65 6c 6c 6f 20 77 6f 72 6c 64 21 >> eval(repr(b)) == b True # inter-compatibility >> DByt('yes') == Byt('yes') True >> DByt('yes') + Byt('no') DByt('yesno') >> print(DByt('yes') + Byt('no')) 79 65 73 6e 6f
Documentation
All the options are documented in the docstrings for the classes. These can be viewed in a Python shell using:
>> from byt import Byt, DByt >> print(Byt.__doc__) >> print(DByt.__doc__)
or, in IPython using:
>> from byt import Byt >> Byt? >> DByt?
License
Copyright 2017 Guillaume Schworer
patiencebar is free software made available under the GNU General Public License v3 or later (GPLv3+) license (see LICENSE).
Changelog
1.1.0 (2017-11-12)
Added docs and tests
New initialization of Byt/DByt object: Byt(1,2,3)
1.0.12 (2017-08-12)
Added DByt hexadecimal as the default __str__, while __repr__ remains ASCII-based
New method ‘str’
1.0.8 (2017-06-26)
Fixed repr method and backslashing
1.0.6 (2017-03-22)
Fixed install bug through pip
1.0.4 (2017-03-22)
Added fromhex classmethod
1.0.3 (2017-02-22)
Fixed bug when invoking isinstance on a Byt object
1.0.0 (2017-02-19)
Initial release.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file byt-1.1.0.tar.gz
.
File metadata
- Download URL: byt-1.1.0.tar.gz
- Upload date:
- Size: 7.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8cea556cb1379ae038c20cc98ff47a353e310680209b6f7bec96f99c0ae68ce0 |
|
MD5 | 1842159cb972ce7fb6a2961e243e6ff4 |
|
BLAKE2b-256 | 5d946c2738b1add2eb6344a78522774ce565ae07ef4df7ee0b2934abcc92a814 |