Doing magic with python metaclasses
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Doing magic with python metaclasses

on

  • 9,138 views

 

Statistics

Views

Total Views
9,138
Views on SlideShare
4,444
Embed Views
4,694

Actions

Likes
5
Downloads
60
Comments
1

18 Embeds 4,694

http://agiliq.com 3763
http://uswaretech.com 873
http://einztein.com 16
http://127.0.0.1:8000 13
http://127.0.0.1 6
https://agiliq.com 5
http://translate.googleusercontent.com 4
http://buyitnw.appspot.com 2
http://www.slideshare.net 2
http://xss.yandex.net 2
http://djangovideos.com 1
http://feeds.feedburner.com 1
http://www.iweb34.com 1
http://webcache.googleusercontent.com 1
http://dev.agiliq.com 1
https://www.4proxy.de 1
http://beta.einztein.com 1
http://www.agiliq.com 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Doing magic with python metaclasses Presentation Transcript

  • 1. Python Meta classes and how Django uses them Lakshman Prasad
  • 2. Meta Classes  A Design pattern  Dynamically create classes  Enables Flexibility, Good API  Used by Frameworks  Perform “Magic”
  • 3. Where Meta Classes?  Frameworks, frameworks, frameworks.  Django uses them extensively.  Scrappy, a web scrapping framework.  Flexible Django apps. Eg: Django-registration
  • 4. Executes the code when encountered Creates namespace and stores all variables in it
  • 5. `type` is an inbuilt MetaClass Dynamic class = Static classes
  • 6. `__metaclass__` defines metaclases
  • 7. Meta Class Magic Define a class extending object Examined type is string, Magic! Enables Declarative Syntax
  • 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. Django Models
  • 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. 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. Django Forms
  • 13.  Rendering in html  Validation for right data  Displaying errors  Customization options  Provided model, storing to the database
  • 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. Dynamic Django Forms - 2 When high complexity, instantiate a form, dynamically
  • 16. Resources Python, Django Documentation, Source. Pro Django, by Marty Alchin Django-registration v0.7 by James Bennett
  • 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