<commit_before>#!/usr/bin/env python

DOCUMENTATION = '''
---
module: docker-imp
short_description: improved docker container management module
description:
    - Manage docker containers with ansible
options:
    name:
        description:
            - Set the name of the container
        required: false
        default: null
        aliases: ["id"]
    state:
        description:
            - Set the state of the container
        required: true
        default: present
        choices: ["present", "running", "stopped", "absent", "restarted"]
        aliases: []
    image:
        description:
            - Set the image for the container
        required: false
        default: null
        aliases: []
    env:
        description:
            - Set the container environment variables
        required: false
        default: null
        aliases: []
    volumes:
        description:
            - Mount voulumes for the container
        required: false
        default: null
        aliases: []
    ports:
        description:
            - Map ports for container
        required: false
        default: null
        aliases: []
    command:
        description:
            - Set the command for container
        required: false
        default: null
        aliases: []
    expose:
        description:
            - Expose ports
        required: false
        default: null
        aliases: []
    links:
        description:
            - Link containers
        required: false
        default: null
        aliases: []
    client_url:
        description:
            - Client base url
        required: false
        default: "unix://var/run/docker.sock"
        aliases: []
'''
import sys
import copy
try:
    import docker.client
except ImportError as e:
    print("Failed to import module {1}".format(e))
    sys.exit(1)

class ContainerManagerException(Exception):
    pass

class ContainerManager():

    def __init__(self, module):
        self.module = module
        self.client = docker.Client(base_url = module.params.get('client_url'))
        self.changed = False
        self.check_mode = module.check_mode
        self.changes_made = {}
        self.params = self.fix_parameters()

    def fix_parameters(self):
        params = copy.deepcopy(self.module.params)
        if params.get('volumes'):
            try:
                if type(params['volumes']) is str:
                    volumes = params['volumes'].split(",")
                elif type(params['volumes']) is list:
                    volumes = params['volumes']
                else:
                    raise ContainerManagerException({'Invalid argument': params['volumes']})
                mount_points = [x.split(":")[1] for x in volumes]

                binds = {}
                for i in volumes:
                    j = i.split(":")
                    ro = j[2] if len(j) == 3 else False
                    binds[j[0]] = {'bind': j[1], 'ro': ro}

                params['binds'] = binds
                params['volumes'] = mount_points
            except IndexError as e:
                raise ContainerManagerException({'Invalid argument': params['volumes']})

        if params.get('ports'):
            try:
                if type(params['ports']) is str:
                    port_params = params['ports'].split(",")
                elif type(params['ports']) is list:
                    port_params = params['ports']
                else:
                    raise ContainerManagerException({'Invalid argument': params['ports']})

                ports = []
                for i in port_params:
                    values = i.split(":")
                    len_values = len(values)
                    if len_values != 2 and len_values != 3:
                        raise ContainerManagerException({'Invalid argument': params['ports']})
                    port_and_prot = values[-1].split("/")
                    len_port_and_prot = len(port_and_prot)
                    if len_port_and_prot > 2:
                        raise ContainerManagerException({'Invalid argument': params['ports']})
                    p = (port_and_prot[0], port_and_prot[1]) if len_port_and_prot == 2 else port_and_prot[0]
                    ports.append(p)

                port_bindings = {}
                for i in port_params:
                    values = i.split(":")
                    len_values = len(values)
                    if len_values == 2:
                        host_port = values[0]
                        prot_and_port = values[1]
                        bind_ip = None
                    elif len_values == 3:
                        host_port = values[1]
                        prot_and_port = values[2]
                        bind_ip = values[0]
                    else:
                        raise ContainerManagerException({'Invalid argument': params['ports']})
                    prot_and_port = prot_and_port.split("/")
                    len_prot_and_port = len(prot_and_port)
                    if len_prot_and_port == 2:
                        key = "{0}/{1}".format(prot_and_port[0], prot_and_port[1])
                    elif len_prot_and_port == 1:
                        key = prot_and_port[0]
                    else:
                        raise ContainerManagerException({'Invalid argument': params['ports']})
                    if bind_ip:
                        val = (bind_ip, host_port) if host_port else (bind_ip,)
                    else:
                        val = host_port or None
                    port_bindings[key] = val

                params['ports'] = ports
                params['port_bindings'] = port_bindings

            except IndexError as e:
                raise ContainerManagerException({'Invalid argument': params['ports'], 'error': e})

        return params

    def ensure_present(self):
        params = self.params
        if not params.get('name'):
            raise ContainerManagerException("This state requires name or id")
        if not params.get('image'):
            raise ContainerManagerException("This state requires image")
        container, _ = self.find_container(params.get('name'))
        if not container:
            container = self.create_container()
        elif not self.ensure_same(container):
            self.ensure_absent()
            container = self.ensure_present()
        return container

    def ensure_running(self):
        container = self.ensure_present()
        if not self.find_container(container['Id'])[1]:
            self.start_container(container)

    def ensure_stopped(self):
        params = self.params
        if not params.get('name'):
            raise ContainerManagerException("This state requires name or id")
        container, running = self.find_container(params.get('name'))
        if not container:
            raise ContaqinerManagerException("Container not found")
        if running:
            self.stop_container(container)

    def ensure_absent(self):
        params = self.params
        if not params.get('name'):
            raise ContainerManagerException("This state requires name or id")
        container, running = self.find_container(params.get('name'))
        if running:
            self.stop_container(container)
        if container:
            self.remove_container(container)

    def restart(self):
        params = self.params
        if not params.get('name'):
            raise ContainerManagerException("This state requires name or id")
        container, running = self.find_container(params.get('name'))
        if not container:
            raise ContainerManagerException("Container not found")
        if not running:
            raise ContainerManagerException("Container not running")
        self.restart_container(container)

    def find_container(self, name):
        containers = self.client.containers()
        c = [x for x in containers if
                (x['Names'][0] == "/{0}".format(name)) or
                (x['Id'] == name)]
        if c:
            return c[0], True
        containers = self.client.containers(all = True)
        c = [x for x in containers if
                (x['Names'][0] == "/{0}".format(name)) or
                (x['Id'] == name)]
        if c:
            return c[0], False
        return None, False

    def running_latest_image(self, container, image):
        if not container.get('Config'):
            container_info = self.client.inspect_container(container)
        else:
            container_info = container
        image_info = self.get_image_info(image)
        if image_info['Id'] == container_info['Image']:
            return True
        else:
            return False

    def get_info(self, container):
        return self.client.inspect_container(container)

    def get_image_info(self, image):
        return self.client.inspect_image(image)

    def create_container(self):
        params = self.params

        key_filter = [
            'image', 'command', 'hostname', 'user',
            'detach', 'stdin_open', 'tty', 'mem_limit',
            'ports', 'environment', 'dns', 'volumes',
            'volumes_from', 'network_disabled', 'name',
            'entrypoint', 'cpu_shares', 'working_dir',
            'memswap_limit'
        ]
        filtered = { x: params[x] for x in key_filter if x in params }

        container = self.client.create_container(**filtered)
        info = self.get_info(container)
        self.write_log('CREATED', info)
        return container

    def start_container(self, container):
        params = self.params
        key_filter = [
            'binds', 'port_bindings', 'lxc_conf',
            'publish_all_ports', 'links', 'privileged',
            'dns', 'dns_search', 'volumes_from', 'network_mode',
            'restart_policy', 'cap_add', 'cap_drop'
        ]
        filtered = { x: params[x] for x in key_filter if x in params }

        self.client.start(container, **filtered)
        info = self.get_info(container)
        self.write_log('STARTED', info)

    def stop_container(self, container):
        self.client.stop(container)
        info = self.get_info(container)
        self.write_log('STOPPED', info)

    def remove_container(self, container):
        self.client.remove_container(container)
        c, _ = self.find_container(container['Id'])
        if c:
            raise ContainerManagerException("Could not remove the container")
        self.write_log('REMOVED', container)

    def restart_container(self, container):
        self.client.restart(container)
        info = self.get_info(container)
        self.write_log('RESTARTED', info)

    def ensure_same(self, container):
        params = self.params
        same = True
        if params['latest_image']:
            self.client.pull(params['image'])
            if not self.running_latest_image(container, params['image']):
                same = False
        return same

    def generate_message(self):
        if not self.has_changes():
            msg = "Up to date. No changes made"
        else:
            msg = self.changes_made
        return msg

    def write_log(self, action, info):
        if not self.changes_made.get(action):
            self.changes_made[action] = []
        self.changes_made[action].append(info)

    def has_changes(self):
        if self.changes_made:
            return True
        return False

def main():
    arguments = {
        'state': {
            'required': True,
            'choices': ["present", "running", "stopped", "absent", "restarted"]
        },
        'name':          { 'default': None, 'aliases': ["id"] },
        'image':         { 'default': None },
        'env':           { 'default': None },
        'volumes':       { 'default': None },
        'ports':         { 'default': None },
        'command':       { 'default': None },
        'expose':        { 'default': None },
        'links':         { 'default': None },
        'latest_image':  { 'default': False, 'choises': 'booleans' }
    }
    #module = AnsibleModule(argument_spec = arguments, supports_check_mode = True)
    module = AnsibleModule(argument_spec = arguments)
    try:

        manager = ContainerManager(module)
        state = module.params.get('state')
        if state == "present":
            manager.ensure_present()
        elif state == "running":
            manager.ensure_running()
        elif state == "stopped":
            manager.ensure_stopped()
        elif state == "absent":
            manager.ensure_absent()
        elif state == "restarted":
            manager.restart()
        module.exit_json(changed = manager.has_changes(), msg = manager.generate_message())

    except ContainerManagerException as e:
        module.fail_json(msg = str(e))
    except docker.errors.APIError as e:
        module.fail_json(msg = str(e))

from ansible.module_utils.basic import *
main()
<commit_msg>Change message style to better<commit_after>#!/usr/bin/env python

DOCUMENTATION = '''
---
module: docker-imp
short_description: improved docker container management module
description:
    - Manage docker containers with ansible
options:
    name:
        description:
            - Set the name of the container
        required: false
        default: null
        aliases: ["id"]
    state:
        description:
            - Set the state of the container
        required: true
        default: present
        choices: ["present", "running", "stopped", "absent", "restarted"]
        aliases: []
    image:
        description:
            - Set the image for the container
        required: false
        default: null
        aliases: []
    env:
        description:
            - Set the container environment variables
        required: false
        default: null
        aliases: []
    volumes:
        description:
            - Mount voulumes for the container
        required: false
        default: null
        aliases: []
    ports:
        description:
            - Map ports for container
        required: false
        default: null
        aliases: []
    command:
        description:
            - Set the command for container
        required: false
        default: null
        aliases: []
    expose:
        description:
            - Expose ports
        required: false
        default: null
        aliases: []
    links:
        description:
            - Link containers
        required: false
        default: null
        aliases: []
    client_url:
        description:
            - Client base url
        required: false
        default: "unix://var/run/docker.sock"
        aliases: []
'''
import sys
import copy
try:
    import docker.client
except ImportError as e:
    print("Failed to import module {1}".format(e))
    sys.exit(1)

class ContainerManagerException(Exception):
    pass

class ContainerManager():

    def __init__(self, module):
        self.module = module
        self.client = docker.Client(base_url = module.params.get('client_url'))
        self.changed = False
        self.check_mode = module.check_mode
        self.changes_made = []
        self.params = self.fix_parameters()

    def fix_parameters(self):
        params = copy.deepcopy(self.module.params)
        if params.get('volumes'):
            try:
                if type(params['volumes']) is str:
                    volumes = params['volumes'].split(",")
                elif type(params['volumes']) is list:
                    volumes = params['volumes']
                else:
                    raise ContainerManagerException({'Invalid argument': params['volumes']})
                mount_points = [x.split(":")[1] for x in volumes]

                binds = {}
                for i in volumes:
                    j = i.split(":")
                    ro = j[2] if len(j) == 3 else False
                    binds[j[0]] = {'bind': j[1], 'ro': ro}

                params['binds'] = binds
                params['volumes'] = mount_points
            except IndexError as e:
                raise ContainerManagerException({'Invalid argument': params['volumes']})

        if params.get('ports'):
            try:
                if type(params['ports']) is str:
                    port_params = params['ports'].split(",")
                elif type(params['ports']) is list:
                    port_params = params['ports']
                else:
                    raise ContainerManagerException({'Invalid argument': params['ports']})

                ports = []
                for i in port_params:
                    values = i.split(":")
                    len_values = len(values)
                    if len_values != 2 and len_values != 3:
                        raise ContainerManagerException({'Invalid argument': params['ports']})
                    port_and_prot = values[-1].split("/")
                    len_port_and_prot = len(port_and_prot)
                    if len_port_and_prot > 2:
                        raise ContainerManagerException({'Invalid argument': params['ports']})
                    p = (port_and_prot[0], port_and_prot[1]) if len_port_and_prot == 2 else port_and_prot[0]
                    ports.append(p)

                port_bindings = {}
                for i in port_params:
                    values = i.split(":")
                    len_values = len(values)
                    if len_values == 2:
                        host_port = values[0]
                        prot_and_port = values[1]
                        bind_ip = None
                    elif len_values == 3:
                        host_port = values[1]
                        prot_and_port = values[2]
                        bind_ip = values[0]
                    else:
                        raise ContainerManagerException({'Invalid argument': params['ports']})
                    prot_and_port = prot_and_port.split("/")
                    len_prot_and_port = len(prot_and_port)
                    if len_prot_and_port == 2:
                        key = "{0}/{1}".format(prot_and_port[0], prot_and_port[1])
                    elif len_prot_and_port == 1:
                        key = prot_and_port[0]
                    else:
                        raise ContainerManagerException({'Invalid argument': params['ports']})
                    if bind_ip:
                        val = (bind_ip, host_port) if host_port else (bind_ip,)
                    else:
                        val = host_port or None
                    port_bindings[key] = val

                params['ports'] = ports
                params['port_bindings'] = port_bindings

            except IndexError as e:
                raise ContainerManagerException({'Invalid argument': params['ports'], 'error': e})

        return params

    def ensure_present(self):
        params = self.params
        if not params.get('name'):
            raise ContainerManagerException("This state requires name or id")
        if not params.get('image'):
            raise ContainerManagerException("This state requires image")
        container, _ = self.find_container(params.get('name'))
        if not container:
            container = self.create_container()
        elif not self.ensure_same(container):
            self.ensure_absent()
            container = self.ensure_present()
        return container

    def ensure_running(self):
        container = self.ensure_present()
        if not self.find_container(container['Id'])[1]:
            self.start_container(container)

    def ensure_stopped(self):
        params = self.params
        if not params.get('name'):
            raise ContainerManagerException("This state requires name or id")
        container, running = self.find_container(params.get('name'))
        if not container:
            raise ContaqinerManagerException("Container not found")
        if running:
            self.stop_container(container)

    def ensure_absent(self):
        params = self.params
        if not params.get('name'):
            raise ContainerManagerException("This state requires name or id")
        container, running = self.find_container(params.get('name'))
        if running:
            self.stop_container(container)
        if container:
            self.remove_container(container)

    def restart(self):
        params = self.params
        if not params.get('name'):
            raise ContainerManagerException("This state requires name or id")
        container, running = self.find_container(params.get('name'))
        if not container:
            raise ContainerManagerException("Container not found")
        if not running:
            raise ContainerManagerException("Container not running")
        self.restart_container(container)

    def find_container(self, name):
        containers = self.client.containers()
        c = [x for x in containers if
                (x['Names'][0] == "/{0}".format(name)) or
                (x['Id'] == name)]
        if c:
            return c[0], True
        containers = self.client.containers(all = True)
        c = [x for x in containers if
                (x['Names'][0] == "/{0}".format(name)) or
                (x['Id'] == name)]
        if c:
            return c[0], False
        return None, False

    def running_latest_image(self, container, image):
        if not container.get('Config'):
            container_info = self.client.inspect_container(container)
        else:
            container_info = container
        image_info = self.get_image_info(image)
        if image_info['Id'] == container_info['Image']:
            return True
        else:
            return False

    def get_info(self, container):
        return self.client.inspect_container(container)

    def get_image_info(self, image):
        return self.client.inspect_image(image)

    def create_container(self):
        params = self.params

        key_filter = [
            'image', 'command', 'hostname', 'user',
            'detach', 'stdin_open', 'tty', 'mem_limit',
            'ports', 'environment', 'dns', 'volumes',
            'volumes_from', 'network_disabled', 'name',
            'entrypoint', 'cpu_shares', 'working_dir',
            'memswap_limit'
        ]
        filtered = { x: params[x] for x in key_filter if x in params }

        container = self.client.create_container(**filtered)
        info = self.get_info(container)
        self.write_log('CREATED', info)
        return container

    def start_container(self, container):
        params = self.params
        key_filter = [
            'binds', 'port_bindings', 'lxc_conf',
            'publish_all_ports', 'links', 'privileged',
            'dns', 'dns_search', 'volumes_from', 'network_mode',
            'restart_policy', 'cap_add', 'cap_drop'
        ]
        filtered = { x: params[x] for x in key_filter if x in params }

        self.client.start(container, **filtered)
        info = self.get_info(container)
        self.write_log('STARTED', info)

    def stop_container(self, container):
        self.client.stop(container)
        info = self.get_info(container)
        self.write_log('STOPPED', info)

    def remove_container(self, container):
        self.client.remove_container(container)
        c, _ = self.find_container(container['Id'])
        if c:
            raise ContainerManagerException("Could not remove the container")
        self.write_log('REMOVED', container)

    def restart_container(self, container):
        self.client.restart(container)
        info = self.get_info(container)
        self.write_log('RESTARTED', info)

    def ensure_same(self, container):
        params = self.params
        same = True
        if params['latest_image']:
            self.client.pull(params['image'])
            if not self.running_latest_image(container, params['image']):
                same = False
        return same

    def generate_message(self):
        if not self.has_changes():
            msg = "Up to date. No changes made"
        else:
            msg = self.changes_made
        return msg

    def write_log(self, action, info):
        key_filter = [
            'Name', 'Id', 'Image',
        ]
        filtered = { x: info[x] for x in key_filter if x in info }
        self.changes_made.append({action: filtered})

    def has_changes(self):
        if self.changes_made:
            return True
        return False

def main():
    arguments = {
        'state': {
            'required': True,
            'choices': ["present", "running", "stopped", "absent", "restarted"]
        },
        'name':          { 'default': None, 'aliases': ["id"] },
        'image':         { 'default': None },
        'env':           { 'default': None },
        'volumes':       { 'default': None },
        'ports':         { 'default': None },
        'command':       { 'default': None },
        'expose':        { 'default': None },
        'links':         { 'default': None },
        'latest_image':  { 'default': False, 'choises': 'booleans' }
    }
    #module = AnsibleModule(argument_spec = arguments, supports_check_mode = True)
    module = AnsibleModule(argument_spec = arguments)
    try:

        manager = ContainerManager(module)
        state = module.params.get('state')
        if state == "present":
            manager.ensure_present()
        elif state == "running":
            manager.ensure_running()
        elif state == "stopped":
            manager.ensure_stopped()
        elif state == "absent":
            manager.ensure_absent()
        elif state == "restarted":
            manager.restart()
        module.exit_json(changed = manager.has_changes(), msg = manager.generate_message())

    except ContainerManagerException as e:
        module.fail_json(msg = str(e))
    except docker.errors.APIError as e:
        module.fail_json(msg = str(e))

from ansible.module_utils.basic import *
main()
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
"""
Created on Sat Jul 23 10:10:48 2016

@author: Dave Ho
"""

import sys
import time
import BaseHTTPServer
import cgi
import json
from urlparse import urlparse
import os.path

HOST_NAME = '192.168.1.65'
#HOST_NAME = '10.78.15.19'
#HOST_NAME = '192.168.1.68'
#HOST_NAME = '209.68.147.236'
PORT_NUMBER = 80

class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler): # set up handler

    def do_HEAD(self):
        self.send_response(200)
        self.send_header("Content-type", "text/html")
        self.end_headers()

    def do_GET(self):
        #file = open('myfile.dat', 'w+')
        print self.path
        query = urlparse(self.path).query
        path = urlparse(self.path).path
        query_components = dict(qc.split("=") for qc in query.split("&"))
        print(query_components)
        try:
            print self.dictionary
            print self.zipDictionary
        except AttributeError:
            self.dictionary = {}
            self.zipDictionary = {}
            with open("records.txt", 'r') as myfile:
                data=myfile.read()
                array = data.splitlines()
                for string in array:
                    self.dictionary[json.loads(string)["phone"]+json.loads(string)["title"]]=string
                    for zipCode in json.loads(string)["zipCodeList"]:
                        if self.zipDictionary.has_key(zipCode):
                            self.zipDictionary[zipCode] = self.zipDictionary[zipCode] + [json.loads(string)["phone"]+json.loads(string)["title"]]
                        else:
                            self.zipDictionary[zipCode] = [json.loads(string)["phone"]+json.loads(string)["title"]]
        if path == '/getDetail':
            idName = query_components['id']
            phoneNumber = query_components['phoneNumber']
            self.send_response(200)
            self.send_header("Content-type", "text/html")
            self.end_headers()
            #print self.dictionary[idName]
            self.wfile.write(self.dictionary[phoneNumber+idName])
        elif path == '/getTitle':
            zipCode = query_components['zipCode']
            resultString = ""
            if self.zipDictionary.has_key(zipCode):
                for idString in self.zipDictionary[zipCode]:
                    resultString += idString + "\n"
                    #resultString += self.dictionary[idString] + "\n"
            else:
                self.zipDictionary[zipCode] = []
            self.send_response(200)
            self.send_header("Content-type", "text/html")
            self.end_headers()
            self.wfile.write(resultString)
        '''
        print self.zipDictionary
        print self.dictionary
        self.send_response(200)
        self.send_header("Content-type", "text/html")
        self.end_headers()
        self.wfile.write(resultString)
        '''

    def do_POST(self):
        #print(self.headers['Content-Type'])
        #print(type(self.headers.getheader('content-length')))
        #print(self.rfile.read(int(self.headers.getheader('content-length'))))
        json_string = self.rfile.read(int(self.headers.getheader('content-length')))
        d = json.loads(json_string)
        try:
            print self.dictionary
            print self.zipDictionary
        except AttributeError:
            self.dictionary = {}
            self.zipDictionary = {}
            with open("records.txt", 'r') as myfile:
                data=myfile.read()
                array = data.splitlines()
                for string in array:
                    self.dictionary[json.loads(string)["phone"]+json.loads(string)["title"]]=string
                    for zipCode in json.loads(string)["zipCodeList"]:
                        if self.zipDictionary.has_key(zipCode):
                            self.zipDictionary[zipCode] = self.zipDictionary[zipCode] + [json.loads(string)["phone"]+json.loads(string)["title"]]
                        else:
                            self.zipDictionary[zipCode] = [json.loads(string)["phone"]+json.loads(string)["title"]]
        if not self.dictionary.has_key(d["phone"]+d["title"]):
            self.dictionary[d["phone"]+d["title"]]=json_string
        for string in d["zipCodeList"]:
            if self.zipDictionary.has_key(string):
                if not self.zipDictionary[string].__contains__(d["phone"]+d["title"]):
                    self.zipDictionary[string] = self.zipDictionary[string] + [d["phone"]+d["title"]]
            else:
                self.zipDictionary[string] = [d["phone"]+d["title"]]
        if not os.path.isfile("records.txt"):
            file = open("records.txt", "w+")
        else:
            file = open("records.txt", "a")
        file.write(json_string+"\n")
        file.close()
        self.send_response(200)
        self.end_headers()
        print('Attempting to write')
        print("Somebody post me.")
        self.wfile.write("successful")

if __name__ == '__main__':
    server_class = BaseHTTPServer.HTTPServer
    httpd = server_class((HOST_NAME, PORT_NUMBER), MyHandler)
    print time.asctime(), "Server Starts - %s:%s" % (HOST_NAME, PORT_NUMBER)
    try:
        httpd.serve_forever()
    except KeyboardInterrupt:
        pass
    httpd.server_close()
    print time.asctime(), "Server Stops - %s:%s" % (HOST_NAME, PORT_NUMBER)<commit_msg>listen on all interfaces<commit_after># -*- coding: utf-8 -*-
"""
Created on Sat Jul 23 10:10:48 2016

@author: Dave Ho
"""

import sys
import time
import BaseHTTPServer
import cgi
import json
from urlparse import urlparse
import os.path

HOST_NAME = '0.0.0.0'
PORT_NUMBER = 80

class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler): # set up handler

    def do_HEAD(self):
        self.send_response(200)
        self.send_header("Content-type", "text/html")
        self.end_headers()

    def do_GET(self):
        print self.path
        query = urlparse(self.path).query
        path = urlparse(self.path).path
        query_components = dict(qc.split("=") for qc in query.split("&"))
        print(query_components)
        try:
            print self.dictionary
            print self.zipDictionary
        except AttributeError:
            self.dictionary = {}
            self.zipDictionary = {}
            with open("records.txt", 'r') as myfile:
                data=myfile.read()
                array = data.splitlines()
                for string in array:
                    self.dictionary[json.loads(string)["phone"]+json.loads(string)["title"]]=string
                    for zipCode in json.loads(string)["zipCodeList"]:
                        if self.zipDictionary.has_key(zipCode):
                            self.zipDictionary[zipCode] = self.zipDictionary[zipCode] + [json.loads(string)["phone"]+json.loads(string)["title"]]
                        else:
                            self.zipDictionary[zipCode] = [json.loads(string)["phone"]+json.loads(string)["title"]]
        if path == '/getDetail':
            idName = query_components['id']
            phoneNumber = query_components['phoneNumber']
            self.send_response(200)
            self.send_header("Content-type", "text/html")
            self.end_headers()
            #print self.dictionary[idName]
            self.wfile.write(self.dictionary[phoneNumber+idName])
        elif path == '/getTitle':
            zipCode = query_components['zipCode']
            resultString = ""
            if self.zipDictionary.has_key(zipCode):
                for idString in self.zipDictionary[zipCode]:
                    resultString += idString + "\n"
                    #resultString += self.dictionary[idString] + "\n"
            else:
                self.zipDictionary[zipCode] = []
            self.send_response(200)
            self.send_header("Content-type", "text/html")
            self.end_headers()
            self.wfile.write(resultString)
        '''
        print self.zipDictionary
        print self.dictionary
        self.send_response(200)
        self.send_header("Content-type", "text/html")
        self.end_headers()
        self.wfile.write(resultString)
        '''

    def do_POST(self):
        #print(self.headers['Content-Type'])
        #print(type(self.headers.getheader('content-length')))
        #print(self.rfile.read(int(self.headers.getheader('content-length'))))
        json_string = self.rfile.read(int(self.headers.getheader('content-length')))
        d = json.loads(json_string)
        try:
            print self.dictionary
            print self.zipDictionary
        except AttributeError:
            self.dictionary = {}
            self.zipDictionary = {}
            with open("records.txt", 'r') as myfile:
                data=myfile.read()
                array = data.splitlines()
                for string in array:
                    self.dictionary[json.loads(string)["phone"]+json.loads(string)["title"]]=string
                    for zipCode in json.loads(string)["zipCodeList"]:
                        if self.zipDictionary.has_key(zipCode):
                            self.zipDictionary[zipCode] = self.zipDictionary[zipCode] + [json.loads(string)["phone"]+json.loads(string)["title"]]
                        else:
                            self.zipDictionary[zipCode] = [json.loads(string)["phone"]+json.loads(string)["title"]]
        if not self.dictionary.has_key(d["phone"]+d["title"]):
            self.dictionary[d["phone"]+d["title"]]=json_string
        for string in d["zipCodeList"]:
            if self.zipDictionary.has_key(string):
                if not self.zipDictionary[string].__contains__(d["phone"]+d["title"]):
                    self.zipDictionary[string] = self.zipDictionary[string] + [d["phone"]+d["title"]]
            else:
                self.zipDictionary[string] = [d["phone"]+d["title"]]
        if not os.path.isfile("records.txt"):
            file = open("records.txt", "w+")
        else:
            file = open("records.txt", "a")
        file.write(json_string+"\n")
        file.close()
        self.send_response(200)
        self.end_headers()
        print('Attempting to write')
        print("Somebody post me.")
        self.wfile.write("successful")

if __name__ == '__main__':
    server_class = BaseHTTPServer.HTTPServer
    httpd = server_class((HOST_NAME, PORT_NUMBER), MyHandler)
    print time.asctime(), "Server Starts - %s:%s" % (HOST_NAME, PORT_NUMBER)
    try:
        httpd.serve_forever()
    except KeyboardInterrupt:
        pass
    httpd.server_close()
    print time.asctime(), "Server Stops - %s:%s" % (HOST_NAME, PORT_NUMBER)<|endoftext|>


<commit_before>import urllib2
from urllib import quote, quote_plus, urlencode
from pulsar.util import notify
from pulsar.logger import log
from pulsar.addon import ADDON

RESOLUTION_UNKNOWN = 0
RESOLUTION_480P = 1
RESOLUTION_720P = 2
RESOLUTION_1080P = 3
RESOLUTION_1440P = 4
RESOLUTION_4K2K = 5

RIP_UNKNOWN = 0
RIP_CAM = 1
RIP_TS = 2
RIP_TC = 3
RIP_SCR = 4
RIP_DVDSCR = 5
RIP_DVD = 6
RIP_HDTV = 7
RIP_WEB = 8
RIP_BLURAY = 9

RATING_UNKNOWN = 0
RATING_PROPER = 1
RATING_NUKED = 2

CODEC_UNKNOWN = 0
CODEC_XVID = 1
CODEC_H264 = 2
CODEC_MP3 = 3
CODEC_AAC = 4
CODEC_AC3 = 5
CODEC_DTS = 6
CODEC_DTSHD = 7
CODEC_DTSHDMA = 8

USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.66 Safari/537.36"
COOKIES = None

class closing(object):
    def __init__(self, thing):
        self.thing = thing
    def __enter__(self):
        return self.thing
    def __exit__(self, *exc_info):
        self.thing.close()

def parse_json(data):
    import json
    return json.loads(data)

def parse_xml(data):
    import xml.etree.ElementTree as ET
    return ET.fromstring(data)

def request(url, params={}, headers={}, data=None, method=None, with_cookies=False):
    if params:
        import urllib
        url = "".join([url, "?", urlencode(params)])

    if with_cookies and COOKIE_OPENER is None:
        from cookielib import CookieJar
        COOKIE_JAR = CookieJar()
        urllib2.install_opener(urllib2.build_opener(urllib2.HTTPCookieProcessor(COOKIE_JAR)))

    req = urllib2.Request(url)
    if method:
        req.get_method = lambda: method
    req.add_header("User-Agent", USER_AGENT)
    req.add_header("Accept-Encoding", "gzip")
    for k, v in headers.items():
        req.add_header(k, v)
    if data:
        req.add_data(data)
    try:
        with closing(urllib2.urlopen(req)) as response:
            data = response.read()
            if response.headers.get("Content-Encoding", "") == "gzip":
                import zlib
                data = zlib.decompressobj(16 + zlib.MAX_WBITS).decompress(data)
            response.data = data
            response.json = lambda: parse_json(data)
            response.xml = lambda: parse_xml(data)
            return response
    except urllib2.HTTPError, e:
        return None, None

HEAD = lambda *args, **kwargs: request(*args, method="POST", **kwargs)
GET = lambda *args, **kwargs: request(*args, method="GET", **kwargs)
POST = lambda *args, **kwargs: request(*args, method="POST", **kwargs)
PUT = lambda *args, **kwargs: request(*args, method="PUT", **kwargs)
DELETE = lambda *args, **kwargs: request(*args, method="DELETE", **kwargs)

def append_headers(uri, headers):
    return uri + "|" + "|".join(["%s=%s" % h for h in headers.items()])

def extract_magnets(data):
    import re
    for magnet in re.findall(r'magnet:\?[^\'"\s<>\[\]]+', data):
        yield {"uri": magnet}

def parse_rss(root):
    NSMAP = {
        "torrent": "http://xmlns.ezrss.it/0.1/",
        "showrss": "http://showrss.info/",
    }
    def _maybe_int(val):
        try:
            return int(val)
        except:
            return 0
    def _text(node, path):
        n = node.find(path)
        if n is not None:
            return n.text
    def _attr(node, path, attrib):
        n = node.find(path)
        if n is not None:
            return n.attrib.get(attrib)
    for item in root.getiterator("item"):
        yield {
            "name": _text(item, "title") or _text(item, ".//{%(torrent)s}fileName" % NSMAP) or _text(item, ".//{%(showrss)s}rawtitle" % NSMAP),
            "info_hash": _text(item, ".//info_hash") or _text(item, ".//{%(showrss)s}info_hash" % NSMAP),
            "uri": _text(item, ".//{%(torrent)s}magnetURI" % NSMAP) or _attr(item, ".//enclosure", "url") or _text(item, "./link"),
            "seeds": _maybe_int(_text(item, ".//{%(torrent)s}seeds" % NSMAP) or _text(item, "numSeeders") or _text(item, "seeders")),
            "peers": _maybe_int(_text(item, ".//{%(torrent)s}peers" % NSMAP) or _text(item, "numLeechers") or _text(item, "leechers")),
            "size": _maybe_int(_text(item, ".//{%(torrent)s}contentLength" % NSMAP) or _attr(item, ".//enclosure", "length")),
        }

# Borrowed from xbmcswift2
def get_setting(key, converter=str, choices=None):
    value = ADDON.getSetting(id=key)
    if converter is str:
        return value
    elif converter is unicode:
        return value.decode('utf-8')
    elif converter is bool:
        return value == 'true'
    elif converter is int:
        return int(value)
    elif isinstance(choices, (list, tuple)):
        return choices[int(value)]
    else:
        raise TypeError('Acceptable converters are str, unicode, bool and '
                        'int. Acceptable choices are instances of list '
                        ' or tuple.')

def set_setting(key, val):
    return ADDON.setSetting(id=key, value=val)

def register(search, search_movie, search_episode):
    import base64
    import json
    import sys

    try:
        payload = json.loads(base64.b64decode(sys.argv[1]))
    except:
        notify("This addon can only be run from within Pulsar", time=1000)
        return

    results = ()
    method = {
        "search": search,
        "search_movie": search_movie,
        "search_episode": search_episode,
    }.get(payload["method"]) or (lambda *a, **kw: [])
    try:
        results = tuple(method(payload["search_object"]))
    finally:
        urllib2.urlopen(
            payload["callback_url"],
            data=json.dumps(results)
        )
<commit_msg>Enable cookies by default, add with_cookies() helper method for providers<commit_after>import urllib2
from urllib import quote, quote_plus, urlencode
from pulsar.util import notify
from pulsar.logger import log
from pulsar.addon import ADDON
from cookielib import CookieJar

RESOLUTION_UNKNOWN = 0
RESOLUTION_480P = 1
RESOLUTION_720P = 2
RESOLUTION_1080P = 3
RESOLUTION_1440P = 4
RESOLUTION_4K2K = 5

RIP_UNKNOWN = 0
RIP_CAM = 1
RIP_TS = 2
RIP_TC = 3
RIP_SCR = 4
RIP_DVDSCR = 5
RIP_DVD = 6
RIP_HDTV = 7
RIP_WEB = 8
RIP_BLURAY = 9

RATING_UNKNOWN = 0
RATING_PROPER = 1
RATING_NUKED = 2

CODEC_UNKNOWN = 0
CODEC_XVID = 1
CODEC_H264 = 2
CODEC_MP3 = 3
CODEC_AAC = 4
CODEC_AC3 = 5
CODEC_DTS = 6
CODEC_DTSHD = 7
CODEC_DTSHDMA = 8

USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.66 Safari/537.36"

COOKIES = CookieJar()
urllib2.install_opener(urllib2.build_opener(urllib2.HTTPCookieProcessor(COOKIES)))

class closing(object):
    def __init__(self, thing):
        self.thing = thing
    def __enter__(self):
        return self.thing
    def __exit__(self, *exc_info):
        self.thing.close()

def parse_json(data):
    import json
    return json.loads(data)

def parse_xml(data):
    import xml.etree.ElementTree as ET
    return ET.fromstring(data)

def request(url, params={}, headers={}, data=None, method=None):
    if params:
        import urllib
        url = "".join([url, "?", urlencode(params)])

    req = urllib2.Request(url)
    if method:
        req.get_method = lambda: method
    req.add_header("User-Agent", USER_AGENT)
    req.add_header("Accept-Encoding", "gzip")
    for k, v in headers.items():
        req.add_header(k, v)
    if data:
        req.add_data(data)
    try:
        with closing(urllib2.urlopen(req)) as response:
            data = response.read()
            if response.headers.get("Content-Encoding", "") == "gzip":
                import zlib
                data = zlib.decompressobj(16 + zlib.MAX_WBITS).decompress(data)
            response.data = data
            response.json = lambda: parse_json(data)
            response.xml = lambda: parse_xml(data)
            return response
    except urllib2.HTTPError, e:
        return None, None

HEAD = lambda *args, **kwargs: request(*args, method="POST", **kwargs)
GET = lambda *args, **kwargs: request(*args, method="GET", **kwargs)
POST = lambda *args, **kwargs: request(*args, method="POST", **kwargs)
PUT = lambda *args, **kwargs: request(*args, method="PUT", **kwargs)
DELETE = lambda *args, **kwargs: request(*args, method="DELETE", **kwargs)

def append_headers(uri, headers):
    return uri + "|" + "|".join(["%s=%s" % h for h in headers.items()])

def with_cookies(uri):
    return uri + "|Cookies=" + "; ".join(["%s=%s" % (c.name, c.value) for c in COOKIE_JAR])

def extract_magnets(data):
    import re
    for magnet in re.findall(r'magnet:\?[^\'"\s<>\[\]]+', data):
        yield {"uri": magnet}

def parse_rss(root):
    NSMAP = {
        "torrent": "http://xmlns.ezrss.it/0.1/",
        "showrss": "http://showrss.info/",
    }
    def _maybe_int(val):
        try:
            return int(val)
        except:
            return 0
    def _text(node, path):
        n = node.find(path)
        if n is not None:
            return n.text
    def _attr(node, path, attrib):
        n = node.find(path)
        if n is not None:
            return n.attrib.get(attrib)
    for item in root.getiterator("item"):
        yield {
            "name": _text(item, "title") or _text(item, ".//{%(torrent)s}fileName" % NSMAP) or _text(item, ".//{%(showrss)s}rawtitle" % NSMAP),
            "info_hash": _text(item, ".//info_hash") or _text(item, ".//{%(showrss)s}info_hash" % NSMAP),
            "uri": _text(item, ".//{%(torrent)s}magnetURI" % NSMAP) or _attr(item, ".//enclosure", "url") or _text(item, "./link"),
            "seeds": _maybe_int(_text(item, ".//{%(torrent)s}seeds" % NSMAP) or _text(item, "numSeeders") or _text(item, "seeders")),
            "peers": _maybe_int(_text(item, ".//{%(torrent)s}peers" % NSMAP) or _text(item, "numLeechers") or _text(item, "leechers")),
            "size": _maybe_int(_text(item, ".//{%(torrent)s}contentLength" % NSMAP) or _attr(item, ".//enclosure", "length")),
        }

# Borrowed from xbmcswift2
def get_setting(key, converter=str, choices=None):
    value = ADDON.getSetting(id=key)
    if converter is str:
        return value
    elif converter is unicode:
        return value.decode('utf-8')
    elif converter is bool:
        return value == 'true'
    elif converter is int:
        return int(value)
    elif isinstance(choices, (list, tuple)):
        return choices[int(value)]
    else:
        raise TypeError('Acceptable converters are str, unicode, bool and '
                        'int. Acceptable choices are instances of list '
                        ' or tuple.')

def set_setting(key, val):
    return ADDON.setSetting(id=key, value=val)

def register(search, search_movie, search_episode):
    import base64
    import json
    import sys

    try:
        payload = json.loads(base64.b64decode(sys.argv[1]))
    except:
        notify("This addon can only be run from within Pulsar", time=1000)
        return

    results = ()
    method = {
        "search": search,
        "search_movie": search_movie,
        "search_episode": search_episode,
    }.get(payload["method"]) or (lambda *a, **kw: [])
    try:
        results = tuple(method(payload["search_object"]))
    finally:
        urllib2.urlopen(
            payload["callback_url"],
            data=json.dumps(results)
        )
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from django.conf.urls import patterns, url

from .views import BroadcastDetailView, BroadcastListView

urlpatterns = patterns('',
    url(r'^episode/(?P<slug>[-\w]+)/$', name='episode-detail', view=BroadcastDetailView.as_view()),
    url(r'^broadcasts/$', name='broadcast-list', view=BroadcastListView.as_view()),
)
<commit_msg>Correct the reversal.<commit_after># -*- coding: utf-8 -*-
from django.conf.urls import patterns, url

from .views import BroadcastDetailView, BroadcastListView

urlpatterns = patterns('',
    url(r'^episode/(?P<slug>[-\w]+)/$', name='broadcast-detail', view=BroadcastDetailView.as_view()),
    url(r'^broadcasts/$', name='broadcast-list', view=BroadcastListView.as_view()),
)
<|endoftext|>


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

from scipy import stats, optimize
import numpy as np
import logging
import warnings

class Replicate():

    def __init__(self, num, x, y, owner):
        self.logger = owner.logger
        self.num = num + 1
        self.x = x
        self.y = y
        self.owner = owner
        self.xlim = owner.xlim
        self.fitresult = self.fit()

    def fit(self):
        ind_min_max = np.where((self.x >= self.xlim[0]) &
                               (self.x <= self.xlim[1]))
        x_for_fit = np.take(self.x, ind_min_max)
        y_for_fit = np.take(self.y, ind_min_max)

        # ignore warnings about invalid values in sqrt during linear fitting
        # they occur frequently and will just clutter the cli
        warnings.filterwarnings('ignore',
                                category=RuntimeWarning,
                                message='invalid value encountered in sqrt')

        (slope, intercept,
            r_value,
            p_value,
            std_err) = stats.linregress(x_for_fit, y_for_fit)

        r_squared = r_value**2
        conc = '{} {}'.format(self.owner.concentration,
                              self.owner.concentration_unit)

        self.logger.info('Linear fit for {} #{}:'.format(conc, self.num))
        if r_squared < 0.9:
            msg = '    r-squared: {} < 0.9; Check fit manually!'
            self.logger.warning(msg.format(round(r_squared, 4)))
        else:
            msg = '    r-squared: {}'
            self.logger.info(msg.format(round(r_squared, 4)))
        self.logger.info('    slope: {}'.format(slope))
        if slope < 0:
            self.logger.warning('Slope is negative. Will use absolute value '
                                'for further calculations!')
        self.logger.info('    intercept: {}'.format(slope))

        return {'slope': slope,
                'intercept': intercept,
                'r_value': r_value,
                'r_squared': r_squared,
                'p_value': p_value,
                'std_err': std_err}

class Measurement():

    def __init__(self, x, y, conc, conc_unit, owner):
        self.logger = owner.logger
        self.concentration = float(conc)
        self.concentration_unit = conc_unit
        self.x = x
        self.y = y
        self.replicates = []
        self.owner = owner
        self.xlim = owner.xlim
        self.slopes = []
        self.avg_slope = None
        self.avg_slope_err = None

        # extract number of replicates (num_replicates)
        length_x, num_replicates = self.y.shape

        for n in range(num_replicates):
            self.replicates.append(Replicate(n,
                                             self.x,
                                             self.y[:, n:n+1],
                                             self))

        for r in self.replicates:
            self.slopes.append(r.fitresult['slope'])

        self.avg_slope = np.average(self.slopes)
        self.avg_slope_err = np.std(self.slopes)

        self.logger.info('Average slope: {} ± {}'.format(self.avg_slope,
                                                         self.avg_slope_err))
        if self.avg_slope < 0:
            self.logger.warning('Avererage slope is negative. Will use '
                                'absolute value for further calculations!')
        self.logger.info('-----')

    def get_results(self):
        results = []
        for r in self.replicates:
            results.append(r.fitresult)

        return results

class Experiment():

    def __init__(self, data_files, xlim, do_hill=False, fit_to_replicates=False, logger=None):

        if logger:
            self.logger = logger
        else:
            self.logger = logging.getLogger(__name__)

        # collction of indepentend measurements
        self.measurements = []

        self.fit_to_replicates = fit_to_replicates
        # dictionary to store data for the kinetics calculation
        self.raw_kinetic_data = {'x': [],
                                 'y': [],
                                 'yerr': []}
        self.xlim = xlim

        # parse data files and generate measurements
        for csvfile in data_files:
            tmp = np.genfromtxt(str(csvfile), comments='#')
            with open(str(csvfile)) as datafile:
                head = [next(datafile) for x in range(2)]
            # extract concentration and unit from header
            # TODO: move unit to parameter
            # TODO: More error-proof header detection
            conc = head[0].strip('#').strip()
            unit = head[1].strip('#').strip()
            # split x and y data apart
            # first column is x data (time); following columns contain
            # replicates of y data (absorption, etc.)
            x = tmp[:, 0]
            y = tmp[:, 1:]
            # create new measurement and append to list
            measurement = Measurement(x, y, conc, unit, self)
            self.measurements.append(measurement)

        # iterate over all measurements
        for m in self.measurements:
            if self.fit_to_replicates:
                for r in m.replicates:
                    self.raw_kinetic_data['x'].append(m.concentration)
                    self.raw_kinetic_data['y'].append(np.absolute(r.fitresult['slope']))
                    self.raw_kinetic_data['yerr'].append(r.fitresult['std_err'])

            else:
                # extract relevant data for kinetics calculation
                # (concentration, average slope and error)
                self.raw_kinetic_data['x'].append(m.concentration)
                self.raw_kinetic_data['y'].append(np.absolute(m.avg_slope))
                self.raw_kinetic_data['yerr'].append(m.avg_slope_err)

        # calculate kinetics
        self.mm = self.do_mm_kinetics()
        if do_hill:
            self.hill = self.do_hill_kinetics()
        else:
            self.hill = None

    def plot_data(self, outpath):
        # iterate over all measurements
        for m in self.measurements:
            # plot each measurement
            m.plot(outpath)

    def mm_kinetics_function(self, x, vmax, Km):
        v = (vmax*x)/(Km+x)
        return v

    def hill_kinetics_function(self, x, vmax, Kprime, h):
        v = (vmax*(x**h))/(Kprime+(x**h))
        return v

    def do_mm_kinetics(self):
        try:
            popt, pconv = optimize.curve_fit(self.mm_kinetics_function,
                                             self.raw_kinetic_data['x'],
                                             self.raw_kinetic_data['y'])

            perr = np.sqrt(np.diag(pconv))
            vmax = popt[0]
            Km = popt[1]
            x = np.arange(0, max(self.raw_kinetic_data['x']), 0.0001)

            self.logger.info('Michaelis-Menten Kinetics:')
            self.logger.info('    v_max: {} ± {}'.format(vmax, perr[0]))
            self.logger.info('    Km: {} ± {}'.format(Km, perr[1]))

            return {'vmax': float(vmax),
                    'Km': float(Km),
                    'perr': perr,
                    'x': x}
        except:
            msg = 'Calculation of Michaelis-Menten kinetics failed!'
            if self.logger:
                self.logger.error('{}'.format(msg))
            else:
                print(msg)
            return None

    def do_hill_kinetics(self):
        try:
            popt, pconv = optimize.curve_fit(self.hill_kinetics_function,
                                             self.raw_kinetic_data['x'],
                                             self.raw_kinetic_data['y'])

            perr = np.sqrt(np.diag(pconv))
            vmax = popt[0]
            Kprime = popt[1]
            h = popt[2]

            x = np.arange(0, max(self.raw_kinetic_data['x']), 0.0001)

            self.logger.info('Hill Kinetics:')
            self.logger.info('    v_max: {} ± {}'.format(vmax, perr[0]))
            self.logger.info('    K_prime: {} ± {}'.format(Kprime, perr[1]))
            self.logger.info('    h: {} ± {}'.format(h, perr[2]))

            return {'vmax': float(vmax),
                    'Kprime': float(Kprime),
                    'perr': perr,
                    'h': h,
                    'x': x}
        except:
            msg = 'Calculation of Hill kinetics failed!'
            if self.logger:
                self.logger.error('{}'.format(msg))
            else:
                print(msg)
            return None
<commit_msg>Formatting of logging output<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-

from scipy import stats, optimize
import numpy as np
import logging
import warnings

class Replicate():

    def __init__(self, num, x, y, owner):
        self.logger = owner.logger
        self.num = num + 1
        self.x = x
        self.y = y
        self.owner = owner
        self.xlim = owner.xlim
        self.fitresult = self.fit()

    def fit(self):
        ind_min_max = np.where((self.x >= self.xlim[0]) &
                               (self.x <= self.xlim[1]))
        x_for_fit = np.take(self.x, ind_min_max)
        y_for_fit = np.take(self.y, ind_min_max)

        # ignore warnings about invalid values in sqrt during linear fitting
        # they occur frequently and will just clutter the cli
        warnings.filterwarnings('ignore',
                                category=RuntimeWarning,
                                message='invalid value encountered in sqrt')

        (slope, intercept,
            r_value,
            p_value,
            std_err) = stats.linregress(x_for_fit, y_for_fit)

        r_squared = r_value**2
        conc = '{} {}'.format(self.owner.concentration,
                              self.owner.concentration_unit)

        self.logger.info('Linear fit for {} #{}:'.format(conc, self.num))
        if r_squared < 0.9:
            msg = '    r-squared: {} < 0.9; Check fit manually!'
            self.logger.warning(msg.format(round(r_squared, 4)))
        else:
            msg = '    r-squared: {}'
            self.logger.info(msg.format(round(r_squared, 4)))
        self.logger.info('    slope: {}'.format(slope))
        if slope < 0:
            self.logger.warning('    Slope is negative. Will use absolute '
                                'value for further calculations!')
        self.logger.info('    intercept: {}'.format(slope))

        return {'slope': slope,
                'intercept': intercept,
                'r_value': r_value,
                'r_squared': r_squared,
                'p_value': p_value,
                'std_err': std_err}

class Measurement():

    def __init__(self, x, y, conc, conc_unit, owner):
        self.logger = owner.logger
        self.concentration = float(conc)
        self.concentration_unit = conc_unit
        self.x = x
        self.y = y
        self.replicates = []
        self.owner = owner
        self.xlim = owner.xlim
        self.slopes = []
        self.avg_slope = None
        self.avg_slope_err = None

        # extract number of replicates (num_replicates)
        length_x, num_replicates = self.y.shape

        for n in range(num_replicates):
            self.replicates.append(Replicate(n,
                                             self.x,
                                             self.y[:, n:n+1],
                                             self))

        for r in self.replicates:
            self.slopes.append(r.fitresult['slope'])

        self.avg_slope = np.average(self.slopes)
        self.avg_slope_err = np.std(self.slopes)

        self.logger.info('Average slope: {} ± {}'.format(self.avg_slope,
                                                         self.avg_slope_err))
        if self.avg_slope < 0:
            self.logger.warning('Avererage slope is negative. Will use '
                                'absolute value for further calculations!')
        self.logger.info('-----')

    def get_results(self):
        results = []
        for r in self.replicates:
            results.append(r.fitresult)

        return results

class Experiment():

    def __init__(self, data_files, xlim, do_hill=False, fit_to_replicates=False, logger=None):

        if logger:
            self.logger = logger
        else:
            self.logger = logging.getLogger(__name__)

        # collction of indepentend measurements
        self.measurements = []

        self.fit_to_replicates = fit_to_replicates
        # dictionary to store data for the kinetics calculation
        self.raw_kinetic_data = {'x': [],
                                 'y': [],
                                 'yerr': []}
        self.xlim = xlim

        # parse data files and generate measurements
        for csvfile in data_files:
            tmp = np.genfromtxt(str(csvfile), comments='#')
            with open(str(csvfile)) as datafile:
                head = [next(datafile) for x in range(2)]
            # extract concentration and unit from header
            # TODO: move unit to parameter
            # TODO: More error-proof header detection
            conc = head[0].strip('#').strip()
            unit = head[1].strip('#').strip()
            # split x and y data apart
            # first column is x data (time); following columns contain
            # replicates of y data (absorption, etc.)
            x = tmp[:, 0]
            y = tmp[:, 1:]
            # create new measurement and append to list
            measurement = Measurement(x, y, conc, unit, self)
            self.measurements.append(measurement)

        # iterate over all measurements
        for m in self.measurements:
            if self.fit_to_replicates:
                for r in m.replicates:
                    self.raw_kinetic_data['x'].append(m.concentration)
                    self.raw_kinetic_data['y'].append(np.absolute(r.fitresult['slope']))
                    self.raw_kinetic_data['yerr'].append(r.fitresult['std_err'])

            else:
                # extract relevant data for kinetics calculation
                # (concentration, average slope and error)
                self.raw_kinetic_data['x'].append(m.concentration)
                self.raw_kinetic_data['y'].append(np.absolute(m.avg_slope))
                self.raw_kinetic_data['yerr'].append(m.avg_slope_err)

        # calculate kinetics
        self.mm = self.do_mm_kinetics()
        if do_hill:
            self.hill = self.do_hill_kinetics()
        else:
            self.hill = None

    def plot_data(self, outpath):
        # iterate over all measurements
        for m in self.measurements:
            # plot each measurement
            m.plot(outpath)

    def mm_kinetics_function(self, x, vmax, Km):
        v = (vmax*x)/(Km+x)
        return v

    def hill_kinetics_function(self, x, vmax, Kprime, h):
        v = (vmax*(x**h))/(Kprime+(x**h))
        return v

    def do_mm_kinetics(self):
        try:
            popt, pconv = optimize.curve_fit(self.mm_kinetics_function,
                                             self.raw_kinetic_data['x'],
                                             self.raw_kinetic_data['y'])

            perr = np.sqrt(np.diag(pconv))
            vmax = popt[0]
            Km = popt[1]
            x = np.arange(0, max(self.raw_kinetic_data['x']), 0.0001)

            self.logger.info('Michaelis-Menten Kinetics:')
            self.logger.info('    v_max: {} ± {}'.format(vmax, perr[0]))
            self.logger.info('    Km: {} ± {}'.format(Km, perr[1]))

            return {'vmax': float(vmax),
                    'Km': float(Km),
                    'perr': perr,
                    'x': x}
        except:
            msg = 'Calculation of Michaelis-Menten kinetics failed!'
            if self.logger:
                self.logger.error('{}'.format(msg))
            else:
                print(msg)
            return None

    def do_hill_kinetics(self):
        try:
            popt, pconv = optimize.curve_fit(self.hill_kinetics_function,
                                             self.raw_kinetic_data['x'],
                                             self.raw_kinetic_data['y'])

            perr = np.sqrt(np.diag(pconv))
            vmax = popt[0]
            Kprime = popt[1]
            h = popt[2]

            x = np.arange(0, max(self.raw_kinetic_data['x']), 0.0001)

            self.logger.info('Hill Kinetics:')
            self.logger.info('    v_max: {} ± {}'.format(vmax, perr[0]))
            self.logger.info('    K_prime: {} ± {}'.format(Kprime, perr[1]))
            self.logger.info('    h: {} ± {}'.format(h, perr[2]))

            return {'vmax': float(vmax),
                    'Kprime': float(Kprime),
                    'perr': perr,
                    'h': h,
                    'x': x}
        except:
            msg = 'Calculation of Hill kinetics failed!'
            if self.logger:
                self.logger.error('{}'.format(msg))
            else:
                print(msg)
            return None
<|endoftext|>


<commit_before>from django.test import TestCase
from django.core.exceptions import ImproperlyConfigured
from blti.validators import BLTIOauth, BLTIRoles
from blti.crypto import aes128cbc
from blti import BLTI, BLTIException

class BLTIOAuthTest(TestCase):
    def test_no_config(self):
        self.assertRaises(ImproperlyConfigured, BLTIOauth)

    def test_no_consumer(self):
        with self.settings(LTI_CONSUMERS={'ABC': '12345'}):
            self.assertRaises(BLTIOauth().get_consumer('DEF'), None)

    def test_get_consumer(self):
        with self.settings(LTI_CONSUMERS={'ABC': '12345'}):
            self.assertEquals(BLTIOauth().get_consumer('ABC').secret, '12345')
            self.assertEquals(None, BLTIOauth().get_consumer('XYZ'))

class BLTIRolesTest(TestCase):
    def test_has_admin_role(self):
        self.assertEquals(
            True, BLTIRoles().has_admin_role(['Faculty', 'Alumni']))
        self.assertEquals(False, BLTIRoles().has_admin_role(['Guest']))

    def test_has_instructor_role(self):
        self.assertEquals(
            True, BLTIRoles().has_instructor_role(['TeachingAssistant']))
        self.assertEquals(
            False, BLTIRoles().has_instructor_role(['Faculty', 'Alumni']))

    def test_has_learner_role(self):
        self.assertEquals(
            True, BLTIRoles().has_learner_role(['Member']))
        self.assertEquals(
            False, BLTIRoles().has_learner_role(['Faculty', 'Staff']))

    def test_validate(self):
        blti = {'roles': 'Member'}
        self.assertEquals(None, BLTIRoles().validate(blti, 'member'))
        self.assertRaises(
            BLTIException, BLTIRoles().validate, blti, 'admin')

        blti = {'roles': 'Faculty,Staff'}
        self.assertEquals(None, BLTIRoles().validate(blti, 'member'))
        self.assertEquals(None, BLTIRoles().validate(blti, 'admin'))

class CryptoTest(TestCase):
    test_key = 'DUMMY_KEY_FOR_TESTING_1234567890'
    test_iv = 'DUMMY_IV_TESTING'
    msgs = [
        ('LTI provides a framework through which an LMS can send some '
         'verifiable information about a user to a third party.'),
        "'abc': {'key': value}"
    ]

    def test_encrypt_decrypt(self):
        aes = aes128cbc(self.test_key, self.test_iv)

        for msg in self.msgs:
            enc = aes.encrypt(msg)
            self.assertEquals(aes.decrypt(enc), msg)
<commit_msg>fix test<commit_after>from django.test import TestCase
from django.core.exceptions import ImproperlyConfigured
from blti.validators import BLTIOauth, BLTIRoles
from blti.crypto import aes128cbc
from blti import BLTI, BLTIException

class BLTIOAuthTest(TestCase):
    def test_no_config(self):
        self.assertRaises(ImproperlyConfigured, BLTIOauth)

    def test_no_consumer(self):
        with self.settings(LTI_CONSUMERS={'ABC': '12345'}):
            self.assertEquals(BLTIOauth().get_consumer('DEF'), None)

    def test_get_consumer(self):
        with self.settings(LTI_CONSUMERS={'ABC': '12345'}):
            self.assertEquals(BLTIOauth().get_consumer('ABC').secret, '12345')
            self.assertEquals(None, BLTIOauth().get_consumer('XYZ'))

class BLTIRolesTest(TestCase):
    def test_has_admin_role(self):
        self.assertEquals(
            True, BLTIRoles().has_admin_role(['Faculty', 'Alumni']))
        self.assertEquals(False, BLTIRoles().has_admin_role(['Guest']))

    def test_has_instructor_role(self):
        self.assertEquals(
            True, BLTIRoles().has_instructor_role(['TeachingAssistant']))
        self.assertEquals(
            False, BLTIRoles().has_instructor_role(['Faculty', 'Alumni']))

    def test_has_learner_role(self):
        self.assertEquals(
            True, BLTIRoles().has_learner_role(['Member']))
        self.assertEquals(
            False, BLTIRoles().has_learner_role(['Faculty', 'Staff']))

    def test_validate(self):
        blti = {'roles': 'Member'}
        self.assertEquals(None, BLTIRoles().validate(blti, 'member'))
        self.assertRaises(
            BLTIException, BLTIRoles().validate, blti, 'admin')

        blti = {'roles': 'Faculty,Staff'}
        self.assertEquals(None, BLTIRoles().validate(blti, 'member'))
        self.assertEquals(None, BLTIRoles().validate(blti, 'admin'))

class CryptoTest(TestCase):
    test_key = 'DUMMY_KEY_FOR_TESTING_1234567890'
    test_iv = 'DUMMY_IV_TESTING'
    msgs = [
        ('LTI provides a framework through which an LMS can send some '
         'verifiable information about a user to a third party.'),
        "'abc': {'key': value}"
    ]

    def test_encrypt_decrypt(self):
        aes = aes128cbc(self.test_key, self.test_iv)

        for msg in self.msgs:
            enc = aes.encrypt(msg)
            self.assertEquals(aes.decrypt(enc), msg)
<|endoftext|>


<commit_before>"""
conway.py
Author: Tess Snyder
Credit: Mr. Dennison, Mary Feyrer, https://realpython.com/blog/python/pygame-a-primer/
Assignment:
Write and submit a program that plays Conway's Game of Life, per
https://github.com/HHS-IntroProgramming/Conway-Life
"""
from ggame import App, RectangleAsset
from ggame import LineStyle, Color, Sprite, Sound

SCREEN_WIDTH = 640
SCREEN_HEIGHT = 480

black = Color(0x000000, 1.0)
white = Color(0xFFFFFF, 1.0) #color of dead cell
red = Color(0xff0000, 1.0) #color of first day of life
blue = Color(0x0000ff, 1.0) #color of subsequent days of life

line = LineStyle(.1, black)
noline = LineStyle(0, black)

list1=[]

screen=RectangleAsset(400,400, line, white)
Sprite(screen, (0,0))

square = RectangleAsset(20,20, noline, red)

for x in range(0,20):
    for y in range(0,20):
        s=Sprite(square, (20*x,20*y))
        s.visible= False
        list1.append(s)

class Conway(App):

    def __init__(self):
        super().__init__()
        self.listenMouseEvent('click', self.mouseClick)
        self.running = False
        self.listenKeyEvent("keydown", "space", self.spacebar)

    def mouseClick(self, event):
        x=event.x
        y=event.y
        for sprite in list1:
            xsprite =sprite.x
            ysprite=sprite.y
            if x<= xsprite + 10 and x>= xsprite -10 and y<=ysprite + 10 and y>= ysprite -10:
               sprite.visible= True

    def spacebar(self, event):
        self.running = True

    def step(self):
        if self.running == True:
            for sprite1 in list1:
                surroundlist=[]
                listred=[]
                if sprite1.visible==True:
                    listred.append(sprite1)
                for redsprite in listred:
                    if abs(sprite1.x-redsprite.x) >= 20 and abs(sprite1.y-redsprite.y) >= 20:
                        surroundlist.append(redsprite)
                        num=surroundlist.count
                        if num>3:
                            sprite1.visible=False
                        elif num==3:
                            sprite1.visible=True
                        elif num<2:
                            sprite1.visible=False

myapp = Conway()
myapp.run()<commit_msg>Updated from Brython Server: 3/22/2016 2:49:07 PM<commit_after>"""
conway.py
Author: Tess Snyder
Credit: Mr. Dennison, Mary Feyrer, https://realpython.com/blog/python/pygame-a-primer/
Assignment:
Write and submit a program that plays Conway's Game of Life, per
https://github.com/HHS-IntroProgramming/Conway-Life
"""
from ggame import App, RectangleAsset
from ggame import LineStyle, Color, Sprite, Sound

SCREEN_WIDTH = 640
SCREEN_HEIGHT = 480

black = Color(0x000000, 1.0)
white = Color(0xFFFFFF, 1.0) #color of dead cell
red = Color(0xff0000, 1.0) #color of first day of life
blue = Color(0x0000ff, 1.0) #color of subsequent days of life

line = LineStyle(.1, black)
noline = LineStyle(0, black)

list1=[]

screen=RectangleAsset(400,400, line, white)
Sprite(screen, (0,0))

square = RectangleAsset(20,20, noline, red)

for x in range(0,20):
    for y in range(0,20):
        s=Sprite(square, (20*x,20*y))
        s.visible= False
        list1.append(s)

class Conway(App):

    def __init__(self):
        super().__init__()
        self.listenMouseEvent('click', self.mouseClick)
        self.running = False
        self.listenKeyEvent("keydown", "space", self.spacebar)

    def mouseClick(self, event):
        x=event.x
        y=event.y
        for sprite in list1:
            xsprite =sprite.x
            ysprite=sprite.y
            if x<= xsprite + 10 and x>= xsprite -10 and y<=ysprite + 10 and y>= ysprite -10:
               sprite.visible= True

    def spacebar(self, event):
        self.running = True

    def step(self):
        if self.running == True:
            for sprite1 in list1:
                surroundlist=[]
                listred=[]
                for spr in list1:
                    if spr.visible==True:
                        listred.append(spr)
                for redsprite in listred:
                    if abs(sprite1.x-redsprite.x) >= 20 and abs(sprite1.y-redsprite.y) >= 20:
                        surroundlist.append(redsprite)
                        num=surroundlist.count
                        if num>3:
                            sprite1.visible=False
                        elif num==3:
                            sprite1.visible=True
                        elif num<2:
                            sprite1.visible=False

myapp = Conway()
myapp.run()<|endoftext|>