-
Notifications
You must be signed in to change notification settings - Fork 0
/
Serialisation.py
260 lines (203 loc) · 7.33 KB
/
Serialisation.py
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
### 02/06/2022 # Thursday # 08:00PM to 09:30PM ###
# WHAT IS SERIALISATION ?
'''
- Object serialization is the process of converting state of an object into byte stream.
- We also call this ‘serialization’, ‘marshalling’, or ‘flattening’.
'''
# WHAT IS DE-SERIALISATION ?
'''
- Deserialization is just a reverse process of serialization. Deserialization is the process of transforming a series of
bytes into a structured object.
'''
# ○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○
# JSON # JSON # JSON # JSON # JSON # JSON # JSON # JSON # JSON # JSON # JSON # JSON # JSON # JSON # JSON # JSON # JSON #
import json
'''
- JSON stands for JavaScript Object Notation.
- JSON is a common undestandable language.
- It's a light weight and human understandable.
- The Python module JSON converts a Python dictionary object into JSON object:
dict --->>> object
list, tuple --->>> array
str --->>> string
int, float --->>> number
True --->>> true
False --->>> false
None --->>> null
- Syntax: json.dump(dict, file_pointer)
'''
# Python data to JSON format by using the methods:
'''
1. Dump() - Python object to json file
2. Dumps() - Python object to json string
'''
# JSON format to Python data by using the methods:
'''
1. load() - json formatted stream to a Python object
2. loads() - json document to a python object
'''
# EXAMPLE: 1 -SERIALISATION
'''
dictionary = {'Name':'Neha', 'Age':27, 'City':'Mumbai', 'Email':'[email protected]', 'True':True, 'None':None}
with open('JSON_Dump.json', 'w') as fp:
data = json.dump(dictionary, fp)
print(data)
print(type(data))
'''
# EXAMPLE: 2 -SERIALISATION
'''
dictionary = {'Name':'Ruchita','Age':30,'City':'Panvel','Email':'[email protected]','True':True,'None':None}
with open('JSON_DumpS.json', 'w') as fp:
data = json.dumps(dictionary)
print(data)
print(type(data))
'''
# or
'''
dictionary = {'Name':'Ruchita','Age':30,'City':'Panvel','Email':'[email protected]','True':True,'None':None}
data = json.dumps(dictionary)
print(data)
print(type(data))
'''
# EXAMPLE: 3 -DE-SERIALISATION
'''
with open('JSON_Dump.json', 'r') as fp:
data = json.load(fp)
print(data)
'''
# EXAMPLE: 4 -DE-SERIALISATION
'''
dt = json.loads(data)
print(dt)
'''
# or
'''
dictionary = '{"Name":"Pata Nahi", "Age":"Yaad Nahi", "City":"Galaxy", "Email":"Google se pucho", "True":"True"}'
data = json.loads(dictionary)
print(data)
print(type(data))
print(type(dictionary))
'''
# EXAMPLE: 5 -CREATE DICTIONARY USING "__dict__"
'''
class Student:
def __init__(self, Name, Email, City):
self.Name = Name
self.Email = Email
self.City = City
def show(self):
print(self.Name)
print(self.Email)
print(self.City)
boy = Student('Nitin', '[email protected]', 'Sangli')
print(boy.__dict__)
girl = Student('Neha', '[email protected]', 'Mumbai')
print(girl.__dict__)
girl1 = Student('Ruchita', '[email protected]', 'Panvel')
print(girl1.__dict__)
'''
# ○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○
# PICKLE # PICKLE # PICKLE # PICKLE # PICKLE # PICKLE # PICKLE # PICKLE # PICKLE # PICKLE # PICKLE # PICKLE # PICKLE #
import pickle
'''
- It is also common understandable language.
- Python pickle module is used for serializing and de-serializing python object structures.
- The process to converts any kind of python objects (list, dict, etc.) into byte streams(binary) (0s and 1s) is called
pickling or serialization or flattening or marshalling.
- We can converts the byte stream (generated through pickling) back into python objects by a process called as
unpickling or de-serialisation.
- Only after importing pickle module we can do pickling and unpickling.
import pickle
'''
# METHODS
# Python data to Pickle format by using the methods:
'''
1. dump() − The dump() method serializes to an open file (file-like object).
2. dumps() − Serializes to a binary object.
'''
# Pickle format to Python data by using the methods:
'''
1. load() − Deserializes from an open file.
2. loads() − Deserializes from a binary object.
'''
# EXAMPLE: 1 -SERIALISATION
'''
dictionary = {'Name':'Nitin', 'Age':'26', 'Email':'[email protected]', 'City':'Sangli'}
with open('PICKLE_dump.pickle', 'wb') as fp:
data = pickle.dump(dictionary, fp)
print(data)
'''
# EXAMPLE: 2 -SERIALISATION
'''
dictionary = {"Name":"Neha", "Age":"27", "Email":"[email protected]", "City":"Mumbai"}
data = pickle.dumps(dictionary)
print(data)
'''
# EXAMPLE: 3 -DE-SERIALISATION
'''
with open('PICKLE_dump.pickle', 'rb') as fp:
data = pickle.load(fp)
print(data)
'''
# EXAMPLE: 4 -DE-SERIALISATION
'''
data = pickle.loads(b'\x80\x04\x95Q\x00\x00\x00\x00\x00\x00\x00}\x94(\x8c\x04Name\x94\x8c\x04Neha\x94\x8c\x03Age\x94\x8c\x0227\x94\x8c\x05Email\x94\x8c\[email protected]\x94\x8c\x04City\x94\x8c\x06Mumbai\x94u.')
print(data)
'''
# ○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○
### 03/06/2022 # Friday # 07:30PM to 09:00PM ###
# YAML # YAML # YAML # YAML # YAML # YAML # YAML # YAML # YAML # YAML # YAML # YAML # YAML # YAML # YAML # YAML # YAML #
'''pip install pyyaml'''
import yaml
'''
- YAML stands for YAML Ain't Markup Language.
- YAML a strict superset of JSON, so anything written in JSON can be parsed to YAML.
- The file extension for YAML files is .yaml or .yml
- Everything in the YAML is a key-value pair.
- Light in weight than JSON
- Install a Python module called the pyyaml to work with YAML files.
pip install pyyaml
'''
# METHODS
'''
1. dump() - Python dict to yaml
2. load() - Yaml to Python dict
'''
# EXAMPLE: 1 -SERIALISATION
'''
py_dict = {
'prd_name':'iron',
'prd_id':100,
'prd_qnty':1000
}
ys = yaml.dump(py_dict)
print(ys)
'''
# EXAMPLE: 2 -SERIALISATION
'''
d = """
- 'Nitin Patil'
- 26
"""
About = yaml.safe_load(d)
with open('YAML_sl.yaml', 'w') as fp:
yaml.dump(About, fp)
'''
# EXAMPLE: 3 -DE-SERIALISATION
'''
d = """
- 'Nitin Patil'
- 26
"""
Serial = yaml.safe_load(d)
print(Serial)
'''
# EXAMPLE: 4 -DE-SERIALISATION
'''
with open('YAML_sl.yaml' 'r') as fp:
data = yaml.safe_load(fp)
print(data)
'''
# ☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻