reprb


Namereprb JSON
Version 1.0.8 PyPI version JSON
download
home_pagehttps://github.com/testzero-wz/reprb
SummaryRepresent bytes with printable characters
upload_time2024-09-13 03:13:35
maintainerNone
docs_urlNone
authorT3stzer0
requires_python>=3
licenseNone
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ## reprb

Represent bytes with printable characters, similar to how python built-in functions repr() and eval() do.

## why

Bytes objects in Python 3 can already be read from and written to files (like load/dump), but you can't easily understand or edit them when you open a binary file directly in a text editor.

`reprb`'s goal is to dump bytes to printable bytes and load them back to the original bytes object quickly (at least faster than the built-in `repr()`), especially when you analysis/dump/load bytes contain both printable and unprintable characters(like http message), `reprb` make it more editable and understandable.

## how
### Install
#### from pip
``` bash
python3 -m pip install reprb
```

#### from source
```bash
git clone git@github.com:Testzero-wz/reprb.git

cd reprb && pip3 install .
```

### Usage

repr bytes object:

```python
>>> from reprb import reprb, evalb
>>> msg = "abc123\x00\x07\x11\x90\xff中文№".encode()
>>> repr_bytes = reprb(msg)
>>> repr_bytes
b'abc123\\0\\a\\x11\\xc2\\x90\\xc3\\xbf\\xe4\\xb8\\xad\\xe6\\x96\\x87\\xe2\\x84\\x96'
>>> eval_bytes = evalb(repr_bytes)
>>> eval_bytes == msg
True
```

dump/load bytes from/to file:

```python
from reprb import dump, load, load_iter

dump_bytes = b"abc123\x00\x07\x84\x96"
dump_file = "dump.txt"

# dump bytes to file, seperate by "\n" default
with open(dump_file, "wb") as f:

    # dump bytes object
    dump(dump_bytes, f)

    # dump all bytes object in list
    dump_bytes_list = [dump_bytes, dump_bytes, dump_bytes]
    dump(dump_bytes_list, f)

# load all bytes from file, seperate by "\n" default
load_bytes_from_path = load(dump_file)

# load all bytes from file handler
with open(dump_file, "rb") as f:
    load_bytes_from_file = load(f)

# load iter
load_bytes_from_iter = list(load_iter(dump_file))

assert (
    [dump_bytes] + dump_bytes_list
    == load_bytes_from_path
    == load_bytes_from_file
    == load_bytes_from_iter
)
```

If you want to store bytes with a more formatable structure like json:
``` python
from reprb import reprb, evalb

# you should decode reprb bytes since json.dump() only accept string object.
# btw, you can decode reprb(msg) bytes safely, because eprb(msg) bytes only contain ascii printable chars
stru = {
    "msg": reprb(http_msg).decode(),
    "extra_info": "whatever",
}

json.dump(stru)
```

## Benchmark

``` bash
$ python3 test.py
Test:
(6/6) Testcases passed. 
dump/load test passed.
Bench:
built-in repr: 1.2180822410s, 183074666.47 bytes/s
built-in eval: 4.8808067660s, 131310258.88 bytes/s
reprb/dumpb: 0.7567997570s, 294661828.23 bytes/s
evalb/loadb: 1.2524397970s, 491999696.49 bytes/s
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/testzero-wz/reprb",
    "name": "reprb",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3",
    "maintainer_email": null,
    "keywords": null,
    "author": "T3stzer0",
    "author_email": "testzero.wz@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/62/a4/2607fd041af9849c7dc3c4bcaf8c4647e294f3f2fe50c49601bde1a82e88/reprb-1.0.8.tar.gz",
    "platform": null,
    "description": "## reprb\n\nRepresent bytes with printable characters, similar to how python built-in functions repr() and eval() do.\n\n## why\n\nBytes objects in Python 3 can already be read from and written to files (like load/dump), but you can't easily understand or edit them when you open a binary file directly in a text editor.\n\n`reprb`'s goal is to dump bytes to printable bytes and load them back to the original bytes object quickly (at least faster than the built-in `repr()`), especially when you analysis/dump/load bytes contain both printable and unprintable characters(like http message), `reprb` make it more editable and understandable.\n\n## how\n### Install\n#### from pip\n``` bash\npython3 -m pip install reprb\n```\n\n#### from source\n```bash\ngit clone git@github.com:Testzero-wz/reprb.git\n\ncd reprb && pip3 install .\n```\n\n### Usage\n\nrepr bytes object:\n\n```python\n>>> from reprb import reprb, evalb\n>>> msg = \"abc123\\x00\\x07\\x11\\x90\\xff\u4e2d\u6587\u2116\".encode()\n>>> repr_bytes = reprb(msg)\n>>> repr_bytes\nb'abc123\\\\0\\\\a\\\\x11\\\\xc2\\\\x90\\\\xc3\\\\xbf\\\\xe4\\\\xb8\\\\xad\\\\xe6\\\\x96\\\\x87\\\\xe2\\\\x84\\\\x96'\n>>> eval_bytes = evalb(repr_bytes)\n>>> eval_bytes == msg\nTrue\n```\n\ndump/load bytes from/to file:\n\n```python\nfrom reprb import dump, load, load_iter\n\ndump_bytes = b\"abc123\\x00\\x07\\x84\\x96\"\ndump_file = \"dump.txt\"\n\n# dump bytes to file, seperate by \"\\n\" default\nwith open(dump_file, \"wb\") as f:\n\n    # dump bytes object\n    dump(dump_bytes, f)\n\n    # dump all bytes object in list\n    dump_bytes_list = [dump_bytes, dump_bytes, dump_bytes]\n    dump(dump_bytes_list, f)\n\n# load all bytes from file, seperate by \"\\n\" default\nload_bytes_from_path = load(dump_file)\n\n# load all bytes from file handler\nwith open(dump_file, \"rb\") as f:\n    load_bytes_from_file = load(f)\n\n# load iter\nload_bytes_from_iter = list(load_iter(dump_file))\n\nassert (\n    [dump_bytes] + dump_bytes_list\n    == load_bytes_from_path\n    == load_bytes_from_file\n    == load_bytes_from_iter\n)\n```\n\nIf you want to store bytes with a more formatable structure like json:\n``` python\nfrom reprb import reprb, evalb\n\n# you should decode reprb bytes since json.dump() only accept string object.\n# btw, you can decode reprb(msg) bytes safely, because eprb(msg) bytes only contain ascii printable chars\nstru = {\n    \"msg\": reprb(http_msg).decode(),\n    \"extra_info\": \"whatever\",\n}\n\njson.dump(stru)\n```\n\n## Benchmark\n\n``` bash\n$ python3 test.py\nTest:\n(6/6) Testcases passed. \ndump/load test passed.\nBench:\nbuilt-in repr: 1.2180822410s, 183074666.47 bytes/s\nbuilt-in eval: 4.8808067660s, 131310258.88 bytes/s\nreprb/dumpb: 0.7567997570s, 294661828.23 bytes/s\nevalb/loadb: 1.2524397970s, 491999696.49 bytes/s\n```\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Represent bytes with printable characters",
    "version": "1.0.8",
    "project_urls": {
        "Homepage": "https://github.com/testzero-wz/reprb"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "62a42607fd041af9849c7dc3c4bcaf8c4647e294f3f2fe50c49601bde1a82e88",
                "md5": "312d334e193ef535635512f8acb00ea6",
                "sha256": "f2a956d5836260d09b6db6b2441a80da13263c2727600ddc2b23018aec0e5598"
            },
            "downloads": -1,
            "filename": "reprb-1.0.8.tar.gz",
            "has_sig": false,
            "md5_digest": "312d334e193ef535635512f8acb00ea6",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3",
            "size": 7173,
            "upload_time": "2024-09-13T03:13:35",
            "upload_time_iso_8601": "2024-09-13T03:13:35.761530Z",
            "url": "https://files.pythonhosted.org/packages/62/a4/2607fd041af9849c7dc3c4bcaf8c4647e294f3f2fe50c49601bde1a82e88/reprb-1.0.8.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-09-13 03:13:35",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "testzero-wz",
    "github_project": "reprb",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "reprb"
}
        
Elapsed time: 5.15373s