<jupyter_start><jupyter_text>jsonpicklethe elegent json serialisation library<jupyter_code>pip install jsonpickle<jupyter_output>Collecting jsonpickle
  Downloading jsonpickle-1.3-py2.py3-none-any.whl (32 kB)
Installing collected packages: jsonpickle
Successfully installed jsonpickle-1.3
Note: you may need to restart the kernel to use updated packages.<jupyter_text>directory()simple object browser for diagnostics purpose<jupyter_code>def directory(object):
    print(type(object))
    for key in dir(object):
        if key.startswith('_'):
            continue
        value = getattr(object, key)
        print('\t@%s: %s = %s'%(key, type(value), value))<jupyter_output><empty_output><jupyter_text>perdyanother simple function to print the object perdy like<jupyter_code>def perdy(obj):
    keys = filter(lambda x: not x.startswith('_'), dir(obj))
    d = dict(map(lambda x: (x, getattr(obj,x)), keys))
    print('name={name}\ndob={dob}\nage={age}'.format(**d))<jupyter_output><empty_output><jupyter_text>import<jupyter_code>import json
from datetime import datetime, time
from jsonpickle import encode, decode<jupyter_output><empty_output><jupyter_text>Persona base class to pickle<jupyter_code>class Person(object):
    '''
    base class for all people types
    '''
    
    def __init__(self,
        name=None,
        dob=None,
    ):
        self.name = name
        self.dob = dob
    
    @property
    def age(self):
        return (datetime.now() - self.dob).days/365.25<jupyter_output><empty_output><jupyter_text>Parentthat extends Person and links to Child<jupyter_code>class Parent(Person):
    '''
    lets test inheritance
    '''

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.children = set()

    def add_child(self, child):
        self.children.add(child)
        child.parents.add(self)<jupyter_output><empty_output><jupyter_text>Childthat extends Person and links to Parent<jupyter_code>class Child(Person):
    '''
    lets test relationships
    '''

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.parents = set()

    def add_parent(self, parent):
        self.parents.add(parent)
        parent.children.add(self)<jupyter_output><empty_output><jupyter_text>parentlets create a parent<jupyter_code>dob = datetime.strptime("1970-01-01 00:00:00", "%Y-%m-%d %H:%M:%S")
parent = Parent(name='Dad', dob=dob)
#directory(parent)
perdy(parent)<jupyter_output>name=Dad
dob=1970-01-01 00:00:00
age=50.223134839151264<jupyter_text>childcreate a child and link it to the parent<jupyter_code>dob = datetime.strptime("2000-01-11 00:00:00", "%Y-%m-%d %H:%M:%S")
child = Child(name='Son', dob=dob)
parent.add_child(child)
#directory(child)
perdy(child)<jupyter_output>name=Son
dob=2000-01-11 00:00:00
age=20.197125256673512<jupyter_text>encodecreate a json string representation of the object<jupyter_code>pip install Perdy
js = json.loads(encode(parent))

from Perdy.pretty import prettyPrintLn

prettyPrintLn(js)<jupyter_output>[35m{[0m
  "[36mchildren[0m" : [35m{[0m
    "[32mpy/set[0m" : [36m[[0m
      [35m{[0m
        "[36mdob[0m" : [35m{[0m
          "[32m__reduce__[0m" : [36m[[0m
            [35m{[0m
              "[32mpy/type[0m" : "[31mdatetime.datetime[0m"
            [35m}[0m,
            [36m[[0m
              "[31mB9ABCwAAAAAAAA==[0m"
            [36m][0m
          [36m][0m,
          "[32mpy/object[0m" : "[31mdatetime.datetime[0m"
        [35m}[0m,
        "[32mname[0m" : "[31mSon[0m",
        "[36mparents[0m" : [35m{[0m
          "[32mpy/set[0m" : [36m[[0m
            [35m{[0m
              "[32mpy/id[0m" : [31m0[0m
            [35m}[0m
          [36m][0m
        [35m}[0m,
        "[32mpy/object[0m" : "[31m__main__.Child[0m"
      [35m}[0m
    [36m][0m
  [35m}[0m,
  "[36mdob[0m" : [35m{[0m
    "[32m__reduce__[0m" : [36m[[0m
      [35m{[0m
        "[32mpy/type[0m" : "[31mdatetime.datetime[0m"
      [35m}[0m,[...]<jupyter_text>decodere-construct a python class from json stringmake sure you put classes=[] to reconstitute the correct classes<jupyter_code>spouse = decode(json.dumps(js), classes=[Parent, Child])
#directory(spouse)
perdy(spouse)<jupyter_output>name=Dad
dob=1970-01-01 00:00:00
age=50.223134839151264
{'fraction_non_alphanumeric': 0.16517344360211347, 'fraction_numerical': 0.0629450953365495, 'mean_word_length': 3.864804469273743, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '392680', 'license': 'MIT', 'n_tokens_mistral': 1895, 'n_tokens_neox': 1653, 'n_words': 254, 'path': 'Pickelry.ipynb', 'repo_name': 'eddo888/Carnets', 'size': '4353'}