-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdjangocon.py
More file actions
213 lines (150 loc) · 3.72 KB
/
djangocon.py
File metadata and controls
213 lines (150 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#! from __future__ import print_function
#! import os
#! import sys
#! from prescons.utils import clear_screen
#! from django.conf import settings
#! settings.configure()
#! clear_screen()
###
#
# Advanced Python through Django: Metaclasses
#
# Peter Inglesby
# @inglesp
#
# 16th May 2013
#
# https://github.com/inglesp/Metaclasses
#
###
# The purpose of this talk is to discuss:
# - what metaclasses are, and how they work
# - what they are useful for, and how Django uses them
# Feedback please!
import django
django.get_version()
#! clear_screen()
# Example (i)
from django import forms
class PonyForm(forms.Form):
name = forms.CharField()
colour = forms.CharField()
birthday = forms.DateField()
PonyForm.base_fields
PonyForm.__dict__.keys()
#! clear_screen()
# Example (ii)
from django.db import models
class EquineBase(models.Model):
class Meta:
app_label = 'ponies'
abstrac = True
#! clear_screen()
# Example (iii)
class Stable(models.Model):
class Meta:
app_label = 'ponies'
name = models.CharField()
s = Stable()
s.pony_set
class Pony(models.Model):
class Meta:
app_label = 'ponies'
name = models.CharField()
stable = models.ForeignKey(Stable)
s.pony_set
Stable.__dict__.keys()
#! clear_screen()
# Classes: a recap
class SomeClass(object):
class_attr = 123
def __init__(self, x):
self.instance_attr = x
instance = SomeClass(123)
type(instance)
type(SomeClass)
isinstance(SomeClass, object)
#! clear_screen()
# We can create classes dynamically
name = 'ExampleClass'
bases = (object,)
attrs = {
'__init__': lambda self: print('Hello from __init__!')
}
ExampleClass = type(name, bases, attrs)
ExampleClass
type(ExampleClass)
instance = ExampleClass()
instance
type(instance)
#! clear_screen()
# We can control how classes are created
def create_class(name, bases, attrs):
# We can do anything here!
return type(name, bases, attrs)
#! clear_screen()
# Example of creating a class dynamically
def create_form_class(name, attrs):
bases = (object,)
fields = {k: v for k, v in attrs.items() if isinstance(v, forms.Field)}
attrs['base_fields'] = fields
return type(name, bases, attrs)
name = 'PonyForm'
attrs = {
'name': forms.CharField(),
'colour': forms.CharField(),
'birthday': forms.DateField()
}
PonyForm = create_form_class(name, attrs)
PonyForm
PonyForm.base_fields
#! clear_screen()
# Q: What is `type`?
type(type)
# A: `type` itself is a class
#! clear_screen()
# We can subclass `type`
class FormType(type):
def __new__(cls, name, bases, attrs):
fields = {k: v for k, v in attrs.items() if isinstance(v, forms.Field)}
attrs['base_fields'] = fields
return type.__new__(cls, name, bases, attrs)
name = 'PonyForm'
bases = (object,)
attrs = {
'name': forms.CharField(),
'colour': forms.CharField(),
'birthday': forms.DateField()
}
PonyForm = FormType(name, bases, attrs)
PonyForm
PonyForm.base_fields
#! clear_screen()
# Specifying a metaclass
class Form(object):
__metaclass__ = FormType
class PonyForm(Form):
name = forms.CharField()
colour = forms.CharField()
birthday = forms.DateField()
PonyForm.__metaclass__
PonyForm.base_fields
#! clear_screen()
# More examples in Django
os.system("cd " + os.path.dirname(django.__file__) + "; grep -r '__metaclass__ =' *")
#! clear_screen()
# Python 3
# class Form(metaclass=Formtype):
# ...
#! clear_screen()
# The Zen of Python
import this
#! clear_screen()
# Key takeaways:
# * Classes are factories for creating objects
# * Metaclasses are factories for creating classes
# * Metaclasses allow us to control how classes are created
#! clear_screen()
# Dziękuję bardzo!
# Questions?
# Feedback please!