<commit_before>#!/usr/bin/env python
# encoding: utf-8

"""
    查看email源码， 发现email这个标准库解析邮件头的时候， 暴露的接口只返回第一个邮件地址。
    当收件人有多个的时候， 没法得到所有的收件地址, 所以这里我自己封装下

    ~~~~~~~~~~
    mailparser.py
"""

import logging
import functools
import email
from email._parseaddr import AddressList as _AddressList

def _log_on_error(default=False):
    logger = logging.getLogger(__name__)
    def func(f):
        @functools.wraps(f)
        def _func(*args, **kwargs):
            try:
                return f(*args, **kwargs)
            except Exception as e:
                log = '{0} error: {1}'.format(f.__name__, e)
                print log
                logger.error(log)
                return default
        return _func
    return func

class ParseHeader(object):
    def __init__(self, header):
        header = r"%s"%header
        header = header.encode('utf-8')
        self.msg = email.message_from_string(header)

    @_log_on_error(default=None)
    def get_email_from(self):
        efrom = _AddressList(self.msg.get("from")).addresslist
        return efrom[0][1]

    @_log_on_error(default=[])
    def get_email_to(self, unique=True):
        etos = _AddressList(self.msg.get("to")).addresslist
        etos = [e[1] for e in etos]
        return list(set(etos)) if unique else etos

    @_log_on_error(default=[])
    def get_email_cc(self, unique=True):
        ccs = _AddressList(self.msg.get("cc")).addresslist
        ccs =  [c[1] for c in ccs]
        return list(set(ccs)) if unique else ccs

    @_log_on_error(default=[])
    def get_cc_to(self, unique=True):
        to = self.get_email_to(unique=False)
        cc = self.get_email_cc(unique=False)
        to.extend(cc)
        return list(set(to)) if unique else to

    @_log_on_error(default='')
    def get_subject(self):
        subj = self.msg.get('subject')
        return subj

if __name__ == '__main__':
    header = u'X-Account-Key: account1\r\nX-UIDL: ZC4706-sq0K95C_dbf_wVoKxWyYG29\r\nX-Mozilla-Status: 0001\r\nX-Mozilla-Status2: 10000000\r\nX-Mozilla-Keys:  \r\nX-QQ-mid: Yesmtp11t1346908060t416t1211\r\nReceived: from bigseaPC (unknown [222.95.243.130])\tby esmtp4.qq.com (ESMTP) with SMTP id 0\tfor <list_everyone@test.com>; Thu, 06 Sep 2012 13:07:39 +0800 (CST)\r\nX-QQ-SSF: 00010000000000F0FxF200000000000\r\nFrom:\u5434\u6668\u6656 <test@test.com>\r\nTo: <list_everyone@test.com>\r\nSubject:\u3010\u7fbd\u6bdb\u7403\u6bd4\u8d5b\u3011\r\nDate: Thu, 6 Sep 2012 13:07:39 +0800\r\nMessage-ID: <002501cd8bed$89a4c780$9cee5680$@test.com>\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed;\tboundary="----=_NextPart_000_0026_01CD8C30.97C80780"\r\nX-Mailer: Microsoft Outlook 14.0\r\nThread-Index: Ac2L7ElOSevHm0QbQJWRKIvXeM8sNQ==\r\nContent-Language: zh-cn\r\n'
    p = ParseHeader(header)
    print p.get_email_from()
    print p.get_email_to()
    print p.get_email_cc()
    print p.get_cc_to()
    print p.get_subject()
<commit_msg>unicode<commit_after>#!/usr/bin/env python
# encoding: utf-8

"""
    查看email源码， 发现email这个标准库解析邮件头的时候， 暴露的接口只返回第一个邮件地址。
    当收件人有多个的时候， 没法得到所有的收件地址, 所以这里我自己封装下

    ~~~~~~~~~~
    mailparser.py
"""

import logging
import functools
import email
from email._parseaddr import AddressList as _AddressList

def _log_on_error(default=False):
    logger = logging.getLogger(__name__)
    def func(f):
        @functools.wraps(f)
        def _func(*args, **kwargs):
            try:
                return f(*args, **kwargs)
            except Exception as e:
                logger.error('{0} error: {1}'.format(f.__name__, e))
                return default
        return _func
    return func

def to_unicode(str):
    try:
        return str.decode('utf-8', 'ignore') if not isinstance(str, unicode) else str
    except:
        return str

class ParseHeader(object):
    def __init__(self, header):
        header = r"%s"%header
        header = header.encode('utf-8') if isinstance(header, unicode) else header
        self.msg = email.message_from_string(header)

    @_log_on_error(default=None)
    def get_email_from(self):
        efrom = _AddressList(self.msg.get("from")).addresslist
        efrom = efrom[0][1]
        return efrom

    @_log_on_error(default=[])
    def get_email_to(self, unique=True):
        etos = _AddressList(self.msg.get("to")).addresslist
        etos = [e[1] for e in etos]
        etos = [to_unicode(t) for t in etos]
        return list(set(etos)) if unique else etos

    @_log_on_error(default=[])
    def get_email_cc(self, unique=True):
        ccs = _AddressList(self.msg.get("cc")).addresslist
        ccs = [c[1] for c in ccs]
        ccs = [to_unicode(c) for c in ccs]
        return list(set(ccs)) if unique else ccs

    @_log_on_error(default=[])
    def get_cc_to(self, unique=True):
        to = self.get_email_to(unique=False)
        cc = self.get_email_cc(unique=False)
        to.extend(cc)
        return list(set(to)) if unique else to

    @_log_on_error(default='')
    def get_subject(self):
        subj = self.msg.get('subject')
        return to_unicode(subj)

if __name__ == '__main__':
    header = u'X-Account-Key: account1\r\nX-UIDL: ZC4706-sq0K95C_dbf_wVoKxWyYG29\r\nX-Mozilla-Status: 0001\r\nX-Mozilla-Status2: 10000000\r\nX-Mozilla-Keys:  \r\nX-QQ-mid: Yesmtp11t1346908060t416t1211\r\nReceived: from bigseaPC (unknown [222.95.243.130])\tby esmtp4.qq.com (ESMTP) with SMTP id 0\tfor <list_everyone@test.com>; Thu, 06 Sep 2012 13:07:39 +0800 (CST)\r\nX-QQ-SSF: 00010000000000F0FxF200000000000\r\nFrom:\u5434\u6668\u6656 <test@test.com>\r\nTo: <list_everyone@test.com>\r\nSubject:\u3010\u7fbd\u6bdb\u7403\u6bd4\u8d5b\u3011\r\nDate: Thu, 6 Sep 2012 13:07:39 +0800\r\nMessage-ID: <002501cd8bed$89a4c780$9cee5680$@test.com>\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed;\tboundary="----=_NextPart_000_0026_01CD8C30.97C80780"\r\nX-Mailer: Microsoft Outlook 14.0\r\nThread-Index: Ac2L7ElOSevHm0QbQJWRKIvXeM8sNQ==\r\nContent-Language: zh-cn\r\n'
    p = ParseHeader(header)
    print p.get_email_from()
    print p.get_email_to()
    print p.get_email_cc()
    print p.get_cc_to()
    print p.get_subject()
<|endoftext|>


<commit_before># Copyright (C) 2006, Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

# Rewritten by Eduardo Silva, edsiper@gmail.com

import os

import pygtk
pygtk.require('2.0')
import gtk
import gobject
import pango

from sugar import env

class MultiLogView(gtk.VBox):
    def __init__(self, path, extra_files):
        self._active_log = None
        self._extra_files = extra_files

        # Creating Main treeview with Actitivities list
        self._tv_menu = gtk.TreeView()
        self._tv_menu.connect('cursor-changed', self._load_log)
        self._tv_menu.set_rules_hint(True)

        # Set width
        box_width = gtk.gdk.screen_width() * 80 / 100
        self._tv_menu.set_size_request(box_width*25/100, 0)

        self._store_menu = gtk.TreeStore(str)
        self._tv_menu.set_model(self._store_menu)

        self._add_column(self._tv_menu, 'Sugar logs', 0)
        self._logs_path = os.path.join(env.get_profile_path(), 'logs')
        self._activity = {}

        # Activities menu
        self.hbox = gtk.HBox(False, 3)
        self.hbox.pack_start(self._tv_menu, True, True, 0)

        # Activity log, set width
        self._view = LogView()
        self._view.set_size_request(box_width*75/100, 0)

        self.hbox.pack_start(self._view, True, True, 0)
        self.hbox.show_all()

        gobject.timeout_add(1000, self._update)

    # Load the log information in View (textview)
    def _load_log(self, treeview):
        treeselection = treeview.get_selection()
        treestore, iter = treeselection.get_selected()

        # Get current selection
        act_log = self._store_menu.get_value(iter, 0)

        # Set buffer and scroll down
        self._view.textview.set_buffer(self._activity[act_log])
        self._view.textview.scroll_to_mark(self._activity[act_log].get_insert(), 0);
        self._active_log = act_log

    def _update(self):
        # Searching log files
        for logfile in os.listdir(self._logs_path):
            full_log_path = os.path.join(self._logs_path, logfile)
            self._add_log_file(full_log_path)

        for ext in self._extra_files:
            self._add_log_file(ext)

        return True

    def _get_filename_from_path(self, path):
        return path.split('/')[-1]

    def _add_log_file(self, path):
        if os.path.isdir(path):
            return False

        logfile = self._get_filename_from_path(path)

        if not self._activity.has_key(logfile):
            self._add_activity(logfile)
            model = LogBuffer(path)
            self._activity[logfile] = model

        self._activity[logfile].update()
        written = self._activity[logfile]._written

        # Load the first iter
        if self._active_log == None:
            self._active_log = logfile
            iter = self._tv_menu.get_model().get_iter_root()
            self._tv_menu.get_selection().select_iter(iter)
            self._load_log(self._tv_menu)

        if written > 0 and self._active_log == logfile:
            self._view.textview.scroll_to_mark(self._activity[logfile].get_insert(), 0)

    def _add_activity(self, name):
        self._insert_row(self._store_menu, None, name)

    # Add a new column to the main treeview, (code from Memphis)
    def _add_column(self, treeview, column_name, index):
        cell = gtk.CellRendererText()
        col_tv = gtk.TreeViewColumn(column_name, cell, text=index)
        col_tv.set_resizable(True)
        col_tv.set_property('clickable', True)

        treeview.append_column(col_tv)

        # Set the last column index added
        self.last_col_index = index

    # Insert a Row in our TreeView
    def _insert_row(self, store, parent, name):
        iter = store.insert_before(parent, None)
        index = 0
        store.set_value(iter, index , name)

        return iter

class LogBuffer(gtk.TextBuffer):
    def __init__(self, logfile):
        gtk.TextBuffer.__init__(self)

        self._logfile = logfile
        self._pos = 0
        self.update()

    def update(self):
        f = open(self._logfile, 'r')

        init_pos = self._pos

        f.seek(self._pos)
        self.insert(self.get_end_iter(), f.read())
        self._pos = f.tell()

        f.close()

        self._written = (self._pos - init_pos)
        return True

class LogView(gtk.ScrolledWindow):
    def __init__(self):
        gtk.ScrolledWindow.__init__(self)

        self.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)

        self.textview = gtk.TextView()
        self.textview.set_wrap_mode(gtk.WRAP_WORD)

        font = pango.FontDescription('Sans 8')
        font.set_weight(pango.WEIGHT_LIGHT)
        self.textview.modify_font(font)

        # Set background color
        bgcolor = gtk.gdk.color_parse("#FFFFFF")
        self.textview.modify_base(gtk.STATE_NORMAL, bgcolor)

        self.textview.set_editable(False)

        self.add(self.textview)
        self.textview.show()

class Interface:
    def __init__(self):
        path = None
        xserver_logfile = self._get_xserver_logfile_path()

        # Aditional files to watch in logviewer
        ext_files = []
        ext_files.append(xserver_logfile)
        ext_files.append("/var/log/syslog")
        ext_files.append("/var/log/messages")

        viewer = MultiLogView(path, ext_files)
        self.widget = viewer.hbox

    # Get the Xorg log file path, we have two ways to get the path: do a system
    # call and exec a 'xset -q' or just read directly the file that we know
    # always be the right one for a XO machine...
    def _get_xserver_logfile_path(self):
        path = "/var/log/Xorg.0.log"
        return path
<commit_msg>Console: avoid to add missed logfiles<commit_after># Copyright (C) 2006, Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

# Rewritten by Eduardo Silva, edsiper@gmail.com

import os

import pygtk
pygtk.require('2.0')
import gtk
import gobject
import pango

from sugar import env

class MultiLogView(gtk.VBox):
    def __init__(self, path, extra_files):
        self._active_log = None
        self._extra_files = extra_files

        # Creating Main treeview with Actitivities list
        self._tv_menu = gtk.TreeView()
        self._tv_menu.connect('cursor-changed', self._load_log)
        self._tv_menu.set_rules_hint(True)

        # Set width
        box_width = gtk.gdk.screen_width() * 80 / 100
        self._tv_menu.set_size_request(box_width*25/100, 0)

        self._store_menu = gtk.TreeStore(str)
        self._tv_menu.set_model(self._store_menu)

        self._add_column(self._tv_menu, 'Sugar logs', 0)
        self._logs_path = os.path.join(env.get_profile_path(), 'logs')
        self._activity = {}

        # Activities menu
        self.hbox = gtk.HBox(False, 3)
        self.hbox.pack_start(self._tv_menu, True, True, 0)

        # Activity log, set width
        self._view = LogView()
        self._view.set_size_request(box_width*75/100, 0)

        self.hbox.pack_start(self._view, True, True, 0)
        self.hbox.show_all()

        gobject.timeout_add(1000, self._update)

    # Load the log information in View (textview)
    def _load_log(self, treeview):
        treeselection = treeview.get_selection()
        treestore, iter = treeselection.get_selected()

        # Get current selection
        act_log = self._store_menu.get_value(iter, 0)

        # Set buffer and scroll down
        self._view.textview.set_buffer(self._activity[act_log])
        self._view.textview.scroll_to_mark(self._activity[act_log].get_insert(), 0);
        self._active_log = act_log

    def _update(self):
        # Searching log files
        for logfile in os.listdir(self._logs_path):
            full_log_path = os.path.join(self._logs_path, logfile)
            self._add_log_file(full_log_path)

        for ext in self._extra_files:
            self._add_log_file(ext)

        return True

    def _get_filename_from_path(self, path):
        return path.split('/')[-1]

    def _add_log_file(self, path):
        if os.path.isdir(path):
            return False

        if not os.path.exists(path):
            print "ERROR: %s don't exists"
            return False

        logfile = self._get_filename_from_path(path)

        if not self._activity.has_key(logfile):
            self._add_activity(logfile)
            model = LogBuffer(path)
            self._activity[logfile] = model

        self._activity[logfile].update()
        written = self._activity[logfile]._written

        # Load the first iter
        if self._active_log == None:
            self._active_log = logfile
            iter = self._tv_menu.get_model().get_iter_root()
            self._tv_menu.get_selection().select_iter(iter)
            self._load_log(self._tv_menu)

        if written > 0 and self._active_log == logfile:
            self._view.textview.scroll_to_mark(self._activity[logfile].get_insert(), 0)

    def _add_activity(self, name):
        self._insert_row(self._store_menu, None, name)

    # Add a new column to the main treeview, (code from Memphis)
    def _add_column(self, treeview, column_name, index):
        cell = gtk.CellRendererText()
        col_tv = gtk.TreeViewColumn(column_name, cell, text=index)
        col_tv.set_resizable(True)
        col_tv.set_property('clickable', True)

        treeview.append_column(col_tv)

        # Set the last column index added
        self.last_col_index = index

    # Insert a Row in our TreeView
    def _insert_row(self, store, parent, name):
        iter = store.insert_before(parent, None)
        index = 0
        store.set_value(iter, index , name)

        return iter

class LogBuffer(gtk.TextBuffer):
    def __init__(self, logfile):
        gtk.TextBuffer.__init__(self)

        self._logfile = logfile
        self._pos = 0
        self.update()

    def update(self):
        f = open(self._logfile, 'r')

        init_pos = self._pos

        f.seek(self._pos)
        self.insert(self.get_end_iter(), f.read())
        self._pos = f.tell()

        f.close()

        self._written = (self._pos - init_pos)
        return True

class LogView(gtk.ScrolledWindow):
    def __init__(self):
        gtk.ScrolledWindow.__init__(self)

        self.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)

        self.textview = gtk.TextView()
        self.textview.set_wrap_mode(gtk.WRAP_WORD)

        font = pango.FontDescription('Sans 8')
        font.set_weight(pango.WEIGHT_LIGHT)
        self.textview.modify_font(font)

        # Set background color
        bgcolor = gtk.gdk.color_parse("#FFFFFF")
        self.textview.modify_base(gtk.STATE_NORMAL, bgcolor)

        self.textview.set_editable(False)

        self.add(self.textview)
        self.textview.show()

class Interface:
    def __init__(self):
        path = None
        xserver_logfile = self._get_xserver_logfile_path()

        # Aditional files to watch in logviewer
        ext_files = []
        ext_files.append(xserver_logfile)
        ext_files.append("/var/log/syslog")
        ext_files.append("/var/log/messages")

        viewer = MultiLogView(path, ext_files)
        self.widget = viewer.hbox

    # Get the Xorg log file path, we have two ways to get the path: do a system
    # call and exec a 'xset -q' or just read directly the file that we know
    # always be the right one for a XO machine...
    def _get_xserver_logfile_path(self):
        path = "/var/log/Xorg.0.log"
        return path
<|endoftext|>


<commit_before>"""
This file demonstrates writing tests using the unittest module. These will pass
when you run "manage.py test".

Replace this with more appropriate tests for your application.
"""

from django.test import TestCase
from main.views import _split_message
from main.models import Wall
from django.contrib.auth.models import User
class SMSTest(TestCase):

    def test_single_wall(self):
        user = User.objects.create_user('Bob', 'Bob')
        user.save()

        a = Wall()
        a.hashtag = "#abc"
        a.sms_keyword = 'abc'
        a.user = user
        a.save()

        self.assertEquals(_split_message('This is a sentence'), ('abc', 'This is a sentence'))

    def test_sms_keyword(self):
        user = User.objects.create_user('Bob', 'Bob')
        user.save()

        a = Wall()
        a.hashtag = "#abc"
        a.sms_keyword = 'abc'
        a.user = user
        a.save()

        b = Wall()
        b.hashtah = "#qwe"
        b.sms_keyword = 'qwe'
        b.user = user
        b.save()

        self.assertEquals(_split_message('abc Hello world'), ('abc', 'Hello world'))
        self.assertEquals(_split_message('Hello abc world'), ('abc', 'Hello world'))
        self.assertEquals(_split_message('Hello world abc'), ('abc', 'Hello world'))

        a.sms_keyword = 'AbC'
        a.save()

        self.assertEquals(_split_message('Hello AbC world'), ('AbC', 'Hello world'))

        a.sms_keyword = 'abC'
        a.save()

        self.assertEquals(_split_message('Hello world abC'), ('abC', 'Hello world'))
        self.assertEquals(_split_message('This is a message'), (None, None))
        self.assertEquals(_split_message('How are you'), (None, None))
<commit_msg>Updated tests<commit_after>"""
This file demonstrates writing tests using the unittest module. These will pass
when you run "manage.py test".

Replace this with more appropriate tests for your application.
"""

from django.test import TestCase
from main.views import _split_message
from main.models import Wall
from django.contrib.auth.models import User
from django.conf import settings
from os import environ
class SMSTest(TestCase):

    def test_single_wall(self):
        user = User.objects.create_user('Bob', 'Bob')
        user.save()

        a = Wall()
        a.hashtag = "#abc"
        a.phone_number = '+11112223333'
        a.user = user
        a.save()

        self.assertEquals(_split_message('This is a sentence', ''), (a.hashtag, 'This is a sentence'))

    def test_phone_number(self):

        user = User.objects.create_user('Bob', 'Bob')
        user.save()

        self.assertEquals(_split_message('Test message', '+12223334444'), (None, None))

        a = Wall()
        a.hashtag = "#abc"
        a.phone_number = "+11234567890"
        a.user = user
        a.save()

        b = Wall()
        b.hashtag = "#qwe"
        b.phone_number = "+11234567891"
        b.user = user
        b.save()

        self.assertEquals(_split_message('Hello world', a.phone_number), (a.hashtag, 'Hello world'))
        self.assertEquals(_split_message('Hello world', b.phone_number), (b.hashtag, 'Hello world'))
        self.assertEquals(_split_message('Hello world', '+12223334444'), (None, None))

    def test_purchase_phone_number(self):

<|endoftext|>


<commit_before># -*-coding:Utf-8 -*

# Copyright (c) 2014 LE GOFF Vincent
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
#   list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
# * Neither the name of the copyright holder nor the names of its contributors
#   may be used to endorse or promote products derived from this software
#   without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
# OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

"""Objectif suivre_cap."""

from primaires.vehicule.vecteur import get_direction
from secondaires.navigation.constantes import *
from secondaires.navigation.equipage.objectifs.rejoindre import Rejoindre

# Constantes
DISTANCE_MIN = 3

class SuivreCap(Rejoindre):

    """Objectif suivre_cap.

    Cet objectif, basé sur l'objectif rejoindre, demande à un
    équipage de suivre un cap assigné. Cet objectif est responsable
    de trouver la destination indiquée, la distance qui l'en sépare
    et d'utiliser la vitesse nécssaire, ainsi que de modifier le
    cap si besoin, quand le navire atteint sa destination.

    """

    cle = "suivre_cap"
    def __init__(self, equipage, vitesse_max):
        Rejoindre.__init__(self, equipage)
        self.vitesse_max = vitesse_max

    def trouver_cap(self):
        """Trouve le cap (x, y, vitesse).

        Cette méthode trouve le cap tel que renseigné par
        l'équipage.

        """
        equipage = self.equipage
        navire = self.navire
        if navire.orientation != 0:
            return

        if equipage.destination:
            self.x, self.y = equipage.destination
            distance = self.get_distance()
            norme = distance.norme
            vitesse = get_vitesse_noeuds(norme)
            if vitesse > self.vitesse_max:
                vitesse = self.vitesse_max
            elif norme < 25:
                vitesse = 0.6
            elif norme < 10:
                vitesse = 0.2

            self.vitesse = vitesse
            direction = round(distance.direction)
            if distance.norme <= DISTANCE_MIN:
                # On cherche le point suivant sur la carte
                cle = equipage.caps[0]
                trajet = importeur.navigation.trajets[cle]
                suivant = trajet.points.get(equipage.destination)
                if suivant is None and len(equipage.caps) > 1:
                    del equipage.caps[0]
                    cle = equipage.caps[0]
                    trajet = importeur.navigation.trajets[cle]
                    suivant = trajet.point_depart
                equipage.destination = suivant

            # On retransmet les contrôles
            Rejoindre.trouver_cap(self)
<commit_msg>Correction d'un bug d'enregistrement d'un objectif<commit_after># -*-coding:Utf-8 -*

# Copyright (c) 2014 LE GOFF Vincent
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
#   list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
# * Neither the name of the copyright holder nor the names of its contributors
#   may be used to endorse or promote products derived from this software
#   without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
# OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

"""Objectif suivre_cap."""

from primaires.vehicule.vecteur import get_direction
from secondaires.navigation.constantes import *
from secondaires.navigation.equipage.objectifs.rejoindre import Rejoindre

# Constantes
DISTANCE_MIN = 3

class SuivreCap(Rejoindre):

    """Objectif suivre_cap.

    Cet objectif, basé sur l'objectif rejoindre, demande à un
    équipage de suivre un cap assigné. Cet objectif est responsable
    de trouver la destination indiquée, la distance qui l'en sépare
    et d'utiliser la vitesse nécssaire, ainsi que de modifier le
    cap si besoin, quand le navire atteint sa destination.

    """

    cle = "suivre_cap"
    def __init__(self, equipage, vitesse_max=None):
        Rejoindre.__init__(self, equipage)
        self.vitesse_max = vitesse_max

    def trouver_cap(self):
        """Trouve le cap (x, y, vitesse).

        Cette méthode trouve le cap tel que renseigné par
        l'équipage.

        """
        equipage = self.equipage
        navire = self.navire
        if navire.orientation != 0:
            return

        if equipage.destination:
            self.x, self.y = equipage.destination
            distance = self.get_distance()
            norme = distance.norme
            vitesse = get_vitesse_noeuds(norme)
            if vitesse > self.vitesse_max:
                vitesse = self.vitesse_max
            elif norme < 25:
                vitesse = 0.6
            elif norme < 10:
                vitesse = 0.2

            self.vitesse = vitesse
            direction = round(distance.direction)
            if distance.norme <= DISTANCE_MIN:
                # On cherche le point suivant sur la carte
                cle = equipage.caps[0]
                trajet = importeur.navigation.trajets[cle]
                suivant = trajet.points.get(equipage.destination)
                if suivant is None and len(equipage.caps) > 1:
                    del equipage.caps[0]
                    cle = equipage.caps[0]
                    trajet = importeur.navigation.trajets[cle]
                    suivant = trajet.point_depart
                equipage.destination = suivant

            # On retransmet les contrôles
            Rejoindre.trouver_cap(self)
<|endoftext|>


<commit_before>"""
 Precipitation departure from the climatology grid
"""
import netCDF4
import numpy
import datetime
from pyiem import iemre
from pyiem.plot import MapPlot
from mpl_toolkits.basemap import maskoceans

sts = datetime.datetime(2013,5,1)
ets = datetime.datetime(2013,6,10)

offset1 = iemre.daily_offset(sts)
offset2 = iemre.daily_offset(ets)

nc = netCDF4.Dataset("/mesonet/data/iemre/%s_mw_daily.nc" % (sts.year,), 'r')
p01d = nc.variables['p01d']

ncc = netCDF4.Dataset("/mesonet/data/iemre/mw_dailyc.nc", 'r')
cp01d = ncc.variables['p01d']
for i in range(offset1,offset2):
    if cp01d[i,5,5] > 20:
        print i, p01d[i,5,5], cp01d[i,5,5]

diff = (numpy.sum(p01d[offset1:offset2,:,:],0) -  numpy.sum(cp01d[offset1:offset2,:,:],0)) / 25.4
print numpy.sum(p01d[offset1:offset2,5,5]), numpy.sum(cp01d[offset1:offset2,5,5])
print numpy.max(diff), numpy.min(diff)

lons = nc.variables['lon'][:]
lats = nc.variables['lat'][:]
x,y = numpy.meshgrid(lons, lats)
diff = maskoceans(x, y, diff)
extra = lons[-1] + (lons[-1] - lons[-2])
lons = numpy.concatenate([lons, [extra,]])

extra = lats[-1] + (lats[-1] - lats[-2])
lats = numpy.concatenate([lats, [extra,]])
x,y = numpy.meshgrid(lons, lats)

m = MapPlot(sector='midwest')
m.pcolormesh(x, y, diff, numpy.arange(-8,8.1,1))
m.postprocess(view=True)

nc.close()
ncc.close()

<commit_msg>9 jul feature<commit_after>"""
 Precipitation departure from the climatology grid
"""
import netCDF4
import numpy
import datetime
from pyiem import iemre
from pyiem.plot import MapPlot
from mpl_toolkits.basemap import maskoceans
import matplotlib.cm as cm

sts = datetime.datetime(2013,6,1)
ets = datetime.datetime(2013,7,9)

offset1 = iemre.daily_offset(sts)
offset2 = iemre.daily_offset(ets)

nc = netCDF4.Dataset("/mesonet/data/iemre/%s_mw_daily.nc" % (sts.year,), 'r')
p01d = nc.variables['p01d']

ncc = netCDF4.Dataset("/mesonet/data/iemre/mw_dailyc.nc", 'r')
cp01d = ncc.variables['p01d']
for i in range(offset1,offset2):
    if cp01d[i,5,5] > 20:
        print i, p01d[i,5,5], cp01d[i,5,5]

diff = (numpy.sum(p01d[offset1:offset2,:,:],0) -  numpy.sum(cp01d[offset1:offset2,:,:],0)) / 25.4
print numpy.sum(p01d[offset1:offset2,5,5]), numpy.sum(cp01d[offset1:offset2,5,5])
print numpy.max(diff), numpy.min(diff)

lons = nc.variables['lon'][:]
lats = nc.variables['lat'][:]
x,y = numpy.meshgrid(lons, lats)
diff = maskoceans(x, y, diff)
extra = lons[-1] + (lons[-1] - lons[-2])
#lons = numpy.concatenate([lons, [extra,]])

extra = lats[-1] + (lats[-1] - lats[-2])
#lats = numpy.concatenate([lats, [extra,]])
x,y = numpy.meshgrid(lons, lats)

m = MapPlot(sector='iowa',
            title='1 June - 9 July 2013 Precipitation Departure',
            subtitle='2013 vs 1950-2012 Climatology')
m.contourf(x, y, diff, numpy.arange(-5.,5.1,1.), cmap=cm.get_cmap('RdYlBu'))
m.drawcounties()
m.postprocess(filename='test.ps')
import iemplot
iemplot.makefeature('test')

nc.close()
ncc.close()

<|endoftext|>


<commit_before>from django.shortcuts import render
from django.http import HttpResponse
from .models import Piece
from urllib.request import urlopen

def search(reqest,search_term):
    return urlopen("http://localhost:8983/solr/gettingstarted/select?q="+search_term+"&wt=json&indent=true").read()

# Create your views here.
def download(request,id):
    p = Piece.objects.get(id = id)
    response = HttpResponse()
    response = HttpResponse(content_type='text/rtf')
    response['Content-Disposition'] = 'attachment; filename="{}.rtf"'.format(p.name.encode("utf8"))
    create_rtf_file(response,dictionary_from_piece(p))
    return response
def create_rtf_file(file_stream, piece):
    """
        Writes an rtf file into the file stream,
        from a piece dictionary:
            piece = {
                'name' : piece_name,
                'parts' :[
                    {
                        'name' : part_name,
                        'text' : part_text
                    },...
                ]
            }
    """
    import PyRTF
    import rtfunicode
    doc = PyRTF.Document(default_language = PyRTF.Languages.Hebrew)
    #Writing The book name:
    par = PyRTF.Paragraph(PyRTF.ParagraphPropertySet(alignment=2))
    par.append(piece["name"].encode('rtfunicode'))
    section = PyRTF.Section()
    section.append(par)
    doc.Sections.append(section)

    for part in piece["parts"]:
        par = PyRTF.Paragraph(PyRTF.ParagraphPropertySet(alignment=2))
        par.append((part["name"] + "\n"*2).encode('rtfunicode'))
        par.append((part["text"] + "\n"*2).encode('rtfunicode'))
        section = PyRTF.Section()
        section.append(par)
        doc.Sections.append(section)

    PyRTF.Renderer().Write(doc,file_stream)

def dictionary_from_piece(piece):
     return {
                 'name' : piece.name,
                 'parts' : [ { "name" : j.name, "text" : j.text } for j in
                           sorted(piece.chapters.all(),key = lambda x:x.index)]
               }
<commit_msg>New search_solr view<commit_after>from django.shortcuts import render
from django.http import HttpResponse
from .models import Piece
from urllib.request import urlopen
from urllib.parse import quote

def search(reqest,search_term):
    return HttpResponse(urlopen("http://localhost:8983/solr/gettingstarted/select?q="+quote(search_term)+"&wt=json&indent=true").read())

# Create your views here.
def download(request,id):
    p = Piece.objects.get(id = id)
    response = HttpResponse()
    response = HttpResponse(content_type='text/rtf')
    response['Content-Disposition'] = 'attachment; filename="{}.rtf"'.format(p.name.encode("utf8"))
    create_rtf_file(response,dictionary_from_piece(p))
    return response
def create_rtf_file(file_stream, piece):
    """
        Writes an rtf file into the file stream,
        from a piece dictionary:
            piece = {
                'name' : piece_name,
                'parts' :[
                    {
                        'name' : part_name,
                        'text' : part_text
                    },...
                ]
            }
    """
    import PyRTF
    import rtfunicode
    doc = PyRTF.Document(default_language = PyRTF.Languages.Hebrew)
    #Writing The book name:
    par = PyRTF.Paragraph(PyRTF.ParagraphPropertySet(alignment=2))
    par.append(piece["name"].encode('rtfunicode'))
    section = PyRTF.Section()
    section.append(par)
    doc.Sections.append(section)

    for part in piece["parts"]:
        par = PyRTF.Paragraph(PyRTF.ParagraphPropertySet(alignment=2))
        par.append((part["name"] + "\n"*2).encode('rtfunicode'))
        par.append((part["text"] + "\n"*2).encode('rtfunicode'))
        section = PyRTF.Section()
        section.append(par)
        doc.Sections.append(section)

    PyRTF.Renderer().Write(doc,file_stream)

def dictionary_from_piece(piece):
     return {
                 'name' : piece.name,
                 'parts' : [ { "name" : j.name, "text" : j.text } for j in
                           sorted(piece.chapters.all(),key = lambda x:x.index)]
               }
<|endoftext|>


<commit_before>"""Test truncated SVD transformer."""

import numpy as np
import scipy.sparse as sp

import pytest

from sklearn.decomposition import TruncatedSVD, PCA
from sklearn.utils import check_random_state
from sklearn.utils.testing import (assert_array_almost_equal, assert_equal,
                                   assert_raises, assert_greater,
                                   assert_array_less, assert_allclose)

# Make an X that looks somewhat like a small tf-idf matrix.
# XXX newer versions of SciPy >0.16 have scipy.sparse.rand for this.
shape = 60, 55
n_samples, n_features = shape
rng = check_random_state(42)
X = rng.randint(-100, 20, np.product(shape)).reshape(shape)
X = sp.csr_matrix(np.maximum(X, 0), dtype=np.float64)
X.data[:] = 1 + np.log(X.data)
Xdense = X.A

def test_algorithms():
    svd_a = TruncatedSVD(30, algorithm="arpack")
    svd_r = TruncatedSVD(30, algorithm="randomized", random_state=42)

    Xa = svd_a.fit_transform(X)[:, :6]
    Xr = svd_r.fit_transform(X)[:, :6]
    assert_array_almost_equal(Xa, Xr, decimal=5)

    comp_a = np.abs(svd_a.components_)
    comp_r = np.abs(svd_r.components_)
    # All elements are equal, but some elements are more equal than others.
    assert_array_almost_equal(comp_a[:9], comp_r[:9])
    assert_array_almost_equal(comp_a[9:], comp_r[9:], decimal=2)

def test_attributes():
    for n_components in (10, 25, 41):
        tsvd = TruncatedSVD(n_components).fit(X)
        assert_equal(tsvd.n_components, n_components)
        assert_equal(tsvd.components_.shape, (n_components, n_features))

@pytest.mark.parametrize('algorithm', ("arpack", "randomized"))
def test_too_many_components(algorithm):
    for n_components in (n_features, n_features + 1):
        tsvd = TruncatedSVD(n_components=n_components, algorithm=algorithm)
        assert_raises(ValueError, tsvd.fit, X)

@pytest.mark.parametrize('fmt', ("array", "csr", "csc", "coo", "lil"))
def test_sparse_formats(fmt):
    Xfmt = Xdense if fmt == "dense" else getattr(X, "to" + fmt)()
    tsvd = TruncatedSVD(n_components=11)
    Xtrans = tsvd.fit_transform(Xfmt)
    assert_equal(Xtrans.shape, (n_samples, 11))
    Xtrans = tsvd.transform(Xfmt)
    assert_equal(Xtrans.shape, (n_samples, 11))

@pytest.mark.parametrize('algo', ("arpack", "randomized"))
def test_inverse_transform(algo):
    # We need a lot of components for the reconstruction to be "almost
    # equal" in all positions. XXX Test means or sums instead?
    tsvd = TruncatedSVD(n_components=52, random_state=42, algorithm=algo)
    Xt = tsvd.fit_transform(X)
    Xinv = tsvd.inverse_transform(Xt)
    assert_array_almost_equal(Xinv, Xdense, decimal=1)

def test_integers():
    Xint = X.astype(np.int64)
    tsvd = TruncatedSVD(n_components=6)
    Xtrans = tsvd.fit_transform(Xint)
    assert_equal(Xtrans.shape, (n_samples, tsvd.n_components))

def test_explained_variance():
    # Test sparse data
    svd_a_10_sp = TruncatedSVD(10, algorithm="arpack")
    svd_r_10_sp = TruncatedSVD(10, algorithm="randomized", random_state=42)
    svd_a_20_sp = TruncatedSVD(20, algorithm="arpack")
    svd_r_20_sp = TruncatedSVD(20, algorithm="randomized", random_state=42)
    X_trans_a_10_sp = svd_a_10_sp.fit_transform(X)
    X_trans_r_10_sp = svd_r_10_sp.fit_transform(X)
    X_trans_a_20_sp = svd_a_20_sp.fit_transform(X)
    X_trans_r_20_sp = svd_r_20_sp.fit_transform(X)

    # Test dense data
    svd_a_10_de = TruncatedSVD(10, algorithm="arpack")
    svd_r_10_de = TruncatedSVD(10, algorithm="randomized", random_state=42)
    svd_a_20_de = TruncatedSVD(20, algorithm="arpack")
    svd_r_20_de = TruncatedSVD(20, algorithm="randomized", random_state=42)
    X_trans_a_10_de = svd_a_10_de.fit_transform(X.toarray())
    X_trans_r_10_de = svd_r_10_de.fit_transform(X.toarray())
    X_trans_a_20_de = svd_a_20_de.fit_transform(X.toarray())
    X_trans_r_20_de = svd_r_20_de.fit_transform(X.toarray())

    # helper arrays for tests below
    svds = (svd_a_10_sp, svd_r_10_sp, svd_a_20_sp, svd_r_20_sp, svd_a_10_de,
            svd_r_10_de, svd_a_20_de, svd_r_20_de)
    svds_trans = (
        (svd_a_10_sp, X_trans_a_10_sp),
        (svd_r_10_sp, X_trans_r_10_sp),
        (svd_a_20_sp, X_trans_a_20_sp),
        (svd_r_20_sp, X_trans_r_20_sp),
        (svd_a_10_de, X_trans_a_10_de),
        (svd_r_10_de, X_trans_r_10_de),
        (svd_a_20_de, X_trans_a_20_de),
        (svd_r_20_de, X_trans_r_20_de),
    )
    svds_10_v_20 = (
        (svd_a_10_sp, svd_a_20_sp),
        (svd_r_10_sp, svd_r_20_sp),
        (svd_a_10_de, svd_a_20_de),
        (svd_r_10_de, svd_r_20_de),
    )
    svds_sparse_v_dense = (
        (svd_a_10_sp, svd_a_10_de),
        (svd_a_20_sp, svd_a_20_de),
        (svd_r_10_sp, svd_r_10_de),
        (svd_r_20_sp, svd_r_20_de),
    )

    # Assert the 1st component is equal
    for svd_10, svd_20 in svds_10_v_20:
        assert_array_almost_equal(
            svd_10.explained_variance_ratio_,
            svd_20.explained_variance_ratio_[:10],
            decimal=5,
        )

    # Assert that 20 components has higher explained variance than 10
    for svd_10, svd_20 in svds_10_v_20:
        assert_greater(
            svd_20.explained_variance_ratio_.sum(),
            svd_10.explained_variance_ratio_.sum(),
        )

    # Assert that all the values are greater than 0
    for svd in svds:
        assert_array_less(0.0, svd.explained_variance_ratio_)

    # Assert that total explained variance is less than 1
    for svd in svds:
        assert_array_less(svd.explained_variance_ratio_.sum(), 1.0)

    # Compare sparse vs. dense
    for svd_sparse, svd_dense in svds_sparse_v_dense:
        assert_array_almost_equal(svd_sparse.explained_variance_ratio_,
                                  svd_dense.explained_variance_ratio_)

    # Test that explained_variance is correct
    for svd, transformed in svds_trans:
        total_variance = np.var(X.toarray(), axis=0).sum()
        variances = np.var(transformed, axis=0)
        true_explained_variance_ratio = variances / total_variance

        assert_array_almost_equal(
            svd.explained_variance_ratio_,
            true_explained_variance_ratio,
        )

def test_singular_values():
    # Check that the TruncatedSVD output has the correct singular values

    rng = np.random.RandomState(0)
    n_samples = 100
    n_features = 80

    X = rng.randn(n_samples, n_features)

    apca = TruncatedSVD(n_components=2, algorithm='arpack',
                        random_state=rng).fit(X)
    rpca = TruncatedSVD(n_components=2, algorithm='arpack',
                        random_state=rng).fit(X)
    assert_array_almost_equal(apca.singular_values_, rpca.singular_values_, 12)

    # Compare to the Frobenius norm
    X_apca = apca.transform(X)
    X_rpca = rpca.transform(X)
    assert_array_almost_equal(np.sum(apca.singular_values_**2.0),
                              np.linalg.norm(X_apca, "fro")**2.0, 12)
    assert_array_almost_equal(np.sum(rpca.singular_values_**2.0),
                              np.linalg.norm(X_rpca, "fro")**2.0, 12)

    # Compare to the 2-norms of the score vectors
    assert_array_almost_equal(apca.singular_values_,
                              np.sqrt(np.sum(X_apca**2.0, axis=0)), 12)
    assert_array_almost_equal(rpca.singular_values_,
                              np.sqrt(np.sum(X_rpca**2.0, axis=0)), 12)

    # Set the singular values and see what we get back
    rng = np.random.RandomState(0)
    n_samples = 100
    n_features = 110

    X = rng.randn(n_samples, n_features)

    apca = TruncatedSVD(n_components=3, algorithm='arpack',
                        random_state=rng)
    rpca = TruncatedSVD(n_components=3, algorithm='randomized',
                        random_state=rng)
    X_apca = apca.fit_transform(X)
    X_rpca = rpca.fit_transform(X)

    X_apca /= np.sqrt(np.sum(X_apca**2.0, axis=0))
    X_rpca /= np.sqrt(np.sum(X_rpca**2.0, axis=0))
    X_apca[:, 0] *= 3.142
    X_apca[:, 1] *= 2.718
    X_rpca[:, 0] *= 3.142
    X_rpca[:, 1] *= 2.718

    X_hat_apca = np.dot(X_apca, apca.components_)
    X_hat_rpca = np.dot(X_rpca, rpca.components_)
    apca.fit(X_hat_apca)
    rpca.fit(X_hat_rpca)
    assert_array_almost_equal(apca.singular_values_, [3.142, 2.718, 1.0], 14)
    assert_array_almost_equal(rpca.singular_values_, [3.142, 2.718, 1.0], 14)

def test_truncated_svd_eq_pca():
    # TruncatedSVD should be equal to PCA on centered data

    X_c = X - X.mean(axis=0)

    params = dict(n_components=10, random_state=42)

    svd = TruncatedSVD(algorithm='arpack', **params)
    pca = PCA(svd_solver='arpack', **params)

    Xt_svd = svd.fit_transform(X_c)
    Xt_pca = pca.fit_transform(X_c)

    assert_allclose(Xt_svd, Xt_pca, rtol=1e-9)
    assert_allclose(pca.mean_, 0, atol=1e-9)
    assert_allclose(svd.components_, pca.components_)
<commit_msg>TST refactor test_truncated_svd (#14140)<commit_after>"""Test truncated SVD transformer."""

import numpy as np
import scipy.sparse as sp

import pytest

from sklearn.decomposition import TruncatedSVD, PCA
from sklearn.utils import check_random_state
from sklearn.utils.testing import assert_array_less, assert_allclose

SVD_SOLVERS = ['arpack', 'randomized']

@pytest.fixture(scope='module')
def X_sparse():
    # Make an X that looks somewhat like a small tf-idf matrix.
    rng = check_random_state(42)
    X = sp.random(60, 55, density=0.2, format="csr", random_state=rng)
    X.data[:] = 1 + np.log(X.data)
    return X

@pytest.mark.parametrize("solver", ['randomized'])
@pytest.mark.parametrize('kind', ('dense', 'sparse'))
def test_solvers(X_sparse, solver, kind):
    X = X_sparse if kind == 'sparse' else X_sparse.toarray()
    svd_a = TruncatedSVD(30, algorithm="arpack")
    svd = TruncatedSVD(30, algorithm=solver, random_state=42)

    Xa = svd_a.fit_transform(X)[:, :6]
    Xr = svd.fit_transform(X)[:, :6]
    assert_allclose(Xa, Xr, rtol=2e-3)

    comp_a = np.abs(svd_a.components_)
    comp = np.abs(svd.components_)
    # All elements are equal, but some elements are more equal than others.
    assert_allclose(comp_a[:9], comp[:9], rtol=1e-3)
    assert_allclose(comp_a[9:], comp[9:], atol=1e-2)

@pytest.mark.parametrize("n_components", (10, 25, 41))
def test_attributes(n_components, X_sparse):
    n_features = X_sparse.shape[1]
    tsvd = TruncatedSVD(n_components).fit(X_sparse)
    assert tsvd.n_components == n_components
    assert tsvd.components_.shape == (n_components, n_features)

@pytest.mark.parametrize('algorithm', SVD_SOLVERS)
def test_too_many_components(algorithm, X_sparse):
    n_features = X_sparse.shape[1]
    for n_components in (n_features, n_features + 1):
        tsvd = TruncatedSVD(n_components=n_components, algorithm=algorithm)
        with pytest.raises(ValueError):
            tsvd.fit(X_sparse)

@pytest.mark.parametrize('fmt', ("array", "csr", "csc", "coo", "lil"))
def test_sparse_formats(fmt, X_sparse):
    n_samples = X_sparse.shape[0]
    Xfmt = (X_sparse.toarray()
            if fmt == "dense" else getattr(X_sparse, "to" + fmt)())
    tsvd = TruncatedSVD(n_components=11)
    Xtrans = tsvd.fit_transform(Xfmt)
    assert Xtrans.shape == (n_samples, 11)
    Xtrans = tsvd.transform(Xfmt)
    assert Xtrans.shape == (n_samples, 11)

@pytest.mark.parametrize('algo', SVD_SOLVERS)
def test_inverse_transform(algo, X_sparse):
    # We need a lot of components for the reconstruction to be "almost
    # equal" in all positions. XXX Test means or sums instead?
    tsvd = TruncatedSVD(n_components=52, random_state=42, algorithm=algo)
    Xt = tsvd.fit_transform(X_sparse)
    Xinv = tsvd.inverse_transform(Xt)
    assert_allclose(Xinv, X_sparse.toarray(), rtol=1e-1, atol=2e-1)

def test_integers(X_sparse):
    n_samples = X_sparse.shape[0]
    Xint = X_sparse.astype(np.int64)
    tsvd = TruncatedSVD(n_components=6)
    Xtrans = tsvd.fit_transform(Xint)
    assert Xtrans.shape == (n_samples, tsvd.n_components)

@pytest.mark.parametrize('kind', ('dense', 'sparse'))
@pytest.mark.parametrize('n_components', [10, 20])
@pytest.mark.parametrize('solver', SVD_SOLVERS)
def test_explained_variance(X_sparse, kind, n_components, solver):
    X = X_sparse if kind == 'sparse' else X_sparse.toarray()
    svd = TruncatedSVD(n_components, algorithm=solver)
    X_tr = svd.fit_transform(X)
    # Assert that all the values are greater than 0
    assert_array_less(0.0, svd.explained_variance_ratio_)

    # Assert that total explained variance is less than 1
    assert_array_less(svd.explained_variance_ratio_.sum(), 1.0)

    # Test that explained_variance is correct
    total_variance = np.var(X_sparse.toarray(), axis=0).sum()
    variances = np.var(X_tr, axis=0)
    true_explained_variance_ratio = variances / total_variance

    assert_allclose(
        svd.explained_variance_ratio_,
        true_explained_variance_ratio,
    )

@pytest.mark.parametrize('kind', ('dense', 'sparse'))
@pytest.mark.parametrize('solver', SVD_SOLVERS)
def test_explained_variance_components_10_20(X_sparse, kind, solver):
    X = X_sparse if kind == 'sparse' else X_sparse.toarray()
    svd_10 = TruncatedSVD(10, algorithm=solver).fit(X)
    svd_20 = TruncatedSVD(20, algorithm=solver).fit(X)

    # Assert the 1st component is equal
    assert_allclose(
        svd_10.explained_variance_ratio_,
        svd_20.explained_variance_ratio_[:10],
        rtol=3e-3,
    )

    # Assert that 20 components has higher explained variance than 10
    assert (
        svd_20.explained_variance_ratio_.sum() >
        svd_10.explained_variance_ratio_.sum()
    )

@pytest.mark.parametrize('solver', SVD_SOLVERS)
def test_singular_values_consistency(solver):
    # Check that the TruncatedSVD output has the correct singular values
    rng = np.random.RandomState(0)
    n_samples, n_features = 100, 80
    X = rng.randn(n_samples, n_features)

    pca = TruncatedSVD(n_components=2, algorithm=solver,
                       random_state=rng).fit(X)

    # Compare to the Frobenius norm
    X_pca = pca.transform(X)
    assert_allclose(np.sum(pca.singular_values_**2.0),
                    np.linalg.norm(X_pca, "fro")**2.0, rtol=1e-2)

    # Compare to the 2-norms of the score vectors
    assert_allclose(pca.singular_values_,
                    np.sqrt(np.sum(X_pca**2.0, axis=0)), rtol=1e-2)

@pytest.mark.parametrize('solver', SVD_SOLVERS)
def test_singular_values_expected(solver):
    # Set the singular values and see what we get back
    rng = np.random.RandomState(0)
    n_samples = 100
    n_features = 110

    X = rng.randn(n_samples, n_features)

    pca = TruncatedSVD(n_components=3, algorithm=solver,
                       random_state=rng)
    X_pca = pca.fit_transform(X)

    X_pca /= np.sqrt(np.sum(X_pca**2.0, axis=0))
    X_pca[:, 0] *= 3.142
    X_pca[:, 1] *= 2.718

    X_hat_pca = np.dot(X_pca, pca.components_)
    pca.fit(X_hat_pca)
    assert_allclose(pca.singular_values_, [3.142, 2.718, 1.0], rtol=1e-14)

def test_truncated_svd_eq_pca(X_sparse):
    # TruncatedSVD should be equal to PCA on centered data

    X_dense = X_sparse.toarray()

    X_c = X_dense - X_dense.mean(axis=0)

    params = dict(n_components=10, random_state=42)

    svd = TruncatedSVD(algorithm='arpack', **params)
    pca = PCA(svd_solver='arpack', **params)

    Xt_svd = svd.fit_transform(X_c)
    Xt_pca = pca.fit_transform(X_c)

    assert_allclose(Xt_svd, Xt_pca, rtol=1e-9)
    assert_allclose(pca.mean_, 0, atol=1e-9)
    assert_allclose(svd.components_, pca.components_)
<|endoftext|>


<commit_before>#!/usr/bin/env python
#
# This program returns the grey matter segmentation given anatomical, landmarks and t2star images
#
# ---------------------------------------------------------------------------------------
# Copyright (c) 2013 Polytechnique Montreal <www.neuro.polymtl.ca>
# Authors: Benjamin De Leener, Augustin Roux
# Created: 2014-10-18
#
# About the license: see the file LICENSE.TXT
#########################################################################################
import sct_utils as sct
import os
import time
import sys
import getopt

def main():
    fname_ref = ''
    fname_moving = ''
    transformation = 'SyN'
    gradient_step = '0.25'
    fname_seg_fixed = ''
    fname_seg_moving = ''
    fname_output = ''
    padding = '5'

    moving_name = 'moving'
    fixed_name = 'fixed'
    moving_seg_name = 'moving_seg'
    fixed_seg_name = 'fixed_seg'

    remove_temp = 1

    # Check input param
    try:
        opts, args = getopt.getopt(sys.argv[1:],'hi:d:t:s:g:o:')
    except getopt.GetoptError as err:
        print str(err)
        usage()
    for opt, arg in opts:
        if opt == '-h':
            usage()
        elif opt in ('-d'):
            fname_moving = arg
        elif opt in ('-i'):
            fname_ref = arg
        elif opt in ('-t'):
            transformation = arg
        elif opt in ('-s'):
            fname_seg_fixed = arg
        elif opt in ('-g'):
            fname_seg_moving = arg
        elif opt in ('-o'):
            fname_output = arg

    if fname_moving == '' or fname_ref == '':
        usage()

    # check existence of input files
    sct.check_file_exist(fname_ref)
    sct.check_file_exist(fname_moving)
    if (fname_seg_moving != '' and fname_seg_fixed == '') or (fname_seg_moving == '' and fname_seg_fixed != ''):
        print('\nERROR: You need to provide one mask for each image (moving and fixed)')
        usage()
    if fname_seg_moving != '':
        sct.check_file_exist(fname_seg_moving)
    if fname_seg_fixed != '':
        sct.check_file_exist(fname_seg_fixed)

    # Extract path/file/extension
    path_output, file_output, ext_output = sct.extract_fname(fname_output)

    # create temporary folder
    print('\nCreate temporary folder...')
    path_tmp = 'tmp.'+time.strftime("%y%m%d%H%M%S")
    sct.run('mkdir '+path_tmp)

    # copy files to temporary folder
    print('\nCopy files...')
    sct.run("sct_c3d "+fname_moving+" -o "+path_tmp+"/"+moving_name+".nii")
    sct.run("sct_c3d "+fname_ref+" -o "+path_tmp+"/"+fixed_name+".nii")
    if fname_seg_moving != '':
        sct.run("sct_c3d "+fname_moving+" -o "+path_tmp+"/"+moving_seg_name+".nii")
    if fname_seg_fixed != '':
        sct.run("sct_c3d "+fname_moving+" -o "+path_tmp+"/"+fixed_seg_name+".nii")

    # go to tmp folder
    os.chdir(path_tmp)

    # denoising the fixed image using non-local means from dipy
    # file = nibabel.load(fixed_name+".nii")
    # data = file.get_data()
    # hdr = file.get_header()
    # fixed_name_temp = fixed_name+"_denoised"
    # data_denoised = nlmeans(data,3)
    # fixed_name = fixed_name_temp
    # hdr.set_data_dtype('uint32') # set imagetype to uint32
    # img = nibabel.Nifti1Image(data_denoised, None, hdr)
    # nibabel.save(img, fixed_name+".nii.gz")

    # padding the images
    moving_name_temp = moving_name+"_pad"
    fixed_name_temp = fixed_name+"_pad"
    sct.run("sct_c3d "+moving_name+".nii -pad 0x0x"+padding+"vox 0x0x"+padding+"vox 0 -o "+moving_name_temp+".nii")
    sct.run("sct_c3d "+fixed_name+".nii -pad 0x0x"+padding+"vox 0x0x"+padding+"vox 0 -o "+fixed_name_temp+".nii")
    moving_name = moving_name_temp
    fixed_name = fixed_name_temp
    if fname_seg_moving != '':
        moving_seg_name_temp = moving_seg_name+"_pad"
        sct.run("sct_c3d "+moving_seg_name+".nii -pad 0x0x"+padding+"vox 0x0x"+padding+"vox 0 -o "+moving_seg_name_temp+".nii")
        moving_seg_name = moving_seg_name_temp
    if fname_seg_fixed != '':
        fixed_seg_name_temp = fixed_seg_name+"_pad"
        sct.run("sct_c3d "+fixed_seg_name+".nii -pad 0x0x"+padding+"vox 0x0x"+padding+"vox 0 -o "+fixed_seg_name_temp+".nii")
        fixed_seg_name = fixed_seg_name_temp

    # binarise the moving image
    moving_name_temp_bin = moving_name_temp + "_bin"
    cmd = 'fslmaths ' + moving_name_temp + '.nii -thr 0.25 ' + moving_name_temp_bin + '.nii'
    sct.run(cmd, 1)

    cmd = 'fslmaths ' + moving_name_temp_bin + '.nii -bin ' + moving_name_temp_bin + '.nii'
    sct.run(cmd, 1)

    moving_name = moving_name_temp_bin

    # register template to anat file: this generate warp_template2anat.nii
    # cmd = "sct_register_to_template -i " + fname_anat + " -l " + fname_landmarks + " -m " + fname_seg + " -s normal"
    # sct.run(cmd)
    # warp_template2anat = ""

    # # register anat file to t2star: generate  warp_anat2t2star
    # cmd = "sct_register_multimodal -i " + fname_anat + " -d " + fname_t2star
    # sct.run(cmd)
    # warp_anat2t2star = ""

    # # concatenation of the two warping fields
    # warp_template2t2star = "warp_template2t2star.nii.gz"
    # cmd = "sct_concat_transfo -w " + warp_template2anat + ',' + warp_anat2t2star + " -d " + fname_t2star + " -o " + warp_template2t2star
    # sct.run(cmd)

    # # apply the concatenated warping field to the template
    # cmd = "sct_warp_template -d " + fname_t2star + " -w " + warp_template2anat + " -s 1 -o template_in_t2star_space"
    # sct.run(cmd)

    #sct_register_to_template --> warp_template2anat
    # register anat file to t2star
    #sct_register_multimodal --> warp_anat2t2star
    # concatenate warp_template2anat with warp_anat2t2star
    #--> warp_template2t2star

    # registration of the grey matter
    print('\nDeforming the image...')
    moving_name_temp = moving_name+"_deformed"
    cmd = "sct_antsRegistration --dimensionality 3 --transform "+ transformation +"["+gradient_step+",3,0] --metric CC["+moving_name+".nii,"+fixed_name+".nii,1,5] --convergence 20x15 --shrink-factors 2x1 --smoothing-sigmas 0mm --Restrict-Deformation 1x1x0 --output ["+moving_name_temp+","+moving_name_temp+".nii]"
    if fname_seg_moving != '':
        cmd += " --masks["+moving_seg_name+".nii,"+fixed_seg_name+".nii]"
    sct.run(cmd)
    moving_name = moving_name_temp

    moving_name_temp = file_output+ext_output
    sct.run("sct_crop_image -i "+moving_name+".nii -dim 1 -start "+padding+" -end -"+padding+" -o "+file_output+ext_output)
    moving_name = moving_name_temp

    # move output files to initial folder
    sct.run("cp "+file_output+"* ../")

    # remove temporary file
    if remove_temp == 1:
        print('\nRemove temporary files...')
        sct.run("rm -rf "+path_tmp)

    return

#=======================================================================================================================
# usage
#=======================================================================================================================
def usage():
    print '\n' \
        ''+os.path.basename(__file__)+'\n' \
        '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n' \
        'Part of the Spinal Cord Toolbox <https://sourceforge.net/projects/spinalcordtoolbox>\n' \
        '\n'\
        'DESCRIPTION\n' \
        '  This function returns the grey matter segmentation.\n' \
        '\n'\
        'USAGE\n' \
        '  '+os.path.basename(__file__)+' -i <anat volume> -t <t2star volume> -l <landmarks>\n' \
        '\n'\
        'MANDATORY ARGUMENTS\n' \
        '  -i                   input image (with white/gray matter contrast).\n' \
        '  -d                   moving image.\n' \
        '  -o                   output name (for warping field and image).\n' \
        'OPTIONAL ARGUMENTS\n' \
        '  -t                   transformation {SyN, BSplineSyN}.\n' \
        '  -s                   input image segmentation.\n' \
        '  -g                   moving image segmentation.\n'
    sys.exit(2)

if __name__ == "__main__":
    # initialize parameters
    # param = param()
    # call main function
    main()

<commit_msg>BUG: mistake dim 2 for cropping in segment_graymatter<commit_after>#!/usr/bin/env python
#
# This program returns the grey matter segmentation given anatomical, landmarks and t2star images
#
# ---------------------------------------------------------------------------------------
# Copyright (c) 2013 Polytechnique Montreal <www.neuro.polymtl.ca>
# Authors: Benjamin De Leener, Augustin Roux
# Created: 2014-10-18
#
# About the license: see the file LICENSE.TXT
#########################################################################################
import sct_utils as sct
import os
import time
import sys
import getopt

def main():
    fname_ref = ''
    fname_moving = ''
    transformation = 'SyN'
    gradient_step = '0.25'
    fname_seg_fixed = ''
    fname_seg_moving = ''
    fname_output = ''
    padding = '5'

    moving_name = 'moving'
    fixed_name = 'fixed'
    moving_seg_name = 'moving_seg'
    fixed_seg_name = 'fixed_seg'

    remove_temp = 1

    # Check input param
    try:
        opts, args = getopt.getopt(sys.argv[1:],'hi:d:t:s:g:o:')
    except getopt.GetoptError as err:
        print str(err)
        usage()
    for opt, arg in opts:
        if opt == '-h':
            usage()
        elif opt in ('-d'):
            fname_moving = arg
        elif opt in ('-i'):
            fname_ref = arg
        elif opt in ('-t'):
            transformation = arg
        elif opt in ('-s'):
            fname_seg_fixed = arg
        elif opt in ('-g'):
            fname_seg_moving = arg
        elif opt in ('-o'):
            fname_output = arg

    if fname_moving == '' or fname_ref == '':
        usage()

    # check existence of input files
    sct.check_file_exist(fname_ref)
    sct.check_file_exist(fname_moving)
    if (fname_seg_moving != '' and fname_seg_fixed == '') or (fname_seg_moving == '' and fname_seg_fixed != ''):
        print('\nERROR: You need to provide one mask for each image (moving and fixed)')
        usage()
    if fname_seg_moving != '':
        sct.check_file_exist(fname_seg_moving)
    if fname_seg_fixed != '':
        sct.check_file_exist(fname_seg_fixed)

    # Extract path/file/extension
    path_output, file_output, ext_output = sct.extract_fname(fname_output)

    # create temporary folder
    print('\nCreate temporary folder...')
    path_tmp = 'tmp.'+time.strftime("%y%m%d%H%M%S")
    sct.run('mkdir '+path_tmp)

    # copy files to temporary folder
    print('\nCopy files...')
    sct.run("sct_c3d "+fname_moving+" -o "+path_tmp+"/"+moving_name+".nii")
    sct.run("sct_c3d "+fname_ref+" -o "+path_tmp+"/"+fixed_name+".nii")
    if fname_seg_moving != '':
        sct.run("sct_c3d "+fname_moving+" -o "+path_tmp+"/"+moving_seg_name+".nii")
    if fname_seg_fixed != '':
        sct.run("sct_c3d "+fname_moving+" -o "+path_tmp+"/"+fixed_seg_name+".nii")

    # go to tmp folder
    os.chdir(path_tmp)

    # denoising the fixed image using non-local means from dipy
    # file = nibabel.load(fixed_name+".nii")
    # data = file.get_data()
    # hdr = file.get_header()
    # fixed_name_temp = fixed_name+"_denoised"
    # data_denoised = nlmeans(data,3)
    # fixed_name = fixed_name_temp
    # hdr.set_data_dtype('uint32') # set imagetype to uint32
    # img = nibabel.Nifti1Image(data_denoised, None, hdr)
    # nibabel.save(img, fixed_name+".nii.gz")

    # padding the images
    moving_name_temp = moving_name+"_pad"
    fixed_name_temp = fixed_name+"_pad"
    sct.run("sct_c3d "+moving_name+".nii -pad 0x0x"+padding+"vox 0x0x"+padding+"vox 0 -o "+moving_name_temp+".nii")
    sct.run("sct_c3d "+fixed_name+".nii -pad 0x0x"+padding+"vox 0x0x"+padding+"vox 0 -o "+fixed_name_temp+".nii")
    moving_name = moving_name_temp
    fixed_name = fixed_name_temp
    if fname_seg_moving != '':
        moving_seg_name_temp = moving_seg_name+"_pad"
        sct.run("sct_c3d "+moving_seg_name+".nii -pad 0x0x"+padding+"vox 0x0x"+padding+"vox 0 -o "+moving_seg_name_temp+".nii")
        moving_seg_name = moving_seg_name_temp
    if fname_seg_fixed != '':
        fixed_seg_name_temp = fixed_seg_name+"_pad"
        sct.run("sct_c3d "+fixed_seg_name+".nii -pad 0x0x"+padding+"vox 0x0x"+padding+"vox 0 -o "+fixed_seg_name_temp+".nii")
        fixed_seg_name = fixed_seg_name_temp

    # binarise the moving image
    moving_name_temp_bin = moving_name_temp + "_bin"
    cmd = 'fslmaths ' + moving_name_temp + '.nii -thr 0.25 ' + moving_name_temp_bin + '.nii'
    sct.run(cmd, 1)

    cmd = 'fslmaths ' + moving_name_temp_bin + '.nii -bin ' + moving_name_temp_bin + '.nii'
    sct.run(cmd, 1)

    moving_name = moving_name_temp_bin

    # register template to anat file: this generate warp_template2anat.nii
    # cmd = "sct_register_to_template -i " + fname_anat + " -l " + fname_landmarks + " -m " + fname_seg + " -s normal"
    # sct.run(cmd)
    # warp_template2anat = ""

    # # register anat file to t2star: generate  warp_anat2t2star
    # cmd = "sct_register_multimodal -i " + fname_anat + " -d " + fname_t2star
    # sct.run(cmd)
    # warp_anat2t2star = ""

    # # concatenation of the two warping fields
    # warp_template2t2star = "warp_template2t2star.nii.gz"
    # cmd = "sct_concat_transfo -w " + warp_template2anat + ',' + warp_anat2t2star + " -d " + fname_t2star + " -o " + warp_template2t2star
    # sct.run(cmd)

    # # apply the concatenated warping field to the template
    # cmd = "sct_warp_template -d " + fname_t2star + " -w " + warp_template2anat + " -s 1 -o template_in_t2star_space"
    # sct.run(cmd)

    #sct_register_to_template --> warp_template2anat
    # register anat file to t2star
    #sct_register_multimodal --> warp_anat2t2star
    # concatenate warp_template2anat with warp_anat2t2star
    #--> warp_template2t2star

    # registration of the grey matter
    print('\nDeforming the image...')
    moving_name_temp = moving_name+"_deformed"
    cmd = "sct_antsRegistration --dimensionality 3 --transform "+ transformation +"["+gradient_step+",3,0] --metric CC["+fixed_name+".nii,"+moving_name+".nii.gz,1,5] --convergence 20x15 --shrink-factors 2x1 --smoothing-sigmas 0mm --Restrict-Deformation 1x1x0 --output ["+moving_name_temp+","+moving_name_temp+".nii]"
    if fname_seg_moving != '':
        cmd += " --masks["+moving_seg_name+".nii,"+fixed_seg_name+".nii]"
    sct.run(cmd)
    moving_name = moving_name_temp

    moving_name_temp = file_output+ext_output
    sct.run("sct_crop_image -i "+moving_name+".nii -dim 2 -start "+padding+" -end -"+padding+" -o "+file_output+ext_output)
    moving_name = moving_name_temp

    # move output files to initial folder
    sct.run("cp "+file_output+"* ../")

    # remove temporary file
    if remove_temp == 1:
        print('\nRemove temporary files...')
        sct.run("rm -rf "+path_tmp)

    return

#=======================================================================================================================
# usage
#=======================================================================================================================
def usage():
    print '\n' \
        ''+os.path.basename(__file__)+'\n' \
        '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n' \
        'Part of the Spinal Cord Toolbox <https://sourceforge.net/projects/spinalcordtoolbox>\n' \
        '\n'\
        'DESCRIPTION\n' \
        '  This function returns the grey matter segmentation.\n' \
        '\n'\
        'USAGE\n' \
        '  '+os.path.basename(__file__)+' -i <ref> -d <moving> -o <output>\n' \
        '  '+os.path.basename(__file__)+' -i <anat volume> -t <t2star volume> -l <landmarks>\n' \
        '\n'\
        'MANDATORY ARGUMENTS\n' \
        '  -i                   input image (with white/gray matter contrast).\n' \
        '  -d                   moving image.\n' \
        '  -o                   output name (for warping field and image).\n' \
        'OPTIONAL ARGUMENTS\n' \
        '  -t                   transformation {SyN, BSplineSyN}.\n' \
        '  -s                   input image segmentation.\n' \
        '  -g                   moving image segmentation.\n'
    sys.exit(2)

if __name__ == "__main__":
    # initialize parameters
    # param = param()
    # call main function
    main()

<|endoftext|>


<commit_before>import datetime

from collections import OrderedDict

from django.shortcuts import render, redirect
from django.http import HttpResponse, HttpResponseRedirect
from django.contrib import messages
from django.urls import reverse
from django.core.mail import send_mail
from django.template.loader import render_to_string

from okthess import settings
from .models import Team, Mentor, Meetup, Coworking, Post, Event, About, OkthessMeetup, ResourceCategory, Resource, ApplyText, Partner
from .forms import ApplicationForm, ContactForm

def index(request):
    if request.LANGUAGE_CODE == 'en':
        featured_posts_list = Post.en_objects.filter(is_featured=True).order_by('-date')
        for post in featured_posts_list:
            post.title = post.title_en
            post.teaser = post.teaser_en
            post.body = post.body_en
    else:
        featured_posts_list = Post.el_objects.filter(is_featured=True).order_by('-date')
        for post in featured_posts_list:
            post.title = post.title_el
            post.teaser = post.teaser_el
            post.body = post.body_el
    events_list = Event.objects.order_by('date').filter(date__gte=datetime.datetime.now())[:3]
    return render(request, 'main/index.html', {
        'featured_posts_list': featured_posts_list,
        'events_list': events_list,
    })

def about(request):
    about_texts = About.objects.first()
    if request.LANGUAGE_CODE == 'en':
        partners_list = Partner.objects.order_by('name')
        for partner in partners_list:
            partner.description = partner.description_en
        if about_texts:
            about_texts.what_text = about_texts.what_text_en
            about_texts.how_text = about_texts.how_text_en
            about_texts.participate_text = about_texts.participate_text_en
            about_texts.coworking_text = about_texts.coworking_text_en
    else:
        partners_list = Partner.objects.order_by('name')
        for partner in partners_list:
            partner.description = partner.description_el
        if about_texts:
            about_texts.what_text = about_texts.what_text_el
            about_texts.how_text = about_texts.how_text_el
            about_texts.participate_text = about_texts.participate_text_el
            about_texts.coworking_text = about_texts.coworking_text_el
    return render(request, 'main/about.html', {
        'about_texts': about_texts,
        'partners_list': partners_list,
    })

def program_redir(request):
    return redirect(reverse('main:program_teams'))

def program_teams(request):
    if request.LANGUAGE_CODE == 'en':
        teams_list = Team.en_objects.filter(alumni=False).order_by('name')
        for team in teams_list:
            team.description = team.description_en
    else:
        teams_list = Team.el_objects.filter(alumni=False).order_by('name')
        for team in teams_list:
            team.description = team.description_el
    return render(request, 'main/program-teams.html', {
        'teams_list': teams_list,
    })

def program_mentors(request):
    if request.LANGUAGE_CODE == 'en':
        mentors_list = Mentor.en_objects.order_by('name')
        for mentor in mentors_list:
            mentor.description = mentor.description_en
    else:
        mentors_list = Mentor.el_objects.order_by('name')
        for mentor in mentors_list:
            mentor.description = mentor.description_el
    return render(request, 'main/program-mentors.html', {
        'mentors_list': mentors_list,
    })

def program_partners(request):
    partners_list = Partner.objects.order_by('name')
    return render(request, 'main/program-partners.html', {
        'partners_list': partners_list,
    })

def program_alumni(request):
    if request.LANGUAGE_CODE == 'en':
        teams_list = Team.en_objects.filter(alumni=True).order_by('name')
        for team in teams_list:
            team.description = team.description_el
    else:
        teams_list = Team.el_objects.filter(alumni=True).order_by('name')
        for team in teams_list:
            team.description = team.description_el
    return render(request, 'main/program-alumni.html', {
        'teams_list': teams_list,
    })

def meetup(request):
    latest_meetup = OkthessMeetup.objects.order_by('-date').first()
    return render(request, 'main/meetup.html', {
        'latest_meetup': latest_meetup,
    })

def events(request):
    events_list_all = Event.objects.order_by('-date')
    events_archive = {}
    for event in events_list_all:
        if event.date.year not in events_archive:
            events_archive[event.date.year] = {}
        if event.date.month >= 1 and event.date.month <= 3:
            events_archive[event.date.year].setdefault('Jan-Feb-Mar', []).append(event)
        elif event.date.month >= 4 and event.date.month <= 6:
            events_archive[event.date.year].setdefault('Apr-May-Jun', []).append(event)
        elif event.date.month >= 7 and event.date.month <= 9:
            events_archive[event.date.year].setdefault('Jul-Aug-Sep', []).append(event)
        elif event.date.month >= 10 and event.date.month <= 12:
            events_archive[event.date.year].setdefault('Oct-Nov-Dev', []).append(event)
    events_archive = OrderedDict(reversed(sorted(events_archive.items())))
    now = datetime.datetime.now() - datetime.timedelta(1)
    future_events_number = 0
    for event in events_list_all:
        if datetime.datetime.combine(event.date, datetime.time(0, 0)) >= now:
            future_events_number += 1
    events_list_future = reversed(Event.objects.order_by('-date')[:future_events_number])
    events_list_past = Event.objects.order_by('-date')[future_events_number:10]
    meetups = Meetup.objects.order_by('name')
    return render(request, 'main/events.html', {
        'events_archive': events_archive,
        'events_list_past': events_list_past,
        'events_list_future': events_list_future,
        'meetups': meetups,
    })

def blog(request):
    if request.LANGUAGE_CODE == 'en':
        posts_list = Post.en_objects.order_by('-date')[:10]
        for post in posts_list:
            post.title = post.title_en
            post.teaser = post.teaser_en
            post.body = post.body_en
    else:
        posts_list = Post.el_objects.order_by('-date')[:10]
        for post in posts_list:
            post.title = post.title_el
            post.teaser = post.teaser_el
            post.body = post.body_el
    if request.LANGUAGE_CODE == 'en':
        posts_list_all = Post.en_objects.all().order_by('-date')
        for post in posts_list_all:
            post.title = post.title_en
            post.teaser = post.teaser_en
            post.body = post.body_en
    else:
        posts_list_all = Post.el_objects.all().order_by('-date')
        for post in posts_list_all:
            post.title = post.title_el
            post.teaser = post.teaser_el
            post.body = post.body_el
    archives_list = []
    for post in posts_list_all:
        post_date = post.date.replace(day=1)
        if not post_date in archives_list:
            archives_list.append(post_date)
    resources_list = Resource.objects.filter(is_pinned=True)
    return render(request, 'main/blog.html', {
        'posts_list': posts_list,
        'archives_list': archives_list,
        'resources_list': resources_list,
    })

def blog_post(request, post_slug):
    if request.LANGUAGE_CODE == 'en':
        post = Post.en_objects.get(slug=post_slug)
        post.title = post.title_en
        post.teaser = post.teaser_en
        post.body = post.body_en
    else:
        post = Post.el_objects.get(slug=post_slug)
        post.title = post.title_el
        post.teaser = post.teaser_el
        post.body = post.body_el
    return render(request, 'main/post.html', {
        'post': post,
    })

def blog_archives(request, archive_year, archive_month):
    if request.LANGUAGE_CODE == 'en':
        posts_list = Post.en_objects.filter(date__year=archive_year, date__month=archive_month)
        for post in posts_list:
            post.title = post.title_en
            post.teaser = post.teaser_en
            post.body = post.body_en
    else:
        posts_list = Post.el_objects.filter(date__year=archive_year, date__month=archive_month)
        for post in posts_list:
            post.title = post.title_el
            post.teaser = post.teaser_el
            post.body = post.body_el
    posts_list_all = Post.objects.all()
    archives_list = []
    for post in posts_list_all:
        post_date = post.date.replace(day=1)
        if not post_date in archives_list:
            archives_list.append(post_date)
    resources_list = Resource.objects.filter(is_pinned=True)
    return render(request, 'main/blog-archive.html', {
        'posts_list': posts_list,
        'archives_list': archives_list,
        'archive_year': archive_year,
        'resources_list': resources_list,
    })

def resources(request):
    categories_list = ResourceCategory.objects.all()
    return render(request, 'main/resources.html', {
        'categories_list': categories_list,
    })

def contact(request):
    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            content_vars = {
                'name': form.cleaned_data['name'],
                'email': form.cleaned_data['email'],
                'message': form.cleaned_data['message'],
            }
            send_mail(
                'Contact form submission from okthess.gr',
                render_to_string('main/contact-email.txt', content_vars),
                settings.DEFAULT_FROM_EMAIL,
                [settings.CONTACT_TO_EMAIL],
            )
            messages.info(request, "Thank you for contacting us! We'll answer ASAP.")
            return HttpResponseRedirect(reverse('main:contact'))
        else:
            return HttpResponse('Contact form is invalid.')
    else:
        return render(request, 'main/contact.html')

def apply(request):
    if request.method == 'POST':
        form = ApplicationForm(request.POST)
        if form.is_valid():
            form.save()
            applicant_email = form.cleaned_data['email']
            send_mail(
                'OK!Thess application submission',
                render_to_string('main/application-email.txt'),
                settings.DEFAULT_FROM_EMAIL,
                [applicant_email],
            )
            messages.info(request, 'Your application has been submitted. Thank you!')
            return HttpResponseRedirect(reverse('main:apply'))
        else:
            return HttpResponse('Application form submission is invalid.')
    else:
        form = ApplicationForm()
        apply_text = ApplyText.objects.first()
        application_content = ''
        if apply_text:
            if request.LANGUAGE_CODE == 'en':
                application_content = apply_text.content_en
            else:
                application_content = apply_text.content_el
        return render(request, 'main/apply.html', {
            'form': form,
            'application_content': application_content,
        })

def weekends(request):
    return render(request, 'main/weekends.html')
<commit_msg>fix multi day events to be active correctly<commit_after>import datetime

from collections import OrderedDict

from django.shortcuts import render, redirect
from django.http import HttpResponse, HttpResponseRedirect
from django.contrib import messages
from django.urls import reverse
from django.core.mail import send_mail
from django.template.loader import render_to_string

from okthess import settings
from .models import Team, Mentor, Meetup, Coworking, Post, Event, About, OkthessMeetup, ResourceCategory, Resource, ApplyText, Partner
from .forms import ApplicationForm, ContactForm

def index(request):
    if request.LANGUAGE_CODE == 'en':
        featured_posts_list = Post.en_objects.filter(is_featured=True).order_by('-date')
        for post in featured_posts_list:
            post.title = post.title_en
            post.teaser = post.teaser_en
            post.body = post.body_en
    else:
        featured_posts_list = Post.el_objects.filter(is_featured=True).order_by('-date')
        for post in featured_posts_list:
            post.title = post.title_el
            post.teaser = post.teaser_el
            post.body = post.body_el
    events_list = Event.objects.order_by('date').filter(date__gte=datetime.datetime.now())[:3]
    return render(request, 'main/index.html', {
        'featured_posts_list': featured_posts_list,
        'events_list': events_list,
    })

def about(request):
    about_texts = About.objects.first()
    if request.LANGUAGE_CODE == 'en':
        partners_list = Partner.objects.order_by('name')
        for partner in partners_list:
            partner.description = partner.description_en
        if about_texts:
            about_texts.what_text = about_texts.what_text_en
            about_texts.how_text = about_texts.how_text_en
            about_texts.participate_text = about_texts.participate_text_en
            about_texts.coworking_text = about_texts.coworking_text_en
    else:
        partners_list = Partner.objects.order_by('name')
        for partner in partners_list:
            partner.description = partner.description_el
        if about_texts:
            about_texts.what_text = about_texts.what_text_el
            about_texts.how_text = about_texts.how_text_el
            about_texts.participate_text = about_texts.participate_text_el
            about_texts.coworking_text = about_texts.coworking_text_el
    return render(request, 'main/about.html', {
        'about_texts': about_texts,
        'partners_list': partners_list,
    })

def program_redir(request):
    return redirect(reverse('main:program_teams'))

def program_teams(request):
    if request.LANGUAGE_CODE == 'en':
        teams_list = Team.en_objects.filter(alumni=False).order_by('name')
        for team in teams_list:
            team.description = team.description_en
    else:
        teams_list = Team.el_objects.filter(alumni=False).order_by('name')
        for team in teams_list:
            team.description = team.description_el
    return render(request, 'main/program-teams.html', {
        'teams_list': teams_list,
    })

def program_mentors(request):
    if request.LANGUAGE_CODE == 'en':
        mentors_list = Mentor.en_objects.order_by('name')
        for mentor in mentors_list:
            mentor.description = mentor.description_en
    else:
        mentors_list = Mentor.el_objects.order_by('name')
        for mentor in mentors_list:
            mentor.description = mentor.description_el
    return render(request, 'main/program-mentors.html', {
        'mentors_list': mentors_list,
    })

def program_partners(request):
    partners_list = Partner.objects.order_by('name')
    return render(request, 'main/program-partners.html', {
        'partners_list': partners_list,
    })

def program_alumni(request):
    if request.LANGUAGE_CODE == 'en':
        teams_list = Team.en_objects.filter(alumni=True).order_by('name')
        for team in teams_list:
            team.description = team.description_el
    else:
        teams_list = Team.el_objects.filter(alumni=True).order_by('name')
        for team in teams_list:
            team.description = team.description_el
    return render(request, 'main/program-alumni.html', {
        'teams_list': teams_list,
    })

def meetup(request):
    latest_meetup = OkthessMeetup.objects.order_by('-date').first()
    return render(request, 'main/meetup.html', {
        'latest_meetup': latest_meetup,
    })

def events(request):
    events_list_all = Event.objects.order_by('-date')
    events_archive = {}
    for event in events_list_all:
        if event.date.year not in events_archive:
            events_archive[event.date.year] = {}
        if event.date.month >= 1 and event.date.month <= 3:
            events_archive[event.date.year].setdefault('Jan-Feb-Mar', []).append(event)
        elif event.date.month >= 4 and event.date.month <= 6:
            events_archive[event.date.year].setdefault('Apr-May-Jun', []).append(event)
        elif event.date.month >= 7 and event.date.month <= 9:
            events_archive[event.date.year].setdefault('Jul-Aug-Sep', []).append(event)
        elif event.date.month >= 10 and event.date.month <= 12:
            events_archive[event.date.year].setdefault('Oct-Nov-Dev', []).append(event)
    events_archive = OrderedDict(reversed(sorted(events_archive.items())))
    now = datetime.datetime.now() - datetime.timedelta(1)
    future_events_number = 0
    for event in events_list_all:
        if event.date_end:
            if datetime.datetime.combine(event.date_end, datetime.time(0, 0)) >= now:
                future_events_number += 1
        else:
            if datetime.datetime.combine(event.date, datetime.time(0, 0)) >= now:
                future_events_number += 1
    events_list_future = reversed(Event.objects.order_by('-date')[:future_events_number])
    events_list_past = Event.objects.order_by('-date')[future_events_number:10]
    meetups = Meetup.objects.order_by('name')
    return render(request, 'main/events.html', {
        'events_archive': events_archive,
        'events_list_past': events_list_past,
        'events_list_future': events_list_future,
        'meetups': meetups,
    })

def blog(request):
    if request.LANGUAGE_CODE == 'en':
        posts_list = Post.en_objects.order_by('-date')[:10]
        for post in posts_list:
            post.title = post.title_en
            post.teaser = post.teaser_en
            post.body = post.body_en
    else:
        posts_list = Post.el_objects.order_by('-date')[:10]
        for post in posts_list:
            post.title = post.title_el
            post.teaser = post.teaser_el
            post.body = post.body_el
    if request.LANGUAGE_CODE == 'en':
        posts_list_all = Post.en_objects.all().order_by('-date')
        for post in posts_list_all:
            post.title = post.title_en
            post.teaser = post.teaser_en
            post.body = post.body_en
    else:
        posts_list_all = Post.el_objects.all().order_by('-date')
        for post in posts_list_all:
            post.title = post.title_el
            post.teaser = post.teaser_el
            post.body = post.body_el
    archives_list = []
    for post in posts_list_all:
        post_date = post.date.replace(day=1)
        if not post_date in archives_list:
            archives_list.append(post_date)
    resources_list = Resource.objects.filter(is_pinned=True)
    return render(request, 'main/blog.html', {
        'posts_list': posts_list,
        'archives_list': archives_list,
        'resources_list': resources_list,
    })

def blog_post(request, post_slug):
    if request.LANGUAGE_CODE == 'en':
        post = Post.en_objects.get(slug=post_slug)
        post.title = post.title_en
        post.teaser = post.teaser_en
        post.body = post.body_en
    else:
        post = Post.el_objects.get(slug=post_slug)
        post.title = post.title_el
        post.teaser = post.teaser_el
        post.body = post.body_el
    return render(request, 'main/post.html', {
        'post': post,
    })

def blog_archives(request, archive_year, archive_month):
    if request.LANGUAGE_CODE == 'en':
        posts_list = Post.en_objects.filter(date__year=archive_year, date__month=archive_month)
        for post in posts_list:
            post.title = post.title_en
            post.teaser = post.teaser_en
            post.body = post.body_en
    else:
        posts_list = Post.el_objects.filter(date__year=archive_year, date__month=archive_month)
        for post in posts_list:
            post.title = post.title_el
            post.teaser = post.teaser_el
            post.body = post.body_el
    posts_list_all = Post.objects.all()
    archives_list = []
    for post in posts_list_all:
        post_date = post.date.replace(day=1)
        if not post_date in archives_list:
            archives_list.append(post_date)
    resources_list = Resource.objects.filter(is_pinned=True)
    return render(request, 'main/blog-archive.html', {
        'posts_list': posts_list,
        'archives_list': archives_list,
        'archive_year': archive_year,
        'resources_list': resources_list,
    })

def resources(request):
    categories_list = ResourceCategory.objects.all()
    return render(request, 'main/resources.html', {
        'categories_list': categories_list,
    })

def contact(request):
    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            content_vars = {
                'name': form.cleaned_data['name'],
                'email': form.cleaned_data['email'],
                'message': form.cleaned_data['message'],
            }
            send_mail(
                'Contact form submission from okthess.gr',
                render_to_string('main/contact-email.txt', content_vars),
                settings.DEFAULT_FROM_EMAIL,
                [settings.CONTACT_TO_EMAIL],
            )
            messages.info(request, "Thank you for contacting us! We'll answer ASAP.")
            return HttpResponseRedirect(reverse('main:contact'))
        else:
            return HttpResponse('Contact form is invalid.')
    else:
        return render(request, 'main/contact.html')

def apply(request):
    if request.method == 'POST':
        form = ApplicationForm(request.POST)
        if form.is_valid():
            form.save()
            applicant_email = form.cleaned_data['email']
            send_mail(
                'OK!Thess application submission',
                render_to_string('main/application-email.txt'),
                settings.DEFAULT_FROM_EMAIL,
                [applicant_email],
            )
            messages.info(request, 'Your application has been submitted. Thank you!')
            return HttpResponseRedirect(reverse('main:apply'))
        else:
            return HttpResponse('Application form submission is invalid.')
    else:
        form = ApplicationForm()
        apply_text = ApplyText.objects.first()
        application_content = ''
        if apply_text:
            if request.LANGUAGE_CODE == 'en':
                application_content = apply_text.content_en
            else:
                application_content = apply_text.content_el
        return render(request, 'main/apply.html', {
            'form': form,
            'application_content': application_content,
        })

def weekends(request):
    return render(request, 'main/weekends.html')
<|endoftext|>