Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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
"""
Original file line number Diff line number Diff line change
@@ -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
"""
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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()
Original file line number Diff line number Diff line change
@@ -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()
Original file line number Diff line number Diff line change
@@ -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
"""
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
"""