From 16d5e9e6976f257f1336e231d61b8e6ac8f23547 Mon Sep 17 00:00:00 2001 From: jkushner Date: Mon, 20 Jun 2022 20:54:26 +0200 Subject: [PATCH] Add serializing chapter and move some files from Misc --- 450 - Serialization/README.md | 17 +++++++++ 450 - Serialization/json_example.py | 33 +++++++++++++++++ 450 - Serialization/pickle_example.py | 35 +++++++++++++++++++ .../pickle_example_ml.py | 0 500 - Misc/decorators.py | 27 -------------- 500 - Misc/function_args.py | 35 ------------------- 500 - Misc/json_example.py | 25 ------------- {500 - Misc => 500 - Python style}/README.md | 8 +---- .../enum_exercise.py | 0 .../enum_exercise_solution.py | 0 .../if_name_main/__init__.py | 0 .../if_name_main/file1.py | 0 .../if_name_main/file2.py | 0 .../python_path.py | 0 14 files changed, 86 insertions(+), 94 deletions(-) create mode 100644 450 - Serialization/README.md create mode 100644 450 - Serialization/json_example.py create mode 100644 450 - Serialization/pickle_example.py rename 500 - Misc/pickle_example.py => 450 - Serialization/pickle_example_ml.py (100%) delete mode 100644 500 - Misc/decorators.py delete mode 100644 500 - Misc/function_args.py delete mode 100644 500 - Misc/json_example.py rename {500 - Misc => 500 - Python style}/README.md (94%) rename {500 - Misc => 500 - Python style}/enum_exercise.py (100%) rename {500 - Misc => 500 - Python style}/enum_exercise_solution.py (100%) rename {500 - Misc => 500 - Python style}/if_name_main/__init__.py (100%) rename {500 - Misc => 500 - Python style}/if_name_main/file1.py (100%) rename {500 - Misc => 500 - Python style}/if_name_main/file2.py (100%) rename {500 - Misc => 850 - os module}/python_path.py (100%) diff --git a/450 - Serialization/README.md b/450 - Serialization/README.md new file mode 100644 index 0000000..b0e82ff --- /dev/null +++ b/450 - Serialization/README.md @@ -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 diff --git a/450 - Serialization/json_example.py b/450 - Serialization/json_example.py new file mode 100644 index 0000000..83f5ef5 --- /dev/null +++ b/450 - Serialization/json_example.py @@ -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) diff --git a/450 - Serialization/pickle_example.py b/450 - Serialization/pickle_example.py new file mode 100644 index 0000000..c177a5e --- /dev/null +++ b/450 - Serialization/pickle_example.py @@ -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) diff --git a/500 - Misc/pickle_example.py b/450 - Serialization/pickle_example_ml.py similarity index 100% rename from 500 - Misc/pickle_example.py rename to 450 - Serialization/pickle_example_ml.py diff --git a/500 - Misc/decorators.py b/500 - Misc/decorators.py deleted file mode 100644 index a1bc55c..0000000 --- a/500 - Misc/decorators.py +++ /dev/null @@ -1,27 +0,0 @@ -from functools import wraps - - -def parametrized_deco(paramter): - def inner_deco(f): - print('Decorator starts') - - @wraps(f) - def inner_func(*args, **kwargs): - print(f'Inner starts {paramter}') - result = f(*args, **kwargs) - print(f'Inner ends {paramter}') - return result - - print('Decorator ends') - return inner_func - return inner_deco - - -@parametrized_deco('paramter value') -def simple_print_args(arg1, arg2): - print(arg1) - print(arg2) - - -if __name__ == '__main__': - simple_print_args('arg1_val', 'arg2_val') diff --git a/500 - Misc/function_args.py b/500 - Misc/function_args.py deleted file mode 100644 index 8db350f..0000000 --- a/500 - Misc/function_args.py +++ /dev/null @@ -1,35 +0,0 @@ -def sample_function(arg): - print(arg) - - -def get_all_args(*args): - print(args) - - -def get_all_kwargs(**kwargs): - print(kwargs) - - -def mixed_model(var1, *remaining_args, kwarg1, **remining_kwargs): - print(var1) - print(remaining_args) - print(kwarg1) - print(remining_kwargs) - - -def real_life_example(arg1, arg2, *, confusing_parameter=False): - """confusing parameter has to be ALWAYS passed explicitly""" - - -if __name__ == '__main__': - sample_function('val1') - sample_function(arg='val1') - get_all_args('val1', 'val2') - # get_all_args('val1', some_fun_var='val2') - # get_all_kwargs('val1') - get_all_kwargs(some_fun_var='val1', some_fun_var2='val2') - mixed_model('val1', 'val2', kwarg1=1) - mixed_model(*['val1', 'val2'], **dict(kwarg1=1)) - - # real_life_example('val1', 'val2', True) - real_life_example('val1', 'val2', confusing_parameter=True) diff --git a/500 - Misc/json_example.py b/500 - Misc/json_example.py deleted file mode 100644 index fec4c8a..0000000 --- a/500 - Misc/json_example.py +++ /dev/null @@ -1,25 +0,0 @@ -"""Change file name to json""" -import json -from decimal import Decimal - - -def direct_conversion(obj): - obj_json = json.dumps(obj) - print(obj_json) - print(json.loads(obj_json)) - - -def file_conversion(obj, filename): - with open(filename, 'w') as f: - json.dump(obj, f) - with open(filename, 'r') as f: - print(json.load(f)) - - -if __name__ == '__main__': - l1 = [1, 2, 3.14, 'simple_text'] - direct_conversion(l1) - file_conversion(l1, './list.json') - - l1.append(Decimal(10)) - direct_conversion(l1) diff --git a/500 - Misc/README.md b/500 - Python style/README.md similarity index 94% rename from 500 - Misc/README.md rename to 500 - Python style/README.md index 5a7e3b3..87dfc3e 100644 --- a/500 - Misc/README.md +++ b/500 - Python style/README.md @@ -1,4 +1,4 @@ -# Misc +# Python style ## [PEP 20 - The Zen of Python] ```python @@ -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) @@ -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 diff --git a/500 - Misc/enum_exercise.py b/500 - Python style/enum_exercise.py similarity index 100% rename from 500 - Misc/enum_exercise.py rename to 500 - Python style/enum_exercise.py diff --git a/500 - Misc/enum_exercise_solution.py b/500 - Python style/enum_exercise_solution.py similarity index 100% rename from 500 - Misc/enum_exercise_solution.py rename to 500 - Python style/enum_exercise_solution.py diff --git a/500 - Misc/if_name_main/__init__.py b/500 - Python style/if_name_main/__init__.py similarity index 100% rename from 500 - Misc/if_name_main/__init__.py rename to 500 - Python style/if_name_main/__init__.py diff --git a/500 - Misc/if_name_main/file1.py b/500 - Python style/if_name_main/file1.py similarity index 100% rename from 500 - Misc/if_name_main/file1.py rename to 500 - Python style/if_name_main/file1.py diff --git a/500 - Misc/if_name_main/file2.py b/500 - Python style/if_name_main/file2.py similarity index 100% rename from 500 - Misc/if_name_main/file2.py rename to 500 - Python style/if_name_main/file2.py diff --git a/500 - Misc/python_path.py b/850 - os module/python_path.py similarity index 100% rename from 500 - Misc/python_path.py rename to 850 - os module/python_path.py