Skip to main content

TypedBencode

Make it easy to create round-trippable bencoding for objects.

Usage

import typed_bencode
from typing import List

my_type = typed_bencode.for_dict(a=str, b=int, c=bytes, d=List[str])
val = {"a": "hello", "b": 123, "c": b'asd', "d":["hey", "there"]}
encoded = my_type.encode(val)
print(encoded) # => b'd1:a5:hello1:bi123e1:c3:asd1:dl3:hey5:thereee'
print(my_type.decode(encoded) == val) # => True

You can even compose types

my_other_type = typed_bencode.for_dict(a=my_type, b=int)

encoded2 = my_other_type.encode({"a": {"a": "helo", "b": 123, "c": b'asd', "d": ["asd", "asd"]}, "b":123})
print(encoded2) # => b'd1:ad1:a4:helo1:bi123e1:c3:asd1:dl3:asd3:asdee1:bi123ee'

Custom types

You can specify a custom type

class DateEncoder(typed_bencode.StringEncoder):
    def to_bytes(self, val):
        return super().to_bytes(val.isoformat())

class DateDecoder(typed_bencode.StringDecoder):
    def from_bytes(self, b):
        v, pos = super().from_bytes(b)
        return (datetime.datetime.fromisoformat(v), pos)

class DateType(typed_bencode.BaseType):
    def __init__(self):
        super().__init__()
        self.encoder = DateEncoder(self)
        self.decoder = DateDecoder(self)

my_type = DateType()
val = datetime.datetime.now()
encoded = my_type.encode(val) # => '26:2018-06-06T12:12:12.363636'

Metadata

Release files for typed-bencode 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for typed-bencode 0.0.1
File Size Uploaded
typed_bencode-0.0.1.tar.gz 4.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for typed-bencode 0.0.1
File Interpreter ABI Platform
typed_bencode-0.0.1-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 7.4 kB

Release files / typed_bencode-0.0.1.tar.gz

Download URL typed_bencode-0.0.1.tar.gz
Size 4.2 kB
Tags Source
SHA-256 checksum
How to use checksums
f0a4f0d05fa47cf0920cc1c8a1404a3cdad2eefae93d1caf8e5e8134ccd213a3
BLAKE2b-256 checksum
How to use checksums
8ee88ca9a6ec786248e848fe23cd86374eac1708d97164093ea7b15daceb1f39
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.0

Release files / typed_bencode-0.0.1-py2.py3-none-any.whl

Download URL typed_bencode-0.0.1-py2.py3-none-any.whl
Size 3.2 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
5c50be9f07b9b20b6b01f426ab7582d5b82a8519a0b160e5bec5a989dc554f44
BLAKE2b-256 checksum
How to use checksums
04cd104decde5668fcaa604fcf6691d3b3dc6f2ba62849bb9924c1e444904705
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.0

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page