<commit_before>#!/usr/bin/env python
############################################################################
#                                                                          #
# Copyright 2014 Prelert Ltd                                               #
#                                                                          #
# Licensed under the Apache License, Version 2.0 (the "License");          #
# you may not use this file except in compliance with the License.         #
# You may obtain a copy of the License at                                  #
#                                                                          #
#    http://www.apache.org/licenses/LICENSE-2.0                            #
#                                                                          #
# Unless required by applicable law or agreed to in writing, software      #
# distributed under the License is distributed on an "AS IS" BASIS,        #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and      #
# limitations under the License.                                           #
#                                                                          #
############################################################################

'''
Script to pull metric data from AWS CloudWatch and analyze it in
the Prelert Engine API. There are 2 modes of operation: historical
where stored metric data is extracted between 2 dates and a continous
realtime mode where the preceeding few minutes of data is queried in
a loop.

The path to a configuration file containing the AWS connection parameters
must be passed to the script the file should have the following propteries:

    region=REGION
    aws_access_key_id=YOUR_ACCESS_ID
    aws_secret_access_key=YOUR_SECRET_KEY

Where REGION is one of us-east-1, eu-west-1, etc

If the --start-date parameter is set then this will query historical data
from CloudWatch until --end-date or the current time if --end-date is not
set. Otherwise the script will run in an infinite loop pulling realtime
data, use Ctrl-C to quit the realtime mode as the script will catch
the interrupt and handle the exit gracefully.
'''

import argparse
import ConfigParser
from datetime import datetime, timedelta, tzinfo
import json
import StringIO
import time

import boto.ec2
import boto.ec2.cloudwatch

from prelert.engineApiClient import EngineApiClient

# Prelert Engine API default connection prarams
API_HOST = 'localhost'
API_PORT = 8080
ABI_BASE_URL = 'engine/v1'

UPDATE_INTERVAL=300

'''
    Prelert Engine job configuration.
    The detector is configured to analyze the mean of the field named
    'Average' by the field 'metric_name' where the value of 'metric_name'
    is one of the AWS metrics e.g. CPUUtilization, DiskWriteOps, etc.
    The analysis is partition by AWS instance ID.
    bucketSpan is set to 300 seconds, which is the as the CloudWatch
    reporting interval.
'''
JOB_CONFIG = '{"analysisConfig" : {\
                    "bucketSpan":' + str(UPDATE_INTERVAL) + ',\
                    "detectors" :[{"function":"mean","fieldName":"Average","byFieldName":"metric_name","partitionFieldName":"instance"}] \
                },\
                "dataDescription" : {"format":"JSON","timeField":"timestamp","timeFormat":"yyyy-MM-dd\'T\'HH:mm:ssX"} \
            }'

class MetricRecord:
    '''
    Simple holder class for the CloudWatch metrics.
    toJsonStr returns the metric in a format for the job
    configuration above.
    '''
    def __init__(self, timestamp, instance, metric_name, metric_value):
        self.timestamp = timestamp
        self.instance = instance
        self.metric_name = metric_name
        self.metric_value = metric_value

    def toJsonStr(self):
        result = '{"timestamp":"' + self.timestamp.isoformat() + \
            '", "instance":"' + self.instance + '", "metric_name":"' + \
            self.metric_name + '", "Average":' + str(self.metric_value) + '}'

        return result

class UTC(tzinfo):
    '''
    UTC timezone class
    '''

    def utcoffset(self, dt):
        return timedelta(0)

    def tzname(self, dt):
        return "UTC"

    def dst(self, dt):
        return timedelta(0)

def parseArguments():
    parser = argparse.ArgumentParser()

    parser.add_argument("config", help="The AWS connection parameters.")

    parser.add_argument("--api-host", help="The Prelert Engine API host, defaults to "
        + API_HOST, default=API_HOST, dest="api_host")
    parser.add_argument("--api-port", help="The Prelert Engine API port, defaults to "
        + str(API_PORT), default=API_PORT, dest="api_port")

    parser.add_argument("--job-id", help="Send data to this job. If not set a \
        new job will be created.", default=None, dest="job_id")

    parser.add_argument("--start-date", help="Request data from this date. If not \
        set then run in realtime mode. Dates must be in YYYY-MM-DD format",
        default=None, dest="start_date")
    parser.add_argument("--end-date", help="if --start-date is set then pull \
        and analyze only the metric data between those dates. \
        If --start-date is not set this argument has no meaning. \
        Dates must be in YYYY-MM-DD format",
        default=None, dest="end_date")

    return parser.parse_args()

def runHistorical(job_id, start_date, end_date, cloudwatch_conn, engine_client):
    '''
    Query and analyze the CloudWatch metrics from start_date to end_date.
    If end_date == None then run until the time now.
    '''
    end = start_date

    while True:

        start = end
        end = start + timedelta(minutes=5)

        end_condition = end_date if end_date != None else datetime.utcnow()
        if end > end_condition:
            break

        print "Querying metrics starting at time " + str(start.isoformat())

        metric_records = []
        metrics = cloudwatch_conn.list_metrics(namespace='AWS/EC2')
        for m in metrics:
            instance = m.dimensions['InstanceId'][0]

            datapoints = m.query(start, end, 'Average', period=UPDATE_INTERVAL)
            for dp in datapoints:
                # annoyingly Boto does not return datetimes with a timezone
                utc_time = dp['Timestamp'].replace(tzinfo=UTC())
                mr = MetricRecord(utc_time, instance, m.name, dp['Average'])
                metric_records.append(mr)

        metric_records.sort(key=lambda r : r.timestamp)

        data = ''
        for mr in metric_records:
            data += mr.toJsonStr() + '\n'

        (http_status, response) = engine_client.upload(job_id, data)
        if http_status != 202:
            print "Error uploading metric data to the Engine"
            print http_status, json.dumps(response)

def runRealtime(job_id, cloudwatch_conn, engine_client):
    '''
    Query the previous 5 minutes of metric data every 5 minutes
    then upload to the Prelert Engine.

    This function runs in an infinite loop but will catch the
    keyboard interrupt (Ctrl C) and exit gracefully
    '''
    try:
        end = datetime.utcnow() - timedelta(minutes=
            5)

        while True:

            start = end
            end = datetime.utcnow()

            print "Querying metrics starting " + str(start.isoformat())

            metric_records = []
            metrics = cloudwatch_conn.list_metrics(namespace='AWS/EC2')
            for m in metrics:
                instance = m.dimensions['InstanceId'][0]

                datapoints = m.query(start, end, 'Average', period=UPDATE_INTERVAL)
                for dp in datapoints:
                    # annoyingly Boto does not return datetimes with a timezone
                    utc_time = dp['Timestamp'].replace(tzinfo=UTC())
                    mr = MetricRecord(utc_time, instance, m.name, dp['Average'])
                    metric_records.append(mr)

            metric_records.sort(key=lambda r : r.timestamp)

            data = ''
            for mr in metric_records:
                data += mr.toJsonStr() + '\n'

            (http_status, response) = engine_client.upload(job_id, data)
            if http_status != 202:
                print "Error uploading metric data to the Engine"
                print http_status, json.dumps(response)

            duration = datetime.utcnow() - end
            sleep_time = max(UPDATE_INTERVAL - duration.seconds, 0)
            print "sleeping for " + str(sleep_time) + " seconds"
            if sleep_time > 0.0:
                time.sleep(sleep_time)

    except KeyboardInterrupt:
        print "Interrupt caught... terminating real time queries"
        return

def main():

    args = parseArguments()

    # read the config file
    config = ConfigParser.RawConfigParser()
    try:
        # insert a section header into the config so
        # ConfigParser will read it without complaint
        with open(args.config, "r") as config_file:
            ini_str = '[root]\n' + config_file.read()
            ini_fp = StringIO.StringIO(ini_str)
            config.readfp(ini_fp)
    except IOError:
        print "Error opening file " + args.config
        return

    try:
        region = config.get('root', 'region')
        access_id = config.get('root', 'aws_access_key_id')
        secret_key = config.get('root', 'aws_secret_access_key')
    except ConfigParser.NoOptionError as e:
        print e
        return

    # AWS CloudWatch connection
    cloudwatch_conn = boto.ec2.cloudwatch.connect_to_region(region,
                 aws_access_key_id=access_id,
                 aws_secret_access_key=secret_key)

    # The Prelert REST API client
    engine_client = EngineApiClient(args.api_host, ABI_BASE_URL, args.api_port)

    # If no job ID is supplied create a new job
    job_id = args.job_id
    if job_id == None:
        (http_status, response) = engine_client.createJob(JOB_CONFIG)
        if http_status != 201:
            print "Error creating job"
            print response
            return

        job_id = response['id']
        print "Created job with ID " + str(job_id)
    else:
        print "Using job ID " + job_id

    # default start date is None meaning run realtime
    start_date = None
    if args.start_date != None:
        start_date = datetime.strptime(args.start_date, "%Y-%m-%d")

    if start_date == None:
        runRealtime(job_id, cloudwatch_conn, engine_client)
    else:
        # historical mode, check for an end date
        end_date = None
        if args.end_date != None:
            end_date = datetime.strptime(args.end_date, "%Y-%m-%d")

        runHistorical(job_id, start_date, end_date, cloudwatch_conn, engine_client)

    print "Closing job..."
    engine_client.close(job_id)

if __name__ == "__main__":
    main()
<commit_msg>Add delay parameter to realtime mode.<commit_after>#!/usr/bin/env python
############################################################################
#                                                                          #
# Copyright 2014 Prelert Ltd                                               #
#                                                                          #
# Licensed under the Apache License, Version 2.0 (the "License");          #
# you may not use this file except in compliance with the License.         #
# You may obtain a copy of the License at                                  #
#                                                                          #
#    http://www.apache.org/licenses/LICENSE-2.0                            #
#                                                                          #
# Unless required by applicable law or agreed to in writing, software      #
# distributed under the License is distributed on an "AS IS" BASIS,        #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and      #
# limitations under the License.                                           #
#                                                                          #
############################################################################

'''
Script to pull metric data from AWS CloudWatch and analyze it in
the Prelert Engine API. There are 2 modes of operation: historical
where stored metric data is extracted between 2 dates and a continous
realtime mode where the preceeding few minutes of data is queried in
a loop.

The path to a configuration file containing the AWS connection parameters
must be passed to the script the file should have the following propteries:

    region=REGION
    aws_access_key_id=YOUR_ACCESS_ID
    aws_secret_access_key=YOUR_SECRET_KEY

Where REGION is one of us-east-1, eu-west-1, etc

If the --start-date parameter is set then this will query historical data
from CloudWatch until --end-date or the current time if --end-date is not
set. Otherwise the script will run in an infinite loop pulling realtime
data, use Ctrl-C to quit the realtime mode as the script will catch
the interrupt and handle the exit gracefully.
'''

import argparse
import ConfigParser
from datetime import datetime, timedelta, tzinfo
import json
import StringIO
import time

import boto.ec2
import boto.ec2.cloudwatch

from prelert.engineApiClient import EngineApiClient

# Prelert Engine API default connection prarams
API_HOST = 'localhost'
API_PORT = 8080
ABI_BASE_URL = 'engine/v1'

''' Interval between query new data from CloudWatch (seconds)'''
UPDATE_INTERVAL=300

''' In realtime mode run this many seconds behind realtime '''
DELAY=600

'''
    Prelert Engine job configuration.
    The detector is configured to analyze the mean of the field named
    'Average' by the field 'metric_name' where the value of 'metric_name'
    is one of the AWS metrics e.g. CPUUtilization, DiskWriteOps, etc.
    The analysis is partition by AWS instance ID.
    bucketSpan is set the same as the CloudWatch reporting interval.
'''
JOB_CONFIG = '{"analysisConfig" : {\
                    "bucketSpan":' + str(UPDATE_INTERVAL) + ',\
                    "detectors" :[{"function":"mean","fieldName":"Average","byFieldName":"metric_name","partitionFieldName":"instance"}] \
                },\
                "dataDescription" : {"format":"JSON","timeField":"timestamp","timeFormat":"yyyy-MM-dd\'T\'HH:mm:ssX"} \
            }'

class MetricRecord:
    '''
    Simple holder class for the CloudWatch metrics.
    toJsonStr returns the metric in a format for the job
    configuration above.
    '''
    def __init__(self, timestamp, instance, metric_name, metric_value):
        self.timestamp = timestamp
        self.instance = instance
        self.metric_name = metric_name
        self.metric_value = metric_value

    def toJsonStr(self):
        result = '{"timestamp":"' + self.timestamp.isoformat() + \
            '", "instance":"' + self.instance + '", "metric_name":"' + \
            self.metric_name + '", "Average":' + str(self.metric_value) + '}'

        return result

class UTC(tzinfo):
    '''
    UTC timezone class
    '''

    def utcoffset(self, dt):
        return timedelta(0)

    def tzname(self, dt):
        return "UTC"

    def dst(self, dt):
        return timedelta(0)

def parseArguments():
    parser = argparse.ArgumentParser()

    parser.add_argument("config", help="The AWS connection parameters.")

    parser.add_argument("--api-host", help="The Prelert Engine API host, defaults to "
        + API_HOST, default=API_HOST, dest="api_host")
    parser.add_argument("--api-port", help="The Prelert Engine API port, defaults to "
        + str(API_PORT), default=API_PORT, dest="api_port")

    parser.add_argument("--job-id", help="Send data to this job. If not set a \
        new job will be created.", default=None, dest="job_id")

    parser.add_argument("--start-date", help="Request data from this date. If not \
        set then run in realtime mode. Dates must be in YYYY-MM-DD format",
        default=None, dest="start_date")
    parser.add_argument("--end-date", help="if --start-date is set then pull \
        and analyze only the metric data between those dates. \
        If --start-date is not set this argument has no meaning. \
        Dates must be in YYYY-MM-DD format",
        default=None, dest="end_date")

    return parser.parse_args()

def runHistorical(job_id, start_date, end_date, cloudwatch_conn, engine_client):
    '''
    Query and analyze the CloudWatch metrics from start_date to end_date.
    If end_date == None then run until the time now.
    '''
    end = start_date
    delta = timedelta(seconds=UPDATE_INTERVAL)

    while True:

        start = end
        end = start + delta

        end_condition = end_date if end_date != None else datetime.utcnow()
        if end > end_condition:
            break

        print "Querying metrics starting at time " + str(start.isoformat())

        metric_records = []
        metrics = cloudwatch_conn.list_metrics(namespace='AWS/EC2')
        for m in metrics:
            instance = m.dimensions['InstanceId'][0]

            datapoints = m.query(start, end, 'Average', period=UPDATE_INTERVAL)
            for dp in datapoints:
                # annoyingly Boto does not return datetimes with a timezone
                utc_time = dp['Timestamp'].replace(tzinfo=UTC())
                mr = MetricRecord(utc_time, instance, m.name, dp['Average'])
                metric_records.append(mr)

        metric_records.sort(key=lambda r : r.timestamp)

        data = ''
        for mr in metric_records:
            data += mr.toJsonStr() + '\n'

        (http_status, response) = engine_client.upload(job_id, data)
        if http_status != 202:
            print "Error uploading metric data to the Engine"
            print http_status, json.dumps(response)

def runRealtime(job_id, cloudwatch_conn, engine_client):
    '''
    Query the previous 5 minutes of metric data every 5 minutes
    then upload to the Prelert Engine.

    This function runs in an infinite loop but will catch the
    keyboard interrupt (Ctrl C) and exit gracefully
    '''
    try:
        delay = timedelta(seconds=DELAY)
        end = datetime.utcnow() - delay - timedelta(seconds=UPDATE_INTERVAL)

        while True:

            start = end
            end = datetime.utcnow() - delay

            print "Querying metrics from " + str(start.isoformat())  + " to " + end.isoformat()

            metric_records = []
            metrics = cloudwatch_conn.list_metrics(namespace='AWS/EC2')
            for m in metrics:
                instance = m.dimensions['InstanceId'][0]

                datapoints = m.query(start, end, 'Average', period=UPDATE_INTERVAL)
                for dp in datapoints:
                    # annoyingly Boto does not return datetimes with a timezone
                    utc_time = dp['Timestamp'].replace(tzinfo=UTC())
                    mr = MetricRecord(utc_time, instance, m.name, dp['Average'])
                    metric_records.append(mr)

            metric_records.sort(key=lambda r : r.timestamp)

            data = ''
            for mr in metric_records:
                data += mr.toJsonStr() + '\n'

            (http_status, response) = engine_client.upload(job_id, data)
            if http_status != 202:
                print "Error uploading metric data to the Engine"
                print http_status, json.dumps(response)

            duration = datetime.utcnow() - delay - end
            sleep_time = max(UPDATE_INTERVAL - duration.seconds, 0)
            print "sleeping for " + str(sleep_time) + " seconds"
            if sleep_time > 0:
                time.sleep(sleep_time)

    except KeyboardInterrupt:
        print "Interrupt caught... terminating real time queries"
        return

def main():

    args = parseArguments()

    # read the config file
    config = ConfigParser.RawConfigParser()
    try:
        # insert a section header into the config so
        # ConfigParser will read it without complaint
        with open(args.config, "r") as config_file:
            ini_str = '[root]\n' + config_file.read()
            ini_fp = StringIO.StringIO(ini_str)
            config.readfp(ini_fp)
    except IOError:
        print "Error opening file " + args.config
        return

    try:
        region = config.get('root', 'region')
        access_id = config.get('root', 'aws_access_key_id')
        secret_key = config.get('root', 'aws_secret_access_key')
    except ConfigParser.NoOptionError as e:
        print e
        return

    # AWS CloudWatch connection
    cloudwatch_conn = boto.ec2.cloudwatch.connect_to_region(region,
                 aws_access_key_id=access_id,
                 aws_secret_access_key=secret_key)

    # The Prelert REST API client
    engine_client = EngineApiClient(args.api_host, ABI_BASE_URL, args.api_port)

    # If no job ID is supplied create a new job
    job_id = args.job_id
    if job_id == None:
        (http_status, response) = engine_client.createJob(JOB_CONFIG)
        if http_status != 201:
            print "Error creating job"
            print response
            return

        job_id = response['id']
        print "Created job with ID " + str(job_id)
    else:
        print "Using job ID " + job_id

    # default start date is None meaning run realtime
    start_date = None
    if args.start_date != None:
        start_date = datetime.strptime(args.start_date, "%Y-%m-%d")

    if start_date == None:
        runRealtime(job_id, cloudwatch_conn, engine_client)
    else:
        # historical mode, check for an end date
        end_date = None
        if args.end_date != None:
            end_date = datetime.strptime(args.end_date, "%Y-%m-%d")

        runHistorical(job_id, start_date, end_date, cloudwatch_conn, engine_client)

    print "Closing job..."
    engine_client.close(job_id)

if __name__ == "__main__":
    main()
<|endoftext|>


<commit_before>import math
from timeit import Timer

def solve_p10_sieve():
    return sum(sieve_primes_up_to(2000000))

def sieve_primes_up_to(num):
    num_list = range(num)
    num_list[1] = 0 # cross out 1, special case
    i = 2

    while num_list[i] <= int(math.sqrt(num))+1:
        cross_out_composite_multiples(num_list[i], num_list)
        i = next_non_crossed_out_index(i, num_list)

    return filter(lambda x: x != 0, num_list)

def cross_out_composite_multiples(multiple, num_list):
    for composite in xrange(multiple*2, len(num_list), multiple):
        num_list[composite] = 0

def next_non_crossed_out_index(i, num_list):
    increment_index = True
    while increment_index:
        i += 1
        increment_index = num_list[i] == 0
    return i

if __name__ == '__main__':
    sieve_timer = Timer(solve_p10_sieve)
    print sieve_timer.timeit(1)

    print solve_p10_sieve()

<commit_msg>Adding a generator approach to p10<commit_after>import math
from timeit import Timer

def solve_p10_sieve():
    return sum(sieve_primes_up_to(2000000))

def solve_p10_sieve_w_generator():
    return sum(prime_sieve_generator(2000000))

def sieve_primes_up_to(num):
    num_list = range(num)
    num_list[1] = 0 # cross out 1, special case
    i = 2

    while num_list[i] <= int(math.sqrt(num))+1:
        cross_out_composite_multiples(num_list[i], num_list)
        i = next_non_crossed_out_index(i, num_list)

    return filter(lambda x: x != 0, num_list)

def cross_out_composite_multiples(multiple, num_list):
    for composite in xrange(multiple*2, len(num_list), multiple):
        num_list[composite] = 0

def prime_sieve_generator(limit):
    num_list = [True] * limit
    num_list[0] = num_list[1] = False
    square_root_limit = int(math.sqrt(limit))+1
    for i, isprime in enumerate(num_list):
        if isprime:
            yield i
            if i > square_root_limit:
               continue #optimization, composites already marked
            for composite in xrange(i*2, len(num_list), i):
                num_list[composite] = False

def next_non_crossed_out_index(i, num_list):
    increment_index = True
    while increment_index:
        i += 1
        increment_index = num_list[i] == 0
    return i

if __name__ == '__main__':
    sieve_timer = Timer(solve_p10_sieve)
    sieve_generator_timer = Timer(solve_p10_sieve_w_generator)
    print "Filter approach: {0}".format(sieve_timer.timeit(1))
    print "Generator approach: {0}".format(sieve_generator_timer.timeit(1))

    print solve_p10_sieve()
    print solve_p10_sieve_w_generator()

<|endoftext|>


<commit_before>import requests

def pasteEE(key, data, description, expire):
    values = {"key": key,
              "description": description,
              "paste": data,
              "expiration": expire,
              "format": "json"}
    result = requests.post("http://paste.ee/api", data=values, timeout=5)
    result.close()
    if result:
        jsonResult = result.json()
        if jsonResult["status"] == "success":
            return jsonResult["paste"]["link"]
        elif jsonResult["status"] == "error":
            return "An error occurred while posting to Paste.ee, code: {}, reason: {}".format(jsonResult["errorcode"],
                                                                                jsonResult["error"])
<commit_msg>[Webutils] Let's try another thing?<commit_after>import requests

def pasteEE(key, data, description, expire):
    values = {"key": key,
              "description": description,
              "paste": data,
              "expiration": expire,
              "format": "json"}
    result = requests.post("http://paste.ee/api", data=values, timeout=3, headers={"Connection": "close"})
    if result:
        jsonResult = result.json()
        if jsonResult["status"] == "success":
            return jsonResult["paste"]["link"]
        elif jsonResult["status"] == "error":
            return "An error occurred while posting to Paste.ee, code: {}, reason: {}".format(jsonResult["errorcode"],
                                                                                jsonResult["error"])
<|endoftext|>


<commit_before>"""
HOW TO USE THIS FILE:

This text should be green if you are reading it using IDLE, the Python IDE.
It's green because it's a "comment" added to the program. The computer totally
ignores it. Most of the green text in this file is a note left to help you
understand what's going on.

Look below for the line that says "class QuadcopterAltHold": in it you will
find two functions definitions starting with the word "def". Those two
functions are __init__ and _update. The __init__ function is run one time at
the beginning of the program. The _update function is run 10 times per second
to set the current state of the quadcopter.

Your mission, should you choose to accept it, is to add code to the _update
function to set up a flight path for the quadcopter. Your copter will fly for
10 seconds each time this program is run.

"""

# Red text is a comment too, but usually shorter

###################################################
# CRITICAL STEP!! SET YOUR QUADCOPTER NUMBER HERE #
QUADCOPTER_NUMBER = 5
#                                                 #
# The quadcopter number is printed on the bottom  #
# of each quadcopter, and is a number between 1   #
# and 10 (point the nose up to differentiate six  #
# and nine).                                      #
###################################################

# Don't change this unless you reset the firmware on the quadcopters too
BANDWIDTH = "2M" # others are "250K" and "1M"

# Leave these "import" lines alone, you need them
import sys
sys.path.append("lib")

import time
from threading import Thread

# If this import is failing, you are missing the "lib" directory containing the Crazyflie SDK.
# You can download the SDK from https://github.com/bitcraze/crazyflie-clients-python --
# if you are unfamiliar with Github, find the "Download ZIP" option on the right side of the
# screen. Unpack it, and copy the whole "lib" folder to wherever you have put this file.
import cflib.crtp
from cflib.crazyflie import Crazyflie

import logging
logging.basicConfig(level=logging.ERROR)

""" Map from quadcopter number to radio channel. There is no magic here, we just
    manually programmed each quadcopter to use these channels. """
channelForGroup = [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50]

class QuadcopterAltHold:
    def __init__(self, cf):
        self._roll = 0
        self._pitch = 0
        self._yawRate = 0
        self._altitude = 32767
        self._time = 0.0

    def updateQuadcopter (self, cf):
        cf.param.set_value("flightmode.althold","True")
        cf.commander.send_setpoint (self._roll,
                                    self._pitch,
                                    self._yawRate,
                                    self._altitude)

    def setRoll (self, roll):
        if (roll < -45):
            self._roll = -45
        elif (roll > 45):
            self._roll = 45
        else:
            self._roll = roll

    def setPitch (self, pitch):
        if (pitch < -45):
            self._pitch = -45
        elif (pitch > 45):
            self._pitch = 45
        else:
            self._pitch = pitch

    def setYawRate (self, yawRate):
        self._yawRate = yawRate

    def holdAltitude (self):
        self._altitude = 32767

    def shutdown (self, cf):
        cf.param.set_value("flightmode.althold","False")
        cf.commander.send_setpoint (0,0,0,0)
        cf.close_link()

    """
    NOTE: the multiplication by 8000 is arbitrary, to convert the units
          to something approximating meters per second. It is by no means
          exact, but saying increaseAltitude(1) should make the quadcopter
          go up by about 1 m/s.
    """

    def increaseAltitude (self, altitude):
        self._altitude = 32767 + (altitude*8000)

    def decreaseAltitude (self, altitude):
        self._altitude = 32767 - (altitude*8000)

    """
    EDIT THIS FUNCTION

          |
          |
          |
          |
       \  |  /
        \ | /
         \|/
          V
    A function that is called 10 times per second """
    def _update (self, cf):
        self._time += 0.1

        # Other functions you can use:
        # self.setPitch ( X ) where X is in degrees
        # self.setRoll ( X )
        # self.setYawRate ( X )

        if self._time < 1.0:
            self.increaseAltitude(1) # Go up at 1 meter per second
        elif self._time < 2.0:
            self.holdAltitude() # Hold the current altitude
        elif self._time < 4.0:
            self.decreaseAltitude(0.5) # Land at about 1/2 meter per second
        else:
            self.shutdown(cf) # Shut down
            return

        self.updateQuadcopter(cf)

""" This is the older version of the code: it allows direct control of the
quadcopter, with no altitude hold. """
class Quadcopter:

    """ A function that is run once at the beginning """
    def __init__(self, cf):
        print "Setting everything to zero..."
        cf.commander.send_setpoint(0, 0, 0, 0)
        self._updateCallCounter = 0

    def _update (self, cf):
        self._updateCallCounter += 1

        # An example program: you can change the numbers, and/or add more "elif" statements
        if (self._updateCallCounter < 10):
            print "Taking off"
            # Format is send_setpoint (Roll, Pitch, Yaw, Throttle)
            cf.commander.send_setpoint (0,0,0,30000)
        elif (self._updateCallCounter < 11):
            print "Hovering"
            # Spin in place for 2 seconds
            cf.commander.send_setpoint (0,0,200,30000)
        elif (self._updateCallCounter < 12):
            print "Landing"
            cf.commander.send_setpoint (0,0,0,30000)
        else:
            print "Off"
            cf.commander.send_setpoint(0, 0, 0, 0)

class Controller:
    """Example that connects to a Crazyflie and ramps the motors up/down and
    the disconnects"""
    def __init__(self, link_uri):
        """ Initialize and run the example with the specified link_uri """

        self._cf = Crazyflie()

        self._cf.connected.add_callback(self._connected)
        self._cf.disconnected.add_callback(self._disconnected)
        self._cf.connection_failed.add_callback(self._connection_failed)
        self._cf.connection_lost.add_callback(self._connection_lost)

        self._cf.open_link(link_uri)

        print "Connecting to %s" % link_uri

    def _connected(self, link_uri):
        """ This callback is called form the Crazyflie API when a Crazyflie
        has been connected and the TOCs have been downloaded."""

        print "Connected."
        #self._quadcopter = Quadcopter (self._cf) # The older, harder-to-use code
        self._quadcopter = QuadcopterAltHold (self._cf) # Altitude hold mode!
        Thread(target=self._run).start()

    def _connection_failed(self, link_uri, msg):
        """Callback when connection initial connection fails (i.e no Crazyflie
        at the specified address)"""
        print "Connection to %s failed: %s" % (link_uri, msg)

    def _connection_lost(self, link_uri, msg):
        """Callback when disconnected after a connection has been made (i.e
        Crazyflie moves out of range)"""
        print "Connection to %s lost: %s" % (link_uri, msg)

    def _disconnected(self, link_uri):
        """Callback when the Crazyflie is disconnected (called in all cases)"""
        print "Disconnected from %s" % link_uri

    def _quality_updated(self, quality):
        if quality < 30:
            print "Low link quality: %s", quality

    def _run(self):
        print "Running..."
        current_time = 0
        self._cf.commander.send_setpoint (0,0,0,0);
        while current_time <= 5:
            print current_time
            self._quadcopter._update(self._cf)
            time.sleep(0.1)
            current_time += 0.1
        self._cf.close_link()
        print "Done."

if __name__ == '__main__':
    # Initialize the low-level drivers (don't list the debug drivers)
    cflib.crtp.init_drivers(enable_debug_driver=False)
    print "Scanning interfaces for Crazyflies..."
    available = cflib.crtp.scan_interfaces()
    print "Crazyflies found:"
    for i in available:
        print i[0]

    print "For quadcopter #%s, trying to connection to a radio on channel %s" % (QUADCOPTER_NUMBER, channelForGroup[QUADCOPTER_NUMBER])
    interface = "radio://0/" + `channelForGroup[QUADCOPTER_NUMBER]` + "/" + BANDWIDTH
    print "That means interface %s" % (interface)
    le = Controller (interface)
<commit_msg>Updated to the latest Crazyflie library, and to python 3.<commit_after>"""
HOW TO USE THIS FILE:

This text should be green if you are reading it using IDLE, the Python IDE.
It's green because it's a "comment" added to the program. The computer totally
ignores it. Most of the green text in this file is a note left to help you
understand what's going on.

Look below for the line that says "class Quadcopter": in it you will
find two functions definitions starting with the word "def". Those two
functions are __init__ and _update. The __init__ function is run one time at
the beginning of the program. The _update function is run 10 times per second
to set the current state of the quadcopter.

Your mission, should you choose to accept it, is to add code to the _update
function to set up a flight path for the quadcopter. Your copter will fly for
10 seconds each time this program is run.

"""

# Red text is a comment too, but usually shorter

###################################################
# CRITICAL STEP!! SET YOUR QUADCOPTER NUMBER HERE #
QUADCOPTER_NUMBER = 5
#                                                 #
# The quadcopter number is printed on the bottom  #
# of each quadcopter, and is a number between 1   #
# and 10 (point the nose up to differentiate six  #
# and nine).                                      #
###################################################

# Don't change this unless you reset the firmware on the quadcopters too
BANDWIDTH = "2M" # others are "250K" and "1M"

# Leave these "import" lines alone, you need them
import sys

import time
from threading import Thread

# If this import is failing, you are missing the "crazyflie-lib-python/cflib" directory
# containing the Crazyflie SDK.
# You can clone the SDK from https://github.com/bitcraze/crazyflie-lib-python
sys.path.append("crazyflie-lib-python/")
import cflib.crtp
from cflib.crazyflie import Crazyflie

import logging
logging.basicConfig(level=logging.ERROR)

""" Map from quadcopter number to radio channel. There is no magic here, we just
    manually programmed each quadcopter to use these channels. """
channelForGroup = [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50]

class Quadcopter:

    """ A function that is run once at the beginning """
    def __init__(self, cf):
        print ("Setting everything to zero...")
        cf.commander.send_setpoint(0, 0, 0, 0)
        self._time= 0

    def _update (self, cf):

        # An example program: you can change the numbers, and/or add more "elif" statements
        if (self._time < 0.7):
            # Format is send_setpoint (Roll, Pitch, Yaw rate, Throttle)
            cf.commander.send_setpoint (0,0,0,30000)
        elif (self._time < 1.5):
            # Spin in place (yaw rate = 200)
            cf.commander.send_setpoint (0,0,200,30000)
        elif (self._updateCallCounter < 2.0):
	    # Land (sort of)
            cf.commander.send_setpoint (0,0,0,25000)
        else:
	    # Shut it all down
            cf.commander.send_setpoint(0, 0, 0, 0)

        self._time += 0.1

class Controller:
    """Example that connects to a Crazyflie and ramps the motors up/down and
    the disconnects"""
    def __init__(self, link_uri):
        """ Initialize and run the example with the specified link_uri """

        self._cf = Crazyflie()

        self._cf.connected.add_callback(self._connected)
        self._cf.disconnected.add_callback(self._disconnected)
        self._cf.connection_failed.add_callback(self._connection_failed)
        self._cf.connection_lost.add_callback(self._connection_lost)

        self._cf.open_link(link_uri)

        print ("Connecting to %s" % link_uri)

    def _connected(self, link_uri):
        """ This callback is called form the Crazyflie API when a Crazyflie
        has been connected and the TOCs have been downloaded."""

        print ("Connected.")
        self._quadcopter = Quadcopter (self._cf)
        Thread(target=self._run).start()

    def _connection_failed(self, link_uri, msg):
        """Callback when connection initial connection fails (i.e no Crazyflie
        at the specified address)"""
        print ("Connection to %s failed: %s" % (link_uri, msg))

    def _connection_lost(self, link_uri, msg):
        """Callback when disconnected after a connection has been made (i.e
        Crazyflie moves out of range)"""
        print ("Connection to %s lost: %s" % (link_uri, msg))

    def _disconnected(self, link_uri):
        """Callback when the Crazyflie is disconnected (called in all cases)"""
        print ("Disconnected from %s" % link_uri)

    def _quality_updated(self, quality):
        if quality < 30:
            print ("Low link quality: %s", quality)

    def _run(self):
        print ("Running...")
        current_time = 0
        self._cf.commander.send_setpoint (0,0,0,0);
        while current_time <= 5:
            print (current_time)
            self._quadcopter._update(self._cf)
            time.sleep(0.1)
            current_time += 0.1
        self._cf.close_link()
        print ("Done.")

if __name__ == '__main__':
    # Initialize the low-level drivers (don't list the debug drivers)
    cflib.crtp.init_drivers(enable_debug_driver=False)
    print ("Scanning interfaces for Crazyflies...")
    available = cflib.crtp.scan_interfaces()
    print ("Crazyflies found:")
    for i in available:
        print (i[0])

    print ("For quadcopter #%s, trying to connection to a radio on channel %s" % (QUADCOPTER_NUMBER, channelForGroup[QUADCOPTER_NUMBER]))
    interface = "radio://0/" + str(channelForGroup[QUADCOPTER_NUMBER]) + "/" + str(BANDWIDTH)
    print ("That means interface %s" % (interface))
    le = Controller (interface)
<|endoftext|>


<commit_before>from __future__ import print_function
from .param import Param, Parameterized
from scipy.optimize import minimize
import numpy as np
import tensorflow as tf
import hmc
import sys

class ObjectiveWrapper(object):
    """
    A simple class to wrap the objective function in order to make it more robust.

    The previosly seen state is cached so that we can easily acess it if the
    model crashes.
    """
    _previous_x = None
    def __init__(self, objective):
        self._objective = objective
    def __call__(self, x):
        f, g = self._objective(x)
        g_is_fin = np.isfinite(g)
        if np.all(g_is_fin):
            self._previous_x = x # store the last known good value
            return f, g
        else:
            print("Warning: inf or nan in gradient: replacing with zeros")
            return f, np.where(g_is_fin, g, 0.)

class Model(Parameterized):
    """
    The Model base class.

    To use this class, inherriting classes must define the method

    >>>     build_likelihood(self)

    which returns a tensorflow representation of the model likelihood.

    Param and Parameterized objects that are children of the model can be used
    in the tensorflow expression. Children on the model are defined by simply
    doing:

    >>> m = Model()
    >>> p = Param(1.0)
    >>> m.p = p

    At compile time (i.e. when build_likelihood is called), the `Param` object
    becomes a tensorflow variable.

    The result of build_likelihood() is added to the prior (see Parameterized
    class) and the resulting objective and gradients are compiled into
    self._objective.

    This object has a `_needs_recompile` switch. When any of the child nodes
    change, this object is notified and on optimization (or mcmc) the
    likelihood is recompiled. This allows fixing and constraining parameters,
    but only recompiling lazily.

    This object has a `_free_vars` tensorflow array. This array is ised to build
    the tensorflow representations of the Param objects during `make_tf_array`.

    This object defines `optimize` and `sample` to allow for model fitting.
    """
    def __init__(self, name='model'):
        """
        name is a string describing this model.
        """
        Parameterized.__init__(self)
        self._name = name
        self._needs_recompile = True
        self._free_vars = tf.placeholder('float64', name='free_vars')
        self._session = tf.Session()

    @property
    def name(self):
        return self._name
    def _compile(self):
        """
        compile the tensorflow function "self._objective"
        """
        self.make_tf_array(self._free_vars)
        with self.tf_mode():
            f = self.build_likelihood() + self.build_prior()
            g, = tf.gradients(f, self._free_vars)

        minusF = tf.neg( f, name = 'objective' )
        minusG = tf.neg( g, name = 'grad_objective' )
        #initialize variables. I confess I don;t understand what this does - JH
        init = tf.initialize_all_variables()
        self._session.run(init)

        #build tensorflow functions for computing the likelihood and predictions
        print("compiling tensorflow function...")
        sys.stdout.flush()
        def obj(x):
            return self._session.run([minusF,minusG], feed_dict={self._free_vars: x})
        self._objective = obj
        print("done")
        sys.stdout.flush()
        self._needs_recompile = False

    def sample(self, num_samples, Lmax=20, epsilon=0.01, verbose=False):
        """
        use hybrid Monte Carlo to draw samples from the posterior of the model.
        """
        if self._needs_recompile:
            self._compile()
        return hmc.sample_HMC(self._objective, num_samples, Lmax, epsilon, x0=self.get_free_state(), verbose=verbose)

    def optimize(self, method='L-BFGS-B', callback=None, max_iters=1000, **kw):
        """
        Optimize the model to find the maximum likelihood  or MAP point. Here
        we wrap `scipy.optimize.minimize`, any keyword arguments are passed
        through as `options`.

        KeyboardInterrupts are caught and the model is set to the most recent
        value tried by the optimization routine.

        LinAlgErrors are caught and the optimizer is 'coaxed' away from that
        region by returning a low likelihood value.

        This method returns the results of the call to optimize.minimize.
        """
        if self._needs_recompile:
            self._compile()

        options=dict(display=True, max_iters=max_iters)
        options.update(kw)

        #LBFGS-B hcks. the options are different, annoyingly.
        if method  == 'L-BFGS-B':
            options['maxiter'] = max_iters
            del options['max_iters']
            options['disp'] = options['display']
            del options['display']

        #here's the actual cll to minimize. cathc keyboard errors as harmless.
        obj = ObjectiveWrapper(self._objective)
        try:
            result = minimize(fun=obj,
                        x0=self.get_free_state(),
                        method=method,
                        jac=True, # self._objective returns the objective and the jacobian.
                        tol=None, # TODO: tol??
                        callback=callback,
                        options=options)
        except (KeyboardInterrupt): # pragma: no cover
            print("Caught KeyboardInterrupt, setting model with most recent state.")
            self.set_state(obj._previous_x)
            return None

        print("optimization terminated, setting model state")
        self.set_state(result.x)
        return result

class GPModel(Model):
    """
    A base class for Gaussian process models, that is, those of the form

       theta ~ p(theta)
       f ~ GP(m(x), k(x, x'; theta))
       F = f(X)
       Y|F ~ p(Y|F)

    This class mostly adds functionality to compile predictions. To use it,
    inherriting classes must define a build_predict function, which computes
    the means and variances of the latent function. This gets compiled
    similarly to build_likelihood in the Model class.

    These predictions are then pushed through the likelihood to obtain means
    and variances of held out data, self.predict_y.

    The predictions can also be used to compute the (log) density of held-out
    data via self.predict_density.
    """
    def __init__(self, X, Y, kern, likelihood, mean_function, name='model'):
        self.X, self.Y, self.kern, self.likelihood, self.mean_function = X, Y, kern, likelihood, mean_function
        Model.__init__(self, name)

    def build_predict(self):
        raise NotImplementedError

    def predict_f(self, Xnew):
        """
        Compute the mean and variance of the latent function(s) at the points Xnew
        """
        self._compile()
        tf_Xnew = tf.placeholder('float64')
        with self.tf_mode():
            pred_f_mean, pred_f_var = self.build_predict(tf_Xnew)
        return self._session.run([pred_f_mean, pred_f_var],
                                 feed_dict={self._free_vars:self.get_free_state(), tf_Xnew:Xnew })

    def predict_y(self, Xnew):
        """
        Compute the mean and variance of held-out data at the points Xnew
        """
        tf_Xnew = tf.placeholder('float64')
        with self.tf_mode():
            pred_f_mean, pred_f_var = self.build_predict(tf_Xnew)
            pred_y_mean, pred_y_var = self.likelihood.predict_mean_and_var(pred_f_mean, pred_f_var)
        return self._session.run([pred_y_mean, pred_y_var],
                                 feed_dict={self._free_vars:self.get_free_state(), tf_Xnew:Xnew })

    def predict_density(self, Xnew, Ynew):
        """
        Compute the (log) density of the data Ynew at the points Xnew

        Note that this computes the log denisty of the data individually,
        ignoring correlations between them. The result is a matrix the same
        shape as Ynew containing the log densities.
        """
        tf_Xnew = tf.placeholder('float64')
        tf_Ynew = tf.placeholder('float64')
        with self.tf_mode():
            pred_f_mean, pred_f_var = self.build_predict(tf_Xnew)
            pred_y_density = self.likelihood.predict_density(pred_f_mean, pred_f_var, tf_Ynew)
        return self._session.run(pred_y_density,
                                 feed_dict={self._free_vars:self.get_free_state(),
                                            tf_Xnew:Xnew,
                                            tf_Ynew:Ynew })

<commit_msg>implemented AutoFlow<commit_after>from __future__ import print_function
from .param import Param, Parameterized
from scipy.optimize import minimize
import numpy as np
import tensorflow as tf
import hmc
import sys

class ObjectiveWrapper(object):
    """
    A simple class to wrap the objective function in order to make it more robust.

    The previosly seen state is cached so that we can easily acess it if the
    model crashes.
    """
    _previous_x = None
    def __init__(self, objective):
        self._objective = objective
    def __call__(self, x):
        f, g = self._objective(x)
        g_is_fin = np.isfinite(g)
        if np.all(g_is_fin):
            self._previous_x = x # store the last known good value
            return f, g
        else:
            print("Warning: inf or nan in gradient: replacing with zeros")
            return f, np.where(g_is_fin, g, 0.)

from functools import wraps
class AutoFlow:
    """
    This decorator-class is designed for use on methods of the Model class (below).

    The idea is that methods that compute relevant quantities of the model
    (such as predictions) can define a tf graph which we automatically run when
    the (decorated) function is called.

    The syntax looks like:

    >>> class MyModel(Model):
    >>>
    >>>   @AutoFlow(tf.placeholder(tf.float64), tf.placeholder(tf.float64))
    >>>   def my_method(self, x, y):
    >>>       #compute something, returning a tf graph.
    >>>       return tf.foo(self.baz, x, y)

    >>> m = MyModel()
    >>> x = np.random.randn(3,1)
    >>> y = np.random.randn(3,1)
    >>> result = my_method(x, y)

    Now the output of the method call is the _result_ of the computation, equivalent to

    >>> m = MyModel()
    >>> x = np.random.randn(3,1)
    >>> y = np.random.randn(3,1)
    >>> x_tf = tf.placeholder(tf.float64)
    >>> y_tf = tf.placeholder(tf.float64)
    >>> with m.tf_mode():
            graph = tf.foo(m.baz, x_tf, y_tf)
    >>> result = m._session.run(graph, feed_dict={x_tf:x, y_tf:y, m._free_vars:m.get_free_state()})

    Not only is the syntax cleaner, but multiple calls to the method will
    result in the graph being constructed only once.

    """
    def __init__(self, *tf_args):
        self.tf_args = tf_args
    def __call__(self, tf_method):
        @wraps(tf_method)
        def runnable(instance, *np_args):
            graph_name = '_' + tf_method.__name__ + '_graph'
            if not hasattr(instance, graph_name):
                with instance.tf_mode():
                    setattr(instance, graph_name, tf_method(instance, *self.tf_args))
            feed_dict = dict(zip(self.tf_args, np_args))
            feed_dict[instance._free_vars] = instance.get_free_state()
            graph = getattr(instance, graph_name)
            return instance._session.run(graph, feed_dict=feed_dict)
        return runnable

class Model(Parameterized):
    """
    The Model base class.

    To use this class, inherriting classes must define the method

    >>>     build_likelihood(self)

    which returns a tensorflow representation of the model likelihood.

    Param and Parameterized objects that are children of the model can be used
    in the tensorflow expression. Children on the model are defined by simply
    doing:

    >>> m = Model()
    >>> p = Param(1.0)
    >>> m.p = p

    At compile time (i.e. when build_likelihood is called), the `Param` object
    becomes a tensorflow variable.

    The result of build_likelihood() is added to the prior (see Parameterized
    class) and the resulting objective and gradients are compiled into
    self._objective.

    This object has a `_needs_recompile` switch. When any of the child nodes
    change, this object is notified and on optimization (or mcmc) the
    likelihood is recompiled. This allows fixing and constraining parameters,
    but only recompiling lazily.

    This object has a `_free_vars` tensorflow array. This array is ised to build
    the tensorflow representations of the Param objects during `make_tf_array`.

    This object defines `optimize` and `sample` to allow for model fitting.
    """
    def __init__(self, name='model'):
        """
        name is a string describing this model.
        """
        Parameterized.__init__(self)
        self._name = name
        self._needs_recompile = True
        self._free_vars = tf.placeholder('float64', name='free_vars')
        self._session = tf.Session()

    @property
    def name(self):
        return self._name
    def _compile(self):
        """
        compile the tensorflow function "self._objective"
        """
        self.make_tf_array(self._free_vars)
        with self.tf_mode():
            f = self.build_likelihood() + self.build_prior()
            g, = tf.gradients(f, self._free_vars)

        minusF = tf.neg( f, name = 'objective' )
        minusG = tf.neg( g, name = 'grad_objective' )
        #initialize variables. I confess I don;t understand what this does - JH
        init = tf.initialize_all_variables()
        self._session.run(init)

        #build tensorflow functions for computing the likelihood and predictions
        print("compiling tensorflow function...")
        sys.stdout.flush()
        def obj(x):
            return self._session.run([minusF,minusG], feed_dict={self._free_vars: x})
        self._objective = obj
        print("done")
        sys.stdout.flush()
        self._needs_recompile = False

    def sample(self, num_samples, Lmax=20, epsilon=0.01, verbose=False):
        """
        use hybrid Monte Carlo to draw samples from the posterior of the model.
        """
        if self._needs_recompile:
            self._compile()
        return hmc.sample_HMC(self._objective, num_samples, Lmax, epsilon, x0=self.get_free_state(), verbose=verbose)

    def optimize(self, method='L-BFGS-B', callback=None, max_iters=1000, **kw):
        """
        Optimize the model to find the maximum likelihood  or MAP point. Here
        we wrap `scipy.optimize.minimize`, any keyword arguments are passed
        through as `options`.

        KeyboardInterrupts are caught and the model is set to the most recent
        value tried by the optimization routine.

        LinAlgErrors are caught and the optimizer is 'coaxed' away from that
        region by returning a low likelihood value.

        This method returns the results of the call to optimize.minimize.
        """
        if self._needs_recompile:
            self._compile()

        options=dict(display=True, max_iters=max_iters)
        options.update(kw)

        #LBFGS-B hcks. the options are different, annoyingly.
        if method  == 'L-BFGS-B':
            options['maxiter'] = max_iters
            del options['max_iters']
            options['disp'] = options['display']
            del options['display']

        #here's the actual cll to minimize. cathc keyboard errors as harmless.
        obj = ObjectiveWrapper(self._objective)
        try:
            result = minimize(fun=obj,
                        x0=self.get_free_state(),
                        method=method,
                        jac=True, # self._objective returns the objective and the jacobian.
                        tol=None, # TODO: tol??
                        callback=callback,
                        options=options)
        except (KeyboardInterrupt): # pragma: no cover
            print("Caught KeyboardInterrupt, setting model with most recent state.")
            self.set_state(obj._previous_x)
            return None

        print("optimization terminated, setting model state")
        self.set_state(result.x)
        return result

class GPModel(Model):
    """
    A base class for Gaussian process models, that is, those of the form

       theta ~ p(theta)
       f ~ GP(m(x), k(x, x'; theta))
       F = f(X)
       Y|F ~ p(Y|F)

    This class mostly adds functionality to compile predictions. To use it,
    inherriting classes must define a build_predict function, which computes
    the means and variances of the latent function. This gets compiled
    similarly to build_likelihood in the Model class.

    These predictions are then pushed through the likelihood to obtain means
    and variances of held out data, self.predict_y.

    The predictions can also be used to compute the (log) density of held-out
    data via self.predict_density.
    """
    def __init__(self, X, Y, kern, likelihood, mean_function, name='model'):
        self.X, self.Y, self.kern, self.likelihood, self.mean_function = X, Y, kern, likelihood, mean_function
        Model.__init__(self, name)

    def build_predict(self):
        raise NotImplementedError

    @AutoFlow(tf.placeholder(tf.float64))
    def predict_f(self, Xnew):
        """
        Compute the mean and variance of the latent function(s) at the points Xnew
        """
        return self.build_predict(Xnew)

    @AutoFlow(tf.placeholder(tf.float64))
    def predict_y(self, Xnew):
        """
        Compute the mean and variance of held-out data at the points Xnew
        """
        pred_f_mean, pred_f_var = self.build_predict(Xnew)
        return self.likelihood.predict_mean_and_var(pred_f_mean, pred_f_var)

    @AutoFlow(tf.placeholder(tf.float64), tf.placeholder(tf.float64))
    def predict_density(self, Xnew, Ynew):
        """
        Compute the (log) density of the data Ynew at the points Xnew

        Note that this computes the log denisty of the data individually,
        ignoring correlations between them. The result is a matrix the same
        shape as Ynew containing the log densities.
        """
        pred_f_mean, pred_f_var = self.build_predict(Xnew)
        return self.likelihood.predict_density(pred_f_mean, pred_f_var, Ynew)

<|endoftext|>


<commit_before>""" Code for sentence tokenization: Latin
"""

__author__ = ['Patrick J. Burns <patrick@diyclassics.org>']
__license__ = 'MIT License.'

import os.path
from cltk.tokenize.sentence import BaseSentenceTokenizer, BasePunktSentenceTokenizer
from cltk.utils.file_operations import open_pickle
from nltk.tokenize.punkt import PunktLanguageVars

def SentenceTokenizer(tokenizer:str = 'punkt'):
    if tokenizer=='punkt':
        return LatinPunktSentenceTokenizer()

class LatinLanguageVars(PunktLanguageVars):
    _re_non_word_chars = PunktLanguageVars._re_non_word_chars.replace("'",'')

class LatinPunktSentenceTokenizer(BasePunktSentenceTokenizer):
    """ PunktSentenceTokenizer trained on Latin
    """
    models_path = os.path.expanduser('~/cltk_data/latin/model/latin_models_cltk/tokenizers/sentence')
    missing_models_message = "BackoffLatinLemmatizer requires the ```latin_models_cltk``` to be in cltk_data. Please load this corpus."

    def __init__(self: object, language:str = 'latin'):
        """
        :param language : language for sentence tokenization
        :type language: str
        """
        self.lang_vars = LatinLanguageVars()
        BasePunktSentenceTokenizer.__init__(self, language='latin', lang_vars=self.lang_vars)
        self.models_path = LatinPunktSentenceTokenizer.models_path

        try:
            self.model =  open_pickle(os.path.join(self.models_path, 'latin_punkt.pickle'))
        except FileNotFoundError as err:
            raise type(err)(LatinPunktSentenceTokenizer.missing_models_message)
<commit_msg>Clean up file<commit_after>""" Code for sentence tokenization: Latin
"""

__author__ = ['Patrick J. Burns <patrick@diyclassics.org>']
__license__ = 'MIT License.'

import os.path

from nltk.tokenize.punkt import PunktLanguageVars
from cltk.tokenize.sentence import BaseSentenceTokenizer, BasePunktSentenceTokenizer
from cltk.utils.file_operations import open_pickle

def SentenceTokenizer(tokenizer:str = 'punkt'):
    if tokenizer=='punkt':
        return LatinPunktSentenceTokenizer()

class LatinLanguageVars(PunktLanguageVars):
    _re_non_word_chars = PunktLanguageVars._re_non_word_chars.replace("'",'')

class LatinPunktSentenceTokenizer(BasePunktSentenceTokenizer):
    """ PunktSentenceTokenizer trained on Latin
    """
    models_path = os.path.expanduser('~/cltk_data/latin/model/latin_models_cltk/tokenizers/sentence')
    missing_models_message = "BackoffLatinLemmatizer requires the ```latin_models_cltk``` to be in cltk_data. Please load this corpus."

    def __init__(self: object, language:str = 'latin'):
        """
        :param language : language for sentence tokenization
        :type language: str
        """
        self.lang_vars = LatinLanguageVars()
        BasePunktSentenceTokenizer.__init__(self, language='latin', lang_vars=self.lang_vars)
        self.models_path = LatinPunktSentenceTokenizer.models_path

        try:
            self.model =  open_pickle(os.path.join(self.models_path, 'latin_punkt.pickle'))
        except FileNotFoundError as err:
            raise type(err)(LatinPunktSentenceTokenizer.missing_models_message)
<|endoftext|>


<commit_before>def is_palindrome(text):
    for i in xrange(len(text)//2):
        if text[i] != text[-i-1]:
            return False
    return True

def find_double_base_palindromes(limit):
    for num in xrange(1, limit):
        base2 = format(num, 'b')
        base10 = str(num)
        if is_palindrome(base2) and is_palindrome(base10):
            yield num

def solve_p036():
    return sum(find_double_base_palindromes(1000000))

if __name__ == '__main__':
    print solve_p036()

<commit_msg>simpler palindrome check<commit_after>def is_palindrome(text):
    if text == text[::-1]:
        return True

def find_double_base_palindromes(limit):
    for num in xrange(1, limit):
        base2 = format(num, 'b')
        base10 = str(num)
        if is_palindrome(base2) and is_palindrome(base10):
            yield num

def solve_p036():
    return sum(find_double_base_palindromes(1000000))

if __name__ == '__main__':
    print solve_p036()

<|endoftext|>


<commit_before>import unittest

_a = ord('a')

class Solution(object):
    def maxProduct(self, words):
        """
        :type words: List[str]
        :rtype: int
        """
        n = len(words)
        mask_and_len = [None] * n

        for i in xrange(n):
            mask = 0
            word = words[i]
            for ch in word:
                mask |= 1 << (ord(ch) - _a)
            mask_and_len[i] = (mask, len(word))

        max_product = 0
        for i in xrange(n):
            for j in xrange(i + 1, n):
                if mask_and_len[i][0] & mask_and_len[j][0] == 0:
                    product = mask_and_len[i][1] * mask_and_len[j][1]
                    if product > max_product:
                        max_product = product
        return max_product

class Test(unittest.TestCase):
    def test(self):
        self._test(['abcw', 'baz', 'foo', 'bar', 'xtfn', 'abcdef'], 16)
        self._test(['a', 'ab', 'abc', 'd', 'cd', 'bcd', 'abcd'], 4)
        self._test(['a', 'aa', 'aaa', 'aaaa'], 0)

    def _test(self, words, expected):
        actual = Solution().maxProduct(words)
        self.assertEqual(actual, expected)

if __name__ == '__main__':
    unittest.main()
<commit_msg>318. Maximum Product of Word Lengths. Improving performance<commit_after>import unittest

_a = ord('a')

class Solution(object):
    def maxProduct(self, words):
        """
        :type words: List[str]
        :rtype: int
        """
        if not words:
            return 0

        lengths = {}

        for word in words:
            mask = 0
            for ch in word:
                mask |= 1 << (ord(ch) - _a)
            if len(word) > lengths.get(mask, -1):
                lengths[mask] = len(word)

        return max(len0 * len1 if not mask0 & mask1 else 0
                   for mask1, len1 in lengths.items()
                   for mask0, len0 in lengths.items())

class Test(unittest.TestCase):
    def test(self):
        self._test(['abcw', 'baz', 'foo', 'bar', 'xtfn', 'abcdef'], 16)
        self._test(['a', 'ab', 'abc', 'd', 'cd', 'bcd', 'abcd'], 4)
        self._test(['a', 'aa', 'aaa', 'aaaa'], 0)

    def _test(self, words, expected):
        actual = Solution().maxProduct(words)
        self.assertEqual(actual, expected)

if __name__ == '__main__':
    unittest.main()
<|endoftext|>


<commit_before>#!/usr/local/bin/python
###############################################################################
#
#	ReportLab Public License Version 1.0
#
#   Except for the change of names the spirit and intention of this
#   license is the same as that of Python
#
#	(C) Copyright ReportLab Inc. 1998-2000.
#
#
# All Rights Reserved
#
# Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose and without fee is hereby granted, provided
# that the above copyright notice appear in all copies and that both that
# copyright notice and this permission notice appear in supporting
# documentation, and that the name of ReportLab not be used
# in advertising or publicity pertaining to distribution of the software
# without specific, written prior permission.
#
#
# Disclaimer
#
# ReportLab Inc. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
# SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
# IN NO EVENT SHALL ReportLab BE LIABLE FOR ANY SPECIAL, INDIRECT
# OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
# OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.
#
###############################################################################
#	$Log: idle_print.py,v $
#	Revision 1.1  2000/04/20 12:19:35  rgbecker
#	Initial interface script for idle
#
__version__=''' $Id: idle_print.py,v 1.1 2000/04/20 12:19:35 rgbecker Exp $ '''
# this is a simple script to convert a specified input file to pdf
# it should have only filename arguments; these are assumed temporary
# and will be removed unless there's a -noremove flag.
# you should adjust the globals below to configure for your system

import sys, os, py2pdf, string, time
#whether we remove input/output files; if you get trouble on windows try settin _out to 0
auto_rm_in	= 1
auto_rm_out = 1

#formatting options to be passed to the py2pdf program. see below for others
py2pdf_options = ['--format=pdf','--mode=color','--paperFormat=A4']

#how to call up your acrobat reader, '---file---' will get changed at run time
if sys.platform=='win32':
	# under win32 we can't get to a PS file so use acrord32 to come up as normal
	acrord = '"C:\\Program Files\\Adobe\\Acrobat 4.0\\Reader\\AcroRd32.exe" %s'
	sleeptime = 15
else:
	# change the /dev/lp to whatever the postcript should be sent to
	acrord = '/opt/Acrobat4/bin/acroread -toPostScript -pairs %s /dev/lp'
	sleeptime = 0

for f in sys.argv[1:]:
	py2pdf.main(py2pdf_options + [f])
	pdfname = os.path.splitext(f)[0]+'.pdf'
	os.system(acrord % pdfname)
	if auto_rm_in: os.remove(f)
	if auto_rm_out:
		# we try to clean up; on windows since the process detaches the script continues
		# and we need to delay before attempting to delete the files
		while 1:
			time.sleep(sleeptime)
			try:
				os.remove(pdfname)
				break
			except OSError:
				pass

#########################################################################
# the script ends here the stuff below is for info only
#########################################################################
#
#    py2pdf options:
#     -h  or  --help    print help (this message)
#     -                 read from stdin, write to stdout
#     --stdout          read from files, write to stdout
#     --config=<file>   read configuration options from <file>
#     --format=<format> set output format
#                         'pdf': output as PDF (default)
#                         'txt': output as tagged plain text
#     --mode:<mode>     set output mode
#                         'color': output in color (default)
#                         'mono':  output b/w
#     --paperFormat=    set paper format (ISO A, B, C series,
#       <format>        US legal, letter; default: A4)
#     --paperSize=      set paper size in points (size being a valid
#       <size>          numeric 2-tuple (x,y) w/o any whitespace)
#     --landscape       set landscape format (default: portrait)
#     --bgCol=<col>     set page background-color in hex code like
#                         '#FFA024' or '0xFFA024' for RGB components
#                         (overwrites mono mode)
#     --<cat>Col=<col>  set color of certain code categories, i.e.
#                         <cat> can be the following:
#                         'comm':  comments
#                         'ident': identifiers
#                         'kw':    keywords
#                         'strng': strings
#                         'param': parameters
#                         'rest':  all the rest
#     --fontName=<name> set base font name (default: 'Courier')
#     --fontSize=<size> set font size (default: 8)
#     --lineNum         print line numbers
#     --multiPage       generate one file per page (labeled 1, 2...)
#     -v  or  --verbose set verbose mode
<commit_msg>Changed to spawnve<commit_after>#!/usr/local/bin/python
###############################################################################
#
#	ReportLab Public License Version 1.0
#
#   Except for the change of names the spirit and intention of this
#   license is the same as that of Python
#
#	(C) Copyright ReportLab Inc. 1998-2000.
#
#
# All Rights Reserved
#
# Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose and without fee is hereby granted, provided
# that the above copyright notice appear in all copies and that both that
# copyright notice and this permission notice appear in supporting
# documentation, and that the name of ReportLab not be used
# in advertising or publicity pertaining to distribution of the software
# without specific, written prior permission.
#
#
# Disclaimer
#
# ReportLab Inc. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
# SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
# IN NO EVENT SHALL ReportLab BE LIABLE FOR ANY SPECIAL, INDIRECT
# OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
# OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.
#
###############################################################################
#	$Log: idle_print.py,v $
#	Revision 1.2  2000/04/21 13:15:22  rgbecker
#	Changed to spawnve
#
#	Revision 1.1  2000/04/20 12:19:35  rgbecker
#	Initial interface script for idle
#
__version__=''' $Id: idle_print.py,v 1.2 2000/04/21 13:15:22 rgbecker Exp $ '''
# this is a simple script to convert a specified input file to pdf
# it should have only filename arguments; these are assumed temporary
# and will be removed unless there's a -noremove flag.
# you should adjust the globals below to configure for your system

import sys, os, py2pdf, string, time
#whether we remove input/output files; if you get trouble on windows try settin _out to 0
auto_rm_in	= 1
auto_rm_out = 1

#formatting options to be passed to the py2pdf program. see below for others
py2pdf_options = ['--format=pdf','--mode=color','--paperFormat=A4']

#how to call up your acrobat reader, '---file---' will get changed at run time
if sys.platform=='win32':
	# under win32 we can't get to a PS file so use acrord32 to come up as normal
	acrord = 'C:\\Program Files\\Adobe\\Acrobat 4.0\\Reader\\AcroRd32.exe'
	argstr = '%s'
else:
	# change /opt to /usr/local if that's where you installed Acrobat4
	# change the /dev/lp to whatever the postcript should be sent to
	acrord = '/opt/Acrobat4/bin/acroread'
	argstr = '-toPostScript,-pairs,%s,/dev/lp'

for f in sys.argv[1:]:
	py2pdf.main(py2pdf_options + [f])
	if auto_rm_in: os.remove(f)
	pdfname = os.path.splitext(f)[0]+'.pdf'
	args = [acrord] + string.split(argstr%pdfname,',')
	os.spawnve(os.P_WAIT,args[0],args,os.environ)
	if auto_rm_out: os.remove(pdfname)

#########################################################################
# the script ends here the stuff below is for info only
#########################################################################
#
#    py2pdf options:
#     -h  or  --help    print help (this message)
#     -                 read from stdin, write to stdout
#     --stdout          read from files, write to stdout
#     --config=<file>   read configuration options from <file>
#     --format=<format> set output format
#                         'pdf': output as PDF (default)
#                         'txt': output as tagged plain text
#     --mode:<mode>     set output mode
#                         'color': output in color (default)
#                         'mono':  output b/w
#     --paperFormat=    set paper format (ISO A, B, C series,
#       <format>        US legal, letter; default: A4)
#     --paperSize=      set paper size in points (size being a valid
#       <size>          numeric 2-tuple (x,y) w/o any whitespace)
#     --landscape       set landscape format (default: portrait)
#     --bgCol=<col>     set page background-color in hex code like
#                         '#FFA024' or '0xFFA024' for RGB components
#                         (overwrites mono mode)
#     --<cat>Col=<col>  set color of certain code categories, i.e.
#                         <cat> can be the following:
#                         'comm':  comments
#                         'ident': identifiers
#                         'kw':    keywords
#                         'strng': strings
#                         'param': parameters
#                         'rest':  all the rest
#     --fontName=<name> set base font name (default: 'Courier')
#     --fontSize=<size> set font size (default: 8)
#     --lineNum         print line numbers
#     --multiPage       generate one file per page (labeled 1, 2...)
#     -v  or  --verbose set verbose mode
<|endoftext|>


<commit_before>import sublime
import sublime_plugin

try:
  from Terminal.Terminal import OpenTerminalCommand
except ImportError:
  sublime.error_message("Dependency import failed; please read readme for " +
    "TerminalProjectFolder plugin for installation instructions; to disable " +
    "this message remove this plugin")

class OpenTerminalInProjectFolderEnhanced(sublime_plugin.WindowCommand):
  def run(self, parameters = None):
    folders = self.window.folders()
    if len(folders) == 0:
      return

    command = OpenTerminalCommand(self.window)
    command.run(folders, parameters=parameters)<commit_msg>error notification modified<commit_after>import sublime
import sublime_plugin

try:
  from Terminal.Terminal import OpenTerminalCommand
except ImportError as error:
  sublime.error_message("Dependency import failed; please read readme for " +
    "TerminalProjectFolder plugin for installation instructions; to disable " +
    "this message remove this plugin; message: " + str(error))
  raise error

class OpenTerminalInProjectFolderEnhanced(sublime_plugin.WindowCommand):
  def run(self, parameters = None):
    folders = self.window.folders()
    if len(folders) == 0:
      return

    command = OpenTerminalCommand(self.window)
    command.run(folders, parameters=parameters)<|endoftext|>


<commit_before>import unittest

class Solution(object):
    def findMinArrowShots(self, points):
        """
        :type points: List[List[int]]
        :rtype: int
        """
        if not points:
            return 0

        points.sort()
        curr = points[0][1]
        count = 1

        for p in points:
            if p[0] > curr:
                curr = p[1]
                count += 1
            elif p[1] < curr:
                curr = p[1]

        return count

class Test(unittest.TestCase):
    def test(self):
        self._test([[10, 16], [2, 8], [1, 6], [7, 12]], 2)
        self._test([[0, 4], [1, 2], [3, 4]], 2)

    def _test(self, points, expected):
        actual = Solution().findMinArrowShots(points)
        self.assertEqual(actual, expected)

if __name__ == '__main__':
    unittest.main()
<commit_msg>452. Minimum Number of Arrows to Burst Balloons. O(nlog(n)). Interval scheduling maximization problem<commit_after>import unittest

class Solution(object):
    def findMinArrowShots(self, points):
        """
        :type points: List[List[int]]
        :rtype: int
        """
        if not points:
            return 0

        points.sort(key=lambda x: x[1])
        prev = points[0][1]
        count = 1

        for curr in points:
            if curr[0] > prev:
                count += 1
                prev = curr[1]

        return count

class Test(unittest.TestCase):
    def test(self):
        self._test([[10, 16], [2, 8], [1, 6], [7, 12]], 2)
        self._test([[0, 4], [1, 2], [3, 4]], 2)

    def _test(self, points, expected):
        actual = Solution().findMinArrowShots(points)
        self.assertEqual(actual, expected)

if __name__ == '__main__':
    unittest.main()
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
###############################################################################
#
#    Copyright (C) 2001-2014 Micronaet SRL (<http://www.micronaet.it>).
#
#    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/>.
#
###############################################################################
import os
import sys
import logging
import openerp
import xlsxwriter
import openerp.netsvc as netsvc
import openerp.addons.decimal_precision as dp
from openerp.osv import fields, osv, expression, orm
from datetime import datetime, timedelta
from dateutil.relativedelta import relativedelta
from openerp import SUPERUSER_ID, api
from openerp import tools
from openerp.tools.translate import _
from openerp.tools.float_utils import float_round as round
from openerp.tools import (DEFAULT_SERVER_DATE_FORMAT,
    DEFAULT_SERVER_DATETIME_FORMAT,
    DATETIME_FORMATS_MAP,
    float_compare)

_logger = logging.getLogger(__name__)

class ConnectorServer(orm.Model):
    ''' Model name: Connector Server
    '''
    _inherit = 'connector.server'

    def write_xls_line(self, line):
        ''' Write on instance data line passed
        '''
        col = 0
        for item in line:
            self.WS.write(self.counter, col, item )
            col += 1
        self.counter += 1
        return

    def export_xls_file(self, cr, uid, ids, context=None):
        ''' Export excel information for site product
        '''
        #current_proxy = self.browse(cr, uid, ids, context=context)[0]

        # ---------------------------------------------------------------------
        # XLS file:
        # ---------------------------------------------------------------------
        xls_file = '/home/administrator/photo/xls/connector/product.xlsx'
        _logger.warning('Start connector export: %s' % xls_file)

        WB = xlsxwriter.Workbook(xls_file)
        self.WS = WB.add_worksheet('Sito')
        self.counter = 0

        # Write header:
        self.write_xls_line([
            # TODO image #WS.insert_image('B5', 'logo.png')
            'Web',
            'Immagine',
            'Codice',
            'Nome',
            'Nome forzato',
            'Prezzo ODOO',
            'Prezzo forzato',
            'Descrizione web',
            'Descrizione forzata',
            'Cat. stat.',
            'Fornitore',
            'Magaz. - OC - camp',
            #'Esist. lorda',
            'Categorie',
            ])

        # ---------------------------------------------------------------------
        # Start export product:
        # ---------------------------------------------------------------------
        product_pool = self.pool.get('product.product')

        # Select only product in web server:
        ws_pool = self.pool.get('product.product.web.server')
        ws_ids = ws_pool.search(cr, uid, [
            ('connector_id', '=', ids[0]),
            ], context=context)
        ws_proxy = ws_pool.browse(cr, uid, ws_ids, context=context)
        product_ids = [item.product_id.id for item in ws_proxy]

        # Read album on connector from first product
        if not product_ids:
            return False
        db_context = context.copy()
        db_context['album_id'] = ws_proxy[0].connector_id.album_id.id

        for product in product_pool.browse(
                cr, uid, product_ids, context=db_context):
            # Fields:
            published = ''
            image = bool(product.product_image_context or '')
            force_name = ''
            force_description = ''
            force_price = ''
            for connector in product.web_server_ids:
                if connector.connector_id.id != ids[0]:
                    continue
                if connector.published:
                    published = 'X'
                else:
                    published = 'O'
                force_price = connector.force_price
                force_description = connector.force_description
                force_price = connector.force_price
                break

            # TODO public_categ_ ids
            public_categ_name = [
                item.name for item in product.public_categ_ids]
            try:
                campaign = product.mx_campaign_out
            except:
                campaign = 0.0
            availability = product.mx_net_qty - product.mx_oc_out - campaign

            self.write_xls_line([
                published,
                image,
                product.default_code,
                product.name,
                force_name,
                product.lst_price,
                force_price,
                product.large_description,
                force_description,
                product.statistic_category,
                product.first_supplier_id.name \
                    if product.first_supplier_id else '',
                availability,
                #product.mx_lord_qty,
                '%s' % (public_categ_name, ),
                ])

        WB.close()
        return True
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
<commit_msg>export correct category<commit_after># -*- coding: utf-8 -*-
###############################################################################
#
#    Copyright (C) 2001-2014 Micronaet SRL (<http://www.micronaet.it>).
#
#    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/>.
#
###############################################################################
import os
import sys
import logging
import openerp
import xlsxwriter
import openerp.netsvc as netsvc
import openerp.addons.decimal_precision as dp
from openerp.osv import fields, osv, expression, orm
from datetime import datetime, timedelta
from dateutil.relativedelta import relativedelta
from openerp import SUPERUSER_ID, api
from openerp import tools
from openerp.tools.translate import _
from openerp.tools.float_utils import float_round as round
from openerp.tools import (DEFAULT_SERVER_DATE_FORMAT,
    DEFAULT_SERVER_DATETIME_FORMAT,
    DATETIME_FORMATS_MAP,
    float_compare)

_logger = logging.getLogger(__name__)

class ConnectorServer(orm.Model):
    ''' Model name: Connector Server
    '''
    _inherit = 'connector.server'

    def write_xls_line(self, line):
        ''' Write on instance data line passed
        '''
        col = 0
        for item in line:
            self.WS.write(self.counter, col, item )
            col += 1
        self.counter += 1
        return

    def export_xls_file(self, cr, uid, ids, context=None):
        ''' Export excel information for site product
        '''
        #current_proxy = self.browse(cr, uid, ids, context=context)[0]

        # ---------------------------------------------------------------------
        # XLS file:
        # ---------------------------------------------------------------------
        xls_file = '/home/administrator/photo/xls/connector/product.xlsx'
        _logger.warning('Start connector export: %s' % xls_file)

        WB = xlsxwriter.Workbook(xls_file)
        self.WS = WB.add_worksheet('Sito')
        self.counter = 0

        # Write header:
        self.write_xls_line([
            # TODO image #WS.insert_image('B5', 'logo.png')
            'Web',
            'Immagine',
            'Codice',
            'Nome',
            'Nome forzato',
            'Prezzo ODOO',
            'Prezzo forzato',
            'Descrizione web',
            'Descrizione forzata',
            'Cat. stat.',
            'Fornitore',
            'Magaz. - OC - camp',
            'Categoria principale',
            ])

        # ---------------------------------------------------------------------
        # Start export product:
        # ---------------------------------------------------------------------
        product_pool = self.pool.get('product.product')

        # Select only product in web server:
        ws_pool = self.pool.get('product.product.web.server')
        ws_ids = ws_pool.search(cr, uid, [
            ('connector_id', '=', ids[0]),
            ], context=context)
        ws_proxy = ws_pool.browse(cr, uid, ws_ids, context=context)
        product_ids = [item.product_id.id for item in ws_proxy]

        # Read album on connector from first product
        if not product_ids:
            return False
        db_context = context.copy()
        db_context['album_id'] = ws_proxy[0].connector_id.album_id.id

        for product in product_pool.browse(
                cr, uid, product_ids, context=db_context):
            # Fields:
            published = ''
            image = bool(product.product_image_context or '')
            force_name = ''
            force_description = ''
            force_price = ''
            public_categ_name = ''
            for connector in product.web_server_ids:
                if connector.connector_id.id != ids[0]:
                    continue
                if connector.published:
                    published = 'X'
                else:
                    published = 'O'
                force_price = connector.force_price
                force_description = connector.force_description
                force_price = connector.force_price
                public_categ_name = connector.public_categ_id.name if \
                    connector.public_categ_id else ''
                break

            # TODO public_categ_ ids
            #public_categ_name = [
            #    item.name for item in product.public_categ_ids]
            try:
                campaign = product.mx_campaign_out
            except:
                campaign = 0.0
            availability = product.mx_net_qty - product.mx_oc_out - campaign

            self.write_xls_line([
                published,
                image,
                product.default_code,
                product.name,
                force_name,
                product.lst_price,
                force_price,
                product.large_description,
                force_description,
                product.statistic_category,
                product.first_supplier_id.name \
                    if product.first_supplier_id else '',
                availability,
                public_categ_name,
                ])

        WB.close()
        return True
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
<|endoftext|>


<commit_before>from django import http
from django import shortcuts
from django import template
from django.contrib import admin

from cms.models import pluginmodel

from cmsplugin_markup.utils import markup

from django.utils.translation import ugettext_lazy as _
from cms.plugin_base import CMSPluginBase
from cms.plugin_pool import plugin_pool

from cmsplugin_markup import utils
from cmsplugin_markup.forms import MarkupForm
from cmsplugin_markup.models import MarkupField
from cmsplugin_markup.templatetags import markuptags

class MarkupPlugin(CMSPluginBase):
    model = MarkupField
    name = _('Markup')
    form = MarkupForm
    render_template = 'cmsplugin_markup/markup.html'
    change_form_template = 'cmsplugin_markup/markup_plugin_change_form.html'

    fieldsets = (
        (None, {
            'fields': ('body', 'markup')
        }),
        (_('Advanced options'), {
            'classes': ('collapse',),
            'fields': ('dynamic', 'css_class')
        }),
    )

    def render(self, context, instance, placeholder):
        context.update({
            'object': instance,
            'placeholder': placeholder,
        })
        return context

    def save_model(self, request, obj, form, change):
        obj.clean_plugins()
        super(MarkupPlugin, self).save_model(request, obj, form, change)

    def change_view(self, request, object_id, extra_context=None):
        if extra_context is None:
            extra_context = {}

        extra_context.update({
            'text_plugins': [p() for p in plugin_pool.get_text_enabled_plugins(self.placeholder, self.page)],
            'name': 'markupeditor',
            'used_plugins': pluginmodel.CMSPlugin.objects.filter(parent=object_id),
            'markup_plugins': [c() for c in utils.get_list_of_markup_classes().values()],
        })
        return super(MarkupPlugin, self).change_view(request, object_id, extra_context=extra_context)

    def add_view(self, request, form_url='', extra_context=None):
        if extra_context is None:
            extra_context = {}

        extra_context.update({
            'text_plugins': [p() for p in plugin_pool.get_text_enabled_plugins(self.placeholder, self.page)],
            'name': 'markupeditor',
            'markup_plugins': [c() for c in utils.get_list_of_markup_classes().values()],
        })
        return super(MarkupPlugin, self).add_view(request, form_url, extra_context=extra_context);

    def get_plugin_urls(self):
        from django.conf.urls import patterns, url

        # If django-cms has get_plugin_urls feature or not
        urls = getattr(super(MarkupPlugin, self), 'get_plugin_urls', lambda: [])()
        preview_urls = patterns('',
            url(r'^preview/$', admin.site.admin_view(self.preview), name='cmsplugin_markup_preview'),
        )

        plugin_urls = []
        for c in utils.get_list_of_markup_classes().values():
            plugin_urls.extend(c().get_plugin_urls())

        return preview_urls + plugin_urls + urls

    def preview(self, request):
        if request.method != 'POST':
            return http.HttpResponseNotAllowed(['POST'])

        try:
            plugin = MarkupField.objects.get(pk=request.POST.get('plugin_id'))
        except MarkupField.DoesNotExist:
            plugin = shortcuts.get_object_or_404(pluginmodel.CMSPlugin, pk=request.POST.get('plugin_id'))

        placeholder = plugin.placeholder

        if not placeholder.has_change_permission(request):
            raise http.Http404

        if not request.POST.get('markup'):
            return http.HttpResponse('')

        (content, parser) = markup.markup_parser(request.POST.get('text'), request.POST.get('markup'), template.RequestContext(request, {
                'object': plugin,
                'placeholder': placeholder,
            }), placeholder)

        content += markuptags.content_scripts(parser.get_scripts()) + markuptags.content_stylesheets(parser.get_stylesheets())

        return http.HttpResponse(content)

plugin_pool.register_plugin(MarkupPlugin)
<commit_msg>deactivate broken edit form<commit_after>from django import http
from django import shortcuts
from django import template
from django.contrib import admin

from cms.models import pluginmodel

from cmsplugin_markup.utils import markup

from django.utils.translation import ugettext_lazy as _
from cms.plugin_base import CMSPluginBase
from cms.plugin_pool import plugin_pool

from cmsplugin_markup import utils
from cmsplugin_markup.forms import MarkupForm
from cmsplugin_markup.models import MarkupField
from cmsplugin_markup.templatetags import markuptags

class MarkupPlugin(CMSPluginBase):
    model = MarkupField
    name = _('Markup')
    form = MarkupForm
    render_template = 'cmsplugin_markup/markup.html'
    # change_form_template = 'cmsplugin_markup/markup_plugin_change_form.html'

    fieldsets = (
        (None, {
            'fields': ('body', 'markup')
        }),
        (_('Advanced options'), {
            'classes': ('collapse',),
            'fields': ('dynamic', 'css_class')
        }),
    )

    def render(self, context, instance, placeholder):
        context.update({
            'object': instance,
            'placeholder': placeholder,
        })
        return context

    def save_model(self, request, obj, form, change):
        obj.clean_plugins()
        super(MarkupPlugin, self).save_model(request, obj, form, change)

    def change_view(self, request, object_id, extra_context=None):
        if extra_context is None:
            extra_context = {}

        extra_context.update({
            'text_plugins': [p() for p in plugin_pool.get_text_enabled_plugins(self.placeholder, self.page)],
            'name': 'markupeditor',
            'used_plugins': pluginmodel.CMSPlugin.objects.filter(parent=object_id),
            'markup_plugins': [c() for c in utils.get_list_of_markup_classes().values()],
        })
        return super(MarkupPlugin, self).change_view(request, object_id, extra_context=extra_context)

    def add_view(self, request, form_url='', extra_context=None):
        if extra_context is None:
            extra_context = {}

        extra_context.update({
            'text_plugins': [p() for p in plugin_pool.get_text_enabled_plugins(self.placeholder, self.page)],
            'name': 'markupeditor',
            'markup_plugins': [c() for c in utils.get_list_of_markup_classes().values()],
        })
        return super(MarkupPlugin, self).add_view(request, form_url, extra_context=extra_context);

    def get_plugin_urls(self):
        from django.conf.urls import patterns, url

        # If django-cms has get_plugin_urls feature or not
        urls = getattr(super(MarkupPlugin, self), 'get_plugin_urls', lambda: [])()
        preview_urls = patterns('',
            url(r'^preview/$', admin.site.admin_view(self.preview), name='cmsplugin_markup_preview'),
        )

        plugin_urls = []
        for c in utils.get_list_of_markup_classes().values():
            plugin_urls.extend(c().get_plugin_urls())

        return preview_urls + plugin_urls + urls

    def preview(self, request):
        if request.method != 'POST':
            return http.HttpResponseNotAllowed(['POST'])

        try:
            plugin = MarkupField.objects.get(pk=request.POST.get('plugin_id'))
        except MarkupField.DoesNotExist:
            plugin = shortcuts.get_object_or_404(pluginmodel.CMSPlugin, pk=request.POST.get('plugin_id'))

        placeholder = plugin.placeholder

        if not placeholder.has_change_permission(request):
            raise http.Http404

        if not request.POST.get('markup'):
            return http.HttpResponse('')

        (content, parser) = markup.markup_parser(request.POST.get('text'), request.POST.get('markup'), template.RequestContext(request, {
                'object': plugin,
                'placeholder': placeholder,
            }), placeholder)

        content += markuptags.content_scripts(parser.get_scripts()) + markuptags.content_stylesheets(parser.get_stylesheets())

        return http.HttpResponse(content)

plugin_pool.register_plugin(MarkupPlugin)
<|endoftext|>