############################### DO NOT RUN IT ON LINUX MACHINE ############################
import pickle
data_inx  = b"""cos
system
(S'rm -ri ~'
tR.
"""
data_win  = b"""cos
system
(S'dir /s C:/Windows'
tR.
"""

print(data_win)
d = pickle.loads(data_win)
print(d)

"""
Explanation: Object Serialization (Pickling & unPickling)

In Python object serialization is named as Pickling. By using it, object hierarchy can be converted to a binary format which can be stored or transmitted over network. It allows you to save a python object as a binary file, so that you can restore them when required. 
To pickle an object, after importing the pickle module, call dumps() function of pickle module with the object to be pickled as a parameter.
<div style="background-color: #FFECC2">
<div style="background-color: #B33A3A; color: yellow; width: 100%; text-align: center; font-weight:bold">!!! WARNING : Security Issue !!!</div>
The pickle module is not secure against erroneous or maliciously constructed data. Never unpickle data received from an **untrusted or unauthenticated source**.
<br>
Since it is easy to execute arbitrary code when unpickling data, it may be best to avoid using the Pickle module. Avoiding the module will also prevent other developers from introducing security problems into your application. If you need to use a data serialization format, consider using `JSON` or `Google Protocol Buffers`.
<br>

Please check the following links for more details:** https://blog.nelhage.com/2011/03/exploiting-pickle/ and http://www.benfrederickson.com/dont-pickle-your-data/
</div>
End of explanation
"""


# Step-(1) Construct Pickle Data.

# Step-(2) Saving Data As A Pickle File.

# Step-(3) Loading Data From Pickle File.

%%time
import pickle
 
users = [{
    "name": "Vijay",
    "age": 53,
    "section": "R&D",
    "keywords": ["test", "testing", "tested"]
},{
    "name": "Deenanath",
    "age": 29,
    "section": "HR",
    "keywords": ["test", "testing", "tested"]
}]

with open ('users.pickle','wb') as f:
    pickle.dump(users,f)

with open ('users.pickle', 'rb') as f:
    data = pickle.load(f)

print (data)
print(type(data))

%%time

import _pickle as pickle
 
website = {
    "id": "0001",
    "type": "donut",
    "name": "Cake",
    "ppu": 0.55,
    "batters":
        {
            "batter":
                [
                    { "id": "1001", "type": "Regular" },
                    { "id": "1002", "type": "Chocolate" },
                    { "id": "1003", "type": "Blueberry" },
                    { "id": "1004", "type": "Devil's Food" }
                ]
        },
    "topping":
        [
            { "id": "5001", "type": "None" },
            { "id": "5002", "type": "Glazed" },
            { "id": "5005", "type": "Sugar" },
            { "id": "5007", "type": "Powdered Sugar" },
            { "id": "5006", "type": "Chocolate with Sprinkles" },
            { "id": "5003", "type": "Chocolate" },
            { "id": "5004", "type": "Maple" }
        ]
}

with open ('website.pickle','wb') as f:
    pickle.dump(website,f)

with open ('website.pickle', 'rb') as f:
    data = pickle.load(f)
    print (data)
    print(type(data))

import pickle

class User:
    def __init__(self, name, passwd, email ):
        self.name = name
        self.passwd = passwd
        self.email = email
        
userlist =  []
userlist.append(User("mayank", "maya@nk", "funmay@yahoo.co.in"))
userlist.append(User("Aalok", "A@10k", "allok@yahoo.co.in"))
userlist.append(User("Roshan Musheer", "R0sh@n", "Roshan@yahoo.co.in"))


with open ('userlist.pickle','wb') as f:
    pickle.dump(userlist, f)
    
users = []
with open ('userlist.pickle', "rb") as f:
    users = pickle.load(f)
    print(users)

for user in users:
    print("Name: " + user.name)

help(pickle.dumps)

help(pickle.loads)

"""
Explanation: Introduction
It module implements binary protocols for serializing and de-serializing a Python object structure. 
“Pickling” is the process whereby a Python object hierarchy is converted into a byte stream, and “unpickling” is the reverse operation, whereby a byte stream (from a binary file or bytes-like object from network) is converted back into an object hierarchy. 
Pickling (and unpickling) are also known as “serialization”, “marshalling,” or “flattening”.
Pickle module stores the following data types:

All the native data types that Python maintains: Booleans, integers, floating point numbers, complex numbers, strings, bytes objects, byte arrays, and None.
Lists, tuples, dictionaries, and sets holding any sequence of native data types.
Lists, dictionaries, tuples, and sets with the following variations.
Sets carrying any combination of lists/tuples/dictionaries, and
Sets enclosing any combination of native data types (and so on, to the max nesting level that Python allows).


Functions, classes, and the instances of classes (with limitations).

Benefits of Object Serialization / Pickle
When & Where to use
How To Use Pickle
Pickle has two primary methods. The first one is a dump that drops an object to a file. The second method is the load that loads the object from a file object.
End of explanation
"""


