util.py
3.24 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
# -*- coding: utf-8 -*-
import re
from htmlentitydefs import name2codepoint
from django.utils.encoding import smart_unicode, force_unicode
from django.conf.urls import defaults
from django.http import HttpResponseRedirect, Http404
from dictionary.models import Argument, Arg_Possibility, PositionCategory
def error_redirect(request, error, url='/'):
request.session['error'] = error
return HttpResponseRedirect(url)
def message_redirect(request, message, url='/'):
request.session['message'] = message
return HttpResponseRedirect(url)
def make_form(request, form_class, *args, **kwargs):
if request.POST.get('det', '') == form_class.base_fields['det'].initial:
return form_class(*args, data=request.POST, files=request.FILES, **kwargs)
else:
return form_class(*args, **kwargs)
def invert(l):
return dict((e,nr) for (nr,e) in enumerate(l))
def generator_slice(generator, count):
res = []
try:
for i in range(count):
res.append(generator.next())
except StopIteration:
pass
return res
def url(regex, view, **kwargs):
if 'name' not in kwargs:
kwargs['name'] = view
return defaults.url(regex, view, **kwargs)
def stringify_keys(dictionary):
return dict((keyword.encode('ascii'), value)
for keyword, value in dictionary.iteritems())
# copypasta ze standardowego modułu bisect
def bisect_left(a, x, lo=0, hi=None, cmp=None):
if cmp is None:
cmp = __builtins__.cmp
if lo < 0:
raise ValueError('lo must be non-negative')
if hi is None:
hi = len(a)
while lo < hi:
mid = (lo+hi)//2
if cmp(a[mid], x) < 0: lo = mid+1
else: hi = mid
return lo
def triple_arg_poss(arg, positions_cats_ls):
possibilities = []
if arg:
try: # TODO czy dobrze jest pomijac takie nieistniejace, argumenty
arg_obj = Argument.objects.get(text_rep=arg)
# jesli argument nie posiada atrybutow (np. refl) i jego atrybuty nie wystepuja tylko jako realizacje to ok
if arg_obj.atributes.count() == 0 or arg_obj.has_real_only_parameter():
pass
else:
return possibilities
except Argument.DoesNotExist:
return possibilities
for poss_cat in positions_cats_ls:
if poss_cat:
try:
possibility = Arg_Possibility.objects.get(argument=arg_obj,
position_category__category=poss_cat)
except Arg_Possibility.DoesNotExist:
position_category_obj = PositionCategory.objects.get(category=poss_cat)
possibility = Arg_Possibility(argument=arg_obj,
position_category=position_category_obj)
possibility.save()
possibilities.append(possibility)
else:
try:
possibility = Arg_Possibility.objects.get(argument=arg_obj,
position_category=None)
except Arg_Possibility.DoesNotExist:
possibility = Arg_Possibility(argument=arg_obj)
possibility.save()
possibilities.append(possibility)
return possibilities