<commit_before># encoding: utf-8
#!/usr/bin/env python
import logging
import datetime
import time
from sqlalchemy import Column, Table, MetaData, select, create_engine, \
    ForeignKey, bindparam, and_, or_, exc
from sqlalchemy.engine import url
import connectors.task_pb2
import connectors.realtime_pb2
import connectors.type_pb2
import google
import os

def int_to_bitset(s):
    return str(s) if s <= 1 else int_to_bitset(s >> 1) + str(s & 1)

def get_pos_time(sql_time):
    return int(time.mktime(sql_time.timetuple()))

def get_datetime_to_second(sql_time):
    tt = sql_time.timetuple()
    return (tt.tm_hour * 60 * 60) + (tt.tm_min * 60) + tt.tm_sec

def get_navitia_type(object_type):
    if object_type == 'StopPoint':
        return connectors.type_pb2.STOP_POINT
    elif object_type == 'VehicleJourney':
        return connectors.type_pb2.VEHICLE_JOURNEY
    elif object_type == 'Line':
        return connectors.type_pb2.LINE
    elif object_type == 'Network':
        return connectors.type_pb2.NETWORK
    elif object_type == 'Route':
        return connectors.type_pb2.JOURNEY_PATTERN
    elif object_type == 'StopArea':
        return connectors.type_pb2.STOP_AREA
    elif object_type == 'RoutePoint':
        return connectors.type_pb2.JOURNEY_PATTERN_POINT
    else:
        return -1

class AtRealtimeReader(object):

    """
    This class load messages and perturbation from the "alerte trafic" database
    """
    def url_valide(self, config):
        u = url.make_url(config.at_connection_string)
        if u.drivername is None:
            return False
        if u.username is None:
            return False
        if u.password is None:
            return False
        if u.host is None:
            return False
        if u.database is None:
            return False
        return True

    def __init__(self, config, redis_helper):
        self.message_list = []
        self.perturbation_list = []
        if not self.url_valide(config):
            raise ValueError("at-connection-string is not valid: " +
                             config.at_connection_string)
        try:
            self.__engine = create_engine(
                config.at_connection_string + '?charset=utf8')
        except:
            raise ValueError("AT : Connecting at server failed")

        self._redis_helper = redis_helper
        self.meta = MetaData(self.__engine)
        self.event_table = Table('event', self.meta, autoload=True)

        self.impact_table = Table('impact', self.meta,
                                  Column('Event_ID', None,
                                         ForeignKey('event.Event_ID')),
                                  Column('TCObjectRef_ID', None, ForeignKey(
                                      'tcobjectref.TCObjectRef_ID')),
                                  autoload=True)

        self.tcobjectref_table = Table('tcobjectref', self.meta, autoload=True)

        self.impactbroadcast_table = \
            Table('impactbroadcast', self.meta,
                  Column('Impact_ID', None,
                         ForeignKey('impact.Impact_ID')),
                  Column('MsgMedia_ID', None,
                         ForeignKey('msgmedia.MsgMedia_ID')),
                  autoload=True)

        self.msgmedia_table = Table('msgmedia', self.meta, autoload=True)

        self.label_impact_id = 'impact_id'
        self.label_impact_state = 'impact_state'
        self.label_publication_start_date = 'publication_start_date'
        self.label_publication_end_date = 'publication_end_date'
        self.label_application_start_date = 'application_start_date'
        self.label_application_end_date = 'application_end_date'
        self.label_application_daily_start_hour =\
            'application_daily_start_hour'
        self.label_application_daily_end_hour =\
            'application_daily_end_hour'
        self.label_active_days = 'active_days'
        self.label_object_external_code = 'object_external_code'
        self.label_object_type = 'object_type'
        self.label_title = 'title'
        self.label_message = 'message'
        self.label_message_lang = 'lang'

        self.last_exec_file_name = './last_exec_time.txt'
        self.datetime_format = '%Y-%m-%d %H:%M:%S'
        self._collections = {"StopPoint": "stop_point",
                             "VehicleJourney": "vehicle_journey",
                             "Line": "line",
                             "Network": "network",
                             "StopArea": "stop_area"}

    def get_last_execution_time(self):
        if os.path.exists(self.last_exec_file_name):
            last_exec_file = open(self.last_exec_file_name, 'r')
            las_execution_time = last_exec_file.readline().rstrip('\n\r')
            return datetime.datetime.strptime(las_execution_time,
                                              self.datetime_format)
        else:
            return datetime.datetime.now()

    def set_last_execution_time(self, last_execution_time):
        last_exec_file = open(self.last_exec_file_name, 'w')
        last_exec_file.write(
            last_execution_time.strftime(self.datetime_format))

    def get_uri(self, externalcode, object_type):
        uri = None
        if object_type in self._collections.keys():
            self._redis_helper.set_prefix(self._collections[object_type])
            uri = self._redis_helper.get(externalcode)
        return uri

    def create_pertubation(self, message):
        pertubation = connectors.realtime_pb2.AtPerturbation()
        pertubation.uri = message.uri
        pertubation.object.object_uri = message.object.object_uri
        pertubation.object.object_type = message.object.object_type
        pertubation.start_application_date = \
            message.start_publication_date
        pertubation.end_application_date = \
            message.end_application_date
        pertubation.start_application_daily_hour = \
            message.start_application_daily_hour
        pertubation.end_application_daily_hour = \
            message.end_application_daily_hour
        pertubation.active_days = message.active_days
        return pertubation

    def get_status_message(self, status):
        if status.lower() == "information":
            return 0
        if status.lower() == "warning":
            return 1
        if status.lower() == "disrupt":
            return 2
        return None

    def set_message(self, result_proxy):
        """

        :param result_proxy:
        """
        last_impact_id = -1
        for row in result_proxy:
            try:
                current_uri = self.get_uri(
                    row[self.label_object_external_code],
                    row[self.label_object_type])
                if current_uri is None:
                    logging.getLogger('connector').warn(
                        "Object [%s] rejected : "
                        "No extcode and uri correspondance",
                        row[self.label_object_external_code])
                else:
                    if last_impact_id != row[self.label_impact_id]:
                        last_impact_id = row[self.label_impact_id]
                        message = connectors.realtime_pb2.Message()
                        self.message_list.append(message)

                        message.uri = str(row[self.label_impact_id]) + '-' + \
                            row[self.label_message_lang]

                        message.start_publication_date = get_pos_time(
                            row[self.label_publication_start_date])

                        message.end_publication_date = get_pos_time(
                            row[self.label_publication_end_date])
                        message.start_application_date = get_pos_time(
                            row[self.label_application_start_date])
                        message.end_application_date = get_pos_time(
                            row[self.label_application_end_date])
                        # number of seconds since midnigth
                        message.start_application_daily_hour = \
                            get_datetime_to_second(
                                row[self.label_application_daily_start_hour])
                        message.end_application_daily_hour = \
                            get_datetime_to_second(
                                row[self.label_application_daily_end_hour])
                        message.active_days = \
                            int_to_bitset(row[self.label_active_days]) + '1'

                        message.object.object_uri = current_uri

                        message.object.object_type = \
                            get_navitia_type(row[self.label_object_type])

                        message.message_status = self.get_status_message(
                            row[self.label_impact_state])

                    localized_message = message.localized_messages.add()
                    localized_message.language = row[self.label_message_lang]
                    localized_message.body = row[self.label_message]
                    localized_message.title = row[self.label_title]

                    if row[self.label_impact_state] == 'Disrupt':
                        self.perturbation_list.append(
                            self.create_pertubation(message))

                    print str(
                        row[self.label_active_days]) + ' - ' +\
                        message.active_days
            except google.protobuf.message.DecodeError as e:
                logging.getLogger('connector').warn(
                    "message is not a valid "
                    "protobuf task: %s", str(e))

    def set_request(self):
        return select([self.event_table.c.Event_ID,
                       self.impact_table.c.Impact_ID
                       .label(self.label_impact_id),
                       self.impact_table.c.Impact_State
                       .label(self.label_impact_state),
                       self.event_table.c.Event_PublicationStartDate
                       .label(self.label_publication_start_date),
                       self.event_table.c.Event_PublicationEndDate
                       .label(self.label_publication_end_date),
                       self.impact_table.c.Impact_EventStartDate
                       .label(self.label_application_start_date),
                       self.impact_table.c.Impact_EventEndDate
                       .label(self.label_application_end_date),
                       self.impact_table.c.Impact_DailyStartDate
                       .label(self.label_application_daily_start_hour),
                       self.impact_table.c.Impact_DailyEndDate
                       .label(self.label_application_daily_end_hour),
                       self.impact_table.c.Impact_ActiveDays
                       .label(self.label_active_days),
                       self.tcobjectref_table.c.TCObjectCodeExt
                       .label(self.label_object_external_code),
                       self.tcobjectref_table.c.TCObjectType
                       .label(self.label_object_type),
                       self.impactbroadcast_table.c.Impact_Title
                       .label(self.label_title),
                       self.impactbroadcast_table.c.Impact_Msg
                       .label(self.label_message),
                       self.msgmedia_table.c.MsgMedia_Lang
                       .label(self.label_message_lang)],
                      # clause where
                      and_(self.msgmedia_table.c.MsgMedia_Media == bindparam(
                          'media_media'),
                          self.event_table.c.Event_PublicationEndDate
                          >= bindparam('event_publicationenddate'),
                          or_(self.event_table.c.Event_CloseDate == None,
                              self.event_table.c.Event_CloseDate > bindparam(
                                  'event_closedate'),
                              self.impact_table.c
                              .Impact_SelfModificationDate == None,
                              self.impact_table.c
                              .Impact_SelfModificationDate > bindparam(
                                  'impact_modification_date'),
                              self.impact_table.c
                              .Impact_ChildrenModificationDate == None,
                              self.impact_table.c
                              .Impact_ChildrenModificationDate > bindparam(
                                  'impact_modification_date'))
                      ),
                      # jointure
                      from_obj=[self.event_table.join(self.impact_table).join(
                          self.tcobjectref_table).join(
                          self.impactbroadcast_table).join(
                          self.msgmedia_table)]
                      ).order_by(self.impact_table.c.Impact_ID)

    # impact_SelfModificationDate, impact_ChildrenModificationDate

    def execute(self):
        logger = logging.getLogger('connector')
        conn = None
        try:
            conn = self.__engine.connect()
        except exc.SQLAlchemyError as e:
            logger.exception('error durring connection')
        if conn is not None:
            result = None
            try:
                s = self.set_request()
                execution_time = datetime.datetime.now()
                # read last execution time
                last_execution_time = self.get_last_execution_time()
                result = \
                    conn.execute(s,
                                 media_media='Internet',
                                 event_publicationenddate=last_execution_time,
                                 #datetime.datetime(2013, 9, 01),
                                 event_closedate=datetime.datetime.now(),
                                 impact_modification_date=last_execution_time)
                # save execution time
                self.set_last_execution_time(execution_time)
            except exc.SQLAlchemyError as e:
                logger.exception('error during request')
            if result is not None:
                self.set_message(result)
                result.close()
<commit_msg>Tyr: set the charset of url.<commit_after># encoding: utf-8
#!/usr/bin/env python
import logging
import datetime
import time
from sqlalchemy import Column, Table, MetaData, select, create_engine, \
    ForeignKey, bindparam, and_, or_, exc
from sqlalchemy.engine import url
import connectors.task_pb2
import connectors.realtime_pb2
import connectors.type_pb2
import google
import os

def int_to_bitset(s):
    return str(s) if s <= 1 else int_to_bitset(s >> 1) + str(s & 1)

def get_pos_time(sql_time):
    return int(time.mktime(sql_time.timetuple()))

def get_datetime_to_second(sql_time):
    tt = sql_time.timetuple()
    return (tt.tm_hour * 60 * 60) + (tt.tm_min * 60) + tt.tm_sec

def get_navitia_type(object_type):
    if object_type == 'StopPoint':
        return connectors.type_pb2.STOP_POINT
    elif object_type == 'VehicleJourney':
        return connectors.type_pb2.VEHICLE_JOURNEY
    elif object_type == 'Line':
        return connectors.type_pb2.LINE
    elif object_type == 'Network':
        return connectors.type_pb2.NETWORK
    elif object_type == 'Route':
        return connectors.type_pb2.JOURNEY_PATTERN
    elif object_type == 'StopArea':
        return connectors.type_pb2.STOP_AREA
    elif object_type == 'RoutePoint':
        return connectors.type_pb2.JOURNEY_PATTERN_POINT
    else:
        return -1

class AtRealtimeReader(object):

    """
    This class load messages and perturbation from the "alerte trafic" database
    """
    def __init__(self, config, redis_helper):
        self.message_list = []
        self.perturbation_list = []
        url = url.make_url(config.at_connection_string)
        url.query["charset"] = 'utf8'
        try:
            self.__engine = create_engine(url)
        except:
            raise ValueError("AT : Connecting at server failed")

        self._redis_helper = redis_helper
        self.meta = MetaData(self.__engine)
        self.event_table = Table('event', self.meta, autoload=True)

        self.impact_table = Table('impact', self.meta,
                                  Column('Event_ID', None,
                                         ForeignKey('event.Event_ID')),
                                  Column('TCObjectRef_ID', None, ForeignKey(
                                      'tcobjectref.TCObjectRef_ID')),
                                  autoload=True)

        self.tcobjectref_table = Table('tcobjectref', self.meta, autoload=True)

        self.impactbroadcast_table = \
            Table('impactbroadcast', self.meta,
                  Column('Impact_ID', None,
                         ForeignKey('impact.Impact_ID')),
                  Column('MsgMedia_ID', None,
                         ForeignKey('msgmedia.MsgMedia_ID')),
                  autoload=True)

        self.msgmedia_table = Table('msgmedia', self.meta, autoload=True)

        self.label_impact_id = 'impact_id'
        self.label_impact_state = 'impact_state'
        self.label_publication_start_date = 'publication_start_date'
        self.label_publication_end_date = 'publication_end_date'
        self.label_application_start_date = 'application_start_date'
        self.label_application_end_date = 'application_end_date'
        self.label_application_daily_start_hour =\
            'application_daily_start_hour'
        self.label_application_daily_end_hour =\
            'application_daily_end_hour'
        self.label_active_days = 'active_days'
        self.label_object_external_code = 'object_external_code'
        self.label_object_type = 'object_type'
        self.label_title = 'title'
        self.label_message = 'message'
        self.label_message_lang = 'lang'

        self.last_exec_file_name = './last_exec_time.txt'
        self.datetime_format = '%Y-%m-%d %H:%M:%S'
        self._collections = {"StopPoint": "stop_point",
                             "VehicleJourney": "vehicle_journey",
                             "Line": "line",
                             "Network": "network",
                             "StopArea": "stop_area"}

    def get_last_execution_time(self):
        if os.path.exists(self.last_exec_file_name):
            last_exec_file = open(self.last_exec_file_name, 'r')
            las_execution_time = last_exec_file.readline().rstrip('\n\r')
            return datetime.datetime.strptime(las_execution_time,
                                              self.datetime_format)
        else:
            return datetime.datetime.now()

    def set_last_execution_time(self, last_execution_time):
        last_exec_file = open(self.last_exec_file_name, 'w')
        last_exec_file.write(
            last_execution_time.strftime(self.datetime_format))

    def get_uri(self, externalcode, object_type):
        uri = None
        if object_type in self._collections.keys():
            self._redis_helper.set_prefix(self._collections[object_type])
            uri = self._redis_helper.get(externalcode)
        return uri

    def create_pertubation(self, message):
        pertubation = connectors.realtime_pb2.AtPerturbation()
        pertubation.uri = message.uri
        pertubation.object.object_uri = message.object.object_uri
        pertubation.object.object_type = message.object.object_type
        pertubation.start_application_date = \
            message.start_publication_date
        pertubation.end_application_date = \
            message.end_application_date
        pertubation.start_application_daily_hour = \
            message.start_application_daily_hour
        pertubation.end_application_daily_hour = \
            message.end_application_daily_hour
        pertubation.active_days = message.active_days
        return pertubation

    def get_status_message(self, status):
        if status.lower() == "information":
            return 0
        if status.lower() == "warning":
            return 1
        if status.lower() == "disrupt":
            return 2
        return None

    def set_message(self, result_proxy):
        """

        :param result_proxy:
        """
        last_impact_id = -1
        for row in result_proxy:
            try:
                current_uri = self.get_uri(
                    row[self.label_object_external_code],
                    row[self.label_object_type])
                if current_uri is None:
                    logging.getLogger('connector').warn(
                        "Object [%s] rejected : "
                        "No extcode and uri correspondance",
                        row[self.label_object_external_code])
                else:
                    if last_impact_id != row[self.label_impact_id]:
                        last_impact_id = row[self.label_impact_id]
                        message = connectors.realtime_pb2.Message()
                        self.message_list.append(message)

                        message.uri = str(row[self.label_impact_id]) + '-' + \
                            row[self.label_message_lang]

                        message.start_publication_date = get_pos_time(
                            row[self.label_publication_start_date])

                        message.end_publication_date = get_pos_time(
                            row[self.label_publication_end_date])
                        message.start_application_date = get_pos_time(
                            row[self.label_application_start_date])
                        message.end_application_date = get_pos_time(
                            row[self.label_application_end_date])
                        # number of seconds since midnigth
                        message.start_application_daily_hour = \
                            get_datetime_to_second(
                                row[self.label_application_daily_start_hour])
                        message.end_application_daily_hour = \
                            get_datetime_to_second(
                                row[self.label_application_daily_end_hour])
                        message.active_days = \
                            int_to_bitset(row[self.label_active_days]) + '1'

                        message.object.object_uri = current_uri

                        message.object.object_type = \
                            get_navitia_type(row[self.label_object_type])

                        message.message_status = self.get_status_message(
                            row[self.label_impact_state])

                    localized_message = message.localized_messages.add()
                    localized_message.language = row[self.label_message_lang]
                    localized_message.body = row[self.label_message]
                    localized_message.title = row[self.label_title]

                    if row[self.label_impact_state] == 'Disrupt':
                        self.perturbation_list.append(
                            self.create_pertubation(message))

                    print str(
                        row[self.label_active_days]) + ' - ' +\
                        message.active_days
            except google.protobuf.message.DecodeError as e:
                logging.getLogger('connector').warn(
                    "message is not a valid "
                    "protobuf task: %s", str(e))

    def set_request(self):
        return select([self.event_table.c.Event_ID,
                       self.impact_table.c.Impact_ID
                       .label(self.label_impact_id),
                       self.impact_table.c.Impact_State
                       .label(self.label_impact_state),
                       self.event_table.c.Event_PublicationStartDate
                       .label(self.label_publication_start_date),
                       self.event_table.c.Event_PublicationEndDate
                       .label(self.label_publication_end_date),
                       self.impact_table.c.Impact_EventStartDate
                       .label(self.label_application_start_date),
                       self.impact_table.c.Impact_EventEndDate
                       .label(self.label_application_end_date),
                       self.impact_table.c.Impact_DailyStartDate
                       .label(self.label_application_daily_start_hour),
                       self.impact_table.c.Impact_DailyEndDate
                       .label(self.label_application_daily_end_hour),
                       self.impact_table.c.Impact_ActiveDays
                       .label(self.label_active_days),
                       self.tcobjectref_table.c.TCObjectCodeExt
                       .label(self.label_object_external_code),
                       self.tcobjectref_table.c.TCObjectType
                       .label(self.label_object_type),
                       self.impactbroadcast_table.c.Impact_Title
                       .label(self.label_title),
                       self.impactbroadcast_table.c.Impact_Msg
                       .label(self.label_message),
                       self.msgmedia_table.c.MsgMedia_Lang
                       .label(self.label_message_lang)],
                      # clause where
                      and_(self.msgmedia_table.c.MsgMedia_Media == bindparam(
                          'media_media'),
                          self.event_table.c.Event_PublicationEndDate
                          >= bindparam('event_publicationenddate'),
                          or_(self.event_table.c.Event_CloseDate == None,
                              self.event_table.c.Event_CloseDate > bindparam(
                                  'event_closedate'),
                              self.impact_table.c
                              .Impact_SelfModificationDate == None,
                              self.impact_table.c
                              .Impact_SelfModificationDate > bindparam(
                                  'impact_modification_date'),
                              self.impact_table.c
                              .Impact_ChildrenModificationDate == None,
                              self.impact_table.c
                              .Impact_ChildrenModificationDate > bindparam(
                                  'impact_modification_date'))
                      ),
                      # jointure
                      from_obj=[self.event_table.join(self.impact_table).join(
                          self.tcobjectref_table).join(
                          self.impactbroadcast_table).join(
                          self.msgmedia_table)]
                      ).order_by(self.impact_table.c.Impact_ID)

    # impact_SelfModificationDate, impact_ChildrenModificationDate

    def execute(self):
        logger = logging.getLogger('connector')
        conn = None
        try:
            conn = self.__engine.connect()
        except exc.SQLAlchemyError as e:
            logger.exception('error durring connection')
        if conn is not None:
            result = None
            try:
                s = self.set_request()
                execution_time = datetime.datetime.now()
                # read last execution time
                last_execution_time = self.get_last_execution_time()
                result = \
                    conn.execute(s,
                                 media_media='Internet',
                                 event_publicationenddate=last_execution_time,
                                 #datetime.datetime(2013, 9, 01),
                                 event_closedate=datetime.datetime.now(),
                                 impact_modification_date=last_execution_time)
                # save execution time
                self.set_last_execution_time(execution_time)
            except exc.SQLAlchemyError as e:
                logger.exception('error during request')
            if result is not None:
                self.set_message(result)
                result.close()
<|endoftext|>


<commit_before>#  Copyright (c) 2001-2014, Canal TP and/or its affiliates. All rights reserved.
#
# This file is part of Navitia,
#     the software to build cool stuff with public transport.
#
# Hope you'll enjoy and contribute to this project,
#     powered by Canal TP (www.canaltp.fr).
# Help us simplify mobility and open public transport:
#     a non ending quest to the responsive locomotion way of traveling!
#
# LICENCE: This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Stay tuned using
# twitter @navitia
# IRC #navitia on freenode
# https://groups.google.com/d/forum/navitia
# www.navitia.io

"""
This file contains the default values for parameters used in the journey planner.
This parameters are used on the creation of the instances in the tyr database, they will not be updated automatically.
This parameters can be used directly by jormungandr if the instance is not known in tyr, typically in development setup
"""

import logging
import sys

max_walking_duration_to_pt = 30*60

max_bike_duration_to_pt = 30*60

max_bss_duration_to_pt = 30*60

max_car_duration_to_pt = 30*60

max_car_no_park_duration_to_pt = 30*60

walking_speed = 1.12

bike_speed = 4.1

bss_speed = 4.1

car_speed = 11.11

car_no_park_speed = 6.94 #25km/h

max_nb_transfers = 10

journey_order = 'arrival_time'

min_tc_with_car = 5*60

min_tc_with_bike = 5*60

min_tc_with_bss = 5*60

min_bike = 4*60

min_bss = 4*60 + 3*60 #we want 4minute on the bike, so we add the time to pick and put back the bss

min_car = 5*60

#if a journey is X time longer than the earliest one we remove it
factor_too_long_journey = 4

#all journeys with a duration fewer than this value will be kept no matter what even if they are 20 times slower than the earliest one
min_duration_too_long_journey = 15*60

#criteria used for select the journey used for calculate the max duration
max_duration_criteria = 'time'

#only journey with this mode (or slower mode) will be used for calculate the max_duration
max_duration_fallback_mode = 'walking'

# specify the latest time point of request, relative to datetime, in second
max_duration = 60*60*24  # seconds

# the penalty for each walking transfer
walking_transfer_penalty = 120  # seconds

# night bus filtering parameter
night_bus_filter_max_factor = 3

# night bus filtering parameter
night_bus_filter_base_factor = 60*60  # seconds

#priority value to be used to choose a kraken instance among valid instances. The greater it is, the more the instance
# will be chosen.
priority = 0

# activate / desactivate call to bss provider
bss_provider = True

# activate / desactivate call to car parking provider
car_park_provider = True

#Maximum number of connections allowed in journeys is calculated as
#max_additional_connections + minimum connections among the journeys
max_additional_connections = 2

#the id of physical_mode, as sent by kraken to jormungandr, used by _max_successive_physical_mode rule
successive_physical_mode_to_limit_id = 'physical_mode:Bus'

# Number of greenlets simultaneously used for Real-Time proxies calls
realtime_pool_size = 3

# Minimum number of different suggested journeys
min_nb_journeys = 0

# Maximum number of different suggested journeys
max_nb_journeys = float('inf')

# Maximum number of successive physical modes for an itinerary
max_successive_physical_mode = float('inf')

# Minimum number of calls to kraken
min_journeys_calls = 1

# Filter on vj using same lines and same stops
final_line_filter = False

# Maximum number of second pass to get more itinararies
max_extra_second_pass = 0

def get_value_or_default(attr, instance, instance_name):
    if not instance or getattr(instance, attr, None) == None:
        logger = logging.getLogger(__name__)
        value = getattr(sys.modules[__name__], attr)
        logger.warn('instance %s not found in db, we use the default value (%s) for the param %s', instance_name, value, attr)
        return value
    else:
        return getattr(instance, attr)
<commit_msg>max values changed<commit_after>#  Copyright (c) 2001-2014, Canal TP and/or its affiliates. All rights reserved.
#
# This file is part of Navitia,
#     the software to build cool stuff with public transport.
#
# Hope you'll enjoy and contribute to this project,
#     powered by Canal TP (www.canaltp.fr).
# Help us simplify mobility and open public transport:
#     a non ending quest to the responsive locomotion way of traveling!
#
# LICENCE: This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Stay tuned using
# twitter @navitia
# IRC #navitia on freenode
# https://groups.google.com/d/forum/navitia
# www.navitia.io

"""
This file contains the default values for parameters used in the journey planner.
This parameters are used on the creation of the instances in the tyr database, they will not be updated automatically.
This parameters can be used directly by jormungandr if the instance is not known in tyr, typically in development setup
"""

import logging
import sys

max_walking_duration_to_pt = 30*60

max_bike_duration_to_pt = 30*60

max_bss_duration_to_pt = 30*60

max_car_duration_to_pt = 30*60

max_car_no_park_duration_to_pt = 30*60

walking_speed = 1.12

bike_speed = 4.1

bss_speed = 4.1

car_speed = 11.11

car_no_park_speed = 6.94 #25km/h

max_nb_transfers = 10

journey_order = 'arrival_time'

min_tc_with_car = 5*60

min_tc_with_bike = 5*60

min_tc_with_bss = 5*60

min_bike = 4*60

min_bss = 4*60 + 3*60 #we want 4minute on the bike, so we add the time to pick and put back the bss

min_car = 5*60

#if a journey is X time longer than the earliest one we remove it
factor_too_long_journey = 4

#all journeys with a duration fewer than this value will be kept no matter what even if they are 20 times slower than the earliest one
min_duration_too_long_journey = 15*60

#criteria used for select the journey used for calculate the max duration
max_duration_criteria = 'time'

#only journey with this mode (or slower mode) will be used for calculate the max_duration
max_duration_fallback_mode = 'walking'

# specify the latest time point of request, relative to datetime, in second
max_duration = 60*60*24  # seconds

# the penalty for each walking transfer
walking_transfer_penalty = 120  # seconds

# night bus filtering parameter
night_bus_filter_max_factor = 3

# night bus filtering parameter
night_bus_filter_base_factor = 60*60  # seconds

#priority value to be used to choose a kraken instance among valid instances. The greater it is, the more the instance
# will be chosen.
priority = 0

# activate / desactivate call to bss provider
bss_provider = True

# activate / desactivate call to car parking provider
car_park_provider = True

#Maximum number of connections allowed in journeys is calculated as
#max_additional_connections + minimum connections among the journeys
max_additional_connections = 2

#the id of physical_mode, as sent by kraken to jormungandr, used by _max_successive_physical_mode rule
successive_physical_mode_to_limit_id = 'physical_mode:Bus'

# Number of greenlets simultaneously used for Real-Time proxies calls
realtime_pool_size = 3

# Minimum number of different suggested journeys
min_nb_journeys = 0

# Maximum number of different suggested journeys
max_nb_journeys = 99999

# Maximum number of successive physical modes for an itinerary
max_successive_physical_mode = 99

# Minimum number of calls to kraken
min_journeys_calls = 1

# Filter on vj using same lines and same stops
final_line_filter = False

# Maximum number of second pass to get more itinararies
max_extra_second_pass = 0

def get_value_or_default(attr, instance, instance_name):
    if not instance or getattr(instance, attr, None) == None:
        logger = logging.getLogger(__name__)
        value = getattr(sys.modules[__name__], attr)
        logger.warn('instance %s not found in db, we use the default value (%s) for the param %s', instance_name, value, attr)
        return value
    else:
        return getattr(instance, attr)
<|endoftext|>


<commit_before>import os.path
import logging

from pylons.i18n import _

from adhocracy import i18n, templates
from adhocracy.lib.templating import render
from adhocracy.lib.event import formatting

log = logging.getLogger(__name__)

class Notification(object):

    TPL_NAME = os.path.join("", "notifications", "%s.%s.txt")

    def __init__(self, event, user, type=None, watch=None):
        self.event = event
        self._type = type
        self.user = user
        self.watch = watch

    def get_type(self):
        if not self._type:
            self._type = self.event.event
        return self._type

    type = property(get_type)

    def get_priority(self):
        return self.type.priority

    priority = property(get_priority)

    def get_id(self):
        return "n-e%s-u%s" % (self.event.id, self.user.id)

    id = property(get_id)

    def language_context(self):
        return i18n.user_language(self.user)

    def get_subject(self):
        fe = formatting.FormattedEvent(self.event,
                                       lambda f, value: f.unicode(value))
        return self.type.subject() % fe

    subject = property(get_subject)

    def _get_link(self):
        try:
            return self.type.link_path(self.event)
        except:
            return ""

    link = property(_get_link)

    def get_body(self):
        locale = self.language_context()
        data = {'n': self, 'e': self.event, 'u': self.user, 't': self.type}

        tpl_name = self.TPL_NAME % (str(self.type), locale.language[0:2])
        tpl_path = os.path.join(templates.__path__[0], tpl_name)

        if not os.path.exists(tpl_path):
            log.warn("Notification body needs to be localized to "
                     "file %s" % (tpl_path))
            tpl_name = self.TPL_NAME % (
                str(self.type), i18n.get_default_locale().language[0:2])

        body = render(tpl_name, data).strip()
        body += _("\r\n\r\nMore info: %(url)s") % dict(url=self.link)
        return body

    body = property(get_body)

    def __repr__(self):
        return "<Notification(%s,%s,%s)>" % (self.type, self.user.user_name,
                                             self.priority)
<commit_msg>Log errors in link creation<commit_after>import os.path
import logging

from pylons.i18n import _

from adhocracy import i18n, templates
from adhocracy.lib.templating import render
from adhocracy.lib.event import formatting

log = logging.getLogger(__name__)

class Notification(object):

    TPL_NAME = os.path.join("", "notifications", "%s.%s.txt")

    def __init__(self, event, user, type=None, watch=None):
        self.event = event
        self._type = type
        self.user = user
        self.watch = watch

    def get_type(self):
        if not self._type:
            self._type = self.event.event
        return self._type

    type = property(get_type)

    def get_priority(self):
        return self.type.priority

    priority = property(get_priority)

    def get_id(self):
        return "n-e%s-u%s" % (self.event.id, self.user.id)

    id = property(get_id)

    def language_context(self):
        return i18n.user_language(self.user)

    def get_subject(self):
        fe = formatting.FormattedEvent(self.event,
                                       lambda f, value: f.unicode(value))
        return self.type.subject() % fe

    subject = property(get_subject)

    def _get_link(self):
        try:
            return self.type.link_path(self.event)
        except Exception as e:
            log.error('Failed to get link to notification %r: %r' % (self, e))
            return ""

    link = property(_get_link)

    def get_body(self):
        locale = self.language_context()
        data = {'n': self, 'e': self.event, 'u': self.user, 't': self.type}

        tpl_name = self.TPL_NAME % (str(self.type), locale.language[0:2])
        tpl_path = os.path.join(templates.__path__[0], tpl_name)

        if not os.path.exists(tpl_path):
            log.warn("Notification body needs to be localized to "
                     "file %s" % (tpl_path))
            tpl_name = self.TPL_NAME % (
                str(self.type), i18n.get_default_locale().language[0:2])

        body = render(tpl_name, data).strip()
        body += _("\r\n\r\nMore info: %(url)s") % dict(url=self.link)
        return body

    body = property(get_body)

    def __repr__(self):
        return "<Notification(%s,%s,%s)>" % (self.type, self.user.user_name,
                                             self.priority)
<|endoftext|>


<commit_before>#!/usr/bin/env python
from enum import Enum
from random import choice

Event = Enum('Event', 'client_arrive client_buy cashier_finish')

class Simulation(object):

    def __init__(self, n, at, ct, bt, mx, log=None):
        assert n >= 2, 'hard requirement'
        assert callable(at)
        assert callable(ct)
        assert callable(bt)
        if log is not None:
            assert callable(log)

        self._gen_arrive_time = at
        self._gen_cashier_time = ct
        self._gen_buy_time = bt
        self._log_fun = log

        self.day = 0
        self.timestamp = 0.0
        self.time_left = 0
        self.days_left = 0
        self.buy_zone = []
        # Note that the first element of the list is not on the actual queue
        self.cashier_queues = tuple([] for _ in range(n))
        self.client_count = 0

        self.next_client = self._gen_arrive_time()
        self._stats = {
            'timestamps': [],
            'queue_sizes': [],
            'buy_zone_sizes': [],
            'client_history': {},
        }

    def _collect_stats(self):
        s = self._stats
        s['timestamps'].append(self.timestamp)
        # XXX: the first client on the list is not actually on the queue
        s['queue_sizes'].append(tuple(max(len(q) - 1, 0) for q in self.cashier_queues))
        s['buy_zone_sizes'].append(len(self.buy_zone))

    def _compute_stats(self):
        s = self._stats
        deltas = [h[2] - h[1] for h in s['client_history'].itervalues() if 2 in h and 1 in h]
        s['avg_queue_time'] = sum(deltas) / len(deltas)
        s['max_queue_time'] = max(deltas)
        s['max_queue_size'] = max(q for qq in s['queue_sizes'] for q in qq)

    def _log(self, msg):
        if self._log_fun is not None:
            #self._log_fun(self.timestamp, str(self) + ' ' + msg)
            self._log_fun(self.timestamp, msg)

    def _gen_id(self):
        i = self.client_count
        self.client_count = i + 1
        return i

    def _next_event(self):
        """Next event, will return None on some invalid states."""
        infl = [float('inf')]
        cash_lens = [q[0][0] for q in self.cashier_queues if q]
        buys = [c[0] for c in self.buy_zone]
        if self.next_client < min(infl + cash_lens + buys):
            return Event.client_arrive
        elif min(infl + buys) < min(infl + cash_lens):
            return Event.client_buy
        elif min(infl + cash_lens) < infl[0]:
            return Event.cashier_finish

    def _reset_day(self):
        self.time_left = 8 * 60 * 60
        self.days_left -= 1
        self.buy_zone = []
        self.cashier_queues = tuple([] for _ in self.cashier_queues)

    def _advance_time(self, time):
        self.timestamp += time
        self.time_left -= time
        self.next_client -= time
        self.buy_zone = [(t - time, i) for t, i in self.buy_zone]
        self.cashier_queues = [[(t - time, i) for t, i in q] for q in self.cashier_queues]
        if self.time_left <= 0:
            self._reset_day()

    def __str__(self):
        return '{} {}'.format(len(self.buy_zone), tuple(len(q) for q in self.cashier_queues))

    def step(self):
        ev = self._next_event()
        if ev == Event.client_arrive:
            t, i = self.next_client, self._gen_id()
            self._log('client_{} arrived'.format(i))
            self._stats['client_history'][i] = {0: self.timestamp}
            self.buy_zone.append((self._gen_buy_time(), i))
            self.next_client = self._gen_arrive_time()
            self._advance_time(t)
        elif ev == Event.client_buy:
            t, i = min(self.buy_zone)
            self.buy_zone.remove((t, i))
            min_len = min(len(q) for q in self.cashier_queues)
            maybe_queues = [q for q in self.cashier_queues if len(q) == min_len]
            queue = choice(maybe_queues)
            q = self.cashier_queues.index(queue)
            self._stats['client_history'][i][1] = self.timestamp
            if len(queue) == 0:
                self._stats['client_history'][i][2] = self.timestamp
                self._log('client_{} went to cashier_{} (no queue)'.format(i, q))
            else:
                self._log('client_{} went to queue_{}'.format(i, q))
            queue.append((self._gen_cashier_time(), i))
            self._advance_time(t)
        elif ev == Event.cashier_finish:
            (t, i), queue = min((q[0], q) for q in self.cashier_queues if q)
            q = self.cashier_queues.index(queue)
            self._stats['client_history'][i][3] = self.timestamp
            self._log('client_{} left cashier_{}'.format(i, q))
            queue.pop(0)
            if len(queue) > 0:
                k = queue[0][1]
                self._stats['client_history'][k][2] = self.timestamp
                self._log('client_{} went to cashier_{} (from queue)'.format(k, q))
            self._advance_time(t)
        else:
            self._log('invalid event, this is a bug')

        self._collect_stats()

    def simulate(self, days=1):
        self.days_left = days
        self._reset_day()
        #while self.time_left > 0 or self.days_left > 0:
        while self.days_left >= 0:
            self.step()
        self._compute_stats()

    def short_stats(self):
        s = self._stats
        return '\n'.join([
            'avg queue time: {}'.format(s['avg_queue_time']),
            'max queue time: {}'.format(s['max_queue_time']),
            'max queue size: {}'.format(s['max_queue_size']),
        ])

if __name__ == '__main__':
    import sys
    import argparse
    from random import uniform, random
    from math import log

    parser = argparse.ArgumentParser(description='simulation of cashiers')
    parser.add_argument('cashiers', default=4, type=int, help='number of cashiers')
    parser.add_argument('-f', '--client-frequency', default=10.0, type=float, help='frequency of clients per minute')
    parser.add_argument('-d', '--days', default=1, type=int, help='number of days to simulate')
    parser.add_argument('-e', '--export-stats', type=argparse.FileType('w'), help='set the file to export statistics in JSON to')
    parser.add_argument('--log', action='store_true', help='whether we should log the events')
    args = parser.parse_args()

    cashiers_count = args.cashiers
    client_frequency = args.client_frequency
    arrive_time = lambda: 60 * -log(random()) / client_frequency
    cashier_time = lambda: uniform(2 * 60, 6 * 60)
    buy_time = lambda: uniform(30 * 60, 90 * 60)
    lg = args.log and (lambda timestamp, message: sys.stderr.write('{t:09.03f}: {msg}\n'.format(t=timestamp, msg=message))) or None

    s = Simulation(n=cashiers_count, at=arrive_time, ct=cashier_time, bt=buy_time, mx=5, log=lg)
    try:
        print 'Simulating ... ',
        sys.stdout.flush()
        s.simulate(args.days)
        print 'done.'
        print s.short_stats()
        if args.export_stats:
            from json import dump
            dump(s._stats, args.export_stats, indent=2, separators=(',', ': '))
    except KeyboardInterrupt:
        print '\rCancelled.         '
<commit_msg>Added variance to statistics<commit_after>#!/usr/bin/env python
from enum import Enum
from random import choice
from numpy import sqrt

Event = Enum('Event', 'client_arrive client_buy cashier_finish')

class Simulation(object):

    def __init__(self, n, at, ct, bt, mx, log=None):
        assert n >= 2, 'hard requirement'
        assert callable(at)
        assert callable(ct)
        assert callable(bt)
        if log is not None:
            assert callable(log)

        self._gen_arrive_time = at
        self._gen_cashier_time = ct
        self._gen_buy_time = bt
        self._log_fun = log

        self.day = 0
        self.timestamp = 0.0
        self.time_left = 0
        self.days_left = 0
        self.buy_zone = []
        # Note that the first element of the list is not on the actual queue
        self.cashier_queues = tuple([] for _ in range(n))
        self.client_count = 0

        self.next_client = self._gen_arrive_time()
        self._stats = {
            'timestamps': [],
            'queue_sizes': [],
            'buy_zone_sizes': [],
            'client_history': {},
        }

    def _collect_stats(self):
        s = self._stats
        s['timestamps'].append(self.timestamp)
        # XXX: the first client on the list is not actually on the queue
        s['queue_sizes'].append(tuple(max(len(q) - 1, 0) for q in self.cashier_queues))
        s['buy_zone_sizes'].append(len(self.buy_zone))

    def _compute_stats(self):
        s = self._stats
        deltas = [h[2] - h[1] for h in s['client_history'].itervalues() if 2 in h and 1 in h]
        s['avg_queue_time'] = sum(deltas) / len(deltas)
        s['var_queue_time'] = 0
        for i in deltas:
          s['var_queue_time'] += (s['avg_queue_time'] - i) ** 2 / len(deltas)
        s['var_queue_time'] = sqrt(s['var_queue_time'])

        s['max_queue_time'] = max(deltas)
        s['max_queue_size'] = max(q for qq in s['queue_sizes'] for q in qq)

    def _log(self, msg):
        if self._log_fun is not None:
            #self._log_fun(self.timestamp, str(self) + ' ' + msg)
            self._log_fun(self.timestamp, msg)

    def _gen_id(self):
        i = self.client_count
        self.client_count = i + 1
        return i

    def _next_event(self):
        """Next event, will return None on some invalid states."""
        infl = [float('inf')]
        cash_lens = [q[0][0] for q in self.cashier_queues if q]
        buys = [c[0] for c in self.buy_zone]
        if self.next_client < min(infl + cash_lens + buys):
            return Event.client_arrive
        elif min(infl + buys) < min(infl + cash_lens):
            return Event.client_buy
        elif min(infl + cash_lens) < infl[0]:
            return Event.cashier_finish

    def _reset_day(self):
        self.time_left = 8 * 60 * 60
        self.days_left -= 1
        self.buy_zone = []
        self.cashier_queues = tuple([] for _ in self.cashier_queues)

    def _advance_time(self, time):
        self.timestamp += time
        self.time_left -= time
        self.next_client -= time
        self.buy_zone = [(t - time, i) for t, i in self.buy_zone]
        self.cashier_queues = [[(t - time, i) for t, i in q] for q in self.cashier_queues]
        if self.time_left <= 0:
            self._reset_day()

    def __str__(self):
        return '{} {}'.format(len(self.buy_zone), tuple(len(q) for q in self.cashier_queues))

    def step(self):
        ev = self._next_event()
        if ev == Event.client_arrive:
            t, i = self.next_client, self._gen_id()
            self._log('client_{} arrived'.format(i))
            self._stats['client_history'][i] = {0: self.timestamp}
            self.buy_zone.append((self._gen_buy_time(), i))
            self.next_client = self._gen_arrive_time()
            self._advance_time(t)
        elif ev == Event.client_buy:
            t, i = min(self.buy_zone)
            self.buy_zone.remove((t, i))
            min_len = min(len(q) for q in self.cashier_queues)
            maybe_queues = [q for q in self.cashier_queues if len(q) == min_len]
            queue = choice(maybe_queues)
            q = self.cashier_queues.index(queue)
            self._stats['client_history'][i][1] = self.timestamp
            if len(queue) == 0:
                self._stats['client_history'][i][2] = self.timestamp
                self._log('client_{} went to cashier_{} (no queue)'.format(i, q))
            else:
                self._log('client_{} went to queue_{}'.format(i, q))
            queue.append((self._gen_cashier_time(), i))
            self._advance_time(t)
        elif ev == Event.cashier_finish:
            (t, i), queue = min((q[0], q) for q in self.cashier_queues if q)
            q = self.cashier_queues.index(queue)
            self._stats['client_history'][i][3] = self.timestamp
            self._log('client_{} left cashier_{}'.format(i, q))
            queue.pop(0)
            if len(queue) > 0:
                k = queue[0][1]
                self._stats['client_history'][k][2] = self.timestamp
                self._log('client_{} went to cashier_{} (from queue)'.format(k, q))
            self._advance_time(t)
        else:
            self._log('invalid event, this is a bug')

        self._collect_stats()

    def simulate(self, days=1):
        self.days_left = days
        self._reset_day()
        #while self.time_left > 0 or self.days_left > 0:
        while self.days_left >= 0:
            self.step()
        self._compute_stats()

    def short_stats(self):
        s = self._stats
        return '\n'.join([
            'avg queue time: {}'.format(s['avg_queue_time']),
            'var queue time: {}'.format(s['var_queue_time']),
            'max queue time: {}'.format(s['max_queue_time']),
            'max queue size: {}'.format(s['max_queue_size']),
        ])

if __name__ == '__main__':
    import sys
    import argparse
    from random import uniform, random
    from math import log

    parser = argparse.ArgumentParser(description='simulation of cashiers')
    parser.add_argument('cashiers', default=4, type=int, help='number of cashiers')
    parser.add_argument('-f', '--client-frequency', default=10.0, type=float, help='frequency of clients per minute')
    parser.add_argument('-d', '--days', default=1, type=int, help='number of days to simulate')
    parser.add_argument('-e', '--export-stats', type=argparse.FileType('w'), help='set the file to export statistics in JSON to')
    parser.add_argument('--log', action='store_true', help='whether we should log the events')
    args = parser.parse_args()

    cashiers_count = args.cashiers
    client_frequency = args.client_frequency
    arrive_time = lambda: 60 * -log(random()) / client_frequency
    cashier_time = lambda: uniform(2 * 60, 6 * 60)
    buy_time = lambda: uniform(30 * 60, 90 * 60)
    lg = args.log and (lambda timestamp, message: sys.stderr.write('{t:09.03f}: {msg}\n'.format(t=timestamp, msg=message))) or None

    s = Simulation(n=cashiers_count, at=arrive_time, ct=cashier_time, bt=buy_time, mx=5, log=lg)
    try:
        print 'Simulating ... ',
        sys.stdout.flush()
        s.simulate(args.days)
        print 'done.'
        print s.short_stats()
        if args.export_stats:
            from json import dump
            dump(s._stats, args.export_stats, indent=2, separators=(',', ': '))
    except KeyboardInterrupt:
        print '\rCancelled.         '
<|endoftext|>


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

import os,sys,cairo,Image
from math import log, sin, cos, pi, atan2, sqrt
from random import random
from operator import itemgetter
from time import time
import numpy as np
from matplotlib import pylab as plt
import pickle as pkl

PI     = pi
PII    = PI*2.

NUM    = 300    # nodes
MAXFS  = 40     # max friendships pr node
NEARL  = 0.07   # comfort zone
FARL   = 0.1    # ignore nodes beyond this distance

N      = 1000   # size of png image
N2     = N/2
GRAINS = 4      # number of grains in sand painting connections
BACK   = 1.     # background color
OUT    = 'img'  # resulting image name

RAD    = 0.1    # radius of starting circle

STP    = 0.001  # scale motion in each iteration by this
steps  = 1000   # iterations

def pInit(X,Y):
  for i in xrange(0,NUM):
    the = random()*PII
    x = RAD * sin(the)
    y = RAD * cos(the)
    X[i] = 0.5+x
    Y[i] = 0.5+y
  return

def setDistances(X,Y,R,A):
  for i in xrange(0,NUM):
    dx = X[i] - X
    dy = Y[i] - Y
    a  = np.arctan2(dy,dx)
    d  = np.sqrt(dx*dx+dy*dy)
    R[i] = d
    A[i] = a
  return

def makeFriends(i,R,F):
  if sum(F[i]) > MAXFS:
    return

  r = []
  for j in xrange(0,NUM):
    if i != j and sum(F[j]) < MAXFS\
      and not F[j][i]:
        r.append([R[i][j][0],j])
  if not len(r):
    return
  r = sorted(r, key=itemgetter(0))

  index = len(r)-1
  for k in xrange(0,len(r)):
    if random() < 0.1:
      index = k
      break

  F[i][r[index][1]] = True
  F[r[index][1]][i] = True
  return

def getConnectionPoints(X,Y,R,A,F,POINTS):
  for i in xrange(0,NUM):
    for j in xrange(i+1,NUM):
      if not F[i][j]:
        continue

      a = A[i][j]
      sx = cos(a)
      sy = sin(a)

      d = R[i][j]
      scale = random()*d/GRAINS
      if random()<0.5:
        q = i
      else:
        q = j
        scale *= -1

      xp = X[q][0]
      yp = Y[q][0]

      ij = NUM*i + j
      for q in xrange(0,GRAINS):
        xp -= sx*scale
        yp -= sy*scale
        POINTS.extend([ij,xp,yp])

  return

def run(X,Y,SX,SY,R,A,F,NEARL,FARL):
  t = []
  t.append(time())
  setDistances(X,Y,R,A)
  t.append(time())

  SX[:] = 0.
  SY[:] = 0.

  t.append(time())

  for i in xrange(0,NUM):
    xF        = np.logical_not(F[i])
    d         = R[i]
    a         = A[i]
    near      = d > NEARL
    near[xF]  = False
    far       = d < FARL
    far[near] = False
    near[i]   = False
    far[i]    = False
    speed     = FARL - d[far]

    SX[near] += np.cos(a[near])
    SY[near] += np.sin(a[near])
    SX[far]  -= speed*np.cos(a[far])
    SY[far]  -= speed*np.sin(a[far])

  t.append(time())
  X  += SX*STP
  Y  += SY*STP
  t.append(time())
  makeFriends(int(random()*NUM),R,F)
  t.append(time())

  #for ti in xrange(0,len(t)-1):
    #print '{:.9f}'\
      #.format(t[ti+1] - t[ti]),
  #print

def plotIt(X,Y,F):
  #plt.ion() ; plt.figure() # need to run this once on first run.
  plt.clf()
  plt.plot(X,Y,'ro')
  for k,ff in enumerate(F):
    for fi in xrange(0,NUM):
      if ff[fi] and fi > k:
        plt.plot([X[k],X[fi]],[Y[k],Y[fi]],'k-')

  plt.axis([0,1,0,1])
  ax = plt.gca()
  ax.set_autoscale_on(False)
  plt.draw()

def getColors(f):
  scale = 255.
  im = Image.open(f)
  w,h = im.size
  rgbim = im.convert('RGB')
  res = {}
  for i in xrange(0,w):
    for j in xrange(0,h):
      r,g,b = rgbim.getpixel((i,j))
      key = '{:03d}{:03d}{:03d}'\
        .format(r,g,b)
      res[key] = [r/scale,g/scale,b/scale]
  res = [value for key,value in res.iteritems()]
  return res

def main():
  POINTS = []

  farmult =  0.05
  nearmult = 0.02
  g = 3
  h = 3
  FARL = float(farmult) * float(g)
  NEARL = float(nearmult) * float(h)
  X       = np.zeros((NUM,1))
  Y       = np.zeros((NUM,1))
  SX      = np.zeros((NUM,1))
  SY      = np.zeros((NUM,1))
  R       = [np.zeros((NUM,1),dtype=np.bool) for i in xrange(0,NUM)]
  A       = [np.zeros((NUM,1),dtype=np.bool) for i in xrange(0,NUM)]
  F       = [np.zeros((NUM,1),dtype=np.bool) for i in xrange(0,NUM)]

  pInit(X,Y)

  for i in xrange(0,steps):
    if not i%10:
      print i
    run(X,Y,SX,SY,R,A,F,NEARL,FARL)
    getConnectionPoints(X,Y,R,A,F,POINTS)
  print

  f = open('dots.pkl','wb')

  pkl.dump(POINTS,f)

  f.close()

  return

if __name__ == '__main__' : main()

<commit_msg>minor fixes. will attempt to use vectors for calculating grain positions.<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-
#

import os,sys,cairo,Image
from math import log, sin, cos, pi, atan2, sqrt
from random import random
from operator import itemgetter
from time import time
import numpy as np
from matplotlib import pylab as plt
import pickle as pkl

PI     = pi
PII    = PI*2.

NUM    = 300     # nodes
MAXFS  = 30      # max friendships pr node
NEARL  = 0.07    # comfort zone
FARL   = 0.1     # ignore nodes beyond this distance

GRAINS = 10      # number of grains in sand painting connections

RAD    = 0.1     # radius of starting circle

STP    = 0.0001  # scale motion in each iteration by this
steps  = 10000   # iterations

def pInit(X,Y):
  for i in xrange(0,NUM):
    the = random()*PII
    x = RAD * sin(the)
    y = RAD * cos(the)
    X[i] = 0.5+x
    Y[i] = 0.5+y

  return

def setDistances(X,Y,R,A):
  for i in xrange(0,NUM):
    dx = X[i] - X
    dy = Y[i] - Y
    a  = np.arctan2(dy,dx)
    d  = np.sqrt(dx*dx+dy*dy)
    R[i] = d
    A[i] = a

  return

def makeFriends(i,R,F):
  if sum(F[i]) > MAXFS:
    return

  r = []
  for j in xrange(0,NUM):
    if i != j and sum(F[j]) < MAXFS\
      and not F[j][i]:
        r.append([R[i][j][0],j])
  if not len(r):
    return
  r = sorted(r, key=itemgetter(0))

  index = len(r)-1
  for k in xrange(0,len(r)):
    if random() < 0.1:
      index = k
      break

  F[i][r[index][1]] = True
  F[r[index][1]][i] = True

  return

def getConnectionPoints(X,Y,R,A,F,POINTS):
  for i in xrange(0,NUM):
    for j in xrange(i+1,NUM):
      if not F[i][j]:
        continue

      a = A[i][j]
      sx = cos(a)
      sy = sin(a)

      d = R[i][j]
      scale = random()*d/GRAINS
      if random()<0.5:
        q = i
      else:
        q = j
        scale *= -1

      xp = X[q][0]
      yp = Y[q][0]

      ij = NUM*i + j
      for q in xrange(0,GRAINS):
        xp -= sx*scale
        yp -= sy*scale
        POINTS.extend([ij,xp,yp])

  return

def run(X,Y,SX,SY,R,A,F,NEARL,FARL):
  t = []
  setDistances(X,Y,R,A)

  SX[:] = 0.
  SY[:] = 0.

  for i in xrange(0,NUM):
    xF        = np.logical_not(F[i])
    d         = R[i]
    a         = A[i]
    near      = d > NEARL
    near[xF]  = False
    far       = d < FARL
    far[near] = False
    near[i]   = False
    far[i]    = False
    speed     = FARL - d[far]

    SX[near] += np.cos(a[near])
    SY[near] += np.sin(a[near])
    SX[far]  -= speed*np.cos(a[far])
    SY[far]  -= speed*np.sin(a[far])

  X += SX*STP
  Y += SY*STP
  makeFriends(int(random()*NUM),R,F)

  return

def plotIt(X,Y,F):
  #plt.ion() ; plt.figure() # need to run this once on first run.
  plt.clf()
  plt.plot(X,Y,'ro')
  for k,ff in enumerate(F):
    for fi in xrange(0,NUM):
      if ff[fi] and fi > k:
        plt.plot([X[k],X[fi]],[Y[k],Y[fi]],'k-')

  plt.axis([0,1,0,1])
  ax = plt.gca()
  ax.set_autoscale_on(False)
  plt.draw()

  return

def getColors(f):
  scale = 255.
  im = Image.open(f)
  w,h = im.size
  rgbim = im.convert('RGB')
  res = {}
  for i in xrange(0,w):
    for j in xrange(0,h):
      r,g,b = rgbim.getpixel((i,j))
      key = '{:03d}{:03d}{:03d}'\
        .format(r,g,b)
      res[key] = [r/scale,g/scale,b/scale]
  res = [value for key,value in res.iteritems()]

  return res

def main():
  POINTS = []

  farmult =  0.05
  nearmult = 0.02
  g = 3
  h = 3
  FARL  = float(farmult) * float(g)
  NEARL = float(nearmult) * float(h)
  X  = np.zeros((NUM,1))
  Y  = np.zeros((NUM,1))
  SX = np.zeros((NUM,1))
  SY = np.zeros((NUM,1))
  R  = [np.zeros((NUM,1),dtype=np.bool) for i in xrange(0,NUM)]
  A  = [np.zeros((NUM,1),dtype=np.bool) for i in xrange(0,NUM)]
  F  = [np.zeros((NUM,1),dtype=np.bool) for i in xrange(0,NUM)]

  pInit(X,Y)

  for i in xrange(0,steps):
    if not i%10:
      print i
    run(X,Y,SX,SY,R,A,F,NEARL,FARL)
    getConnectionPoints(X,Y,R,A,F,POINTS)
  print

  f = open('dots.pkl','wb')

  pkl.dump(POINTS,f)

  f.close()

  return

if __name__ == '__main__' : main()

<|endoftext|>


<commit_before>"""Utilities for writing code that runs on Python 2 and 3"""

import operator
import sys
import types

__author__ = "Benjamin Peterson <benjamin@python.org>"
__version__ = "1.2.0"

# True if we are running on Python 3.
PY3 = sys.version_info[0] == 3

if PY3:
    string_types = str,
    integer_types = int,
    class_types = type,
    text_type = str
    binary_type = bytes

    MAXSIZE = sys.maxsize
else:
    string_types = basestring,
    integer_types = (int, long)
    class_types = (type, types.ClassType)
    text_type = unicode
    binary_type = str

    if sys.platform == "java":
        # Jython always uses 32 bits.
        MAXSIZE = int((1 << 31) - 1)
    else:
        # It's possible to have sizeof(long) != sizeof(Py_ssize_t).
        class X(object):
            def __len__(self):
                return 1 << 31
        try:
            len(X())
        except OverflowError:
            # 32-bit
            MAXSIZE = int((1 << 31) - 1)
        else:
            # 64-bit
            MAXSIZE = int((1 << 63) - 1)
            del X

def _add_doc(func, doc):
    """Add documentation to a function."""
    func.__doc__ = doc

def _import_module(name):
    """Import module, returning the module after the last dot."""
    __import__(name)
    return sys.modules[name]

class _LazyDescr(object):

    def __init__(self, name):
        self.name = name

    def __get__(self, obj, tp):
        result = self._resolve()
        setattr(obj, self.name, result)
        # This is a bit ugly, but it avoids running this again.
        delattr(tp, self.name)
        return result

class MovedModule(_LazyDescr):

    def __init__(self, name, old, new=None):
        super(MovedModule, self).__init__(name)
        if PY3:
            if new is None:
                new = name
            self.mod = new
        else:
            self.mod = old

    def _resolve(self):
        return _import_module(self.mod)

class MovedAttribute(_LazyDescr):

    def __init__(self, name, old_mod, new_mod, old_attr=None, new_attr=None):
        super(MovedAttribute, self).__init__(name)
        if PY3:
            if new_mod is None:
                new_mod = name
            self.mod = new_mod
            if new_attr is None:
                if old_attr is None:
                    new_attr = name
                else:
                    new_attr = old_attr
            self.attr = new_attr
        else:
            self.mod = old_mod
            if old_attr is None:
                old_attr = name
            self.attr = old_attr

    def _resolve(self):
        module = _import_module(self.mod)
        return getattr(module, self.attr)

class _MovedItems(types.ModuleType):
    """Lazy loading of moved objects"""

_moved_attributes = [
    MovedAttribute("cStringIO", "cStringIO", "io", "StringIO"),
    MovedAttribute("filter", "itertools", "builtins", "ifilter", "filter"),
    MovedAttribute("input", "__builtin__", "builtins", "raw_input", "input"),
    MovedAttribute("map", "itertools", "builtins", "imap", "map"),
    MovedAttribute("reload_module", "__builtin__", "imp", "reload"),
    MovedAttribute("reduce", "__builtin__", "functools"),
    MovedAttribute("StringIO", "StringIO", "io"),
    MovedAttribute("xrange", "__builtin__", "builtins", "xrange", "range"),
    MovedAttribute("zip", "itertools", "builtins", "izip", "zip"),

    MovedModule("builtins", "__builtin__"),
    MovedModule("configparser", "ConfigParser"),
    MovedModule("copyreg", "copy_reg"),
    MovedModule("http_cookiejar", "cookielib", "http.cookiejar"),
    MovedModule("http_cookies", "Cookie", "http.cookies"),
    MovedModule("html_entities", "htmlentitydefs", "html.entities"),
    MovedModule("html_parser", "HTMLParser", "html.parser"),
    MovedModule("http_client", "httplib", "http.client"),
    MovedModule("BaseHTTPServer", "BaseHTTPServer", "http.server"),
    MovedModule("CGIHTTPServer", "CGIHTTPServer", "http.server"),
    MovedModule("SimpleHTTPServer", "SimpleHTTPServer", "http.server"),
    MovedModule("cPickle", "cPickle", "pickle"),
    MovedModule("queue", "Queue"),
    MovedModule("reprlib", "repr"),
    MovedModule("socketserver", "SocketServer"),
    MovedModule("tkinter", "Tkinter"),
    MovedModule("tkinter_dialog", "Dialog", "tkinter.dialog"),
    MovedModule("tkinter_filedialog", "FileDialog", "tkinter.filedialog"),
    MovedModule("tkinter_scrolledtext", "ScrolledText", "tkinter.scrolledtext"),
    MovedModule("tkinter_simpledialog", "SimpleDialog", "tkinter.simpledialog"),
    MovedModule("tkinter_tix", "Tix", "tkinter.tix"),
    MovedModule("tkinter_constants", "Tkconstants", "tkinter.constants"),
    MovedModule("tkinter_dnd", "Tkdnd", "tkinter.dnd"),
    MovedModule("tkinter_colorchooser", "tkColorChooser",
                "tkinter.colorchooser"),
    MovedModule("tkinter_commondialog", "tkCommonDialog",
                "tkinter.commondialog"),
    MovedModule("tkinter_tkfiledialog", "tkFileDialog", "tkinter.filedialog"),
    MovedModule("tkinter_font", "tkFont", "tkinter.font"),
    MovedModule("tkinter_messagebox", "tkMessageBox", "tkinter.messagebox"),
    MovedModule("tkinter_tksimpledialog", "tkSimpleDialog",
                "tkinter.simpledialog"),
    MovedModule("urllib_robotparser", "robotparser", "urllib.robotparser"),
    MovedModule("winreg", "_winreg"),
]
for attr in _moved_attributes:
    setattr(_MovedItems, attr.name, attr)
del attr

moves = sys.modules["six.moves"] = _MovedItems("moves")

def add_move(move):
    """Add an item to six.moves."""
    setattr(_MovedItems, move.name, move)

def remove_move(name):
    """Remove item from six.moves."""
    try:
        delattr(_MovedItems, name)
    except AttributeError:
        try:
            del moves.__dict__[name]
        except KeyError:
            raise AttributeError("no such move, %r" % (name,))

if PY3:
    _meth_func = "__func__"
    _meth_self = "__self__"

    _func_code = "__code__"
    _func_defaults = "__defaults__"

    _iterkeys = "keys"
    _itervalues = "values"
    _iteritems = "items"
else:
    _meth_func = "im_func"
    _meth_self = "im_self"

    _func_code = "func_code"
    _func_defaults = "func_defaults"

    _iterkeys = "iterkeys"
    _itervalues = "itervalues"
    _iteritems = "iteritems"

try:
    advance_iterator = next
except NameError:
    def advance_iterator(it):
        return it.next()
next = advance_iterator

if PY3:
    def get_unbound_function(unbound):
        return unbound

    Iterator = object

    def callable(obj):
        return any("__call__" in klass.__dict__ for klass in type(obj).__mro__)
else:
    def get_unbound_function(unbound):
        return unbound.im_func

    class Iterator(object):

        def next(self):
            return type(self).__next__(self)

    callable = callable
_add_doc(get_unbound_function,
         """Get the function out of a possibly unbound function""")

get_method_function = operator.attrgetter(_meth_func)
get_method_self = operator.attrgetter(_meth_self)
get_function_code = operator.attrgetter(_func_code)
get_function_defaults = operator.attrgetter(_func_defaults)

def iterkeys(d):
    """Return an iterator over the keys of a dictionary."""
    return iter(getattr(d, _iterkeys)())

def itervalues(d):
    """Return an iterator over the values of a dictionary."""
    return iter(getattr(d, _itervalues)())

def iteritems(d):
    """Return an iterator over the (key, value) pairs of a dictionary."""
    return iter(getattr(d, _iteritems)())

if PY3:
    def b(s):
        return s.encode("latin-1")
    def u(s):
        return s
    if sys.version_info[1] <= 1:
        def int2byte(i):
            return bytes((i,))
    else:
        # This is about 2x faster than the implementation above on 3.2+
        int2byte = operator.methodcaller("to_bytes", 1, "big")
    import io
    StringIO = io.StringIO
    BytesIO = io.BytesIO
else:
    def b(s):
        return s
    def u(s):
        return unicode(s, "unicode_escape")
    int2byte = chr
    import StringIO
    StringIO = BytesIO = StringIO.StringIO
_add_doc(b, """Byte literal""")
_add_doc(u, """Text literal""")

if PY3:
    import builtins
    exec_ = getattr(builtins, "exec")

    def reraise(tp, value, tb=None):
        if value.__traceback__ is not tb:
            raise value.with_traceback(tb)
        raise value

    print_ = getattr(builtins, "print")
    del builtins

else:
    def exec_(code, globs=None, locs=None):
        """Execute code in a namespace."""
        if globs is None:
            frame = sys._getframe(1)
            globs = frame.f_globals
            if locs is None:
                locs = frame.f_locals
            del frame
        elif locs is None:
            locs = globs
        exec("""exec code in globs, locs""")

    exec_("""def reraise(tp, value, tb=None):
    raise tp, value, tb
""")

    def print_(*args, **kwargs):
        """The new-style print function."""
        fp = kwargs.pop("file", sys.stdout)
        if fp is None:
            return
        def write(data):
            if not isinstance(data, basestring):
                data = str(data)
            fp.write(data)
        want_unicode = False
        sep = kwargs.pop("sep", None)
        if sep is not None:
            if isinstance(sep, unicode):
                want_unicode = True
            elif not isinstance(sep, str):
                raise TypeError("sep must be None or a string")
        end = kwargs.pop("end", None)
        if end is not None:
            if isinstance(end, unicode):
                want_unicode = True
            elif not isinstance(end, str):
                raise TypeError("end must be None or a string")
        if kwargs:
            raise TypeError("invalid keyword arguments to print()")
        if not want_unicode:
            for arg in args:
                if isinstance(arg, unicode):
                    want_unicode = True
                    break
        if want_unicode:
            newline = unicode("\n")
            space = unicode(" ")
        else:
            newline = "\n"
            space = " "
        if sep is None:
            sep = space
        if end is None:
            end = newline
        for i, arg in enumerate(args):
            if i:
                write(sep)
            write(arg)
        write(end)

_add_doc(reraise, """Reraise an exception.""")

def with_metaclass(meta, base=object):
    """Create a base class with a metaclass."""
    return meta("NewBase", (base,), {})
<commit_msg>check for java properly<commit_after>"""Utilities for writing code that runs on Python 2 and 3"""

import operator
import sys
import types

__author__ = "Benjamin Peterson <benjamin@python.org>"
__version__ = "1.2.0"

# True if we are running on Python 3.
PY3 = sys.version_info[0] == 3

if PY3:
    string_types = str,
    integer_types = int,
    class_types = type,
    text_type = str
    binary_type = bytes

    MAXSIZE = sys.maxsize
else:
    string_types = basestring,
    integer_types = (int, long)
    class_types = (type, types.ClassType)
    text_type = unicode
    binary_type = str

    if sys.platform.startswith("java"):
        # Jython always uses 32 bits.
        MAXSIZE = int((1 << 31) - 1)
    else:
        # It's possible to have sizeof(long) != sizeof(Py_ssize_t).
        class X(object):
            def __len__(self):
                return 1 << 31
        try:
            len(X())
        except OverflowError:
            # 32-bit
            MAXSIZE = int((1 << 31) - 1)
        else:
            # 64-bit
            MAXSIZE = int((1 << 63) - 1)
            del X

def _add_doc(func, doc):
    """Add documentation to a function."""
    func.__doc__ = doc

def _import_module(name):
    """Import module, returning the module after the last dot."""
    __import__(name)
    return sys.modules[name]

class _LazyDescr(object):

    def __init__(self, name):
        self.name = name

    def __get__(self, obj, tp):
        result = self._resolve()
        setattr(obj, self.name, result)
        # This is a bit ugly, but it avoids running this again.
        delattr(tp, self.name)
        return result

class MovedModule(_LazyDescr):

    def __init__(self, name, old, new=None):
        super(MovedModule, self).__init__(name)
        if PY3:
            if new is None:
                new = name
            self.mod = new
        else:
            self.mod = old

    def _resolve(self):
        return _import_module(self.mod)

class MovedAttribute(_LazyDescr):

    def __init__(self, name, old_mod, new_mod, old_attr=None, new_attr=None):
        super(MovedAttribute, self).__init__(name)
        if PY3:
            if new_mod is None:
                new_mod = name
            self.mod = new_mod
            if new_attr is None:
                if old_attr is None:
                    new_attr = name
                else:
                    new_attr = old_attr
            self.attr = new_attr
        else:
            self.mod = old_mod
            if old_attr is None:
                old_attr = name
            self.attr = old_attr

    def _resolve(self):
        module = _import_module(self.mod)
        return getattr(module, self.attr)

class _MovedItems(types.ModuleType):
    """Lazy loading of moved objects"""

_moved_attributes = [
    MovedAttribute("cStringIO", "cStringIO", "io", "StringIO"),
    MovedAttribute("filter", "itertools", "builtins", "ifilter", "filter"),
    MovedAttribute("input", "__builtin__", "builtins", "raw_input", "input"),
    MovedAttribute("map", "itertools", "builtins", "imap", "map"),
    MovedAttribute("reload_module", "__builtin__", "imp", "reload"),
    MovedAttribute("reduce", "__builtin__", "functools"),
    MovedAttribute("StringIO", "StringIO", "io"),
    MovedAttribute("xrange", "__builtin__", "builtins", "xrange", "range"),
    MovedAttribute("zip", "itertools", "builtins", "izip", "zip"),

    MovedModule("builtins", "__builtin__"),
    MovedModule("configparser", "ConfigParser"),
    MovedModule("copyreg", "copy_reg"),
    MovedModule("http_cookiejar", "cookielib", "http.cookiejar"),
    MovedModule("http_cookies", "Cookie", "http.cookies"),
    MovedModule("html_entities", "htmlentitydefs", "html.entities"),
    MovedModule("html_parser", "HTMLParser", "html.parser"),
    MovedModule("http_client", "httplib", "http.client"),
    MovedModule("BaseHTTPServer", "BaseHTTPServer", "http.server"),
    MovedModule("CGIHTTPServer", "CGIHTTPServer", "http.server"),
    MovedModule("SimpleHTTPServer", "SimpleHTTPServer", "http.server"),
    MovedModule("cPickle", "cPickle", "pickle"),
    MovedModule("queue", "Queue"),
    MovedModule("reprlib", "repr"),
    MovedModule("socketserver", "SocketServer"),
    MovedModule("tkinter", "Tkinter"),
    MovedModule("tkinter_dialog", "Dialog", "tkinter.dialog"),
    MovedModule("tkinter_filedialog", "FileDialog", "tkinter.filedialog"),
    MovedModule("tkinter_scrolledtext", "ScrolledText", "tkinter.scrolledtext"),
    MovedModule("tkinter_simpledialog", "SimpleDialog", "tkinter.simpledialog"),
    MovedModule("tkinter_tix", "Tix", "tkinter.tix"),
    MovedModule("tkinter_constants", "Tkconstants", "tkinter.constants"),
    MovedModule("tkinter_dnd", "Tkdnd", "tkinter.dnd"),
    MovedModule("tkinter_colorchooser", "tkColorChooser",
                "tkinter.colorchooser"),
    MovedModule("tkinter_commondialog", "tkCommonDialog",
                "tkinter.commondialog"),
    MovedModule("tkinter_tkfiledialog", "tkFileDialog", "tkinter.filedialog"),
    MovedModule("tkinter_font", "tkFont", "tkinter.font"),
    MovedModule("tkinter_messagebox", "tkMessageBox", "tkinter.messagebox"),
    MovedModule("tkinter_tksimpledialog", "tkSimpleDialog",
                "tkinter.simpledialog"),
    MovedModule("urllib_robotparser", "robotparser", "urllib.robotparser"),
    MovedModule("winreg", "_winreg"),
]
for attr in _moved_attributes:
    setattr(_MovedItems, attr.name, attr)
del attr

moves = sys.modules["six.moves"] = _MovedItems("moves")

def add_move(move):
    """Add an item to six.moves."""
    setattr(_MovedItems, move.name, move)

def remove_move(name):
    """Remove item from six.moves."""
    try:
        delattr(_MovedItems, name)
    except AttributeError:
        try:
            del moves.__dict__[name]
        except KeyError:
            raise AttributeError("no such move, %r" % (name,))

if PY3:
    _meth_func = "__func__"
    _meth_self = "__self__"

    _func_code = "__code__"
    _func_defaults = "__defaults__"

    _iterkeys = "keys"
    _itervalues = "values"
    _iteritems = "items"
else:
    _meth_func = "im_func"
    _meth_self = "im_self"

    _func_code = "func_code"
    _func_defaults = "func_defaults"

    _iterkeys = "iterkeys"
    _itervalues = "itervalues"
    _iteritems = "iteritems"

try:
    advance_iterator = next
except NameError:
    def advance_iterator(it):
        return it.next()
next = advance_iterator

if PY3:
    def get_unbound_function(unbound):
        return unbound

    Iterator = object

    def callable(obj):
        return any("__call__" in klass.__dict__ for klass in type(obj).__mro__)
else:
    def get_unbound_function(unbound):
        return unbound.im_func

    class Iterator(object):

        def next(self):
            return type(self).__next__(self)

    callable = callable
_add_doc(get_unbound_function,
         """Get the function out of a possibly unbound function""")

get_method_function = operator.attrgetter(_meth_func)
get_method_self = operator.attrgetter(_meth_self)
get_function_code = operator.attrgetter(_func_code)
get_function_defaults = operator.attrgetter(_func_defaults)

def iterkeys(d):
    """Return an iterator over the keys of a dictionary."""
    return iter(getattr(d, _iterkeys)())

def itervalues(d):
    """Return an iterator over the values of a dictionary."""
    return iter(getattr(d, _itervalues)())

def iteritems(d):
    """Return an iterator over the (key, value) pairs of a dictionary."""
    return iter(getattr(d, _iteritems)())

if PY3:
    def b(s):
        return s.encode("latin-1")
    def u(s):
        return s
    if sys.version_info[1] <= 1:
        def int2byte(i):
            return bytes((i,))
    else:
        # This is about 2x faster than the implementation above on 3.2+
        int2byte = operator.methodcaller("to_bytes", 1, "big")
    import io
    StringIO = io.StringIO
    BytesIO = io.BytesIO
else:
    def b(s):
        return s
    def u(s):
        return unicode(s, "unicode_escape")
    int2byte = chr
    import StringIO
    StringIO = BytesIO = StringIO.StringIO
_add_doc(b, """Byte literal""")
_add_doc(u, """Text literal""")

if PY3:
    import builtins
    exec_ = getattr(builtins, "exec")

    def reraise(tp, value, tb=None):
        if value.__traceback__ is not tb:
            raise value.with_traceback(tb)
        raise value

    print_ = getattr(builtins, "print")
    del builtins

else:
    def exec_(code, globs=None, locs=None):
        """Execute code in a namespace."""
        if globs is None:
            frame = sys._getframe(1)
            globs = frame.f_globals
            if locs is None:
                locs = frame.f_locals
            del frame
        elif locs is None:
            locs = globs
        exec("""exec code in globs, locs""")

    exec_("""def reraise(tp, value, tb=None):
    raise tp, value, tb
""")

    def print_(*args, **kwargs):
        """The new-style print function."""
        fp = kwargs.pop("file", sys.stdout)
        if fp is None:
            return
        def write(data):
            if not isinstance(data, basestring):
                data = str(data)
            fp.write(data)
        want_unicode = False
        sep = kwargs.pop("sep", None)
        if sep is not None:
            if isinstance(sep, unicode):
                want_unicode = True
            elif not isinstance(sep, str):
                raise TypeError("sep must be None or a string")
        end = kwargs.pop("end", None)
        if end is not None:
            if isinstance(end, unicode):
                want_unicode = True
            elif not isinstance(end, str):
                raise TypeError("end must be None or a string")
        if kwargs:
            raise TypeError("invalid keyword arguments to print()")
        if not want_unicode:
            for arg in args:
                if isinstance(arg, unicode):
                    want_unicode = True
                    break
        if want_unicode:
            newline = unicode("\n")
            space = unicode(" ")
        else:
            newline = "\n"
            space = " "
        if sep is None:
            sep = space
        if end is None:
            end = newline
        for i, arg in enumerate(args):
            if i:
                write(sep)
            write(arg)
        write(end)

_add_doc(reraise, """Reraise an exception.""")

def with_metaclass(meta, base=object):
    """Create a base class with a metaclass."""
    return meta("NewBase", (base,), {})
<|endoftext|>