File: example.py

package info (click to toggle)
serpent 1.42-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 704 kB
  • sloc: java: 4,025; cs: 3,675; python: 1,734; xml: 149; makefile: 38; sh: 11
file content (37 lines) | stat: -rw-r--r-- 968 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import datetime
import serpent


class CustomClass(object):
    def __init__(self, name, age):
        self.name = name
        self.age = age


def example():
    data = {
        "tuple": (1, 2, 3),
        "date": datetime.datetime.now(),
        "set": {'a', 'b', 'c'},
        "class": CustomClass("Sally", 26)
    }

    # serialize the object
    ser = serpent.dumps(data, indent=True)
    # print it to the screen, but usually you'd save the bytes to a file or transfer them over a network connection
    print("Serialized data:")
    print(ser.decode("UTF-8"))

    # deserialize the bytes and print the objects
    obj = serpent.loads(ser)
    print("Deserialized data:")
    print("tuple:", obj["tuple"])
    print("date:", obj["date"])
    print("set:", obj["set"])
    clazz = obj["class"]
    print("class attributes: type={0} name={1} age={2}".format(
        clazz["__class__"], clazz["name"], clazz["age"]))


if __name__ == "__main__":
    example()