while True:
x = range(3)
import time
while True:
x = range(3)
time.sleep(0.1)
Минус в том, что после каждого выполнения скрипт убивается
Смущает его однопоточность.
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'', include('blog.urls')),
url(r'^post_list/', include('blog.urls')),
]
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^$', views.post_list, name='post_list'),
]
class FloodSafeMixin(object):
def __init__(self, *args, **kwargs):
request = kwargs.pop('request', None)
if request:
self._user = request.user.username if request.user.is_authenticated() else None
self._ip = get_ip(request)
self._period = OrderedDict()
self._period['days'] = kwargs.pop('days', None)
self._period['hours'] = kwargs.pop('hours', None)
self._period['minutes'] = kwargs.pop('minutes', None)
self._period['seconds'] = kwargs.pop('seconds', None)
self._period['milliseconds'] = kwargs.pop('milliseconds', None)
if not any(self._period.values()):
self._period = {'minutes': 1}
else:
self._period = OrderedDict([(k, v) for k, v in self._period.items() if v])
else:
self._ip = None
super(FloodSafeMixin, self).__init__(*args, **kwargs)
def clean(self):
cleaned_data = super(FloodSafeMixin, self).clean()
sender_id = hashlib.md5((self._user if self._user else self._ip).encode('utf-8')).hexdigest()
if sender_id:
class_name = self.__class__.__name__
cache_name = 'last-submit.{0}.{1}'.format(class_name, sender_id)
now = datetime.now()
last_submit = cache.get(cache_name, now - timedelta(days=1))
if (now - last_submit) < timedelta(**self._period):
cache.set(cache_name, now)
period = ' '.join([u'{0} {1}'.format(v, _p('genitive', k)) for k, v in self._period.items()])
raise forms.ValidationError(_('Form submitted less than %(period)s ago'), code='flood',
params={'period': period})
else:
if not self.errors:
cache.set(cache_name, now)
return cleaned_data
class FeedbackForm(FloodSafeMixin, forms.Form):
...
class SomeFormHandlerView(FormView):
def get_form_kwargs(self):
kwargs = super(GenericFormHandlerView, self).get_form_kwargs()
kwargs['request'] = self.request
kwargs['minutes'] = 2
return kwargs
...
import decimal
def drange(x, y, jump):
while x < y:
yield float(x)
x += decimal.Decimal(jump)
heights = range(126, 200 + 1, 1)
for height in heights:
weights = drange(30, 140 + 0.5, 0.5)
for weight in weights:
print(height, '-', weight)
import json
from django import forms
class SomeForm(forms.Form):
name = forms.CharField(label=u'Name', max_length=25)
def some_view(request):
json_string = request.POST.get('json_data')
form_data = json.loads(json_string)[0]
f = SomeForm(form_data)
if f.is_valid():
name = f.cleaned_data['name']
# ... Работа с данными ...
else:
# ... Обработка ошибки ...
tagnews = set(tag_tag | tag_city | tag_country)
from django.db.models import Q
tagnews = Entry.objects.filter(Q(tags__slug=slug) | Q(to_city__name_en=slug) | Q(to_city__country_code__name_en=slug))
CharInfo info = new CharInfo();
(_thiscall) CharInfo::CharInfo(info)