Python Meta classes and how
Django uses them Lakshman Prasad
Meta Classes

   A Design pattern

   Dynamically create classes

   Enables Flexibility, Good API

   Used by Framewo...
Where Meta Classes?

   Frameworks, frameworks, frameworks.

   Django uses them extensively.

   Scrappy, a web scrapp...
Executes the code when encountered

Creates namespace and stores all variables in it
`type` is an inbuilt MetaClass

Dynamic class = Static classes
`__metaclass__` defines metaclases
Meta Class Magic

Define a class extending object

Examined type is string, Magic!

Enables Declarative Syntax
MetaClasses, not always

   Simple function decorators

   Unintuitive to end developers, unless
    explained

   Pyth...
Django Models
Django Models Creation

   Declarative Syntax, No `__metaclass__`

   New model created in place

   Options into `Opti...
Inside Django Models

   Return the same registered Model from
    AppCache

   Original options and Meta options stored...
Django Forms
   Rendering in html

   Validation for right data

   Displaying errors

   Customization options

   Provided model...
Dynamic Django forms - 1

Forms API is flexible, but often needs Meta Classes

`base_fields` and `fields` list store all f...
Dynamic Django Forms - 2

When high complexity, instantiate a form,
dynamically
Resources

Python, Django Documentation, Source.

Pro Django, by Marty Alchin

Django-registration v0.7 by James Bennett
Thank You!

lakshman@uswaretech.com

             : Build Cutting Edge Web Apps.

Small Team of expert Django Developers.
...
Doing magic with python metaclasses
Upcoming SlideShare
Loading in...5
×

Doing magic with python metaclasses

8,032

Published on

Published in: Technology
1 Comment
5 Likes
Statistics
Notes
No Downloads
Views
Total Views
8,032
On Slideshare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
61
Comments
1
Likes
5
Embeds 0
No embeds

No notes for slide

Transcript of "Doing magic with python metaclasses"

  1. 1. Python Meta classes and how Django uses them Lakshman Prasad
  2. 2. Meta Classes  A Design pattern  Dynamically create classes  Enables Flexibility, Good API  Used by Frameworks  Perform “Magic”
  3. 3. Where Meta Classes?  Frameworks, frameworks, frameworks.  Django uses them extensively.  Scrappy, a web scrapping framework.  Flexible Django apps. Eg: Django-registration
  4. 4. Executes the code when encountered Creates namespace and stores all variables in it
  5. 5. `type` is an inbuilt MetaClass Dynamic class = Static classes
  6. 6. `__metaclass__` defines metaclases
  7. 7. Meta Class Magic Define a class extending object Examined type is string, Magic! Enables Declarative Syntax
  8. 8. MetaClasses, not always  Simple function decorators  Unintuitive to end developers, unless explained  Python 2.6+ has Class Decorators  But being powerful, where they are needed, indespesible
  9. 9. Django Models
  10. 10. Django Models Creation  Declarative Syntax, No `__metaclass__`  New model created in place  Options into `Options` object  Register in registry called `AppCache`  Add attributes to new model
  11. 11. Inside Django Models  Return the same registered Model from AppCache  Original options and Meta options stored in `_meta`  Add more useful attributes/classes like `DoesnotExist`, `module_name` and `class_name`
  12. 12. Django Forms
  13. 13.  Rendering in html  Validation for right data  Displaying errors  Customization options  Provided model, storing to the database
  14. 14. Dynamic Django forms - 1 Forms API is flexible, but often needs Meta Classes `base_fields` and `fields` list store all fields Change field attributes while initialization
  15. 15. Dynamic Django Forms - 2 When high complexity, instantiate a form, dynamically
  16. 16. Resources Python, Django Documentation, Source. Pro Django, by Marty Alchin Django-registration v0.7 by James Bennett
  17. 17. Thank You! lakshman@uswaretech.com : Build Cutting Edge Web Apps. Small Team of expert Django Developers. Exclusively develop in Django, Python stack. @becomingGuru, @uswaretech
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×