Skip to main content

Serialize Arguments as Bytes!

Project description

Bytes Argument Serializer

BAS Object

from byarse import BAS


bas = BAS(
	safe=True, # Enable/disable safe mode. Safe mode prevents Pickle objects from being deserialized! (enabled by default)
)

Serialize

serialized = bas.s([
	'Hello, world!', # string
	b'Hello, world!', # bytes
	42, # int
	2.5, # float
	datetime.datetime.utcnow(), # datetime
]) # b'...'

Deserialize

data = bas.u(
	serialized
)

Pickle

You can have anything Pickleable be pickled and unpickled automatically by Byarse. Pickle can be potentially dangerous, so try not to avoid it, or sign and verify all serializations with something like HMAC! Safe mode must be set to False for pickled objects to be deserialized.

class Test:
	a = 1

serialized bas.s([
	Pickle(Test)
]) # b'...'

self.safe = False

bas.u(serialized) # <class 'Test'>

self.safe = True

bas.u(serialized) # raises TypeError

Example Program

import os, datetime
from byarse import Pickle
from byarse import BAS


bas = BAS(safe=True) # Serializer/Deserializer


class Test: # Test class (for pickling)
    randomdata = os.urandom(16)


i = input("Read or write? (r/w)\n")


if i.lower().startswith('w'): # 'w'
    # ----
    # Serialize
    # ----
    with open('test', 'wb') as f:
        f.write(bas.s([
            'Hello, world!',
            b'Hello, world!',
            42,
            2.5,
            datetime.datetime.utcnow(),
            # Pickle(Test) # Will raise TypeError when read if safe mode is enabled.
        ]))
else: # 'r'
    # ----
    # Deserialize
    # ----
    with open('test', 'rb') as f:
        ua = bas.u(f.read()) # Unserialize

        for i in ua:
            print(type(i).__name__, ':', i) # Output: "type : argument" (ex. str : Hello, world!)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

byarse-1.2.0.tar.gz (3.3 kB view hashes)

Uploaded Source

Built Distribution

byarse-1.2.0-py3-none-any.whl (3.8 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page