diff --git a/1_core_language/3_datamodel/1_objects_values_and_types/def.txt b/1_core_language/3_datamodel/1_objects_values_and_types/def.txt index b889351..a851227 100644 --- a/1_core_language/3_datamodel/1_objects_values_and_types/def.txt +++ b/1_core_language/3_datamodel/1_objects_values_and_types/def.txt @@ -1,5 +1,13 @@ What is the Data model? +def data model: + a means by which we can implement protocols. + the protocol meaning is determined by the specific type. + example: + - __add__ for a string means concatenate + - __add__ for a Polynomial means add the coefficients + - ... + 1. objects, values and types What is?: id() diff --git a/2_standard_library/4_built_in_types/12_context_manager_types/def.txt b/2_standard_library/4_built_in_types/12_context_manager_types/def.txt new file mode 100644 index 0000000..e69de29 diff --git a/2_standard_library/4_built_in_types/5_iterator_types/2_generator.txt b/2_standard_library/4_built_in_types/5_iterator_types/2_generator.txt index 68cbf72..85b359f 100644 --- a/2_standard_library/4_built_in_types/5_iterator_types/2_generator.txt +++ b/2_standard_library/4_built_in_types/5_iterator_types/2_generator.txt @@ -15,8 +15,9 @@ def generator | generator function: - use cases: - process or generate N values without using up N memory. Especially useful when N is large or even greater than all available memory. - - Just In Time processing: generate values as they become available and process them at call/client side. - + - Just In Time processing: generate values as they become available and return them for processing them at call/client side. + - create coroutines, which allows interleaving client code with library code: + - this enables sequencing - proof: None. It is a definition. References: diff --git a/4_experiments/1_core_language/3_datamodel/3_3_special_method_names/3_3_3_customizing_class_creation/3_3_3_1_metaclasses/library_code_constraint_enforcement/__build_class__/def.txt b/4_experiments/1_core_language/3_datamodel/3_3_special_method_names/3_3_3_customizing_class_creation/3_3_3_1_metaclasses/library_code_constraint_enforcement/__build_class__/def.txt new file mode 100644 index 0000000..e69de29 diff --git a/4_experiments/1_core_language/3_datamodel/3_3_special_method_names/3_3_3_customizing_class_creation/3_3_3_1_metaclasses/library_code_constraint_enforcement/metaclass/library.py b/4_experiments/1_core_language/3_datamodel/3_3_special_method_names/3_3_3_customizing_class_creation/3_3_3_1_metaclasses/library_code_constraint_enforcement/metaclass/library.py new file mode 100644 index 0000000..46ad378 --- /dev/null +++ b/4_experiments/1_core_language/3_datamodel/3_3_special_method_names/3_3_3_customizing_class_creation/3_3_3_1_metaclasses/library_code_constraint_enforcement/metaclass/library.py @@ -0,0 +1,35 @@ + + +class BaseMeta(type): + """ + objective: Implements solution 3. metaclass. + Features: + - allows interception of construction of derived classes. + - they have various data model methods yo can use. + + """ + + def __new__(cls, name, basses, body): + # print('Base.__meta__', cls, name, basses, body) + if not 'foo' in body: + raise TypeError("Missing `foo` method on a subclass") + return super().__new__(cls, name, basses, body) + + +class Base(metaclass=BaseMeta): + def foo(self): + """ + Observation: user.py code might not implement `foo` and this code will break. + Question: + 1. How do you as a library author ensure that user code implements this? + i.e. Enforce constraints on classes that Derive from Base + + + """ + return self.foo() + + +""" +References: + Powell, J. 2017. What Does It Take To Be An Expert At Python. PyData +""" diff --git a/4_experiments/1_core_language/3_datamodel/3_3_special_method_names/3_3_3_customizing_class_creation/3_3_3_1_metaclasses/library_code_constraint_enforcement/metaclass/user.py b/4_experiments/1_core_language/3_datamodel/3_3_special_method_names/3_3_3_customizing_class_creation/3_3_3_1_metaclasses/library_code_constraint_enforcement/metaclass/user.py new file mode 100644 index 0000000..41a0bca --- /dev/null +++ b/4_experiments/1_core_language/3_datamodel/3_3_special_method_names/3_3_3_customizing_class_creation/3_3_3_1_metaclasses/library_code_constraint_enforcement/metaclass/user.py @@ -0,0 +1,21 @@ +from library import Base + + +class Derived(Base): + """ + Observation: Base.foo is not implemented. + """ + """ + # implementing this will fix the error as expected. + def foo(self): + return "foo" + """ + + +if __name__ == "__main__": + c = Derived() + +""" +References: + Powell, J. 2017. What Does It Take To Be An Expert At Python. PyData +""" diff --git a/4_experiments/1_core_language/3_datamodel/3_3_special_method_names/3_3_3_customizing_class_creation/3_3_3_1_metaclasses/library_code_constraint_enforcement/scenarios.txt b/4_experiments/1_core_language/3_datamodel/3_3_special_method_names/3_3_3_customizing_class_creation/3_3_3_1_metaclasses/library_code_constraint_enforcement/scenarios.txt new file mode 100644 index 0000000..9de004b --- /dev/null +++ b/4_experiments/1_core_language/3_datamodel/3_3_special_method_names/3_3_3_customizing_class_creation/3_3_3_1_metaclasses/library_code_constraint_enforcement/scenarios.txt @@ -0,0 +1,38 @@ +two teams: + - library: infrastructure code for reuse by other engineers + - user: code used to build software for users + +issues: + - library code needs to enforce that user code implements the necessary interface + solutions: + 1. Wrap invocation in Exception handling + try: + return self.abstract_method() + except Exception: print("handle") + + Assessment: Not Preferred. Only fails at runtime. + + 2. __build_class__ + Take advantage of the fact that Python is Protocol Orientated. Use __build_class__ + NB: All statements (except 2) in Python are runtime executable. + Example: + 1. This runs + for _ in range(10): + class Bar: pass + 2. class Test: + for _ range(10): + def foo(): + pass + + Assessment: Not Preferred. + + 3. metaclasses + - example: What Does It Take To Be An Expert At Python/metaclasses/library_code_constraint_enforcement/metaclasses + + Assessment: Preferred. + + 4. __init_subclass__ + Assessment: Preferred. + +References: + Powell, J. 2017. What Does It Take To Be An Expert At Python. PyData \ No newline at end of file diff --git a/4_experiments/2_standard_library/29_python_runtime_services/5_top_level_code_environment/bar.py b/4_experiments/2_standard_library/29_python_runtime_services/5_top_level_code_environment/bar.py new file mode 100755 index 0000000..b14862b --- /dev/null +++ b/4_experiments/2_standard_library/29_python_runtime_services/5_top_level_code_environment/bar.py @@ -0,0 +1,12 @@ +from foo import foo + + +def bar(): + print("In bar function") + print(f"top-level code module: {__name__}") + foo() + + +if __name__ == "__main__": + print("In bar module, as top-level code environment") + bar() diff --git a/4_experiments/2_standard_library/29_python_runtime_services/5_top_level_code_environment/foo.py b/4_experiments/2_standard_library/29_python_runtime_services/5_top_level_code_environment/foo.py new file mode 100755 index 0000000..3de3b10 --- /dev/null +++ b/4_experiments/2_standard_library/29_python_runtime_services/5_top_level_code_environment/foo.py @@ -0,0 +1,13 @@ +""" +top level code is also called the entry-point. +""" + + +def foo(): + print("In foo function") + print(f"top-level code module: {__name__}") + + +if __name__ == "__main__": + print("In foo module, as top-level code environment") + foo() diff --git a/4_experiments/2_standard_library/2_builtin_functions/hasattr/user_code_constraint_enforcement/library.py b/4_experiments/2_standard_library/2_builtin_functions/hasattr/user_code_constraint_enforcement/library.py new file mode 100644 index 0000000..e02cf09 --- /dev/null +++ b/4_experiments/2_standard_library/2_builtin_functions/hasattr/user_code_constraint_enforcement/library.py @@ -0,0 +1,10 @@ +class Base(): + def food(self): + "Intentional typo of `foo` as `food" + return "foo" + + +""" +References: + Powell, J. 2017. What Does It Take To Be An Expert At Python. PyData +""" diff --git a/4_experiments/2_standard_library/2_builtin_functions/hasattr/user_code_constraint_enforcement/scenarios.txt b/4_experiments/2_standard_library/2_builtin_functions/hasattr/user_code_constraint_enforcement/scenarios.txt new file mode 100644 index 0000000..242971b --- /dev/null +++ b/4_experiments/2_standard_library/2_builtin_functions/hasattr/user_code_constraint_enforcement/scenarios.txt @@ -0,0 +1,12 @@ +two teams: + - library: infrastructure code for reuse by other engineers + - user: code used to build software for users + +issues: + - user code needs to enforce that library code posses necessary interface + solutions: + - assert interface with hasattr: + example: What Does It Take To Be An Expert At Python/metaclasses/user_code_constraint_enforcement + +References: + Powell, J. 2017. What Does It Take To Be An Expert At Python. PyData \ No newline at end of file diff --git a/4_experiments/2_standard_library/2_builtin_functions/hasattr/user_code_constraint_enforcement/user.py b/4_experiments/2_standard_library/2_builtin_functions/hasattr/user_code_constraint_enforcement/user.py new file mode 100644 index 0000000..ab331d9 --- /dev/null +++ b/4_experiments/2_standard_library/2_builtin_functions/hasattr/user_code_constraint_enforcement/user.py @@ -0,0 +1,29 @@ +from library import Base + + +assert hasattr( + Base, 'foo'), f"Library code for Base has no `foo` method" + + +class Derived(Base): + def foo(self): + """" + Observation: This could break if foo is not defined + Question: + 1. How can prevent this failing before production environment? + - Solution: + 1. write test for bar method in unit tests + 2. check that method exists and fail otherwise(i.e. enforce a constraint) using an assert, like above. + """ + return self.foo() + + +if __name__ == "__main__": + c = Derived() + c.foo() + + +""" +References: + Powell, J. 2017. What Does It Take To Be An Expert At Python. PyData +"""