Convert Django project files in the root to example project.
Move pexp project to 'example' folder too.
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
from django.contrib import admin
|
||||
from polymorphic.admin import PolymorphicParentModelAdmin, PolymorphicChildModelAdmin
|
||||
from pexp.models import *
|
||||
|
||||
|
||||
class ProjectChildAdmin(PolymorphicChildModelAdmin):
|
||||
base_model = Project
|
||||
|
||||
class ProjectAdmin(PolymorphicParentModelAdmin):
|
||||
base_model = Project
|
||||
child_models = (
|
||||
(Project, ProjectChildAdmin),
|
||||
(ArtProject, ProjectChildAdmin),
|
||||
(ResearchProject, ProjectChildAdmin),
|
||||
)
|
||||
|
||||
admin.site.register(Project, ProjectAdmin)
|
||||
|
||||
|
||||
|
||||
class ModelAChildAdmin(PolymorphicChildModelAdmin):
|
||||
base_model = ModelA
|
||||
|
||||
class ModelAAdmin(PolymorphicParentModelAdmin):
|
||||
base_model = ModelA
|
||||
child_models = (
|
||||
(ModelA, ModelAChildAdmin),
|
||||
(ModelB, ModelAChildAdmin),
|
||||
(ModelC, ModelAChildAdmin),
|
||||
)
|
||||
|
||||
admin.site.register(ModelA, ModelAAdmin)
|
||||
|
||||
|
||||
if 'Model2A' in globals():
|
||||
class Model2AChildAdmin(PolymorphicChildModelAdmin):
|
||||
base_model = Model2A
|
||||
|
||||
class Model2AAdmin(PolymorphicParentModelAdmin):
|
||||
base_model = Model2A
|
||||
child_models = (
|
||||
(Model2A, Model2AChildAdmin),
|
||||
(Model2B, Model2AChildAdmin),
|
||||
(Model2C, Model2AChildAdmin),
|
||||
)
|
||||
|
||||
admin.site.register(Model2A, Model2AAdmin)
|
||||
|
||||
|
||||
if 'UUIDModelA' in globals():
|
||||
class UUIDModelAChildAdmin(PolymorphicChildModelAdmin):
|
||||
base_model = UUIDModelA
|
||||
|
||||
class UUIDModelAAdmin(PolymorphicParentModelAdmin):
|
||||
base_model = UUIDModelA
|
||||
child_models = (
|
||||
(UUIDModelA, UUIDModelAChildAdmin),
|
||||
(UUIDModelB, UUIDModelAChildAdmin),
|
||||
(UUIDModelC, UUIDModelAChildAdmin),
|
||||
)
|
||||
|
||||
admin.site.register(UUIDModelA, UUIDModelAAdmin)
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
[
|
||||
{
|
||||
"pk": 1,
|
||||
"model": "pexp.project",
|
||||
"fields": {
|
||||
"topic": "John's gathering",
|
||||
"polymorphic_ctype": 2
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 2,
|
||||
"model": "pexp.project",
|
||||
"fields": {
|
||||
"topic": "Sculpting with Tim",
|
||||
"polymorphic_ctype": 3
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 3,
|
||||
"model": "pexp.project",
|
||||
"fields": {
|
||||
"topic": "Swallow Aerodynamics",
|
||||
"polymorphic_ctype": 4
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 2,
|
||||
"model": "pexp.artproject",
|
||||
"fields": {
|
||||
"artist": "T. Turner"
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 3,
|
||||
"model": "pexp.researchproject",
|
||||
"fields": {
|
||||
"supervisor": "Dr. Winter"
|
||||
}
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,106 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
This module is a scratchpad for general development, testing & debugging
|
||||
Well, even more so than pcmd.py. You best ignore p2cmd.py.
|
||||
"""
|
||||
import uuid
|
||||
|
||||
from django.core.management.base import NoArgsCommand
|
||||
from django.db.models import connection
|
||||
from pprint import pprint
|
||||
import time,sys
|
||||
|
||||
from pexp.models import *
|
||||
|
||||
def reset_queries():
|
||||
connection.queries=[]
|
||||
|
||||
def show_queries():
|
||||
print; print 'QUERIES:',len(connection.queries); pprint(connection.queries); print; connection.queries=[]
|
||||
|
||||
def print_timing(func, message='', iterations=1):
|
||||
def wrapper(*arg):
|
||||
results=[]
|
||||
reset_queries()
|
||||
for i in xrange(iterations):
|
||||
t1 = time.time()
|
||||
x = func(*arg)
|
||||
t2 = time.time()
|
||||
results.append((t2-t1)*1000.0)
|
||||
res_sum=0
|
||||
for r in results: res_sum +=r
|
||||
median = res_sum / len(results)
|
||||
print '%s%-19s: %.4f ms, %i queries (%i times)' % (
|
||||
message,func.func_name,
|
||||
res_sum,
|
||||
len(connection.queries),
|
||||
iterations
|
||||
)
|
||||
sys.stdout.flush()
|
||||
return wrapper
|
||||
|
||||
class Command(NoArgsCommand):
|
||||
help = ""
|
||||
|
||||
def handle_noargs(self, **options):
|
||||
if False:
|
||||
ModelA.objects.all().delete()
|
||||
a=ModelA.objects.create(field1='A1')
|
||||
b=ModelB.objects.create(field1='B1', field2='B2')
|
||||
c=ModelC.objects.create(field1='C1', field2='C2', field3='C3')
|
||||
reset_queries()
|
||||
print ModelC.base_objects.all();
|
||||
show_queries()
|
||||
|
||||
if False:
|
||||
ModelA.objects.all().delete()
|
||||
for i in xrange(1000):
|
||||
a=ModelA.objects.create(field1=str(i%100))
|
||||
b=ModelB.objects.create(field1=str(i%100), field2=str(i%200))
|
||||
c=ModelC.objects.create(field1=str(i%100), field2=str(i%200), field3=str(i%300))
|
||||
if i%100==0: print i
|
||||
|
||||
f=print_timing(poly_sql_query,iterations=1000)
|
||||
f()
|
||||
|
||||
f=print_timing(poly_sql_query2,iterations=1000)
|
||||
f()
|
||||
|
||||
return
|
||||
|
||||
nModelA.objects.all().delete()
|
||||
a=nModelA.objects.create(field1='A1')
|
||||
b=nModelB.objects.create(field1='B1', field2='B2')
|
||||
c=nModelC.objects.create(field1='C1', field2='C2', field3='C3')
|
||||
qs=ModelA.objects.raw("SELECT * from pexp_modela")
|
||||
for o in list(qs): print o
|
||||
|
||||
from django.db import connection, transaction
|
||||
from random import Random
|
||||
rnd=Random()
|
||||
|
||||
def poly_sql_query():
|
||||
cursor = connection.cursor()
|
||||
cursor.execute("""
|
||||
SELECT id, pexp_modela.field1, pexp_modelb.field2, pexp_modelc.field3
|
||||
FROM pexp_modela
|
||||
LEFT OUTER JOIN pexp_modelb
|
||||
ON pexp_modela.id = pexp_modelb.modela_ptr_id
|
||||
LEFT OUTER JOIN pexp_modelc
|
||||
ON pexp_modelb.modela_ptr_id = pexp_modelc.modelb_ptr_id
|
||||
WHERE pexp_modela.field1=%i
|
||||
ORDER BY pexp_modela.id
|
||||
""" % rnd.randint(0,100) )
|
||||
#row=cursor.fetchone()
|
||||
return
|
||||
|
||||
def poly_sql_query2():
|
||||
cursor = connection.cursor()
|
||||
cursor.execute("""
|
||||
SELECT id, pexp_modela.field1
|
||||
FROM pexp_modela
|
||||
WHERE pexp_modela.field1=%i
|
||||
ORDER BY pexp_modela.id
|
||||
""" % rnd.randint(0,100) )
|
||||
#row=cursor.fetchone()
|
||||
return
|
||||
@@ -0,0 +1,35 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
This module is a scratchpad for general development, testing & debugging.
|
||||
"""
|
||||
|
||||
from django.core.management.base import NoArgsCommand
|
||||
from django.db.models import connection
|
||||
from pprint import pprint
|
||||
|
||||
from pexp.models import *
|
||||
|
||||
def reset_queries():
|
||||
connection.queries=[]
|
||||
|
||||
def show_queries():
|
||||
print; print 'QUERIES:',len(connection.queries); pprint(connection.queries); print; connection.queries=[]
|
||||
|
||||
class Command(NoArgsCommand):
|
||||
help = ""
|
||||
|
||||
def handle_noargs(self, **options):
|
||||
Project.objects.all().delete()
|
||||
a=Project.objects.create(topic="John's gathering")
|
||||
b=ArtProject.objects.create(topic="Sculpting with Tim", artist="T. Turner")
|
||||
c=ResearchProject.objects.create(topic="Swallow Aerodynamics", supervisor="Dr. Winter")
|
||||
print Project.objects.all()
|
||||
print
|
||||
|
||||
ModelA.objects.all().delete()
|
||||
a=ModelA.objects.create(field1='A1')
|
||||
b=ModelB.objects.create(field1='B1', field2='B2')
|
||||
c=ModelC.objects.create(field1='C1', field2='C2', field3='C3')
|
||||
print ModelA.objects.all()
|
||||
print
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
This module is a scratchpad for general development, testing & debugging
|
||||
"""
|
||||
|
||||
from django.core.management.base import NoArgsCommand
|
||||
from django.db.models import connection
|
||||
from pprint import pprint
|
||||
import sys
|
||||
from pexp.models import *
|
||||
|
||||
num_objects=1000
|
||||
|
||||
def reset_queries():
|
||||
connection.queries=[]
|
||||
|
||||
def show_queries():
|
||||
print; print 'QUERIES:',len(connection.queries); pprint(connection.queries); print; reset_queries()
|
||||
|
||||
import time
|
||||
|
||||
###################################################################################
|
||||
### benchmark wrappers
|
||||
|
||||
def print_timing(func, message='', iterations=1):
|
||||
def wrapper(*arg):
|
||||
results=[]
|
||||
reset_queries()
|
||||
for i in xrange(iterations):
|
||||
t1 = time.time()
|
||||
x = func(*arg)
|
||||
t2 = time.time()
|
||||
results.append((t2-t1)*1000.0)
|
||||
res_sum=0
|
||||
for r in results: res_sum +=r
|
||||
median = res_sum / len(results)
|
||||
print '%s%-19s: %.0f ms, %i queries' % (
|
||||
message,func.func_name,
|
||||
median,
|
||||
len(connection.queries)/len(results)
|
||||
)
|
||||
sys.stdout.flush()
|
||||
return wrapper
|
||||
|
||||
def run_vanilla_any_poly(func, iterations=1):
|
||||
f=print_timing(func,' ', iterations)
|
||||
f(nModelC)
|
||||
f=print_timing(func,'poly ', iterations)
|
||||
f(ModelC)
|
||||
|
||||
|
||||
###################################################################################
|
||||
### benchmarks
|
||||
|
||||
def bench_create(model):
|
||||
for i in xrange(num_objects):
|
||||
model.objects.create(field1='abc'+str(i), field2='abcd'+str(i), field3='abcde'+str(i))
|
||||
#print 'count:',model.objects.count()
|
||||
|
||||
def bench_load1(model):
|
||||
for o in model.objects.all():
|
||||
pass
|
||||
|
||||
def bench_load1_short(model):
|
||||
for i in xrange(num_objects/100):
|
||||
for o in model.objects.all()[:100]:
|
||||
pass
|
||||
|
||||
def bench_load2(model):
|
||||
for o in model.objects.all():
|
||||
f1=o.field1
|
||||
f2=o.field2
|
||||
f3=o.field3
|
||||
|
||||
def bench_load2_short(model):
|
||||
for i in xrange(num_objects/100):
|
||||
for o in model.objects.all()[:100]:
|
||||
f1=o.field1
|
||||
f2=o.field2
|
||||
f3=o.field3
|
||||
|
||||
def bench_delete(model):
|
||||
model.objects.all().delete()
|
||||
|
||||
###################################################################################
|
||||
### Command
|
||||
|
||||
class Command(NoArgsCommand):
|
||||
help = ""
|
||||
|
||||
def handle_noargs(self, **options):
|
||||
func_list = [
|
||||
( bench_delete, 1 ),
|
||||
( bench_create, 1 ),
|
||||
( bench_load1, 5 ),
|
||||
( bench_load1_short, 5 ),
|
||||
( bench_load2, 5 ),
|
||||
( bench_load2_short, 5 )
|
||||
]
|
||||
for f,iterations in func_list:
|
||||
run_vanilla_any_poly(f,iterations=iterations)
|
||||
|
||||
print
|
||||
@@ -0,0 +1,30 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
This module is a scratchpad for general development, testing & debugging
|
||||
"""
|
||||
|
||||
from django.core.management.base import NoArgsCommand
|
||||
from django.db.models import connection
|
||||
from pprint import pprint
|
||||
|
||||
from pexp.models import *
|
||||
|
||||
def reset_queries():
|
||||
connection.queries=[]
|
||||
|
||||
def show_queries():
|
||||
print; print 'QUERIES:',len(connection.queries); pprint(connection.queries); print; connection.queries=[]
|
||||
|
||||
class Command(NoArgsCommand):
|
||||
help = ""
|
||||
|
||||
def handle_noargs(self, **options):
|
||||
Project.objects.all().delete()
|
||||
o=Project.objects.create(topic="John's gathering")
|
||||
o=ArtProject.objects.create(topic="Sculpting with Tim", artist="T. Turner")
|
||||
o=ResearchProject.objects.create(topic="Swallow Aerodynamics", supervisor="Dr. Winter")
|
||||
print Project.objects.all()
|
||||
print
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from django.db import models
|
||||
|
||||
from polymorphic import PolymorphicModel, PolymorphicManager, PolymorphicQuerySet
|
||||
from polymorphic.showfields import ShowFieldContent, ShowFieldType, ShowFieldTypeAndContent
|
||||
|
||||
class Project(ShowFieldContent, PolymorphicModel):
|
||||
topic = models.CharField(max_length=30)
|
||||
class ArtProject(Project):
|
||||
artist = models.CharField(max_length=30)
|
||||
class ResearchProject(Project):
|
||||
supervisor = models.CharField(max_length=30)
|
||||
|
||||
class ModelA(ShowFieldTypeAndContent, PolymorphicModel):
|
||||
field1 = models.CharField(max_length=10)
|
||||
class ModelB(ModelA):
|
||||
field2 = models.CharField(max_length=10)
|
||||
class ModelC(ModelB):
|
||||
field3 = models.CharField(max_length=10)
|
||||
|
||||
class nModelA(models.Model):
|
||||
field1 = models.CharField(max_length=10)
|
||||
class nModelB(nModelA):
|
||||
field2 = models.CharField(max_length=10)
|
||||
class nModelC(nModelB):
|
||||
field3 = models.CharField(max_length=10)
|
||||
|
||||
# for Django 1.2+, test models with same names in different apps
|
||||
# (the other models with identical names are in polymorphic/tests.py)
|
||||
from django import VERSION as django_VERSION
|
||||
if not (django_VERSION[0]<=1 and django_VERSION[1]<=1):
|
||||
class Model2A(PolymorphicModel):
|
||||
field1 = models.CharField(max_length=10)
|
||||
class Model2B(Model2A):
|
||||
field2 = models.CharField(max_length=10)
|
||||
class Model2C(Model2B):
|
||||
field3 = models.CharField(max_length=10)
|
||||
|
||||
try: from polymorphic.test_tools import UUIDField
|
||||
except: pass
|
||||
if 'UUIDField' in globals():
|
||||
class UUIDModelA(ShowFieldTypeAndContent, PolymorphicModel):
|
||||
uuid_primary_key = UUIDField(primary_key = True)
|
||||
field1 = models.CharField(max_length=10)
|
||||
class UUIDModelB(UUIDModelA):
|
||||
field2 = models.CharField(max_length=10)
|
||||
class UUIDModelC(UUIDModelB):
|
||||
field3 = models.CharField(max_length=10)
|
||||
Reference in New Issue
Block a user