Skip to content

Instantly share code, notes, and snippets.

@irmen
Last active December 11, 2015 10:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save irmen/4588429 to your computer and use it in GitHub Desktop.
Save irmen/4588429 to your computer and use it in GitHub Desktop.
Simple example session with serpent serializer
>>> import decimal, uuid, datetime
>>> import serpent
>>> data={
... "str": "hello",
... "unicode": u"\u20ac",
... "numbers": [123456789012345678901234567890, 999.1234, 4+7j, decimal.Decimal("1.99999999999999999991")],
... "bytes": bytearray(20),
... "list": [1, 2, 3, 4],
... "tuple": (1, 2, 3, 4),
... "set": {1, 2, 3, 4},
... "dict": dict((i, str(i) * 4) for i in range(4)),
... "exc": ZeroDivisionError("fault"),
... "dates": [
... datetime.datetime.now(),
... datetime.time(23, 59, 45, 999888),
... datetime.timedelta(seconds=500)
... ],
... "uuid": uuid.uuid4()
... }
>>>
>>> ser=serpent.serialize(data, True)
>>>
>>> print(ser.decode('utf-8'))
# serpent utf-8 python3.3
{
'list': [
1,
2,
3,
4
],
'numbers': [
123456789012345678901234567890,
999.1234,
(4+7j),
'1.99999999999999999991'
],
'str': 'hello',
'tuple': (
1,
2,
3,
4
),
'dict': {
0: '0000',
1: '1111',
2: '2222',
3: '3333'
},
'dates': [
'2013-01-21T20:03:27.773968',
'23:59:45.999888',
500.0
],
'set': {
1,
2,
3,
4
},
'bytes': {
'data': 'AAAAAAAAAAAAAAAAAAAAAAAAAAA=',
'encoding': 'base64'
},
'exc': {
'__class__': 'ZeroDivisionError',
'message': 'fault',
'__exception__': True,
'args': (
'fault',
)
},
'unicode': '€',
'uuid': 'f7facf02-e384-4a2a-ac7b-d2444a7276cf'
}
>>>
>>> serpent.deserialize(ser)
{'list': [1, 2, 3, 4], 'numbers': [123456789012345678901234567890, 999.1234, (4+7j), '1.99999999999999999991'], 'str': 'hello', 'tuple': (1, 2, 3, 4), 'dates': ['2013-01-21T20:03:27.773968', '23:59:45.999888', 500.0], 'set': {1, 2, 3, 4}, 'dict': {0: '0000', 1: '1111', 2: '2222', 3: '3333'}, 'exc':
{'__class__': 'ZeroDivisionError', '__exception__': True, 'message': 'fault', 'args': ('fault',)}, 'unicode': '€', 'uuid': 'f7facf02-e384-4a2a-ac7b-d2444a7276cf', 'bytes': {'data': 'AAAAAAAAAAAAAAAAAAAAAAAAAAA=', 'encoding': 'base64'}}
>>>
>>> import ast
>>> ast.literal_eval(ser.decode('utf-8'))
{'list': [1, 2, 3, 4], 'numbers': [123456789012345678901234567890, 999.1234, (4+7j), '1.99999999999999999991'], 'str': 'hello', 'tuple': (1, 2, 3, 4), 'dates': ['2013-01-21T20:03:27.773968', '23:59:45.999888', 500.0], 'set': {1, 2, 3, 4}, 'dict': {0: '0000', 1: '1111', 2: '2222', 3: '3333'}, 'exc':
{'__class__': 'ZeroDivisionError', '__exception__': True, 'message': 'fault', 'args': ('fault',)}, 'unicode': '€', 'uuid': 'f7facf02-e384-4a2a-ac7b-d2444a7276cf', 'bytes': {'data': 'AAAAAAAAAAAAAAAAAAAAAAAAAAA=', 'encoding': 'base64'}}
>>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment