# -*- coding: utf-8 -*-
import operator
import datetime
import math

from django.http import Http404, HttpResponse, HttpResponseRedirect
from django.core.urlresolvers import reverse, resolve
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.views.decorators.cache import never_cache
from django.db.models import Q, Max, Min
from django import db
from django.conf import settings
from django.template.defaultfilters import date as tmp_date

from user_registration.func import *
from kinoinfo_folder.func import low, capit, del_separator
from api.models import FilmsName
from api.views import get_film_data, film_poster2
from base.models import *
from base.func import create_kinoafisha_button
from articles.views import pagination as pagi
from movie_online.IR import check_int_rates

def get_bg(request, site=None):
    new_view = 1
    if '/dajaxice/' in request.path or '/user/' in request.path and '/login/' not in request.path:
        new_view = 0

    btype = '2'

    price = get_adv_price(btype)

    advert_img, advert_url, advert_id = ('', '', '')

    now = datetime.datetime.now()

    if request.current_site.domain in ('kinoinfo.ru', 'kinoafisha.ru'):
        filter = {'sites': request.current_site, 'btype': btype, 'user__personinterface__money__gte': price, 'balance__gte': price, 'deleted': False, 'dtime__lt': now}
        if site:
            filter['sites'] = site
        banners = SiteBanners.objects.filter(Q(country__pk=request.current_user_country_id) | Q(country=None), **filter).values('file', 'url', 'id', 'views', 'country', 'cities__pk', 'dtime').order_by('-cities__pk')

        banner = None
        get_country = False
        for i in banners:
            future = i['dtime'].date() + datetime.timedelta(days=13)
            if now.date() <= future:
                # если баннер для моего города
                if i['cities__pk'] == request.current_user_city_id:
                    banner = i
                    break
                # если баннер для моей страны
                elif i['country'] == request.current_user_country_id and not i['cities__pk']:
                    banner = i
                    get_country = True
                elif not get_country and not i['country'] and not i['cities__pk']:
                    banner = i

        if banner:
            advert_img, advert_url = (banner['file'], banner['url'])
            advert_id = banner['id']
            if new_view and not request.bot:
                set_adv_view(request, banner['id'])

    else:
        bg_filter = {'country__id': request.current_user_country_id, 'city__id': request.current_user_city_id, 'site': request.current_site, 'subdomain': request.subdomain}
        bg_alt_filter = {'country__id': request.current_user_country_id, 'city': None, 'site': request.current_site, 'subdomain': request.subdomain}
        bg = Background.objects.filter(Q(**bg_filter) | Q(**bg_alt_filter)).order_by('-date_adding')
        if bg:
            advert_img, advert_url = ('%s%s' % (settings.MEDIA_URL, bg[0].image), bg[0].url)

    return {'img': advert_img, 'url': advert_url, 'id': advert_id}

@never_cache
def search(request):

    query_orig = request.GET.get('query', '')
    category = request.GET.get('filter', '')
    objs = []
    msg = ''
    element = ''
    data = {}
    count = 0

    if query_orig:

        query = low(query_orig)

        slug = del_separator(query)
        if len(query) > 2:

            # фильмы
            if category == '1':
                data = []
                tmp_query = query.decode('utf-8').encode('ascii', 'xmlcharrefreplace')
                element = 'фильмов'
                try:

                    '''
                    Получаем ID фильмов по поисковому запросу
                    upd : проверка ч/з пересечение множеств показала,
                    что такой вариант идентичен предыдущему(конкатенации двух сетов)
                    upd : убрал поиск по слагу, т.к. формирование слага происходит посредством склейки
                    всех слов из тайтла, а это приводит к нерелевантным результатам поиска
                    (см. "San tau jin zi lei saam" фильм)
                    '''

                    ids = set(list(
                        FilmsName.objects.using('afisha').filter(
                            Q(name__icontains = query),
                            status=1
                        ).values_list('film_id', flat=True)))

                    objs = FilmsName \
                        .objects \
                        .using('afisha') \
                        .only(
                            'name',
                            'film_id',
                            'film_id__description',
                            'film_id__year',
                            'film_id__genre1',
                            'film_id__genre2',
                            'film_id__imdb',
                              ) \
                        .select_related('film_id') \
                        .filter(film_id__id__in=ids, status=1, type=2) \
                        .order_by('-film_id__year','name')

                    tmp = None
                    tmp_first = []
                    relevant = []
                    nonrelevant = []

                    for i in objs:

                        '''
                        Получаем постер
                        '''

                        temp_poster = Objxres.objects.using('afisha').select_related('extresid').filter(
                            objtypeid__in=[301, 300],
                            objpkvalue=i.film_id_id)

                        posters = {}
                        for p in temp_poster:
                            if posters.get(p.objpkvalue):
                                if p.objtypeid == 301:
                                    posters[p.objpkvalue]['poster'].append(p)
                                else:
                                    posters[p.objpkvalue]['slides'].append(p)
                            else:
                                posters[p.objpkvalue] = {'poster': [], 'slides': []}
                                if p.objtypeid == 301:
                                    posters[p.objpkvalue]['poster'].append(p)
                                else:
                                    posters[p.objpkvalue]['slides'].append(p)

                        if posters and posters[i.film_id_id]['poster']:
                            i.poster = film_poster2(posters[i.film_id_id]['poster'], 'big')

                        '''
                        Получаем мету
                        '''
                        meta = Film.objects.using('afisha').get(id=i.film_id_id)

                        '''
                        Из полученных метаданных получаем наименования жанров
                        '''
                        i.genres = AfishaGenre.objects.using('afisha').filter(
                            pk__in=[meta.genre1_id, meta.genre2_id, meta.genre3_id])

                        '''
                        Из полученных метаданных получаем рейтинг по IMDB
                        грязнохак - шаблонизатор django не понимает числовых циклов с условиями
                        '''
                        if(meta.imdb):
                            i.imdb = {}
                            imdb_val = meta.imdb
                            i.imdb['range'] = range(
                                0,
                                int(
                                    math.ceil(
                                        float(
                                            imdb_val.replace(',', '.')
                                        )
                                    )
                                )
                                ,1)
                            i.imdb['value'] = meta.imdb

                        '''
                        Получаем рейтинг взвешенный по системе
                        '''
                        int_rate, show_ir, show_imdb, rotten = check_int_rates(i.film_id_id)
                        i.rating = {'rate': int_rate, 'show_ir': show_ir, 'show_imdb': show_imdb, 'rotten': rotten}

                        '''
                        upd: переработка сортировки по релевантности, признак - наличие поисковой фразы в описании
                        '''
                        tmp = i.film_id_id

                        if(tmp not in tmp_first):

                            if(
                                        (meta.description is not None)
                                    and (meta.description.find(tmp_query))
                            ):
                                relevant.append(i)
                            else:
                                nonrelevant.append(i)

                            tmp_first.append(tmp)

                    data = relevant+nonrelevant

                    count = len(tmp_first)

                    if count == 1:
                        return HttpResponseRedirect(reverse('get_film', kwargs={'id': tmp}))

                except db.backend.Database._mysql.OperationalError:
                    pass
            # персоны
            elif category == '2':
                data = {'first': [], 'middle': [], 'last': []}

                element = 'персон'

                tmp_query = query.decode('utf-8').encode('ascii', 'xmlcharrefreplace')

                ids1 = set(list(NamePerson.objects.exclude(person__kid=None).filter(Q(name__iexact=tmp_query) | Q(name__istartswith=slug), status__in=(1,2)).values_list('person__id', flat=True)))

                ids2 = set(list(NamePerson.objects.exclude(person__id__in=ids1, person__kid=None).filter(name__icontains=slug, status=2).values_list('person__id', flat=True)))

                ids = set(list(ids1) + list(ids2))

                objs1 = list(NamePerson.objects.filter(person__id__in=ids, status=1, language__id=1).order_by('name').values('person__id', 'name', 'person__kid'))

                result_ids = set([i['person__id'] for i in objs1])

                objs2 = list(NamePerson.objects.exclude(person__id__in=result_ids).filter(person__id__in=ids, status=1, language__id=2).order_by('name').values('person__id', 'name', 'person__kid'))

                tmp = None

                tmp_first = []

                for objs in [objs1, objs2]:
                    for i in objs:
                        tmp = i['person__kid']
                        if long(i['person__id']) in ids1:
                            if low(i['name'].encode('utf-8')) == tmp_query:
                                if tmp not in tmp_first:
                                    data['first'].append((i['person__kid'], i['name']))
                                    tmp_first.append(tmp)
                            else:
                                if tmp not in tmp_first:
                                    data['middle'].append((i['person__kid'], i['name']))
                                    tmp_first.append(tmp)
                        else:
                            if tmp not in tmp_first:
                                data['last'].append((i['person__kid'], i['name']))
                                tmp_first.append(tmp)

                count = len(tmp_first)

                if count == 1:
                    return HttpResponseRedirect(reverse('get_person', kwargs={'id': tmp}))

            elif category == '3':
                # music
                if request.subdomain == 'music':
                    data = {'first': [], 'middle': [], 'last': [], 'artist': []}

                    element = ''

                    tmp_query = query.decode('utf-8').encode('ascii', 'xmlcharrefreplace')

                    ids1 = set(list(Composition.objects.filter(Q(name__name__iexact=tmp_query) | Q(name__name__istartswith=slug), name__status__in=(2,5)).values_list('id', flat=True)))

                    ids2 = set(list(Composition.objects.exclude(pk__in=ids1).filter(name__name__icontains=slug,  name__status__in=(2,5)).values_list('id', flat=True)))

                    ids = set(list(ids1) + list(ids2))

                    comp_rels = {}
                    for i in CompositionPersonRel.objects.filter(composition__pk__in=ids, type__name='исполнение').values('person', 'person__name__name', 'composition'):
                        comp_rels[i['composition']] = {'pid': i['person'], 'pname': i['person__name__name']}

                    objs1 = list(CompositionName.objects.filter(composition__pk__in=ids, status=2).order_by('name').values('composition__id', 'name'))

                    tmp = None

                    tmp_first = []

                    for i in objs1:
                        tmp = i['composition__id']
                        if long(i['composition__id']) in ids1:
                            if low(i['name'].encode('utf-8')) == query:
                                if tmp not in tmp_first:
                                    artsit = comp_rels.get(i['composition__id'])
                                    data['first'].append((i['composition__id'], i['name'], artsit))
                                    tmp_first.append(tmp)
                            else:
                                if tmp not in tmp_first:
                                    artsit = comp_rels.get(i['composition__id'])
                                    data['middle'].append((i['composition__id'], i['name'], artsit))
                                    tmp_first.append(tmp)
                        else:
                            if tmp not in tmp_first:
                                artsit = comp_rels.get(i['composition__id'])
                                data['last'].append((i['composition__id'], i['name'], artsit))
                                tmp_first.append(tmp)

                    count = len(tmp_first)

                    artists = Person.objects.filter(Q(name__name__iexact=tmp_query) | Q(name__name__istartswith=slug) | Q(name__name__icontains=slug), artist=True, name__status=4).values('id', 'name__name')

                    data['artist'] = artists

                    count += artists.count()
                # Кинотеатры
                else:

                    element = 'кинотеатров'

                    tmp_query = query.decode('utf-8').encode('ascii', 'xmlcharrefreplace')

                    ids1 = set(list(Organization.objects.exclude(kid=None).filter(Q(name__iexact=tmp_query) | Q(name__istartswith=slug)).values_list('id', flat=True)))
                    ids2 = set(list(Organization.objects.exclude(Q(id__in=ids1) | Q(kid=None)).filter(name__icontains=slug).values_list('id', flat=True)))

                    ids = set(list(ids1) + list(ids2))

                    buildings = list(Building.objects.filter(organization__id__in=ids, city__name__status=1).values('city', 'city__name__name', 'city__country', 'city__country__name', 'city__kid', 'organization', 'organization__name', 'organization__uni_slug'))

                    cities = {}
                    count = 0
                    tmp = None
                    for i in buildings:
                        count += 1
                        tmp = i['organization__uni_slug']
                        if not cities.get(i['city']):
                            cities[i['city']] = {
                                'id': i['city'],
                                'name': i['city__name__name'],
                                'country': i['city__country__name'],
                                'cinemas': []
                            }

                        cities[i['city']]['cinemas'].append({'name': i['organization__name'], 'slug': i['organization__uni_slug']})

                    cities = sorted(cities.values(), key=operator.itemgetter('name'))

                    data = {}
                    for i in cities:
                        if not data.get(i['country']):
                            data[i['country']] = []
                        data[i['country']].append(i)

                    if count == 1:
                        return HttpResponseRedirect(reverse('organization_cinema', kwargs={'id': tmp}))

        else:
            msg = 'Слишком короткий запрос'

    return render_to_response('search_result.html', {'objs': data, 'element': element, 'msg': msg, 'srch_category': category, 'query': query_orig, 'count': count},  context_instance=RequestContext(request))

@never_cache
def kinoafisha_old_button(request, id):
    if request.current_site.domain == 'kinoafisha.ru':
        try:
            film = Film.objects.using('afisha').get(idalldvd=int(id))
        except Film.DoesNotExist:
            raise Http404

        img = create_kinoafisha_button(film, id)

        with open(img['output_path'], 'rb') as f:
            return HttpResponse(f.read(), mimetype="image/png")

    raise Http404

@never_cache
def kinoafisha_button(request):
    from urlparse import urlparse, parse_qs
    from release_parser.imdb import create_film_by_imdb_id

    warning = ''
    img = ()

    if request.POST:
        url = request.POST.get('url','').strip()
        if url:
            imdb_id = None
            film_id = None
            if 'imdb.com/title/tt' in url:
                xurl = url.split('imdb.com/title/tt')[1].split('/')[0].replace('/','')
                xurl = xurl.split('?')[0]
                imdb_id = int(xurl)
            elif 'kinoafisha.ru/' in url:
                xurl = urlparse(url)
                if xurl.query:
                    params = parse_qs(xurl.query)
                    film_id = params.get('id1')
                    film_id = film_id[0] if film_id else None
                else:
                    film_id = xurl.path.replace('/film/','').replace('/','')

                try:
                    film_id = int(film_id)
                except ValueError:
                    film_id = None

            if imdb_id or film_id:
                if imdb_id:
                    try:
                        film = Film.objects.using('afisha').get(idalldvd=imdb_id)
                    except Film.DoesNotExist:
                        try:
                            film = create_film_by_imdb_id(imdb_id)
                        except:
                            film = None
                else:
                    try:
                        film = Film.objects.using('afisha').get(pk=film_id)
                    except Film.DoesNotExist:
                        film = None

                if film:
                    img = create_kinoafisha_button(film, imdb_id)
                else:
                    warning = 'Фильм не найден в базе'
            else:
                warning = 'Неверный URL'
        else:
            warning = 'Вы не ввели URL'
    return render_to_response('kinoafisha/button.html', {'warning': warning, 'img': img},  context_instance=RequestContext(request))

def disable_adv_bg_func():
    price = 1
    now = datetime.datetime.now()

    objs = SiteBanners.objects.select_related('user', 'user__personinterface').filter(btype='0', user__personinterface__money__gte=price, balance__gte=price)

    for i in objs:
        interface = i.user.personinterface
        if interface.money >= price and i.balance >= price:
            to = now + datetime.timedelta(days=1)
            if i.bg_disable_dtime_to.date() != to.date():
                interface.money -= price
                interface.save()
                i.bg_disable_dtime_to = to
                i.balance -= price
                i.spent += price
                i.save()

@never_cache
def get_torrent(request, id):

    price = 1

    try:
        torrent = Torrents.objects.exclude(path=None).get(pk=id)
    except Torrents.DoesNotExist:
        raise Http404

    try:
        access = TorrentsUsers.objects.get(torrent=torrent, profile=request.profile)
    except TorrentsUsers.DoesNotExist:
        access = False

    interface = request.profile.personinterface

    if not access:
        if interface.money >= price:

            interface.money -= price
            interface.save()

            access, created = TorrentsUsers.objects.get_or_create(
                torrent = torrent,
                profile = request.profile,
                defaults = {
                    'torrent': torrent,
                    'profile': request.profile,
                })

    if access:
        full_path = '%s%s' % (settings.MEDIA_ROOT, torrent.path)
        filename = full_path.split('/')[-1]
        with open(full_path, 'r') as f:
            response = HttpResponse(f.read(), mimetype='application/x-bittorrent')
            response['Content-Disposition'] = 'attachment; filename=%s' % filename
            access.got = True
            access.save()
            return response

    raise Http404

@never_cache
def new_torrents(request):
    from slideblok.views import releasedata
    from movie_online.IR import check_int_rates_inlist

    price = 100

    interface = request.profile.personinterface

    year, genre, country, rate = (None, None, None, None)
    rate_filter = [u'ВСЕ',]
    year_filter = [u'ВСЕ',]
    genre_filter = {0: {'id': 0, 'name': u'ВСЕ'}}
    country_filter = {0: {'id': 0, 'name': u'ВСЕ'}}

    set_filter = False
    access = False
    if interface.money >= price or request.user.is_superuser:
        access = True

        torrents_kids = list(Torrents.objects.exclude(path=None).distinct('film').values('id', 'film'))

        kids = [i['film'] for i in torrents_kids]

        opinions = {'good': [], 'bad': []}
        for i in NewsFilms.objects.filter(kid__in=kids, message__visible=True, message__autor=request.profile, rate_1__gt=0).values('kid', 'rate'):
            if i['rate'] >= 4:
                opinions['good'].append(i['kid'])
            else:
                opinions['bad'].append(i['kid'])

        kids = set(kids) - set(opinions['bad'])

        exist_kids = []
        for i in list(Film.objects.using('afisha').filter(pk__in=kids, date__gte=datetime.date(1900,1,1)).values('year', 'genre1', 'genre2', 'genre3', 'genre1__name', 'genre2__name', 'genre3__name', 'country', 'country2', 'country__name', 'country2__name', 'id')):
            if i['year']:
                year_filter.append(int(i['year']))

            for j in ((i['genre1'], i['genre1__name']), (i['genre2'], i['genre2__name']), (i['genre3'], i['genre3__name'])):
                if j[0] and not genre_filter.get(j[0]):
                    genre_filter[int(j[0])] = {'id': int(j[0]), 'name': j[1]}

            for j in ((i['country'], i['country__name']), (i['country2'], i['country2__name'])):
                if j[0] and not country_filter.get(j[0]):
                    country_filter[int(j[0])] = {'id': int(j[0]), 'name': j[1]}

            exist_kids.append(i['id'])

        year_filter = sorted(set(year_filter), reverse=True)

        rates = check_int_rates_inlist(exist_kids)

        rates_tmp = set([i['int_rate'] for i in rates.values() if i['int_rate']])

        for i in rates_tmp:
            rate_filter.append(i)

        if request.POST:
            if 'filter' in request.POST:
                try: year = int(request.POST.get('year'))
                except ValueError: pass
                try: genre = int(request.POST.get('genre'))
                except ValueError: pass
                try: country = int(request.POST.get('country'))
                except ValueError: pass
                try: rate = int(request.POST.get('rate'))
                except ValueError: pass

        if year not in year_filter:
            year = year_filter[0]
        if genre not in genre_filter:
            genre = genre_filter[0]['id']
        if country not in country_filter:
            country = country_filter[0]['id']
        if rate not in rate_filter:
            rate = rate_filter[0]

        if year != u'ВСЕ' or rate != u'ВСЕ' or genre != 0 or country != 0:
            set_filter = True

        genre_filter = sorted(genre_filter.values(), key=operator.itemgetter('name'))
        country_filter = sorted(country_filter.values(), key=operator.itemgetter('name'))

        filter = {'pk__in': kids, 'date__gte': datetime.date(1900,1,1)}

        if year != u'ВСЕ':
            filter['year__exact'] = year

        queries = []
        if genre:
            for q in [Q(genre1__pk=genre), Q(genre2__pk=genre), Q(genre3__pk=genre)]:
                queries.append(q)
        if country:
            for q in [Q(country__pk=country), Q(country2__pk=country)]:
                queries.append(q)

        if queries:
            query = queries.pop()
            for item in queries:
                query |= item

            torrents_kids = list(Film.objects.using('afisha').filter(query, **filter).values('pk', 'date'))
        else:
            torrents_kids = list(Film.objects.using('afisha').filter(**filter).values('pk', 'date'))

        kids = [i['pk'] for i in torrents_kids]

        tusers = list(TorrentsUsers.objects.filter(profile=request.profile, torrent__film__in=kids, got=True).values_list('torrent__film', flat=True))

        for i in torrents_kids:
            if not i['date']:
                i['date'] = datetime.datetime(3000,1,1)

        torrents_kids = sorted(torrents_kids, key=operator.itemgetter('date'), reverse=True)

        page = request.GET.get('page')
        try:
            page = int(page)
        except (ValueError, TypeError):
            page = 1

        p, page = pagi(page, torrents_kids, 20)

        torrents_films_dict = {}
        for i in p.object_list:
            torrents_films_dict[i['pk']] = {}

        data = []

        for i in releasedata(torrents_films_dict, {}, persons=True, likes=True, trailers=True, reviews=True, poster_size='big'):
            i['got'] = True if i['id'] in tusers else False
            if not i['release_date']:
                i['release_date'] = datetime.datetime(3000,1,1)

            if i['id'] in opinions['good']:
                i['opinion'] = True

            if rate == u'ВСЕ' or int(rate) == int(i['rate']):
                data.append(i)

        data = sorted(data, key=operator.itemgetter('release_date'), reverse=True)
    else:
        p = None
        page = 1
        data = []

    tmplt = 'kinoafisha/new_torrents.html'

    url_name = resolve(request.path_info).url_name

    return render_to_response(tmplt, {'data': data, 'page': page, 'p': p, 'url_name': url_name, 'year_filter': year_filter, 'genre_filter': genre_filter, 'country_filter': country_filter, 'year': year, 'genre': genre, 'country': country, 'access': access, 'rate_filter': rate_filter, 'rate': rate, 'set_filter': set_filter}, context_instance=RequestContext(request))

@never_cache
def best_torrents(request):
    from slideblok.views import releasedata

    price = 100

    interface = request.profile.personinterface

    access = False
    if interface.money >= price:
        access = True

    else:
        pass

    tmplt = 'kinoafisha/best_torrents.html'

    url_name = resolve(request.path_info).url_name

    return render_to_response(tmplt, {'data': data, 'page': page, 'p': p, 'url_name': url_name, 'year_filter': year_filter, 'genre_filter': genre_filter, 'country_filter': country_filter, 'year': year, 'genre': genre, 'country': country, 'access': access}, context_instance=RequestContext(request))

@only_superuser
@never_cache
def torrents_listing(request, source):
    from slideblok.views import releasedata
    from news.views import cut_description
    from movie_online.IR import check_int_rates_inlist
    from release_parser.func import get_file_modify_datetime

    source = ImportSources.objects.get(pk=source)

    today = datetime.datetime.now().date()
    yesterday = today - datetime.timedelta(days=1)

    file_add = 'xml' if source.url == 'http://rutracker.org/' else 'html'
    file_path = '%s__%s__films.xml' % (file_add, source.dump)
    parser_time = get_file_modify_datetime(settings.SUCCESS_LOG_PATH, file_path)
    parser_time = parser_time + datetime.timedelta(hours=3)

    release, new, rate = (None, None, None)

    release_filter = {
        0: {'id': 0, 'name': u'ВСЕ'},
        1: {'id': 1, 'name': u'Кинопрокат'},
        2: {'id': 2, 'name': u'Прочие'},
    }

    new_filter = {
        0: {'id': 0, 'name': u'ВСЕ'},
        1: {'id': 1, 'name': u'Новые'},
    }

    rate_filter = [u'ВСЕ',]

    if request.POST:
        if 'checker' in request.POST:
            checker = request.POST.getlist('checker')
            if checker:
                SourceFilms.objects.filter(source_obj=source, pk__in=checker).update(rel_ignore=True)
            return HttpResponseRedirect(reverse('torrents_listing', kwargs={'source': source.pk}))
        else:
            release = request.POST.get('release')
            new = request.POST.get('new')
            rate = request.POST.get('rate')

    kids = list(SourceFilms.objects.filter(source_obj=source, rel_ignore=False).values_list('kid', flat=True))

    rates = check_int_rates_inlist(kids)
    rates_tmp = set([i['int_rate'] for i in rates.values() if i['int_rate']])
    for i in rates_tmp:
        rate_filter.append(i)

    if rate:
        try:
            rate = int(rate)
        except ValueError:
            rate = u'ВСЕ'
    if release:
        release = int(release)
    if new:
        new = int(new)
    sess_filter = request.session.get('torrents_listing_filter',{})

    if rate == None and release == None and new == None:
        sess_filter = request.session.get('torrents_listing_filter',{})
        if sess_filter:
            rate = sess_filter['rate']
            release = sess_filter['release']
            new = sess_filter['new']
        else:
            new = 1

    if not new:
        source_films = SourceFilms.objects.filter(source_obj=source, rel_ignore=False)
    elif new == 1:
        source_films = SourceFilms.objects.filter(source_obj=source, extra='new', rel_ignore=False)

    tmp_date = datetime.datetime(3000,1,1)

    films = {}
    for i in source_films:
        new = True if i.extra == 'new' else False
        films[i.kid] = {'source_id': i.source_id, 'new': new, 'pk': i.id, 'release': tmp_date, 'kid': i.kid}

    for i in list(Film.objects.using('afisha').filter(pk__in=films.keys(), date__gte=datetime.datetime(1900,1,1)).values('id', 'date')):
        films[i['id']]['release'] = i['date']

    films_sorted = sorted(films.values(), key=operator.itemgetter('release'), reverse=True)

    page = request.GET.get('page')
    try:
        page = int(page)
    except (ValueError, TypeError):
        page = 1

    p, page = pagi(page, films_sorted, 100)

    films_ids = {}
    for i in p.object_list:
        films_ids[i['kid']] = {}

    torrents = {}
    for i in Torrents.objects.filter(film__in=films_ids.keys()).exclude(path=None):
        if not torrents.get(i.film):
            torrents[i.film] = {'0': None, '1': None, '2': None}
        q = i.quality_avg if i.quality_avg else '1'
        torrents[i.film][q] = i

    data_tmp = releasedata(films_ids, {}, persons=False, likes=False, trailers=False, reviews=False, poster_size='small')

    upd = []

    data = []
    for i in data_tmp:
        torrent = torrents.get(i['id'])
        f = films.get(i['id'])
        txt_cut = cut_description(i['descript'], True, 150)
        i['descript_cut'] = txt_cut
        i['descript'] = ''
        i['new'] = f['new']
        i['source_id'] = f['pk']
        i['torrent'] = torrent

        if not i['release_date']:
            i['release_date'] = datetime.datetime(3000,1,1)

        if source.url == 'http://cinemate.cc/':
            i['source_url'] = u'http://cinemate.cc/movie/%s/' % f['source_id']
        elif source.url == 'http://rutracker.org/':
            i['source_url'] = u'http://rutracker.org/forum/viewtopic.php?t=%s' % f['source_id']

        if f['new']:
            upd.append(f['pk'])

        next = True

        if release == 1 and i['release_date'].year == 3000:
            next = False
        elif release == 2 and i['release_date'].year < 3000:
            next = False

        if next:
            if rate:
                if rate == u'ВСЕ' or rate == int(i['rate']):
                    data.append(i)
            else:
                data.append(i)

    films = sorted(data, key=operator.itemgetter('release_date'), reverse=True)

    SourceFilms.objects.filter(pk__in=upd).update(extra=None)

    request.session['torrents_listing_filter'] = {'rate': rate, 'new': new, 'release': release}

    return render_to_response('kinoafisha/torrents_listing.html', {'data': films, 'source': source, 'p': p, 'page': page, 'release_filter': release_filter.values(), 'release': release, 'new_filter': new_filter.values(), 'new': new, 'rate_filter': rate_filter, 'rate': rate, 'parser_time': parser_time, 'today': today, 'yesterday': yesterday}, context_instance=RequestContext(request))

@never_cache
def booking(request):

    timer = time.time()

    today = datetime.datetime.today().date()

    def get_timeline():
        next_day = today + datetime.timedelta(days=1)
        timeline = [{'time': datetime.datetime(today.year, today.month, today.day, 9, 0), 'minutes': False}]
        while True:
            time_to = timeline[-1]['time'] + datetime.timedelta(minutes=15)
            if time_to.date() == next_day:
                break
            minutes = False if time_to.minute == 0 else True
            timeline.append({'time': time_to, 'minutes': minutes})
        return timeline

    today = datetime.date.today()
    date_from = today

    # Получаем релизы по стране
    country = 2

    if request.POST:
        country = int(request.POST.get('country', 2))
        date_from = request.POST.get('date_from', str(date_from))
        date_from = datetime.datetime.strptime(date_from, '%Y-%m-%d')

    releases = []

    if country == 2:
        releases = list(Film.objects.using('afisha').filter(date__gte=today).order_by('date').values('id', 'date'))
    elif country == 43:
        for i in UkrainianReleases.objects.filter(release__gte=today).order_by('release'):
            releases.append({'id': i.kid, 'date': i.release})

    kids = set([i['id'] for i in releases])

    names = {}
    for i in FilmsName.objects.using('afisha').filter(film_id__pk__in=kids, status=1, type__in=(1, 2)):
        if not names.get(i.film_id_id):
            names[i.film_id_id] = {}
        names[i.film_id_id][i.type] = i.name

    for i in releases:
        name = names.get(i['id'])
        i['name'] = name[2] if name.get(2) else name[1]

    # Проверяем права
    if request.user.is_superuser:
        access = True
    else:
        access = request.user.groups.filter(name='Букер').exists()

    articles = []
    days_range = []
    halls = {}
    data = {}

    if access:

        # Получаем заметки букера
        try:
            osm = OrgSubMenu.objects.get(page_type='1', booker_profile=request.profile)
        except OrgSubMenu.DoesNotExist:
            pass
        else:
            articles = osm.news.all().order_by('-dtime')

        # все кинотеатры из настроек букера
        cinemas = {}
        cinemas_kids = []
        for i in Cinema.objects.filter(bookingsettings__profile=request.profile).distinct('pk').values('id', 'code', 'bookercinemas__permission'):
            cinemas[i['id']] = {'id': i['id'], 'kid': i['code'], 'name': '', 'access': i['bookercinemas__permission']}
            cinemas_kids.append(i['code'])

        for i in list(NameCinema.objects.filter(cinema__code__in=cinemas_kids, status=1).values('name', 'cinema')):
            cinemas[i['cinema']]['name'] = i['name']

        # залы
        for i in list(NameHall.objects.filter(status=1, hall__cinema__code__in=cinemas_kids).distinct('hall').values('name', 'hall', 'hall__cinema')):
            cinema = cinemas.get(i['hall__cinema'])
            halls[i['hall']] = {'hall_id': i['hall'], 'cinema': cinema, 'cinema_name': cinema['name'], 'name': i['name'], 'dates': {}, 'date_range': {}}

        # диапозон дат сеансов
        days = BookingSchedules.objects.filter(hall__cinema__code__in=cinemas_kids, dtime__gte=today).values_list('dtime', flat=True)
        days_range = list(set([i.date() for i in days]))
        days_range.sort()

        # сеансы от ... и далее
        date_to = date_from + datetime.timedelta(days=1)
        schedules = BookingSchedules.objects.select_related('hall').filter(hall__cinema__code__in=cinemas_kids, dtime__gte=date_from)

        # фильмы
        schedules_ids = [i.id for i in schedules]
        source_films_data = list(SourceFilms.objects.filter(bookingschedules__pk__in=schedules_ids).values('kid', 'bookingschedules'))
        source_kids = set([i['kid'] for i in source_films_data])

        for i in FilmsName.objects.using('afisha').filter(film_id__pk__in=source_kids, status=1, type__in=(1, 2)).exclude(film_id__id__in=names.keys()):
            if not names.get(i.film_id_id):
                names[i.film_id_id] = {}
            names[i.film_id_id][i.type] = i.name

        source_films = {}
        for i in source_films_data:
            if not source_films.get(i['bookingschedules']):
                source_films[i['bookingschedules']] = []
            name = names.get(i['kid'])
            name = name[2] if name.get(2) else name[1]
            source_films[i['bookingschedules']].append({'kid': i['kid'], 'name': name})

        # удобно складываем все данные
        for i in schedules:
            if not data.get(i.hall_id):
                #cinema = cinemas.get(i.hall.cinema_id)
                hall = halls.get(i.hall_id)
                data[i.hall_id] = {'hall_id': i.hall_id, 'cinema': hall['cinema'], 'name': hall['name'], 'dates': {}, 'date_range': {}}
            films = source_films.get(i.id)

            if not data[i.hall_id]['dates'].get(i.dtime.date()):
                data[i.hall_id]['dates'][i.dtime.date()] = {'date': i.dtime.date(), 'ids': [], 'times': []}

            data[i.hall_id]['dates'][i.dtime.date()]['times'].append({'time': i.dtime, 'films': films, 'tmp': i.temp, 'id': i.id})
            data[i.hall_id]['dates'][i.dtime.date()]['ids'].append('%s%s' % (i.dtime.time(), films))
            data[i.hall_id]['dates'][i.dtime.date()]['ids'].sort()

        # формируем диапозон дат с одинкаовыми сеансами
        for i in data.values():
            i['dates'] = sorted(i['dates'].values(), key=operator.itemgetter('date'))

            old_date_times = []
            sch_date_from = None
            sch_date_to = None
            for j in i['dates']:
                set_dates = True
                if old_date_times:
                    if old_date_times == j['ids']:
                        sch_date_to = j['date']
                        set_dates = False

                if set_dates:
                    sch_date_from = j['date']
                    sch_date_to = j['date']

                old_date_times = j['ids']

                if not i['date_range'].get(sch_date_from):
                    i['date_range'][sch_date_from] = {'from': sch_date_from, 'to': None, 'times': j['times']}
                i['date_range'][sch_date_from]['to'] = sch_date_to

        for i in data.values():
            i['date_range'] = sorted(i['date_range'].values(), key=operator.itemgetter('from'))
            for j in i['date_range']:
                j['times'] = sorted(j['times'], key=operator.itemgetter('time'))

    halls = sorted(halls.values(), key=operator.itemgetter('cinema_name'))

    timeline = get_timeline()
    url_name = resolve(request.path_info).url_name
    try:
        date_from = date_from.date()
    except AttributeError:
        pass

    timer = '%5.2f' % (time.time()-timer)
    return render_to_response('kinoafisha/booking.html', {'url_name': url_name, 'timeline': timeline, 'country': country, 'releases': releases, 'articles': articles, 'access': access, 'days_range': days_range, 'data': data.values(), 'date_from': date_from, 'timer': timer, 'cached_page': '', 'halls': halls}, context_instance=RequestContext(request))

@never_cache
def booking_article_add(request):
    from letsgetrhythm.views import view_func
    if request.POST:

        if request.user.is_superuser:
            access = True
        else:
            access = request.user.groups.filter(name='Букер').exists()

        articles = []
        if access:
            osm, created = OrgSubMenu.objects.get_or_create(
                page_type = '1',
                booker_profile = request.profile,
                defaults = {
                    'name': 'Booking заметки',
                    'page_type': '1',
                    'booker_profile': request.profile,
                })

            data = view_func(request, osm.id, None, 'booking', access)

            return HttpResponseRedirect(reverse('booking'))

    raise Http404

@never_cache
def booking_get_excel_doc(request):
    from openpyxl import Workbook
    from openpyxl.cell import get_column_letter
    from openpyxl.styles import Font

    if request.user.is_superuser:
        access = True
    else:
        access = request.user.groups.filter(name='Букер').exists()

    data = {}
    halls = {}
    if access and request.POST:
        '''
        halls_ids = [3552, 1970, 2539]
        date_from = datetime.date(2016, 4, 25)
        date_to = datetime.date(2016, 5, 5)
        date_to = date_to + datetime.timedelta(days=1)
        print_temp = False
        all_dates = False
        '''

        halls_ids = request.POST.getlist('ex_halls')
        date_from = request.POST.get('ex_date_from')
        date_to = request.POST.get('ex_date_to')
        all_dates = request.POST.get('ex_date_all')
        print_temp = request.POST.get('ex_temp')

        date_from = datetime.datetime.strptime(str(date_from), '%Y-%m-%d')
        date_to = datetime.datetime.strptime(str(date_to), '%Y-%m-%d')
        all_dates = True if all_dates else False
        print_temp = True if print_temp else False

        # все кинотеатры из настроек букера
        cinemas = {}
        cinemas_kids = []
        for i in Cinema.objects.filter(bookingsettings__profile=request.profile, bookercinemas__permission='1').distinct('pk').values('id', 'code', 'bookercinemas__permission'):
            cinemas[i['id']] = {'id': i['id'], 'kid': i['code'], 'name': '', 'access': i['bookercinemas__permission']}
            cinemas_kids.append(i['code'])

        for i in list(NameCinema.objects.filter(cinema__code__in=cinemas_kids, status=1).values('name', 'cinema')):
            cinemas[i['cinema']]['name'] = i['name']

        # залы
        for i in list(NameHall.objects.filter(status=1, hall__cinema__code__in=cinemas_kids).distinct('hall').values('name', 'hall', 'hall__cinema')):
            cinema = cinemas.get(i['hall__cinema'])
            halls[i['hall']] = {'hall_id': i['hall'], 'cinema': cinema, 'cinema_name': cinema['name'], 'name': i['name'], 'dates': {}, 'date_range': {}}

        filter = {'hall__id__in': halls_ids, 'temp': print_temp}
        if all_dates:
            filter['dtime__gte'] = datetime.date.today()
        else:
            filter['dtime__gte'] = date_from
            filter['dtime__lt'] = date_to

        schedules = BookingSchedules.objects.select_related('hall').filter(**filter)

        schedules_ids = [i.id for i in schedules]
        source_films_data = list(SourceFilms.objects.filter(bookingschedules__pk__in=schedules_ids).values('kid', 'bookingschedules'))
        source_kids = set([i['kid'] for i in source_films_data])

        names = {}
        for i in FilmsName.objects.using('afisha').filter(film_id__pk__in=source_kids, status=1, type__in=(1, 2)).exclude(film_id__id__in=names.keys()):
            if not names.get(i.film_id_id):
                names[i.film_id_id] = {}
            names[i.film_id_id][i.type] = i.name

        # фильмы
        source_films = {}
        for i in source_films_data:
            if not source_films.get(i['bookingschedules']):
                source_films[i['bookingschedules']] = []
            name = names.get(i['kid'])
            name = name[2] if name.get(2) else name[1]
            source_films[i['bookingschedules']].append({'kid': i['kid'], 'name': name})

        # удобно складываем все данные
        for i in schedules:
            if not data.get(i.hall_id):
                #cinema = cinemas.get(i.hall.cinema_id)
                hall = halls.get(i.hall_id)
                data[i.hall_id] = {'hall_id': i.hall_id, 'cinema': hall['cinema'], 'name': hall['name'], 'dates': {}, 'date_range': {}}
            films = source_films.get(i.id)

            if not data[i.hall_id]['dates'].get(i.dtime.date()):
                data[i.hall_id]['dates'][i.dtime.date()] = {'date': i.dtime.date(), 'ids': [], 'times': []}

            data[i.hall_id]['dates'][i.dtime.date()]['times'].append({'time': i.dtime, 'films': films, 'tmp': i.temp, 'id': i.id})
            data[i.hall_id]['dates'][i.dtime.date()]['ids'].append('%s%s' % (i.dtime.time(), films))
            data[i.hall_id]['dates'][i.dtime.date()]['ids'].sort()

        # формируем диапозон дат с одинкаовыми сеансами
        for i in data.values():
            i['dates'] = sorted(i['dates'].values(), key=operator.itemgetter('date'))

            old_date_times = []
            sch_date_from = None
            sch_date_to = None
            for j in i['dates']:
                set_dates = True
                if old_date_times:
                    if old_date_times == j['ids']:
                        sch_date_to = j['date']
                        set_dates = False

                if set_dates:
                    sch_date_from = j['date']
                    sch_date_to = j['date']

                old_date_times = j['ids']

                if not i['date_range'].get(sch_date_from):
                    i['date_range'][sch_date_from] = {'from': sch_date_from, 'to': None, 'times': j['times']}
                i['date_range'][sch_date_from]['to'] = sch_date_to

        wb = Workbook()

        for index, i in enumerate(data.values()):
            if index == 0:
                ws = wb.active
            else:
                ws = wb.create_sheet()

            ws.title = u'%s, %s' % (i['cinema']['name'][:13], i['name'][:13])

            count = 0
            for j in i['date_range'].values():
                count += len(j['times']) + 1

            last_row = 0
            for j in sorted(i['date_range'].values(), key=operator.itemgetter('from')):
                last_row += 1

                cells = 'A%s:B%s' % (last_row, last_row)

                ws.merge_cells(cells)
                ws["A%s" % last_row].font = Font(name='Calibri', size=12, bold=True)
                ws["A%s" % last_row] = '%s - %s' % (j['from'], j['to'])

                j['times'] = sorted(j['times'], key=operator.itemgetter('time'))

                for row in range(len(j['times'])):
                    last_row += 1

                    t = tmp_date(j['times'][row]['time'], 'H:i')
                    t = t.encode('utf-8')
                    tfilms = ''
                    for f in j['times'][row]['films']:
                        if tfilms:
                            tfilms += u', '
                        tfilms += f['name']

                    for ind, col in enumerate((t, tfilms)):
                        ws.cell(column=ind+1, row=last_row, value=col)

        file_name = 'booking_schedules_%s.xlsx' % request.user.id
        file_path = '%s/%s' % (settings.BOOKING_EXCEL_PATH, file_name)

        wb.save(filename=file_path)

        with open(file_path, 'r') as f:
            file = f.read()

        response = HttpResponse(file, mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
        response['Content-Disposition'] = 'attachment; filename=%s' % file_name
        return response

        '''
        doc = ''

        for i in data.values():
            doc += u'<b>%s / %s</b><br />' % (i['cinema']['name'][:13], i['name'][:13])

            for j in sorted(i['date_range'].values(), key=operator.itemgetter('from')):

                doc += u'- - - - - %s - %s<br />' % (j['from'], j['to'])

                for t in sorted(j['times'], key=operator.itemgetter('time')):

                    doc += u'%s, ' % t['time'].time()
                    for f in t['films']:
                        doc += u'%s, ' % f['name']
                    doc += u'<br />'

        return HttpResponse(str(doc.encode('utf-8')))
        '''

    raise Http404

def yandex(request):
    return render_to_response('yandex_35c18ae022681480.html')


#!/usr/bin/python
# -*- coding: utf-8 -*-

#Utils
import sys
import datetime

#Custom Classes
sys.dont_write_bytecode = True
from modules.erb import *
from modules.measurement import *
from modules.timer import *
from modules.utils import *
from modules.techniques.technique import *

if __name__ == "__main__":

	#Get passed parameters
	if len(sys.argv) > 1:
		GRID_POINTS_FILE = sys.argv[1]
	else:
		GRID_POINTS_FILE = 'data/grid_points/squaremap_15m.json'

	#Set script arguments
	test_sample_folder = 'data/samples/'
	test_file = 'test.csv'
	verbose = True
	log = True

	#Read training dataset from csv file
	erb_list = ReadERBsFromFile('data/erbs.csv')
	test_list = ReadMeasurementsFromFile(erb_list, test_sample_folder + test_file)

	#Opens log file
	log_file = None
	if(log):
		d = datetime.datetime.now()
		log_file = open("log/test_%s-%s-%s__%s:%s.log" % (d.day, d.month, d.year, d.hour, d.minute), "w")

	#Put all techniques in a list
	from list_techniques import *
	technique_list = GetTechniques(erb_list, GRID_POINTS_FILE)

	#Test a measure set with every technique
	PrintAndLog("TEST FILE:", log_file, verbose)
	PrintAndLog("\t`{}`\n".format(test_file), log_file, verbose)
	PrintAndLog("NUMBER OF TECHNIQUES:", log_file, verbose)
	PrintAndLog("\t{}\n".format(len(technique_list)), log_file, verbose)
	PrintAndLog("INDIVIDUAL RESULTS:", log_file, verbose)
	time_count = Timer()
	time_count.Start()
	ordered = []
	for tech in technique_list:
		tech.Load()
		results = tech.TestList(test_list)
		ordered.append( (tech.name, results ) )

		#Print technique results
		PrintAndLog("\tTECHNIQUE NAME: {}".format(tech.name), log_file, verbose)
		PrintAndLog("\t[MIN ERROR: {:.3f} km, MAX ERROR: {:.3f} km, AVG ERROR: {:.3f} km, RMS ERROR: {:.3f} km]".format(results[0], results[1], results[2], results[3]), log_file, verbose)

	#Print time spent testing all algorithms
	elapsed_time = int(time_count.Elapsed())
	minutes = elapsed_time / 60
	seconds = elapsed_time - minutes*60
	PrintAndLog("\nELAPSED TIME:", log_file, verbose)
	PrintAndLog("\t{}m {}s\n".format(minutes, seconds), log_file, verbose)

	#Print best algorithm for each metric
	PrintAndLog("BEST RESULTS:", log_file, verbose)

	ordered.sort(key=lambda x: x[1][0])
	PrintAndLog("\tBEST BY MIN ERROR: {}".format(ordered[0][0]), log_file, verbose)
	PrintAndLog("\t[MIN ERROR: {:.3f} km, MAX ERROR: {:.3f} km, AVG ERROR: {:.3f} km, RMS ERROR: {:.3f} km]".format(ordered[0][1][0], ordered[0][1][1], ordered[0][1][2], ordered[0][1][3]), log_file, verbose)

	ordered.sort(key=lambda x: x[1][1])
	PrintAndLog("\tBEST BY MAX ERROR: {}".format(ordered[0][0]), log_file, verbose)
	PrintAndLog("\t[MIN ERROR: {:.3f} km, MAX ERROR: {:.3f} km, AVG ERROR: {:.3f} km, RMS ERROR: {:.3f} km]".format(ordered[0][1][0], ordered[0][1][1], ordered[0][1][2], ordered[0][1][3]), log_file, verbose)

	ordered.sort(key=lambda x: x[1][2])
	PrintAndLog("\tBEST BY AVG ERROR: {}".format(ordered[0][0]), log_file, verbose)
	PrintAndLog("\t[MIN ERROR: {:.3f} km, MAX ERROR: {:.3f} km, AVG ERROR: {:.3f} km, RMS ERROR: {:.3f} km]".format(ordered[0][1][0], ordered[0][1][1], ordered[0][1][2], ordered[0][1][3]), log_file, verbose)

	ordered.sort(key=lambda x: x[1][3])
	PrintAndLog("\tBEST BY RMS ERROR: {}".format(ordered[0][0]), log_file, verbose)
	PrintAndLog("\t[MIN ERROR: {:.3f} km, MAX ERROR: {:.3f} km, AVG ERROR: {:.3f} km, RMS ERROR: {:.3f} km]".format(ordered[0][1][0], ordered[0][1][1], ordered[0][1][2], ordered[0][1][3]), log_file, verbose)

	#Close log file after experiment
	if log_file != None and (not log_file.closed):
		log_file.close()


from string import ascii_lowercase
from time import time
from random import seed, randint

current_miliseconds = int(round(time() * 1000))
seed(current_miliseconds)

OPERATOR_AND = "AND";
OPERATOR_OR = "OR";
OPERATOR_NOT = "NOT";
CONSTANT_TRUE = "TRUE";
CONSTANT_FALSE = "FALSE";
OPERATOR_L_BRACKET = '(';
OPERATOR_R_BRACKET = ')';

possible_operators = [OPERATOR_NOT, OPERATOR_AND, OPERATOR_OR]
possible_seperators = [OPERATOR_L_BRACKET, OPERATOR_R_BRACKET]
posible_const_values = [CONSTANT_FALSE, CONSTANT_TRUE]

MAX_VARIABLES = len(ascii_lowercase)
MAX_OPERATORS = 100000

def pick_number_of_var_op():
	var_num = randint(1, MAX_VARIABLES)
	op_num = randint(var_num - 1, MAX_OPERATORS)
	return (var_num, op_num)

def pick_variables(var_num):
	return list(ascii_lowercase[0:var_num])

def is_operator(element):
	return element in (possible_operators + possible_seperators)

def determine_pool(element, possible_values, l_bracket_count):
	if (element == OPERATOR_NOT):
		return possible_values + [OPERATOR_L_BRACKET] + [OPERATOR_NOT] + posible_const_values

	if(element == OPERATOR_L_BRACKET):
		return possible_values + [OPERATOR_L_BRACKET] + [OPERATOR_NOT] + posible_const_values

	if(element == OPERATOR_R_BRACKET):
		return possible_operators[1:]

	if (element in possible_operators[1:]):
		return possible_values + posible_const_values + [OPERATOR_NOT] + [OPERATOR_L_BRACKET]

	if (element in (possible_values + posible_const_values)):
		ret_arr = possible_operators[1:]
		if (l_bracket_count > 0):
			ret_arr += OPERATOR_R_BRACKET

		return ret_arr

	return []  # Error

def add_to_string(element, output_string):
	if(element in OPERATOR_L_BRACKET):
		return output_string + element

	if(element in OPERATOR_R_BRACKET):
		return output_string[:-1] + element + " "

	return output_string + element + " "

def check_for_bracket(element):
	if (element == OPERATOR_L_BRACKET): return 1
	if (element == OPERATOR_R_BRACKET): return -1
	return 0;

def pick_element(pool_of_possibilities):
	is_not = (OPERATOR_NOT in pool_of_possibilities) and (randint(0,100) < 75)
	if (is_not): return OPERATOR_NOT
	else:
		el_index = randint(0, len(pool_of_possibilities) - 1)
		return pool_of_possibilities[el_index]

if __name__ == "__main__":
	var_num, op_num = pick_number_of_var_op()
	possible_values = pick_variables(var_num)

	l_bracket_count = 0
	pool_of_possibilities = possible_values + [OPERATOR_NOT] + [OPERATOR_L_BRACKET] + posible_const_values

	output_string = ""
	used_variables = set()

	while (var_num > 0 and op_num > 0):
		#print(pool_of_possibilities)
		element = pick_element(pool_of_possibilities)

		output_string = add_to_string(element, output_string)

		l_bracket_count += check_for_bracket(element)

		pool_of_possibilities = determine_pool(element, possible_values, l_bracket_count)
		if (is_operator(element)): op_num -= 1
		else:
			if (element not in used_variables):
				used_variables.add(element)
				var_num -= 1

	for i in range(l_bracket_count):
		output_string = add_to_string(OPERATOR_R_BRACKET, output_string)
	print (output_string)


from django.contrib.auth.decorators import login_required
from django.contrib import messages
from django.shortcuts import render, redirect, get_object_or_404
from jfeature.models import Feature
from jfeature.forms import FeatureForm

# Create your views here.
@login_required
def upsert(request, fid=None):
    if fid:
        feature = get_object_or_404(Feature, pk=fid)
    else:
        feature = Feature()

    if request.method == 'POST':
        form = FeatureForm(request.POST or None, instance=feature)

        if form.is_valid():
            feature_obj = form.save(commit=False)
            feature_obj.owner = request.user
            feature_obj.save()
            messages.add_message(request, level=messages.SUCCESS, message='درخواست با موفقیت ثبت گردید.')
            return redirect('feature:create')
        else:
            messages.error(request, 'بروز خطا، داده ثبت نشد.')
            return redirect('feature:create')
    else:
        form = FeatureForm(instance=feature)

    features = Feature.objects.all()
    context = {
        'form': form,
        'features': features
    }
    return render(request, 'jfeature/edit_or_create.html', context)


#! /usr/bin/env python
from __future__ import print_function
from builtins import range
from numpy import *
import proteus
from proteus.MeshTools import *
from proteus.FemTools import *

## \ingroup scripts
#
# \file readFunLite.py
#
# \brief A lightweight method for reading .3dm and .dat files and generating a finite element function.

def readFunLite():
    from optparse import OptionParser
    usage = "usage: %readFunLite [options] meshFile funFile funOutFile"
    parser = OptionParser(usage=usage)
    parser.add_option("-m", "--matlab",
                      help="print edges to files for plotting in matlab",
                      action="store_true",
                      dest="matlab",
                      default=False)
    parser.add_option("-M", "--view-matlab",
                      help="plot edges in matlab",
                      action="store_true",
                      dest="viewMatlab",
                      default=False)
    parser.add_option("-x", "--nnodes-x",
                      help="use NX nodes in the x direction",
                      action="store",
                      type="int",
                      dest="nx",
                      default=2)
    parser.add_option("-y", "--nnodes-y",
                      help="use NY nodes in the y direction",
                      action="store",
                      type="int",
                      dest="ny",
                      default=2)
    parser.add_option("-z", "--nnodes-z",
                      help="use NZ nodes in the z direction",
                      action="store",
                      type="int",
                      dest="nz",
                      default=2)
    (opts, args) = parser.parse_args()
    if len(args) == 3:
        meshFilename = args[0]
        funFilename = args[1]
        funOutFilename = args[2]
    else:
        print(usage)
        return
    #just get the nodes from the mesh file
    meshIn= open(meshFilename,'r')
    print("Reading nodes of the mesh from file=%s" % (meshFilename))
    line = meshIn.readline()
    columns = line.split()
    nodes=[]
    adhBase = 1
    while (columns[0] != 'ND'):
        line = meshIn.readline()
        columns = line.split()
    else:
        while (line and columns[0] == 'ND'):
            nodes.append(Node(int(columns[1]) - adhBase,
                              float(columns[2]),
                              float(columns[3]),
                              float(columns[4])))
            line=meshIn.readline()
            columns=line.split()
    funIn = open(funFilename,'r')
    nNodes=0
    fun = []
    for i in range(6):
        line = funIn.readline()
        print(line.strip())
        words = line.split()
        if words[0] == 'ND':
            nNodes = int(words[1])
            if nNodes != len(nodes):
                print("the number of nodes in mesh and function files don't match")
    line = funIn.readline()
    while line.strip() != 'ENDDS':
        print("Reading "+line.strip())
        words = line.split()
        u = zeros(nNodes,Float)
        for i in range(nNodes):
            u[i] = float(funIn.readline())
        fun.append(u)
        line = funIn.readline()
    print("Read %i timesteps" % len(fun))
    print("Writing coordinate files x.grf, y.grf, and z.grf")
    xiList={}
    xOut = open('x.grf','w')
    yOut = open('y.grf','w')
    zOut = open('z.grf','w')
    for n in nodes:
        xOut.write('%15.8e \n'% n.x())
        yOut.write('%15.8e \n'% n.y())
        zOut.write('%15.8e \n'% n.z())
    xOut.close()
    yOut.close()
    zOut.close()
    for ts,f in enumerate(fun):
        funOutFileTS = funOutFilename+repr(ts)+'.grf'
        print("Writing time step=%i to file=%s" % (ts,funOutFileTS))
        funOut = open(funOutFileTS,'w')
        for v in f:
            funOut.write('%15.8e \n'% v)
        funOut.close()
if __name__ == '__main__':
    import sys
    #import profile
    #profile.run('readFunLite(sys.argv[1],sys.argv[2],sys.argv[3])','readFunLiteProf')
    #import pstats
    #p = pstats.Stats('readFunLiteProf')
    #p.sort_stats('cumulative').print_stats(20)
    #p.sort_stats('time').print_stats(20)
    readFunLite()


class Solution:
    def findPeakElement(self, A: List[int]):
        if not A:
            return None
        if len(A) == 1:
            return 0
        if len(A) == 2:
            return 0 if A[0] > A[1] else 1
        start, end = 0, len(A)-1
        while start+1 < end:
            mid = (start+end)//2
            if A[mid] > A[mid+1]:
                end = mid
            else:
                start = mid
        return start if A[start] > A[end] else end


#Program to print Fibonacci Numbers Without Recursion
n=int(input("Enter the value of 'n': "))
#first two terms are first and second
first=0
second=1
sum=0
count=1
print("Fibonacci Sequence: ")
# Count should not be more then n.
while(count<=n):
  print(sum)
  count+=1
  first=second
  second=sum
  sum=first+second

#Program to generate Fibonacci sequence recursively
def Fibonacci(Number):
    if(Number==0):
        return 0
    elif(Number==1):
        return 1
    else:
        return (Fibonacci(Number-2)+Fibonacci(Number-1)) #return the sum of two prevous terms
n=int(input("Enter the value of 'n': "))
print("Fibonacci Sequence:")
for n in range(0, n):
  print(Fibonacci(n))


#**********QR and Barcode detection using Pyzbar library**************

# Import necessary libraries
import numpy as np
import cv2
from pyzbar.pyzbar import decode                  # For decoding of QR and Barcode
import qrcode                                     # To generate custom QR Codes
import barcode                                    # To generate custom Barcode
from barcode import EAN13
from barcode.writer import ImageWriter

#+++++++++++++++++++++++++Generate a QR code+++++++++++++++++++++++++++++++++++
qr_code = qrcode.make("I am very bad")
qr_code.save('my_intro_qr.png')

           #-----------More control over the QRCode generation-------
controlled_qr_code = qrcode.QRCode(
    version=1,                                            # 1(smallest) to 40 --> indicates the size of QRCode
    error_correction= qrcode.constants.ERROR_CORRECT_M,   # controls the error correction used for the QR Code
                                                                # ERROR_CORRECT_L, ERROR_CORRECT_Q, ERROR_CORRECT_H
    box_size= 15,
    border=5
)
data = 'nothing is free'
controlled_qr_code.add_data(data)
controlled_qr_code.make(fit=True)
image = controlled_qr_code.make_image(fill_color='black', back_color='white')
#image.save("controlled_QR_generated.png")

#++++++++++++++++++++++++++++Barcode Generation+++++++++++++++++++++++++++++++++++

with open('barcode.png', 'wb') as f:
    EAN13('123456789102', writer=ImageWriter()).write(f)        # Accepts ONLY 12 digits. 13th digit is auto-generated

#+++++++++++++++++++Decode QRcode from an image++++++++++++++++++++++++++++++++++++

image = cv2.imread('barcode.png')   # eg:'controlled_QR_generated.png'
code = decode(image)
print(code)
cv2.namedWindow('image', flags=cv2.WINDOW_NORMAL)      # WINDOW_NORMAL or WINDOW_AUTOSIZE
cv2.imshow('image', image)
cv2.waitKey(0)
#image = cv2.imread('barcode.jpg')   # 'controlled_QR_generated.png'

#++++++++++++++++++++Decode multiple QR Codes in an image+++++++++++++++++++++++++++

#code = decode(image)
for decoded_code in code:
    print(decoded_code.data)
    my_data = decoded_code.data.decode('utf-8')       # Gives the actual data that was encoded
    print(my_data)
    points = np.array([decoded_code.polygon], np.int32)
    points = points.reshape((-1, 1, 2))
    cv2.polylines(image, [points], True, (255, 0, 255), 2)
    pts2 = decoded_code.rect
    cv2.putText(image, my_data, (pts2[0],pts2[1]), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255,0,255),2)

cv2.imshow("Result", image)
cv2.waitKey(0)

#+++++++++++++++++++++Decode images captured from the camera+++++++++++++++++++++++++

def decode_camera_capture():
    camera = cv2.VideoCapture(0)
    camera.set(3,640)
    camera.set(4,480)
    while True:
        success, img = camera.read()
        for decoded_code in code:
            print(decoded_code.data)
            my_data = decoded_code.data.decode('utf-8')  # Gives the actual data that was encoded
            print(my_data)
            points = np.array([decoded_code.polygon], np.int32)
            points = points.reshape((-1,1,2))
            cv2.polylines(img, [points], True, (255,0,255), 5)
            pts2 = decoded_code.rect
            cv2.putText(image, my_data, (pts2[0],pts2[1]), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255,0,255),2)

        cv2.imshow("Result", img)
        cv2.waitKey(1)
        if success == False:
            break
#decode_camera_capture()

cv2.destroyAllWindows()


# -*- coding:utf-8 -*-

from django.conf.urls import url
from axf import views

urlpatterns = [
    url(r'home/$',views.home),
    url(r'market/(\w+)/(\w+)/(\w+)/$',views.market),
    url(r'cart/$',views.cart),
    url(r'mine/$',views.mine),

    #获取商品信息
    # url(r'products/(\w+)/$', views.products)
]


import json
from django.views.generic import TemplateView
from django.conf import settings
from django.utils.translation import ugettext as _
from django.utils import formats

class EventCalendarView(TemplateView):
    template_name = 'event_calendar/calendar.html'

    def get_context_data(self, **kwargs):
        day_names = [
            _('Mon'),
            _('Tue'),
            _('Wed'),
            _('Thu'),
            _('Fri'),
            _('Sat'),
            _('Sun')
        ]
        month_names = [
            _('January'),
            _('February'),
            _('March'),
            _('April'),
            _('May'),
            _('June'),
            _('July'),
            _('August'),
            _('September'),
            _('October'),
            _('November'),
            _('December')
        ]

        kwargs['EC_BUILTIN_JQUERY'] = getattr(settings, 'EC_BUILTIN_JQUERY', True)
        kwargs['DAY_NAMES'] = json.dumps(day_names)
        kwargs['MONTH_NAMES'] = json.dumps(month_names)
        kwargs['DATE_FORMAT'] = getattr(settings, 'EC_DATE_FORMAT', 'YYYY-MM-DD')
        return kwargs


mtStr='파이썬은 재밌어요.'
strPosList=[]
index=0

while True:
    try:
        index = myStr.index('파이썬', index)
        strPosList.append(index)
        index = index+1     #다음 위치부터 찾음
    except:
        break

print('파이썬 글자 위치 -->.',strPosList)


import warnings

import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Conv2D, Flatten, Dense, MaxPooling2D, Dropout

warnings.filterwarnings("ignore", category=DeprecationWarning)

# Conv2D — сверточный слой
# Activation — слой активации
# MaxPooling2D -слой дискретизации на основе выборки.
#   Цель состоит в том, чтобы уменьшить выборку входного представления (изображение, выходную матрицу скрытого слоя и т. Д.),
#   Уменьшив его размерность и сделав допущения относительно предположений о свойствах, содержащихся в выбранных субрегионах.
#   Работает на основе выбора максимального значения из подвыборки размером pool_size.
# Dropout — слой прореживания для решения проблемы переобучения сети
# Flatten — преобразование в одномерный вектор
# Dense — полносвязный слой

class Running:
    @classmethod
    def train(cls, listTrain, listTest, num_epochs):
        train_answers = np.zeros((6600, 10))
        test_answers = np.zeros((2200, 10))
        train_answers = cls.form_output(train_answers, 660)
        test_answers = cls.form_output(test_answers, 220)

        print("Обучение...")
        model = Sequential()

        model.add(Conv2D(32, kernel_size=(3, 3),
                         activation='relu',
                         input_shape=(93, 13, 1)))  # Входной блок 93 строки, 13 столбцов, 1 элемент
        # #Операция максимальной подвыборки
        model.add(MaxPooling2D(pool_size=(2, 2)))
        # слой выравнивания
        model.add(Dropout(0.25))
        # одно измерение
        model.add(Flatten())
        # полносвязный слой
        model.add(Dense(128, activation='relu'))
        # слой выравнивания
        model.add(Dropout(0.5))
        # полносвязный слой
        model.add(Dense(10, activation='softmax'))

        # блок по 93, 13 в строке, 1 значение
        # model.add(Conv2D(64, kernel_size=3, activation='relu', input_shape=(93, 13, 1)))
        # model.add(MaxPooling2D())
        # model.add(Conv2D(128, kernel_size=3, activation='relu'))
        # model.add(Flatten())  # – слой, преобразующий 2D-данные в 1D-данные.
        # model.add(Dense(10, activation='softmax'))
        #############################################

        # optimizer = 'adam'(Адам: метод стохастической оптимизации).Функция потерь: loss = 'categorical_crossentropy'
        # категориальная перекрестная энтропия(categorical crossentropy CCE)
        # model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
        model.compile(optimizer='sgd', loss='mean_squared_error', metrics=['accuracy'])

        history = model.fit(listTrain, train_answers, epochs=num_epochs, validation_data=(listTest, test_answers))

        print("История:")
        print(history.history)
        model.save('Model')
        cls.draw_graph(history)

    # формируем структуру выхода (10 классов (по датасету))
    @classmethod
    def form_output(cls, output_list, data_len):
        for j in range(0, 10):
            for i in range(0, data_len):
                val = np.zeros(10)
                val[j] = 1
                output_list[j * data_len + i] = val
        return output_list

    @classmethod
    def draw_graph(cls, history):
        plt.subplot(2, 1, 1)
        plt.plot(history.history['accuracy'])
        plt.title('Точность при обучении')
        plt.ylabel('Точность')
        plt.xlabel('Эпохи')
        plt.subplot(2, 1, 2)
        plt.plot(history.history['loss'])
        plt.title('Потери при обучении')
        plt.ylabel('Потеря')
        plt.xlabel('Эпохи')
        plt.show()

    @classmethod
    def test(cls, listTest):
        print("Тест")
        model = tf.keras.models.load_model('Model')
        test_answers = np.zeros((2200, 10))
        test_answers = cls.form_output(test_answers, 220)
        score = model.evaluate(listTest, test_answers, verbose=0)
        print('Test loss:', score[0])
        print('Test accuracy:', score[1])

        res = model.predict(listTest)
        c = 1
        while c != 0:
            print("Enter index, 0 - Exit")
            c = int(input())
            elem = res[c]
            max = 0
            index = -1
            for i in range(10):
                if max < elem[i]:
                    max = elem[i]
                    index = i
            print(index + 1)
            max = 0
            index = 0


import logging
import sys

from cliff.command import Command

import utils
import settings

LOG = logging.getLogger(__name__)

class install(object):

    def __init__(self):
        pass

    def install(self, node, pkg_name):
        utils.apt_get(node, pkg_name)

    def conf(self, node, content, remote_path, tmp_path='/tmp/salt_tmp.conf'):
        '''
        args type:
          node: str
          content: str
          remote_path: str
        '''

        utils.write_data(tmp_path, content)
        utils.upload_file(tmp_path, remote_path, node)

    def service(self, node, service_name):
        ''' restart service no remote host'''
        cmd = "service {0} restart".format(service_name)
        utils.exec_remote_cmd(cmd, node)

    def iptables(self, node, port):
        cmd = r''' grep -q "{0}" /etc/iptables/rules.v4 || echo none'''.format(port)
        result = utils.exec_remote_cmd(cmd, node)
        if result == 'none':
            cmd = ''' sed -r -i '/INPUT -p icmp/s/(.*)/\1\n-A INPUT -p tcp -m multiport --ports {0} -j ACCEPT/' /etc/iptables/rules.v4 '''.format(port)
            utils.exec_remote_cmd(cmd, node)
            self.service(node, 'iptables-persistent')

class salt_minion(install):
    def __init__(self, nodes, salt_master, pkg_name='salt-minion'):
        self.pkg_name = pkg_name
        self.salt_master = salt_master

        if isinstance(nodes, list):
            for node in nodes:
                LOG.info('\033[1;31mstart to deploy salt-minion on {0}:\033[0m'.format(node))
                self.deploy(node)
        elif isinstance(nodes, str):
            LOG.info('\033[1;31mstart to deploy salt-minion on {0}:\033[0m'.format(node))
            self.deploy(nodes)
        else:
            LOG.error("can't install salt-minion on {0}.".format(nodes))
            sys.exit(1)

    def deploy(self, node):
        LOG.info('\033[1;31mcreating /etc/salt/minion_id on {0}:\033[0m'.format(node))
        mkdir = '''mkdir -p /etc/salt/'''
        utils.exec_remote_cmd(mkdir, node)
        minion_id = '''echo {0} > /etc/salt/minion_id'''.format(node)
        utils.exec_remote_cmd(minion_id, node)

        LOG.info('\033[1;31minstalling salt-minion on {0}:\033[0m'.format(node))
        self.install(node, self.pkg_name)

        LOG.info('\033[1;31mcopy conf_file to {0}:\033[0m'.format(node))
        remote_path = '/etc/salt/minion.d/master.conf'
        content = 'master: {0}'.format(self.salt_master)
        self.conf(node, content, remote_path)

        LOG.info('\033[1;31mrestart salt-minion on {0}:\033[0m'.format(node))
        self.service(node, 'salt-minion')
        self.iptables(node, '4505')
        self.iptables(node, '4506')

class salt_api(install):
    def __init__(self, nodes, pkg_name='salt-api python-cherrypy3'):
        self.pkg_name = pkg_name

        if isinstance(nodes, list):
            for node in nodes:
                LOG.info('\033[1;31mstart to deploy salt-api on {0}:\033[0m'.format(node))
                self.deploy(node)
        elif isinstance(nodes, str):
            LOG.info('\033[1;31mstart to deploy salt-api on {0}:\033[0m'.format(nodes))
            self.deploy(nodes)
        else:
            LOG.error("can't install salt-api on {0}.".format(nodes))
            sys.exit(1)

    def deploy(self, node, api_user='salt_api', api_pass='salt_api', api_port=18000, api_listen='0.0.0.0'):
        LOG.info('\033[1;31minstalling salt-api on {0}:\033[0m'.format(node))
        self.install(node, self.pkg_name)
        LOG.info('\033[1;31mcopy conf_file to {0}:\033[0m'.format(node))
        remote_path = '/etc/salt/master.d/api.conf'

        content = ['external_auth:',
                   '  pam:',
                   '    {0}:'.format(api_user),
                   '      - .*',
                   'rest_cherrypy:',
                   '  port: {0}'.format(api_port),
                   '  host: {0}'.format(api_listen),
                   '  debug: True',
                   '  ssl_crt: /etc/pki/tls/certs/localhost.crt',
                   '  ssl_key: /etc/pki/tls/certs/localhost.key',
                   '\n',]
        content = '\n'.join(content)
        self.conf(node, content, remote_path)
        LOG.info('\033[1;31madd salt_api user on {0}:\033[0m'.format(node))
        cmd = " grep -q {0} /etc/passwd || useradd {0}".format(api_user)
        utils.exec_remote_cmd(cmd, node)
        cmd = " echo {0}:{1} | chpasswd".format(api_user, api_pass)
        utils.exec_remote_cmd(cmd, node)

        LOG.info('\033[1;31mgenrate keys on {0}:\033[0m'.format(node))
        cmd = 'salt-call --local tls.create_self_signed_cert '
        utils.exec_remote_cmd(cmd, node)

        LOG.info('\033[1;31mrestart salt-master salt-api on {0}:\033[0m'.format(node))
        self.service(node, 'salt-master')
        self.service(node, 'salt-api')

        self.iptables(node, api_port)

class salt_master(install):

    def __init__(self, nodes, groups, pkg_name='salt-master'):
        self.pkg_name = pkg_name
        self.groups = groups

        if isinstance(nodes, list):
            for node in nodes:
                LOG.info('\033[1;31mstart to deploy salt-master on {0}:\033[0m'.format(node))
                self.deploy(node)
        elif isinstance(nodes, str):
            LOG.info('\033[1;31mstart to deploy salt-master on {0}:\033[0m'.format(nodes))
            self.deploy(nodes)
        else:
            LOG.error("can't install salt-master on {0}.".format(nodes))
            sys.exit(1)

    def deploy(self, node):
        LOG.info('\033[1;31minstalling salt-master on {0}:\033[0m'.format(node))
        self.install(node, self.pkg_name)
        LOG.info('\033[1;31mcopy conf_file to {0}:\033[0m'.format(node))
        remote_path = '/etc/salt/master.d/nodegroups.conf'
        content = 'auto_accept: True\n'
        content += 'nodegroups:\n'
        for role, nodes in self.groups.items():
            content += '  {0}: {1}\n'.format(role, ','.join(nodes))
        self.conf(node, content, remote_path)
        LOG.info('\033[1;31mrestart salt-master on {0}:\033[0m'.format(node))
        self.service(node, 'salt-master')

        self.iptables(node, '4505')
        self.iptables(node, '4506')

class saltstack(Command):
    '''install saltstack in openstack cluster. primary-controller is salt-master'''

    def get_parser(self, prog_name):
        parser = super(saltstack, self).get_parser(prog_name)
        parser.add_argument('--env', type=int, required=True, help='Fuel cluster ID')
        parser.add_argument('--master', action='store_true', help='Install salt-master on primary-controller')
        parser.add_argument('--api', action='store_true', help='Install salt-api on primary-controller')
        parser.add_argument('--minion', action='store_true', help='Install salt-minion on all nodes')
        return parser

    def take_action(self, parsed_args):
        self._get_nodedata(parsed_args.env)

        #for node in self.nodes:
        #    LOG.info('\033[1;31mdeploy mirror_cilent no {0}:\033[0m'.format(node))
        #    self.mirror_client(node)

        if parsed_args.master:
            salt_master(self.primary_controller, self.groups)

        if parsed_args.api:
            salt_api(self.primary_controller)

        if parsed_args.minion:
            salt_minion(self.nodes, self.primary_controller)

        if (not parsed_args.master) and (not parsed_args.minion) and (not parsed_args.api):
            salt_master(self.primary_controller, self.groups)
            salt_api(self.primary_controller)
            salt_minion(self.nodes, self.primary_controller)

    def _get_nodedata(self, env):
        astute = '{0}{1}{2}'.format('/etc/fuel/cluster/',env,'/astute.yaml')
        data = utils.parse_yaml(astute)

        self.groups = {}

        for node, metadata in data['network_metadata']['nodes'].items():

            for role in metadata['node_roles']:
                if role in self.groups:
                    self.groups[role].append(node)
                else:
                    self.groups[role] = [node]

            if 'primary-controller' in metadata['node_roles']:
                self.primary_controller = node
                break
        self.nodes = data['network_metadata']['nodes'].keys()
        self.app.stdout.write('\033[1;31msalt-master: {0}\nsalt-minion: {1} \033[0m\n'.format(
                              self.primary_controller,
                              ','.join(self.nodes),))
        self.app.stdout.write('*' * 100 + '\n')

    def mirror_client(self, node):
        cmd1 = 'echo {0} > {1}'.format(settings.deb_client_content, settings.deb_client_listfile)
        cmd2 = 'wget -q -O - {0} | sudo apt-key add -'.format(settings.deb_public_key)
        cmd3 = 'apt-get -y update'
        utils.exec_remote_cmd(cmd1, node)
        utils.exec_remote_cmd(cmd2, node)
        utils.exec_remote_cmd(cmd3, node)


#!/usr/bin/env python
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.api import memcache
import wsgiref.handlers
import os
import re
import logging
import urllib
import simplejson
from operator import itemgetter
import ConfigParser
from main import BaseRequestHandler
import datetime

config = ConfigParser.ConfigParser()
config.read('config.cfg')

class MainHandler(BaseRequestHandler):

	def fetch_day(self):
		date = self.request.get("date")

		if (not(date) or date == 'latest'):
			today = datetime.date.today()
			yesterday = today - datetime.timedelta(1)
			date = yesterday.strftime("%Y-%m-%d")

		if (not(date) or not(re.match("^\d\d\d\d-\d\d-\d\d$", date))):
			return ([], None)

		twends = memcache.get("twends/date/%s" % (date))
		if (twends is not None):
			return (twends, date)

		base_url = 'http://search.twitter.com/trends/daily.json'
		json = simplejson.load(urllib.urlopen(base_url + '?' + urllib.urlencode({
								'date': date
							})
		))

		try:
				trends = json['trends']
		except (KeyError, IndexError):
				return (None, None)
		structured_trends = {}
		all_times = []

		for date_time in trends.keys():
			date = date_time[:10]
			time = re.sub(".*? (.*?)", "\1", date_time)
			all_times.append(time)

			for trend in trends[date_time]:
				if (not(trend["name"] in structured_trends)):
					structured_trends[trend["name"]] = { "count": 0, "timelist": [] }

				structured_trends[trend["name"]]["timelist"].append(time)

		all_times = sorted(all_times)

		for trend in structured_trends:
			structured_trends[trend]["timelist"] = sorted(structured_trends[trend]["timelist"])
			structured_trends[trend]["count"] = len(structured_trends[trend]["timelist"])
			i = 0
			timespans = []

			while i < len(structured_trends[trend]["timelist"]):
				all_times_index = all_times.index(structured_trends[trend]["timelist"][i])
				trend_index_start = i
				trend_index_counter = i

				while ((trend_index_counter < len(structured_trends[trend]["timelist"]) and
							cmp(all_times[all_times_index], structured_trends[trend]["timelist"][trend_index_counter]) == 0)):
					all_times_index += 1
					trend_index_counter += 1

				timespans.append((structured_trends[trend]["timelist"][i],structured_trends[trend]["timelist"][trend_index_counter-1]))
				i = trend_index_counter
			structured_trends[trend]["timelist"] = timespans

		sorted_trends = sorted(structured_trends.items(), key=lambda k: k[1]['count'])
		sorted_trends.reverse()
		memcache.set("twends/date/%s" % (date), sorted_trends)
		return (sorted_trends, date)

	def get(self):
		(twends, date) = self.fetch_day()

		context = {
				"twends": twends,
				"date": date,
				"success": True
				}

		if (date != None):
			(year, month, day) = date.split('-')
			today = datetime.date(int(year), int(month), int(day))

			start_date = datetime.date(2008,11,19)
			diff = start_date - today
			if (diff.days > 0):
				context['too_early'] = True
				context['success'] = False
			else:
				context['wikipedia_date'] = today.strftime("%B_%Y#%Y_%B_%d")

				yesterday = today - datetime.timedelta(1)
				tomorrow = today + datetime.timedelta(1)
				context['yesterday'] = yesterday.strftime("%Y-%m-%d")

				delta = datetime.date.today() - today
				if (delta.days != 1):
					context["tomorrow"] = tomorrow.strftime("%Y-%m-%d")

				if (delta.days < 0):
					context["in_future"] = True
					context["success"] = False
		else:
			context["success"] = False

		context['google_analytics_key'] = config.get('blog', 'google_analytics_key')
		self.render('projects/twends/base.html', context)

def main():
	logging.getLogger().setLevel(logging.DEBUG)

	application = webapp.WSGIApplication(
																				[
																					(r'.*', MainHandler),
																				],
																				debug=True)
	wsgiref.handlers.CGIHandler().run(application)

if __name__ == '__main__':
	main()


# -*- coding: utf-8 -*-

import numpy as np
import json
from argparse import ArgumentParser

def get_intersections(coastline_points, validate_line):
    validate_diff = validate_line[1]-validate_line[0]
    norm = np.sqrt((validate_diff**2).sum())
    points_diff = coastline_points - validate_line[0]
    t = (points_diff*validate_diff).sum(axis=1)/norm**2
    dist = (np.matmul(points_diff, np.array([[0, -1], [1, 0]]))*validate_diff).sum(axis=1)/norm
    valid_positive_mask = (t >= 0)*(t <= 1)*(dist >= 0)
    valid_negative_mask = (t >= 0)*(t <= 1)*(dist < 0)
    positives = np.array([t[valid_positive_mask],
                          dist[valid_positive_mask]]).T
    negatives = np.array([t[valid_negative_mask],
                          dist[valid_negative_mask]]).T
    if len(positives) and len(negatives):
        positive_points = np.concatenate([positives, coastline_points[valid_positive_mask]],
                                         axis=1)
        negative_points = np.concatenate([negatives, coastline_points[valid_negative_mask]],
                                         axis=1)
        positive_mask = positive_points[:, 1] <= 1
        positive_near = positive_points[positive_mask]
        negative_mask = negative_points[:, 1] >= -1
        negative_near = negative_points[negative_mask]

        intersections = []
        if len(positive_near):
            intersections.append(validate_line[0] + validate_diff*positive_near[:, :1])
        if len(negative_near):
            intersections.append(validate_line[0] + validate_diff*negative_near[:, :1])

        if len(intersections):
            intersections = np.concatenate(intersections)

            return 0, intersections
        else:
            positive_nearest = positive_points[np.argmin(positive_points, axis=0)[1]]
            negative_nearest = negative_points[np.argmax(negative_points, axis=0)[1]]
            r = np.abs(negative_nearest[1])/(np.abs(negative_nearest[1])+positive_nearest[1])
            intersection = r*positive_nearest[-2:]+(1-r)*negative_nearest[-2:]

            return 1, intersection
    else:
        return 2, None

def ExtractError(sub, ans, options):
    eval_files = set(sub).intersection(set(ans))
    score = 0
    miss_cost = options['miss_cost']
    for eval_file in sorted(eval_files):
        validate_lines = np.array(ans[eval_file]['validate_lines'])
        ans_points = []
        valid_validate_lines = []
        for validate_line in validate_lines:
            flag, point = get_intersections(np.array(ans[eval_file]['coastline_points']), validate_line)
            if flag==0:
                ans_points.append(point.mean(axis=0))
                valid_validate_lines.append(validate_line)
            elif flag==1:
                ans_points.append(point)
                valid_validate_lines.append(validate_line)
        ans_points = np.array(ans_points)
        valid_validate_lines = np.array(valid_validate_lines)
        sub_points = np.array(sub[eval_file])
        err = 0
        for ans_point, validate_line in zip(ans_points, valid_validate_lines):
            flag, point = get_intersections(sub_points, validate_line)
            if flag==0:
                e = np.sqrt(((ans_point-point)**2).sum(axis=1).max())
                err += e
            elif flag==1:
                e = np.sqrt(((ans_point-point)**2).sum())
                err += e
            else:
                err += miss_cost
        error = err/len(ans_points)
        print(eval_file, 'Extract Error: {}'.format(error))
        score += error

    return score/len(eval_files)

def parse_args():
    parser = ArgumentParser()
    parser.add_argument('--prediction-file', default = 'predictions.json')
    parser.add_argument('--annotation-file', default = 'annotations.json')

    return parser.parse_args()

def main():
    args = parse_args()

    with open(args.prediction_file) as f:
        sub = json.load(f)

    with open(args.annotation_file) as f:
        ans = json.load(f)

    options = {'miss_cost': 100}

    score = ExtractError(sub, ans, options)
    print('\nOverall:', score)

if __name__ == '__main__':
    main()


# -*- coding: utf-8 -*-
"""
Created on Sun Oct 18 12:10:49 2020

@author: koushb
"""

import numpy as np
import pandas as pd
from sklearn.metrics import mean_squared_error as mse
from sklearn.metrics import r2_score
import matplotlib.pyplot as plt
from sklearn.model_selection import cross_val_score

import lightgbm as lgbm
from sklearn.model_selection import GridSearchCV

from sklearn.model_selection import train_test_split

df_train=pd.read_csv("../Data/House Price Prediction/train.csv")
df_train['date']=pd.to_datetime(df_train['date'])
df_train['year']=df_train['date'].dt.year
df_train['month']=df_train['date'].dt.month

y=df_train['price']
X=df_train.drop(['price','id','date'],axis=1)

reg=lgbm.LGBMRegressor(
    objective='regression',
    boosting_type='gbdt',
    learning_rate=0.1,
    num_leaves=31,
    n_estimators=25,
    min_split_gain=0.1,
    subsample=0.6,
    colsample_bytree=0.8,
    reg_alpha=5,
    reg_lambda=5,
    )

reg.fit(X,y)
pred=reg.predict(X)
pred[pred>df_train['price'].max()]=df_train['price'].max()
pred[pred<df_train['price'].min()]=df_train['price'].min()
print('MSE is:',np.sqrt(mse(y,pred)))
print('R2 is:',r2_score(y,pred))
plt.scatter(y,pred,s=1)
plt.xlim(0,8000000)
plt.ylim(0,8000000)
scores=cross_val_score(reg,X=X,y=y,cv=5,scoring='r2')
print(scores)
print('cv_mean:',round(scores.mean(),2))
print('cv_std:',round(scores.std(),2))

gridparam={
    'num_leaves':[30,50],
    'boosting_type':['gbdt','dart'],
    'learning_rate':[0.1,0.01],
    'n_estimators':[25,50],
    'subsample':[0.7,0.9],
    'colsample_bytree':[0.8,0.9],
    'reg_alpha':[0,5,10],
    'reg_lambda':[0,5,10]

    }

grid=GridSearchCV(reg,gridparam,cv=4,verbose=1)
grid.fit(X,y)

print(grid.best_score_)
print(grid.best_params_)

reg=lgbm.LGBMRegressor(
    objective='regression',
    boosting_type=grid.best_params_['boosting_type'],
    learning_rate=grid.best_params_['learning_rate'],
    num_leaves=grid.best_params_['num_leaves'],
    n_estimators=grid.best_params_['n_estimators'],
    min_split_gain=0.1,
    subsample=grid.best_params_['subsample'],
    colsample_bytree=grid.best_params_['colsample_bytree'],
    reg_alpha=grid.best_params_['reg_alpha'],
    reg_lambda=grid.best_params_['reg_lambda'],
    )

reg.fit(X,y)
pred=reg.predict(X)
pred[pred>df_train['price'].max()]=df_train['price'].max()
pred[pred<df_train['price'].min()]=df_train['price'].min()
print('MSE is:',np.sqrt(mse(y,pred)))
print('R2 is:',r2_score(y,pred))
plt.scatter(y,pred,s=1)
plt.xlim(0,8000000)
plt.ylim(0,8000000)
scores=cross_val_score(reg,X=X,y=y,cv=5,scoring='r2')
print(scores)
print('cv_mean:',round(scores.mean(),2))
print('cv_std:',round(scores.std(),2))

#######################################################
params = {
    'application': 'regression',
#     'num_class' : 1, # used for multi-classes
    'boosting': 'gbdt', # traditional gradient boosting decision tree
    'num_iterations': 1000,
    'learning_rate': 0.05,
    'num_leaves': 62,
    'device': 'cpu', # you can use GPU to achieve faster learning
    'max_depth': -1, # <0 means no limit
    'max_bin': 510, # Small number of bins may reduce training accuracy but can deal with over-fitting
    'lambda_l1': 5, # L1 regularization
    'lambda_l2': 10, # L2 regularization
    'metric' : 'rmse',
    'subsample_for_bin': 200, # number of samples for constructing bins
    'subsample': 0.8, # subsample ratio of the training instance
    'colsample_bytree': 0.8, # subsample ratio of columns when constructing the tree
    'min_split_gain': 0.5, # minimum loss reduction required to make further partition on a leaf node of the tree
    'min_child_weight': 1, # minimum sum of instance weight (hessian) needed in a leaf
    'min_child_samples': 5# minimum number of data needed in a leaf
}

X_train,X_valid,y_train,y_valid=train_test_split(X,y,test_size=0.2)
d_train=lgbm.Dataset(X_train,label=y_train)
d_valid=lgbm.Dataset(X_valid,label=y_valid)

model=lgbm.train(params,train_set=d_train,num_boost_round=1000,valid_sets=d_valid,early_stopping_rounds=50,verbose_eval=4)


def primo(p):
    if p > 2:
        for x in range (2,p):
            if p%x == 0:
                return False
    else:
        return False
    return True

def maior_primo_menor_que(n):
    lista=[]
    for i in range (n+1):
        primos=primo(i)
        if primos== True:
            lista.append(i)

    if len(lista)==0:
        return -1
    maximo=max(lista)
    return maximo

print(maior_primo(6))


import objects
import solver_gpu
import dxchange
import tomopy
import numpy as np

def gaussian(size, rin=0.8, rout=1):
    r, c = np.mgrid[:size, :size] + 0.5
    rs = np.sqrt((r - size/2)**2 + (c - size/2)**2)
    rmax = np.sqrt(2) * 0.5 * rout * rs.max() + 1.0
    rmin = np.sqrt(2) * 0.5 * rin * rs.max()
    img = np.zeros((size, size), dtype='float32')
    img[rs < rmin] = 1.0
    img[rs > rmax] = 0.0
    zone = np.logical_and(rs > rmin, rs < rmax)
    img[zone] = np.divide(rmax - rs[zone], rmax - rmin)
    return img

def scanner3(theta, shape, sx, sy, margin=[0, 0], offset=[0, 0], spiral=0):
    a = spiral
    scan = []
    lenmaxx = 0
    lenmaxy = 0

    for m in range(len(theta)):
        s = objects.Scanner(shape, sx, sy, margin, offset=[
                            offset[0], np.mod(offset[1]+a, sy)])
        scan.append(s)
        a += spiral
        lenmaxx = max(lenmaxx, len(s.x))
        lenmaxy = max(lenmaxy, len(s.y))

    scanax = -1+np.zeros([len(theta), lenmaxx], dtype='int32')
    scanay = -1+np.zeros([len(theta), lenmaxy], dtype='int32')

    for m in range(len(theta)):
        scanax[m, :len(scan[m].x)] = scan[m].x
        scanay[m, :len(scan[m].y)] = scan[m].y

    return scan, scanax, scanay

if __name__ == "__main__":

    tau= 1e3
    alpha = 1e-2
    eta = 0.25/720/64
    titer = 1
    NITER = 257
    maxint = 10
    voxelsize = 1e-6
    energy = 5

    # Load a 3D object.
    beta = dxchange.read_tiff(
        'data/test-beta-128.tiff').astype('float32')[::2, ::2, ::2]
    delta = dxchange.read_tiff(
        'data/test-delta-128.tiff').astype('float32')[::2, ::2, ::2]
    #beta=beta/beta.max()
    #delta=delta/delta.max()

    # Create object.
    obj = objects.Object(beta, delta, voxelsize)
    # Create probe.
    prb = objects.Probe(gaussian(15, rin=0.8, rout=1.0), maxint=maxint)
    # Detector parameters.
    det = objects.Detector(63, 63)
    # Define rotation angles.
    theta = np.linspace(0, 2*np.pi, 720).astype('float32')
    # Raster scan parameters for each rotation angle.
    scan, scanax, scanay = scanner3(theta, beta.shape, 6, 6, margin=[
                                    prb.size, prb.size], offset=[0, 0], spiral=1)
    tomoshape = [len(theta), obj.shape[1], obj.shape[2]]

    # class solver
    slv = solver_gpu.Solver(prb, scanax, scanay,
                            theta, det, voxelsize, energy, tomoshape)

    # Project
    data = slv.fwd_tomo(obj.complexform)
    phi = np.zeros([3, *obj.shape], dtype='complex64', order='C')
    mu = np.zeros([3, *obj.shape], dtype='complex64', order='C')
    x = objects.Object(np.zeros(obj.shape, dtype='float32', order='C'), np.zeros(
        obj.shape, dtype='float32', order='C'), voxelsize)

    # ADMM
    x = slv.admm_tomo(data, phi, mu, x, tau, eta, alpha, titer, NITER)

    # Save result
    dxchange.write_tiff(x.beta,  'beta/beta')
    dxchange.write_tiff(x.delta,  'delta/delta')


import matplotlib.pyplot as plt
import numpy as np
import math as math
#from scipy import spatial

import sys
print(sys.version)
import warnings
warnings.filterwarnings('ignore')

from sklearn.neighbors import KDTree

def SVDTransformFinder(X,P):

    Mu_X=np.mean(X, axis=1,keepdims=True)
    Mu_P=np.mean(P, axis=1,keepdims=True)

    X_Prime=X-Mu_X
    P_Prime=P-Mu_P
    W=np.dot(X_Prime,P_Prime.T)

    u,s,vh=np.linalg.svd(W , full_matrices=True)

    print("---------------SVD result----------------\n")

    R=np.matmul(u,vh)
    print ("rotation: ",R)
    T=Mu_X-np.matmul(R,Mu_P)
    print ("Translation: ",T)
    return R,T

if __name__ == "__main__":

    point_count = 50
    th = np.pi / 8
    move = np.array([[0.40], [0.5]])
    x1 = np.linspace(0, 1.1, point_count)
    y1 = np.sin(x1 * np.pi)
    d1 = np.array([x1, y1])

    rot = np.array([[np.cos(th), -np.sin(th)], [np.sin(th), np.cos(th)]])
    d2 = np.dot(rot, d1) + move
    #d2 = np.dot(rot, d1)
    #d2 = d1 + move

    print ("-------------original values---------------\n")
    print ("rot", rot)
    print ("move", move)

    print ("---------------------------------\n")

    xmin=-2
    xmax=6
    ymin=-1
    ymax=4

    plt.axis([xmin,xmax,ymin,ymax])

#Ploting points
    plt.plot(d1[0], d1[1])
    plt.plot(d2[0], d2[1])

    x1=d1[0]
    y1=d1[1]
    x2=d2[0]
    y2=d2[1]

    print ("---------------------------x1.shape-----------------------------\n")

    print (x1.shape)

    plt.plot([x1, x2], [y1, y2], color='k', linestyle='-', linewidth=1)

    R,T=SVDTransformFinder(d2,d1)

    d = np.dot(R, d1)+T

    print ("rotation: ",R)

    print ("Translation: ",T)

    plt.plot(d[0], d[1],color='green')

    plt.show()

    print ("-------------------------------------------------------------\n")

    tmp= d1.T
    qurey_point= d2.T

    print ("qurey_point.shape", qurey_point.shape)
    tree = KDTree(tmp)
    nearest_dist, nearest_ind = tree.query(qurey_point, k=1)  # k=2 nearest neighbors where k1 = identity
    print ("nearest_ind", nearest_ind )    # drop id)
    print ("tmp[nearest_ind]", tmp[nearest_ind])

    result=tmp[nearest_ind]
    result=result.reshape(50,2)
    print ( "result.shape", result.shape)

    x1=result[:,0]
    y1=result[:,1]

    x2=qurey_point[:,0]
    y2=qurey_point[:,1]

    plt.axis([xmin,xmax,ymin,ymax])

    print ("--------------------------------------")
    print (x2)
    print ("--------------------------------------")
    print (y2)

    plt.plot([x1, x2], [y1, y2], color='k', linestyle='-', linewidth=1)
    plt.scatter([x1, x2], [y1, y2])

    plt.show()

#     tmp=np.array([ [1, 1], [2, 2], [3, 3], [4, 4], [5, 5] ]    )
#     qurey_point=np.array(    [ [1.5, 1.4] ,  [1.5, 1.9]  ]    )
#     tree = KDTree(tmp)
#     nearest_dist, nearest_ind = tree.query(qurey_point, k=1)  # k=2 nearest neighbors where k1 = identity
#     print(nearest_ind)     # drop id
#     print(tmp[nearest_ind])

#     tree = KDTree(d1)
#     nearest_dist, nearest_ind = tree.query(d2, k=1)
#
#
#
#     print "----------------------------nearest_ind---------------------------------\n"
#
#     print nearest_ind
#
#     print nearest_dist
#
#     print "----------------------------########################---------------------------------\n"
#
#
#
#     x1=d1[nearest_ind][0]
#     y1=d1[nearest_ind][1]
#
#
#     x1=x1.T
#     y1=y1.T
#
#     print x1.shape
#     print y1.shape
#
#
#
#     x1=x1.reshape(50,)
#     y1=y1.reshape(50,)
#
#
#     x2=d2[0]
#     y2=d2[1]
#
#
#
#
#
# #    x2=x2.reshape(50,1)
# #    y2=y2.reshape(50,1)
#
#     print x2.shape
#     print y2.shape
#
#     plt.plot([x1, x2], [y1, y2], color='k', linestyle='-', linewidth=1)
#
#     plt.show()