forked from shnela/python_course
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add serializing chapter and move some files from Misc
- Loading branch information
Showing
14 changed files
with
86 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Serialization | ||
## [JSON] | ||
[python json] | ||
* [json_example.py](../450%20-%20serialization/json_example.py) | ||
|
||
## [Pickle] | ||
### Examples | ||
* [pickle_example.py](../450%20-%20serialization/pickle_example.py) | ||
* [pickle_example_ml.py](../450%20-%20serialization/pickle_example_ml.py) | ||
|
||
## Exercises: | ||
TODO | ||
|
||
|
||
[python json]: https://docs.python.org/3/library/json.html | ||
[JSON]: https://www.w3schools.com/whatis/whatis_json.asp | ||
[Pickle]: https://docs.python.org/3/library/pickle.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import json | ||
from decimal import Decimal | ||
|
||
|
||
def direct_conversion(obj): | ||
obj_json = json.dumps(obj) | ||
deserialized_object = json.loads(obj_json) | ||
return deserialized_object | ||
|
||
|
||
def file_conversion(obj, filename): | ||
with open(filename, 'w') as f: | ||
json.dump(obj, f) | ||
with open(filename, 'r') as f: | ||
deserialized_object = json.load(f) | ||
return deserialized_object | ||
|
||
|
||
if __name__ == '__main__': | ||
elements = [1, 2, 3.14, 'simple_text'] | ||
direct_conversion(elements) | ||
file_conversion(elements, './list.json') | ||
|
||
""" | ||
What's the problem with float? | ||
>>> 1.1 + 2.2 | ||
3.3000000000000003 | ||
>>> Decimal('1.1') + Decimal('2.2') | ||
Decimal('3.3') | ||
""" | ||
|
||
elements.append(Decimal(10)) | ||
json.dumps(elements) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import pickle | ||
from decimal import Decimal | ||
|
||
|
||
def direct_conversion(obj): | ||
obj_json = pickle.dumps(obj) | ||
deserialized_object = pickle.loads(obj_json) | ||
return deserialized_object | ||
|
||
|
||
def file_conversion(obj, filename): | ||
with open(filename, 'wb') as f: | ||
pickle.dump(obj, f) | ||
with open(filename, 'rb') as f: | ||
deserialized_object = pickle.load(f) | ||
return deserialized_object | ||
|
||
|
||
if __name__ == '__main__': | ||
elements = [1, 2, 3.14, 'simple_text'] | ||
direct_conversion(elements) | ||
direct_conversion(elements) | ||
file_conversion(elements, './list.json') | ||
|
||
""" | ||
What's the problem with float? | ||
>>> 1.1 + 2.2 | ||
3.3000000000000003 | ||
>>> Decimal('1.1') + Decimal('2.2') | ||
Decimal('3.3') | ||
""" | ||
|
||
elements.append(Decimal(10)) | ||
pickled_elements = pickle.dumps(elements) | ||
print(pickled_elements) |
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.