Performace improvement & tests

This commit is contained in:
AppleGrew (applegrew) 2012-11-25 00:32:56 +05:30
parent 1c9a259de0
commit 434f89cdfc
7 changed files with 902955 additions and 20 deletions

View file

@ -0,0 +1,12 @@
The following license applies to the wordlist (for model testmain.word) included in testapp/testmain/fixtures/initial_data.json.
Src: http://dreamsteep.com/projects/the-english-open-word-list.html
======================================================================================
UK Advanced Cryptics Dictionary Licensing Information:
Copyright © J Ross Beresford 1993-1999. All Rights Reserved.
The following restriction is placed on the use of this publication: if the UK Advanced Cryptics Dictionary is used in a software package or redistributed in any form, the copyright notice must be prominently displayed and the text of this document must be included verbatim.
There are no other restrictions: I would like to see the list distributed as widely as possible.

View file

@ -296,7 +296,8 @@ class ChoiceMixin(object):
def __deepcopy__(self, memo):
result = super(ChoiceMixin, self).__deepcopy__(memo)
result._choices = copy.deepcopy(self._choices, memo)
if hasattr(self, '_choices'):
result._choices = copy.deepcopy(self._choices, memo)
return result
@ -407,6 +408,10 @@ class HeavySelect2FieldBaseMixin(object):
be raised.
"""
from . import util
if logger.isEnabledFor(logging.DEBUG):
t1 = util.timer_start('HeavySelect2FieldBaseMixin.__init__')
data_view = kwargs.pop('data_view', None)
choices = kwargs.pop('choices', [])
@ -423,17 +428,22 @@ class HeavySelect2FieldBaseMixin(object):
# This piece of code is needed here since (God knows why) Django's Field class does not call
# super(); because of that __init__() of classes would get called after Field.__init__().
# If did had super() call there then we could have simply moved AutoViewFieldMixin at the
# If it did had super() call there then we could have simply moved AutoViewFieldMixin at the
# end of the MRO list. This way it would have got widget instance instead of class and it
# could have directly set field_id on it.
if hasattr(self, 'field_id'):
self.widget.field_id = self.field_id
# ModelChoiceField will set this to ModelChoiceIterator
if not choices and hasattr(self, 'choices'):
choices = self.choices
self.choices = choices
if logger.isEnabledFor(logging.DEBUG):
t2 = util.timer_start('HeavySelect2FieldBaseMixin.__init__:choices initialization')
# ModelChoiceField will set self.choices to ModelChoiceIterator
if choices and not (hasattr(self, 'choices') and isinstance(self.choices, forms.models.ModelChoiceIterator)):
self.choices = choices
if logger.isEnabledFor(logging.DEBUG):
util.timer_end(t2)
util.timer_end(t1)
class HeavyChoiceField(ChoiceMixin, forms.Field):
"""

View file

@ -270,22 +270,30 @@ def get_field(id_):
"""
return __id_store.get(id_, None)
def timer_start(name):
import sys, time
if sys.platform == "win32":
# On Windows, the best timer is time.clock()
default_timer = time.clock
multiplier = 1.0
else:
# On most other platforms the best timer is time.time()
default_timer = time.time
multiplier = 1000.0
return (name, default_timer, multiplier, default_timer())
def timer_end(t):
(name, default_timer, multiplier, timeS) = t
timeE = default_timer()
logger.debug("Time taken by %s: %0.3f ms" % (name, (timeE - timeS) * multiplier))
def timer(f):
def inner(*args, **kwargs):
import sys, time
if sys.platform == "win32":
# On Windows, the best timer is time.clock()
default_timer = time.clock
multiplier = 1.0
else:
# On most other platforms the best timer is time.time()
default_timer = time.time
multiplier = 1000.0
timeS = default_timer()
t = timer_start(f.func_name)
ret = f(*args, **kwargs)
timeE = default_timer()
timer_end(t)
logger.debug("Time taken by %s: %0.3f ms" % (f.func_name, (timeE - timeS) * multiplier))
return ret
return inner

Binary file not shown.

File diff suppressed because it is too large Load diff

View file

@ -2,7 +2,7 @@ from django import forms
from django_select2 import *
from .models import Employee, Dept, ClassRoom, Lab
from .models import Employee, Dept, ClassRoom, Lab, Word
class EmployeeChoices(AutoModelSelect2Field):
queryset = Employee.objects
@ -16,6 +16,10 @@ class ClassRoomSingleChoices(AutoModelSelect2Field):
queryset = ClassRoom.objects
search_fields = ['number__icontains', ]
class WordChoices(AutoModelSelect2Field):
queryset = Word.objects
search_fields = ['word__icontains', ]
class EmployeeForm(forms.ModelForm):
manager = EmployeeChoices(required=False)
dept = ModelSelect2Field(queryset=Dept.objects)
@ -69,6 +73,7 @@ class MixedForm(forms.Form):
emp2 = EmployeeChoices()
rooms2 = ClassRoomChoices()
rooms3 = ClassRoomSingleChoices()
any_word = WordChoices()
self_choices = SelfChoices(label='Self copy choices')
self_multi_choices = SelfMultiChoices(label='Self copy multi-choices')

View file

@ -29,3 +29,8 @@ class Employee(models.Model):
def __unicode__(self):
return unicode(self.name)
class Word(models.Model):
word = models.CharField(max_length=15)
def __unicode__(self):
return unicode(self.word)