11. The magic (1/4)
from django.conf import settings
for concept_name, mapping in settings.CONCEPTS.items():
for klass, fields in mapping.items():
...
● The concepts app
○ Loops over settings.CONCEPTS
12. The magic (2/4)
from concepts.util import load_class
● Create a dynamic class
● With
in
...
concept = type(
proxy = True
Meta
concept_name,
(load_class(klass),),
{
'__module__': __name__,
'Meta': type('Meta', (), {'proxy': True}),
},
)
...
13. The magic (3/4)
...
for _property, _field in fields.items():
if not hasattr(concept, _property):
setattr(
concept,
_property,
property(
lambda self: getattr(self, _field),
lambda self, value:
setattr(self, _field, value),
),
)
...
● Create properties for
all fields
14. The magic (4/4)
import concepts
● Bind the concept to
the concepts module
...
setattr(concepts, concept_name, concept)
15. Conclusion
● Concepts make apps reusable
○ Without dependencies
● The only dependency
○ Is a concept
● The concept producer
○ You may choose yourself
○ On existing apps
16. Try it yourself
● github.com /
douwevandermeij /
concepts_example
● Related work
○ Django Swappable Models
■ Like swapping auth.User model in 1.5+
○ Zope Component Architecture
■ Interfaces: implement/adapt
17. Thank you!
Douwe van der Meij
Goldmund, Wyldebeast & Wunderliebe
vandermeij@gw20e.com
github.com/douwevandermeij