Putting it together
- Python course
- Expert
- Lesson 47/47
- 6 min
Combine all expert topics into a working app.
Combine all layers
In the final lesson you combine all expert topics in one application. The abs_val function calls libc.abs; Pool.map applies it in parallel to the list [-1, -2, -3].
This combines the metaprogramming (Field + ModelMeta), multiprocessing (Pool) and ctypes (CDLL + abs) layers. The starting code includes the previous four lessons' solutions. You only add the abs_val function and the Pool.map call. Result: [1, 2, 3].
Practice Combine everything: abs_val + Pool.map
The starting code already has Tool, Field, ModelMeta, square and libc. Now write an abs_val(x) function returning libc.abs(x). Inside a with Pool() as p: block assign p.map(abs_val, [-1, -2, -3]) to result. Print result.
Starter code
# Build the Tool class with type()
Tool = type("Tool", (), {
"name": "arac",
"run": lambda self: f"{self.name} calisti",
})
# Add Field descriptor and ModelMeta
class Field:
def __set_name__(self, owner, name):
self.name = name
if not hasattr(owner, "fields"):
owner.fields = []
owner.fields.append(name)
class ModelMeta(type):
def __new__(mcs, name, bases, ns):
return super().__new__(mcs, name, bases, ns)
class Model(metaclass=ModelMeta):
ad = Field()
# Add parallel square computation
from multiprocessing import Pool
def square(x):
return x * x
# Add native abs call
import ctypes
libc = ctypes.CDLL(None)
# Integration: abs_val and Pool.map
# call libc.abs inside abs_val
# process the list of negative numbers
# print the resultSample solution
# Build the Tool class with type()
Tool = type("Tool", (), {
"name": "arac",
"run": lambda self: f"{self.name} calisti",
})
# Add Field descriptor and ModelMeta
class Field:
def __set_name__(self, owner, name):
self.name = name
if not hasattr(owner, "fields"):
owner.fields = []
owner.fields.append(name)
class ModelMeta(type):
def __new__(mcs, name, bases, ns):
return super().__new__(mcs, name, bases, ns)
class Model(metaclass=ModelMeta):
ad = Field()
# Add parallel square computation
from multiprocessing import Pool
def square(x):
return x * x
# Add native abs call
import ctypes
libc = ctypes.CDLL(None)
def abs_val(x):
return libc.abs(x)
with Pool() as p:
result = p.map(abs_val, [-1, -2, -3])
print(result)Expected output
[1, 2, 3]
Project complete
Congratulations! You completed the expert final. In five lessons you built a data processing toolkit: a dynamic class with type(), metaprogramming with Field + ModelMeta, parallel processing with Pool, and native code calls with CDLL.
In the final lesson you combined everything: the abs_val function calls libc.abs and runs in parallel with Pool.map. This brings together the core techniques of real Python frameworks. All expert-level topics are now at your fingertips.
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Match all expert concepts
| Term | Meaning |
|---|---|
type() |
Dynamic class |
Field |
Descriptor |
Pool |
Parallel pool |
CDLL |
C library |
2 Complete the abs_val definition
def abs_val(x):
return libc.____(x)
def abs_val(x):
return libc.abs(x)
Explanation: abs_val calls the libc.abs function.
3 What does the integration do?
- Only uses CDLL
- Only uses Pool
- Only uses type()
- Calls abs_val in parallel with Pool.map Correct answer
Explanation: The integration calls abs_val in parallel with Pool.map; it combines all layers.