Skip to content

Commit

Permalink
Add serializing chapter and move some files from Misc
Browse files Browse the repository at this point in the history
  • Loading branch information
shnela committed Jun 20, 2022
1 parent 41ec546 commit 16d5e9e
Show file tree
Hide file tree
Showing 14 changed files with 86 additions and 94 deletions.
17 changes: 17 additions & 0 deletions 450 - Serialization/README.md
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
33 changes: 33 additions & 0 deletions 450 - Serialization/json_example.py
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)
35 changes: 35 additions & 0 deletions 450 - Serialization/pickle_example.py
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.
27 changes: 0 additions & 27 deletions 500 - Misc/decorators.py

This file was deleted.

35 changes: 0 additions & 35 deletions 500 - Misc/function_args.py

This file was deleted.

25 changes: 0 additions & 25 deletions 500 - Misc/json_example.py

This file was deleted.

8 changes: 1 addition & 7 deletions 500 - Misc/README.md β†’ 500 - Python style/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Misc
# Python style

## [PEP 20 - The Zen of Python]
```python
Expand Down Expand Up @@ -58,11 +58,6 @@ Python Lint tools:

### [🎡 The PEP 8 Song 🎡]

## [Pickle]
### Examples
* [json_example.py](json_example.py)
* [pickle_example.py](pickle_example.py)

## Exercises:
* [enum_exercise.py](enum_exercise.py)

Expand All @@ -74,4 +69,3 @@ Python Lint tools:
[flake8]:https://flake8.pycqa.org/en/latest/
[pylint]: https://pylint.org/
[🎡 The PEP 8 Song 🎡]: https://www.youtube.com/watch?v=hgI0p1zf31k
[Pickle]: https://docs.python.org/3/library/pickle.html
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 16d5e9e

Please sign in to comment.