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

import telebot
from telebot import types

TOKEN = '228469213:AAEnb6laZGjxnkTQBbEhZuxdEjHFAqhznzc' #Ponemos nuestro TOKEN generado con el @BotFather
bot = telebot.TeleBot(TOKEN)

Lina H, [05.06.16 11:30]

STATES = {
    'Initial': {
        'message': (
            "Hi there. My name is Hadi :) \n"
            "What language do you want to speak with me? \n"
            "مرحبا! \n"
            "اسمي هادي  بأية لغة تود أن نتحدث؟"
        ),
        'options': [
            'English',
            'العربية',
        ]
    },
    'العربية': {
        'message': (
            "أنا هنا من أجلك. ماذا تفعل؟"
        ),
        'options': [
            "أشعر حتى! :D",
            "لا شعور عظيم اليوم :(",
        ]
    },
    'English': {
        'message': (
            "I'm here for you. What's up?"
        ),
        'options': [
            "I'm feeling up! :D",
            "Not feeling great today :(",
        ]
    },
    "I'm feeling up! :D": {
        'message': (
            "Great!"
            "Would you like to cheer someone up?"
        ),
        'options': [
            "Yeah!",
            "Maybe on another time",
        ]
    },
    "Not feelin great today :(": {
        'message': ('Ooops'),
        'options': []
    },
    "I'm stressed with paperwork": {
        'message': (
            "Sorry, I can't help you with this but I know who can!"
            "You should check Burocrazy, those guys are cool"
        ),
        'options': [

        ]
    },
    "I need help with my paperwork": {
        'message': (
            "Sorry, I can't help you with this but I know who can!"
            "You should check Burocrazy, those guys are cool"
        ),
        'options': [

        ]
    },
    "None of that": {
        'message': (
            "Sorry, I can't help you with this"
        ),
        'options': [

        ]
    },
    "Maybe on another time": {
        'message': (
            "Oops, I'm sorry to hear that"
            "Talk to me later if you want"
        ),
        'options': [

        ]
    },
    "Yeah, I would like talk with someone": {
        'message': (
            "Let me introduce you to my friend Hans!"
        ),
        'options': [

        ]
    },
    "HotLine": {
        'message': (
            "You need inmediate help!"
            "Call the emergency hotline"
            "http://www.suicide.org/hotlines/international/germany-suicide-hotlines.html"
            "http://www.frauenhauskoordinierung.de/english-summary.html"
        ),
        'options': [

        ]
    },
    "Yes, of course!": {
        'message': (
            "Check this web page!"
            "https://www.youtube.com/results?search_query=funny"
        ),
        'options': [

        ]
    },
    "I'm mentally exhausted": {
        'message': (
            "Try an app like HeadSpace to learn how to meditate"
            "https://www.headspace.com/"
        ),
        'options': [

        ]
    },
    "Yeah, I'd love to!": {
        'message': (
            "Here you can find cinemas around you!"
            "https://www.google.com/maps?q=cinema"
        ),
        'options': [

        ]
    },
    "No, not right now": {
        'message': (
            "Here you can find sports events (and more)!"
            "https://www.sportsworld.co.uk/events"
        ),
        'options': [
            "Ok, not bored anymore",
            "Not interested, I'm still bored"
        ]
    },
    "Ok, not bored anymore": {
        'message': (
            "Great!"
        ),
        'options': []
    },
    "Not interested, I'm still bored": {
        'message': (
            "Some ideas for you to enjoy:"
            "https://www.buzzfeed.com/leonoraepstein/things-to-do-when-you-are-bored-out-of-your-mind?utm_term=.qi5x2v7Gd#.qv6JEANy7"
        ),
        'options': []
    },
    "Not really": {
        'message': (
            "Do you prefer to go to cinema?"
        ),
        'options': [
            "Yeah, I'd love to!",
            "Not right now",
        ]
    },
    "Not right now": {
        'message': (
            "What about watching some funny videos?"
        ),
        'options': [
            "Yes, of course!",
            "Not really",
        ]
    },
    "Yeah, sure!": {
        'message': (
            "Check MeetUp! You'll find something you like for sure"
            "http://www.meetup.com/"
        ),
        'options': [

        ]
    },
    "I would like to find cool things to do around": {
        'message': (
            "Check MeetUp! You'll find something you like for sure"
            "http://www.meetup.com/"
        ),
        'options': [

        ]
    },
    "It would be cool to join some activity": {
        'message': (
            "Check MeetUp! You'll find something you like for sure"
            "http://www.meetup.com/"
        ),
        'options': [

        ]
    },
    "I'm bored": {
        'message': (
            "Would you like to meet people with your same interests?"
        ),
        'options': [
            "Yeah, sure!",
            "No, not right now",
        ]
    },
    "I'm tired": {
        'message': (
            "Why are you tired?"
        ),
        'options': [
            "I'm mentally exhausted",
        ]
    },
    "I'm feeling ill": {
        'message': (
            "Look this awesome link to german national health system"
            "http://www.howtogermany.com/pages/healthinsurance.html"
        ),
        'options': [

        ]
    },
    "Not right now": {
        'message': (
            "Watch this inpirational talks on how to deal with feeling lonely"
            "http://www.keepinspiring.me/25-creative-and-surprising-things-to-do-when-you-feel-lonely/"
        ),
        'options': [

        ]
    },
    "Some food would be great": {
        'message': (
            "Try searching at GoogleMaps :)"
            "https://www.google.com/maps"
        ),
        'options': [

        ]
    },
    "I want to go somewhere": {
        'message': (
            "Try searching at GoogleMaps :)"
            "https://www.google.com/maps"
        ),
        'options': [

        ]
    },
    "I miss my country": {
        'message': (
            "It's hard to be away from home"
        ),
        'options': [
            "Some food would be great",
            "I would like to find cool things to do around",
        ]
    },
    "Someone to talk to": {
        'message': (
            "Would you like to meet new people?"
        ),
        'options': [
            "Yeah, I would like talk with someone",
            "It would be cool to join some activity",
            "Not right now",
        ]
    },
    "I feel lonely": {
        'message': (
            "Would you like to meet new people?"
        ),
        'options': [
            "Yeah, I would like talk with someone",
            "It would be cool to join some activity",
            "Not right now",
        ]
    },
    "I'm lost": {
        'message': (
            "What are you looking for?"
        ),
        'options': [
            "I want to go somewhere",
            "I need help with my paperwork",
            "Someone to talk to",
        ]
    },
    "Not feeling great today :(": {
        'message': (
            "So sorry to hear that. "
            "Whats wrong?"
        ),
        'options': [
            "I'm bored",
            "I'm stressed with paperwork",
            "I'm lost",
            "I'm tired",
            "I miss my country",
            "I feel lonely",
            "I'm feeling ill",
            "None of that",
        ]
    },
    "Yeah!": {
        'message': (
            "Let me introduce you to my friend Laila!"
        ),
        'options': [

        ]
    }
}

def generate_markup(state):
    if not state['options']:
        return types.ReplyKeyboardHide(selective=False)

    markup = types.ReplyKeyboardMarkup()
    for option in state['options']:
        markup.row(option)
    return markup

# @bot.message_handler(commands=['start', 'help'])
# def send_welcome(answer):
#     initial = STATES['Initial']
#     markup = generate_markup(initial)
#     if markup:
#         bot.send_message(
#             answer.chat.id,
#             initial['message'],
#             reply_markup=markup)
#     else:
#         bot.send_message(
#             answer.chat.id,
#             initial['message'])

@bot.message_handler(func=lambda m: True)
def handle_message(answer):
    chat_id = answer.chat.id
    next_state = STATES.get(answer.text, STATES['Initial'])
    markup = generate_markup(next_state)
    bot.send_message(
        chat_id,
        next_state['message'],
        reply_markup=markup)

bot.polling(none_stop=True)
<commit_msg>clean shit from copypaste<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import telebot
from telebot import types

TOKEN = '228469213:AAEnb6laZGjxnkTQBbEhZuxdEjHFAqhznzc' #Ponemos nuestro TOKEN generado con el @BotFather
bot = telebot.TeleBot(TOKEN)

STATES = {
    'Initial': {
        'message': (
            "Hi there. My name is Hadi :) \n"
            "What language do you want to speak with me? \n"
            "مرحبا! \n"
            "اسمي هادي  بأية لغة تود أن نتحدث؟"
        ),
        'options': [
            'English',
            'العربية',
        ]
    },
    'العربية': {
        'message': (
            "أنا هنا من أجلك. ماذا تفعل؟"
        ),
        'options': [
            "أشعر حتى! :D",
            "لا شعور عظيم اليوم :(",
        ]
    },
    'English': {
        'message': (
            "I'm here for you. What's up?"
        ),
        'options': [
            "I'm feeling up! :D",
            "Not feeling great today :(",
        ]
    },
    "I'm feeling up! :D": {
        'message': (
            "Great!"
            "Would you like to cheer someone up?"
        ),
        'options': [
            "Yeah!",
            "Maybe on another time",
        ]
    },
    "Not feelin great today :(": {
        'message': ('Ooops'),
        'options': []
    },
    "I'm stressed with paperwork": {
        'message': (
            "Sorry, I can't help you with this but I know who can!"
            "You should check Burocrazy, those guys are cool"
        ),
        'options': [

        ]
    },
    "I need help with my paperwork": {
        'message': (
            "Sorry, I can't help you with this but I know who can!"
            "You should check Burocrazy, those guys are cool"
        ),
        'options': [

        ]
    },
    "None of that": {
        'message': (
            "Sorry, I can't help you with this"
        ),
        'options': [

        ]
    },
    "Maybe on another time": {
        'message': (
            "Oops, I'm sorry to hear that"
            "Talk to me later if you want"
        ),
        'options': [

        ]
    },
    "Yeah, I would like talk with someone": {
        'message': (
            "Let me introduce you to my friend Hans!"
        ),
        'options': [

        ]
    },
    "HotLine": {
        'message': (
            "You need inmediate help!"
            "Call the emergency hotline"
            "http://www.suicide.org/hotlines/international/germany-suicide-hotlines.html"
            "http://www.frauenhauskoordinierung.de/english-summary.html"
        ),
        'options': [

        ]
    },
    "Yes, of course!": {
        'message': (
            "Check this web page!"
            "https://www.youtube.com/results?search_query=funny"
        ),
        'options': [

        ]
    },
    "I'm mentally exhausted": {
        'message': (
            "Try an app like HeadSpace to learn how to meditate"
            "https://www.headspace.com/"
        ),
        'options': [

        ]
    },
    "Yeah, I'd love to!": {
        'message': (
            "Here you can find cinemas around you!"
            "https://www.google.com/maps?q=cinema"
        ),
        'options': [

        ]
    },
    "No, not right now": {
        'message': (
            "Here you can find sports events (and more)!"
            "https://www.sportsworld.co.uk/events"
        ),
        'options': [
            "Ok, not bored anymore",
            "Not interested, I'm still bored"
        ]
    },
    "Ok, not bored anymore": {
        'message': (
            "Great!"
        ),
        'options': []
    },
    "Not interested, I'm still bored": {
        'message': (
            "Some ideas for you to enjoy:"
            "https://www.buzzfeed.com/leonoraepstein/things-to-do-when-you-are-bored-out-of-your-mind?utm_term=.qi5x2v7Gd#.qv6JEANy7"
        ),
        'options': []
    },
    "Not really": {
        'message': (
            "Do you prefer to go to cinema?"
        ),
        'options': [
            "Yeah, I'd love to!",
            "Not right now",
        ]
    },
    "Not right now": {
        'message': (
            "What about watching some funny videos?"
        ),
        'options': [
            "Yes, of course!",
            "Not really",
        ]
    },
    "Yeah, sure!": {
        'message': (
            "Check MeetUp! You'll find something you like for sure"
            "http://www.meetup.com/"
        ),
        'options': [

        ]
    },
    "I would like to find cool things to do around": {
        'message': (
            "Check MeetUp! You'll find something you like for sure"
            "http://www.meetup.com/"
        ),
        'options': [

        ]
    },
    "It would be cool to join some activity": {
        'message': (
            "Check MeetUp! You'll find something you like for sure"
            "http://www.meetup.com/"
        ),
        'options': [

        ]
    },
    "I'm bored": {
        'message': (
            "Would you like to meet people with your same interests?"
        ),
        'options': [
            "Yeah, sure!",
            "No, not right now",
        ]
    },
    "I'm tired": {
        'message': (
            "Why are you tired?"
        ),
        'options': [
            "I'm mentally exhausted",
        ]
    },
    "I'm feeling ill": {
        'message': (
            "Look this awesome link to german national health system"
            "http://www.howtogermany.com/pages/healthinsurance.html"
        ),
        'options': [

        ]
    },
    "Not right now": {
        'message': (
            "Watch this inpirational talks on how to deal with feeling lonely"
            "http://www.keepinspiring.me/25-creative-and-surprising-things-to-do-when-you-feel-lonely/"
        ),
        'options': [

        ]
    },
    "Some food would be great": {
        'message': (
            "Try searching at GoogleMaps :)"
            "https://www.google.com/maps"
        ),
        'options': [

        ]
    },
    "I want to go somewhere": {
        'message': (
            "Try searching at GoogleMaps :)"
            "https://www.google.com/maps"
        ),
        'options': [

        ]
    },
    "I miss my country": {
        'message': (
            "It's hard to be away from home"
        ),
        'options': [
            "Some food would be great",
            "I would like to find cool things to do around",
        ]
    },
    "Someone to talk to": {
        'message': (
            "Would you like to meet new people?"
        ),
        'options': [
            "Yeah, I would like talk with someone",
            "It would be cool to join some activity",
            "Not right now",
        ]
    },
    "I feel lonely": {
        'message': (
            "Would you like to meet new people?"
        ),
        'options': [
            "Yeah, I would like talk with someone",
            "It would be cool to join some activity",
            "Not right now",
        ]
    },
    "I'm lost": {
        'message': (
            "What are you looking for?"
        ),
        'options': [
            "I want to go somewhere",
            "I need help with my paperwork",
            "Someone to talk to",
        ]
    },
    "Not feeling great today :(": {
        'message': (
            "So sorry to hear that. "
            "Whats wrong?"
        ),
        'options': [
            "I'm bored",
            "I'm stressed with paperwork",
            "I'm lost",
            "I'm tired",
            "I miss my country",
            "I feel lonely",
            "I'm feeling ill",
            "None of that",
        ]
    },
    "Yeah!": {
        'message': (
            "Let me introduce you to my friend Laila!"
        ),
        'options': [

        ]
    }
}

def generate_markup(state):
    if not state['options']:
        return types.ReplyKeyboardHide(selective=False)

    markup = types.ReplyKeyboardMarkup()
    for option in state['options']:
        markup.row(option)
    return markup

# @bot.message_handler(commands=['start', 'help'])
# def send_welcome(answer):
#     initial = STATES['Initial']
#     markup = generate_markup(initial)
#     if markup:
#         bot.send_message(
#             answer.chat.id,
#             initial['message'],
#             reply_markup=markup)
#     else:
#         bot.send_message(
#             answer.chat.id,
#             initial['message'])

@bot.message_handler(func=lambda m: True)
def handle_message(answer):
    chat_id = answer.chat.id
    next_state = STATES.get(answer.text, STATES['Initial'])
    markup = generate_markup(next_state)
    bot.send_message(
        chat_id,
        next_state['message'],
        reply_markup=markup)

bot.polling(none_stop=True)
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
"""
Created on Wed Nov 23 08:01:42 2016

<select name="js_dropdown" class="js-dropdown">

     <option value="/menu/?location=17th-and-broadway"
             selected="selected"
             >17th & Broadway</option>

https://www.haleandhearty.com/menu/?location=

@author: gfrazier
"""

import requests
from bs4 import BeautifulSoup
import arrow
import time
import collections
import csv
import os
import string

def get_soup(url):

    html = requests.get(url)

    if html.status_code == 200:
        # soup = BeautifulSoup(html.text, "html.parser") #changed to own function
        soup = extract_soup(html)
        return soup
    else:
        response = html.status_code
        print('{} returned a {} status code.'.format(url, response))
        return none

def extract_soup(html):
        soup = BeautifulSoup(html.text, "html.parser")
        return soup

def get_locations(soup):

    locs = soup.select('option[value^="/menu/?location="]')
    n = len(locs)
    #value iterable from BS4
    locations = [value['value'] for value in locs]
    #print (locations, names)
    return locations

def get_names(soup):
    #extract option tag with value="matching string"
    storeid = soup.find(id="our-menu").find_all('option')
    n = len(storeid)
    names = [storeid[i].text for i in range(n)]
    return names

def get_menu_items(soup):

    #menu = soup.select("menu-item__name")
    items = soup.find_all(class_="menu-item__name") #has tags
    prices = soup.find_all(class_="menu-item__price") #has tags
    n = len(items)

    item_name = [items[i].text for i in range(n)]
    item_price = [prices[i].text for i in range(n)]
    strprice = [item_price[i].strip() for i in range(n)] #strip newlines
    justprice = [strprice[i].strip('\n\n+') for i in range(n)] #remove extra /n bits
    menu_items = dict(zip(item_name, justprice)) #zip into dict with item:price
    return menu_items

def form_url(site, loc):

    url = site + loc

    return url

def write_menu(date, sname, name, menu, path, file):
    file_name = path + file
    with open(file_name, "a") as csv_file:
        writer = csv.writer(csv_file, delimiter=',')

        for item, price in menu.items():
            line = (date, sname, name, item, price)
            writer.writerow(line)

def main():
    path = os.getcwd() + '/'
    date = arrow.now('US/Eastern').format('YYYY-MM-DD')
    site = 'https://www.haleandhearty.com'
    soup = get_soup(site)
    #soup = extract_soup(html)
    locations = get_locations(soup)
    names = get_names(soup)
    for i in locations:
        # loc = (value['value']) #value iterable from BS4
        #loc= ['17th-and-broadway'] #single loc for testing
        url = form_url(site, loc) #list value for testing
        soup = get_soup(url)
        sname = loc[i].strip(r'/menu/?location=')
        name = names[i]
        menu = get_menu_items(soup) #return dictionary
        write_menu(date, sname, name, menu, path, 'day-menu.csv')
        mcount = len(menu)
        print('Store {} has a menu of {} items.'.format(name, mcount))
if __name__ == "__main__":
    main()
<commit_msg>refactored iteration of stores<commit_after># -*- coding: utf-8 -*-
"""
Created on Wed Nov 23 08:01:42 2016

<select name="js_dropdown" class="js-dropdown">

     <option value="/menu/?location=17th-and-broadway"
             selected="selected"
             >17th & Broadway</option>

https://www.haleandhearty.com/menu/?location=

@author: gfrazier
"""

import requests
from bs4 import BeautifulSoup
import arrow
import time
import collections
import csv
import os
import string

def get_soup(url):

    html = requests.get(url)

    if html.status_code == 200:
        # soup = BeautifulSoup(html.text, "html.parser") #changed to own function
        soup = extract_soup(html)
        return soup
    else:
        response = html.status_code
        print('{} returned a {} status code.'.format(url, response))
        return none

def extract_soup(html):
        soup = BeautifulSoup(html.text, "html.parser")
        return soup

def get_locations(soup):

    locs = soup.select('option[value^="/menu/?location="]')
    n = len(locs)
    #value iterable from BS4
    locations = [value['value'] for value in locs]
    #print (locations, names)
    return locations

def get_names(soup):
    #extract option tag with value="matching string"
    storeid = soup.find(id="our-menu").find_all('option')
    n = len(storeid)
    names = [storeid[i].text for i in range(n)]
    return names

def get_menu_items(soup):

    #menu = soup.select("menu-item__name")
    items = soup.find_all(class_="menu-item__name") #has tags
    prices = soup.find_all(class_="menu-item__price") #has tags
    n = len(items)

    item_name = [items[i].text for i in range(n)]
    item_price = [prices[i].text for i in range(n)]
    strprice = [item_price[i].strip() for i in range(n)] #strip newlines
    justprice = [strprice[i].strip('\n\n+') for i in range(n)] #remove extra /n bits
    menu_items = dict(zip(item_name, justprice)) #zip into dict with item:price
    return menu_items

def form_url(site, loc):

    url = site + loc

    return url

def write_menu(date, sname, name, menu, path, file):
    file_name = path + file
    with open(file_name, "a") as csv_file:
        writer = csv.writer(csv_file, delimiter=',')

        for item, price in menu.items():
            line = (date, sname, name, item, price)
            writer.writerow(line)

def main():
    path = os.getcwd() + '/'
    date = arrow.now('US/Eastern').format('YYYY-MM-DD')
    site = 'https://www.haleandhearty.com'
    soup = get_soup(site)
    #soup = extract_soup(html)
    locations = get_locations(soup)
    names = get_names(soup)
    for i in range(len(locations)):
        # loc = (value['value']) #value iterable from BS4
        #loc= ['17th-and-broadway'] #single loc for testing
        loc = locations[i]
        url = form_url(site, loc) #list value for testing
        soup = get_soup(url)
        sname = loc.strip(r'/menu/?location=')
        name = names[i]
        menu = get_menu_items(soup) #return dictionary
        write_menu(date, sname, name, menu, path, 'day-menu.csv')
        mcount = len(menu)
        print('Store {} has a menu of {} items.'.format(name, mcount))
if __name__ == "__main__":
    main()
<|endoftext|>


<commit_before>#to send a file of gcode to the printer
from printrun.printcore import printcore
from printrun import gcoder
<commit_msg>Delete PrintCube.py<commit_after><|endoftext|>


<commit_before>import requests
import xml.etree.ElementTree as ET
import time

TIME_FORMAT = '%Y%m%d'

class Worker:

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

    def initialize(self):
        res = requests.get(self.url_root + 'skema.xml')
        self.root = ET.fromstring(res.text)

    def find_current_schedule(self, timenow=time.gmtime()):
        print("Finding current schedule...")
        for schedule in self.root.findall('aaetlun'):
            start = time.strptime(schedule.get('fra'), TIME_FORMAT)
            end = time.strptime(schedule.get('til'), TIME_FORMAT)
            #timenow = time.gmtime()
            if start < timenow and timenow < end:
                self.current_schedule_url = schedule.get('skra')
                print('...found ' + self.current_schedule_url)

    def download_current_schedule(self):
        if not hasattr(self, 'current_schedule_url'):
            self.find_current_schedule()

        print('Downloading: ' + self.current_schedule_url)<commit_msg>housekeeping<commit_after>import requests
import xml.etree.ElementTree as ET
import time

TIME_FORMAT = '%Y%m%d'

class Worker:

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

    def initialize(self):
        res = requests.get(self.url_root + 'skema.xml')
        self.root = ET.fromstring(res.text)

    def find_current_schedule(self, timenow=time.gmtime()):
        print("Finding current schedule...")
        for schedule in self.root.findall('aaetlun'):
            start = time.strptime(schedule.get('fra'), TIME_FORMAT)
            end = time.strptime(schedule.get('til'), TIME_FORMAT)
            if start < timenow and timenow < end:
                self.current_schedule_url = schedule.get('skra')
                print('...found ' + self.current_schedule_url)

    def download_current_schedule(self):
        if not hasattr(self, 'current_schedule_url'):
            self.find_current_schedule()

        print('Downloading: ' + self.current_schedule_url)<|endoftext|>


<commit_before>"""
	This file includes several data structures used in LeetCode question.
"""
# Definition for a list node.
class ListNode(object):
	def __init__(self, n):
		self.val = n
		self.next = None

def createLinkedList(nodelist):
	#type nodelist: list[int/float]
	#rtype: head of linked list
	linkedList = ListNode(0)
	head = linkedList
	for val in nodelist:
		linkedList.next = ListNode(val)
		linkedList = linkedList.next
	return head.next

def printList(head):
	if not head:
		print "head is None!\n"
		return
	else:
		while head:
			print head.val
			head = head.next
		return

# Definition for a binary tree node.
class TreeNode(object):
	def __init__(self, x):
		self.val = x
		self.left = None
		self.right = None

#TODO finish createBinaryTree
def createBinaryTree(nodelist):
	root = TreeNode(0)
	l = len(nodelist)
	if l == 0:
		return None

n = [1,2,3]
bst = createBinaryTree(n)<commit_msg>Add ifmain<commit_after>"""
	This file includes several data structures used in LeetCode question.
"""
# Definition for a list node.
class ListNode(object):
	def __init__(self, n):
		self.val = n
		self.next = None

def createLinkedList(nodelist):
	#type nodelist: list[int/float]
	#rtype: head of linked list
	linkedList = ListNode(0)
	head = linkedList
	for val in nodelist:
		linkedList.next = ListNode(val)
		linkedList = linkedList.next
	return head.next

def printList(head):
	if not head:
		print "head is None!\n"
		return
	else:
		while head:
			print head.val
			head = head.next
		return

# Definition for a binary tree node.
class TreeNode(object):
	def __init__(self, x):
		self.val = x
		self.left = None
		self.right = None

#TODO finish createBinaryTree
def createBinaryTree(nodelist):
	root = TreeNode(0)
	l = len(nodelist)
	if l == 0:
		return None

if __name__ == '__main__':
# main()
	n = [1,2,3]
	bst = createBinaryTree(n)<|endoftext|>


<commit_before>def configuration(parent_package='', top_path=None):
    from numpy.distutils.misc_util import Configuration

    config = Configuration('TCP', parent_package, top_path)
    config.add_subpackage('common_functions',
            subpackage_path='Software/feature_extract/Code/extractors/common_functions')

    return config

if __name__ == "__main__":
    from numpy.distutils.core import setup

    config = configuration(top_path='').todict()
    setup(**config)
<commit_msg>Fix setup module path<commit_after>def configuration(parent_package='', top_path=None):
    from numpy.distutils.misc_util import Configuration

    config = Configuration('TCP', parent_package, top_path)
    config.add_subpackage('Software.feature_extract.Code.extractors.common_functions')
    return config

if __name__ == "__main__":
    from numpy.distutils.core import setup

    config = configuration(top_path='').todict()
    setup(**config)
<|endoftext|>


<commit_before>from __future__ import absolute_import
# Implementation of RAKE - Rapid Automtic Keyword Exraction algorithm
# as described in:
# Rose, S., D. Engel, N. Cramer, and W. Cowley (2010).
# Automatic keyword extraction from indi-vidual documents.
# In M. W. Berry and J. Kogan (Eds.), Text Mining: Applications and Theory.unknown: John Wiley and Sons, Ltd.

import re
import operator

def is_number(s):
    try:
        float(s) if '.' in s else int(s)
        return True
    except ValueError:
        return False

def SmartStopList():
    from .stoplists import SmartStopList
    return SmartStopList.words()

def FoxStopList():
    from .stoplists import FoxStopList
    return FoxStopList.words()

def MySQLStopList():
    from .stoplists import MySQLStopList
    return MySQLStopList.words()

def NLTKStopList():
    from .stoplists import NLTKStopList
    return NLTKStopList.words()

def load_stop_words(stop_word_file, divide):
    """
    Utility function to load stop words from a file and return as a list of words
    @param stop_word_file Path and file name of a file containing stop words.
    @return list A list of stop words.
    """
    stop_words = []
    for line in open(stop_word_file):
        if divide:
            for word in line.split():  # in case more than one per line
                stop_words.append(word)
    return stop_words

def load_stop_words_delimiter(stop_word_file, delimiter, divide):
    """
    Utility function to load stop words from a file and return as a list of words
    @param stop_word_file Path and file name of a file containing stop words.
    @return list A list of stop words.
    """
    stop_words = []
    if divide:
        for line in open(stop_word_file):
            for word in line.split(delimiter):  # in case more than one per line
                #handles .csvs or a comma seperated list, or equivalent.
                #'asd,'.split(',') returns ['asd', ''] but 'asd '.split() returns 'asd'
                if word !='':
                    stop_words.append(word)
    else:
        for line in open(stop_word_file):
            for word in line.split(delimiter[0]): #motivated as explained right above
                stop_words.append(word)
    return stop_words

def separate_words(text, min_word_return_size):
    """
    Utility function to return a list of all words that are have a length greater than a specified number of characters.
    @param text The text that must be split in to words.
    @param min_word_return_size The minimum no of characters a word must have to be included.
    """
    splitter = re.compile('[^a-zA-Z0-9_\\+\\-/]')
    words = []
    for single_word in splitter.split(text):
        current_word = single_word.strip().lower()
        # leave numbers in phrase, but don't count as words, since they tend to invalidate scores of their phrases
        if len(current_word) > min_word_return_size and current_word != '' and not is_number(current_word):
            words.append(current_word)
    return words

def split_sentences(text):
    """
    Utility function to return a list of sentences.
    @param text The text that must be split in to sentences.
    """
    sentence_delimiters = re.compile(u'[.!?,;:\t\\\\"\\(\\)\\\'\u2019\u2013]|\\s\\-\\s')
    sentences = sentence_delimiters.split(text)
    return sentences

def build_stop_word_regex(stop_word_list):
    stop_word_regex_list = []
    for word in stop_word_list:
        word_regex = r'\b' + word + r'(?![\w-])'
        stop_word_regex_list.append(word_regex)
    stop_word_pattern = re.compile('|'.join(stop_word_regex_list), re.IGNORECASE)
    return stop_word_pattern

def generate_candidate_keywords(sentence_list, stopword_pattern):
    phrase_list = []
    for s in sentence_list:
        tmp = re.sub(stopword_pattern, '|', s.strip())
        phrases = tmp.split("|")
        for phrase in phrases:
            phrase = phrase.strip().lower()
            if phrase != "":
                phrase_list.append(phrase)
    return phrase_list

def calculate_word_scores(phraseList):
    word_frequency = {}
    word_degree = {}
    for phrase in phraseList:
        word_list = separate_words(phrase, 0)
        word_list_length = len(word_list)
        word_list_degree = word_list_length - 1
        for word in word_list:
            word_frequency.setdefault(word, 0)
            word_frequency[word] += 1
            word_degree.setdefault(word, 0)
            word_degree[word] += word_list_degree
    for item in word_frequency:
        word_degree[item] = word_degree[item] + word_frequency[item]

    # Calculate Word scores = deg(w)/frew(w)
    word_score = {}
    for item in word_frequency:
        word_score.setdefault(item, 0)
        word_score[item] = word_degree[item] / (word_frequency[item] * 1.0)
    return word_score

def generate_candidate_keyword_scores(phrase_list, word_score):
    keyword_candidates = {}
    for phrase in phrase_list:
        keyword_candidates.setdefault(phrase, 0)
        word_list = separate_words(phrase, 0)
        candidate_score = 0
        for word in word_list:
            candidate_score += word_score[word]
        keyword_candidates[phrase] = candidate_score
    return keyword_candidates

class Rake(object):
    def __init__(self, stop_words, divide = True, delimiter = ' '):
        #lets users call predefined stopwords easily in a platform agnostic manner or use their own list
        if isinstance(stop_words, list):
            self.__stop_words_pattern = build_stop_word_regex(stop_words)
        else:
            if delimiter == ' ':
                self.__stop_words_pattern = build_stop_word_regex(load_stop_words(stop_words,divide))  # handles normal file paths
            else:
                self.__stop_words_pattern = build_stop_word_regex(load_stop_words_delimiter(stop_words,divide,delimiter))

    def run(self, text):
        sentence_list = split_sentences(text)

        phrase_list = generate_candidate_keywords(sentence_list, self.__stop_words_pattern)

        word_scores = calculate_word_scores(phrase_list)

        keyword_candidates = generate_candidate_keyword_scores(phrase_list, word_scores)

        sorted_keywords = sorted(keyword_candidates.items(), key=operator.itemgetter(1), reverse=True)
        return sorted_keywords
<commit_msg>fixes divide support<commit_after>from __future__ import absolute_import
# Implementation of RAKE - Rapid Automtic Keyword Exraction algorithm
# as described in:
# Rose, S., D. Engel, N. Cramer, and W. Cowley (2010).
# Automatic keyword extraction from indi-vidual documents.
# In M. W. Berry and J. Kogan (Eds.), Text Mining: Applications and Theory.unknown: John Wiley and Sons, Ltd.

import re
import operator

def is_number(s):
    try:
        float(s) if '.' in s else int(s)
        return True
    except ValueError:
        return False

def SmartStopList():
    from .stoplists import SmartStopList
    return SmartStopList.words()

def FoxStopList():
    from .stoplists import FoxStopList
    return FoxStopList.words()

def MySQLStopList():
    from .stoplists import MySQLStopList
    return MySQLStopList.words()

def NLTKStopList():
    from .stoplists import NLTKStopList
    return NLTKStopList.words()

def load_stop_words(stop_word_file, divide):
    """
    Utility function to load stop words from a file and return as a list of words
    @param stop_word_file Path and file name of a file containing stop words.
    @return list A list of stop words.
    """
    stop_words = []
    for line in open(stop_word_file):
        if divide:
            for word in line.split():  # in case more than one per line
                stop_words.append(word)
    return stop_words

def load_stop_words_delimiter(stop_word_file, delimiter):
    """
    Utility function to load stop words from a file and return as a list of words
    @param stop_word_file Path and file name of a file containing stop words.
    @return list A list of stop words.
    """
    stop_words = []
    for line in open(stop_word_file):
        for word in line.split(delimiter):  # in case more than one per line
            #handles .csvs or a comma seperated list, or equivalent.
            #'asd,'.split(',') returns ['asd', ''] but 'asd '.split() returns 'asd'
            if word !='':
                stop_words.append(word)
    return stop_words

def separate_words(text, min_word_return_size):
    """
    Utility function to return a list of all words that are have a length greater than a specified number of characters.
    @param text The text that must be split in to words.
    @param min_word_return_size The minimum no of characters a word must have to be included.
    """
    splitter = re.compile('[^a-zA-Z0-9_\\+\\-/]')
    words = []
    for single_word in splitter.split(text):
        current_word = single_word.strip().lower()
        # leave numbers in phrase, but don't count as words, since they tend to invalidate scores of their phrases
        if len(current_word) > min_word_return_size and current_word != '' and not is_number(current_word):
            words.append(current_word)
    return words

def split_sentences(text):
    """
    Utility function to return a list of sentences.
    @param text The text that must be split in to sentences.
    """
    sentence_delimiters = re.compile(u'[.!?,;:\t\\\\"\\(\\)\\\'\u2019\u2013]|\\s\\-\\s')
    sentences = sentence_delimiters.split(text)
    return sentences

def build_stop_word_regex(stop_word_list):
    stop_word_regex_list = []
    for word in stop_word_list:
        word_regex = r'\b' + word + r'(?![\w-])'
        stop_word_regex_list.append(word_regex)
    stop_word_pattern = re.compile('|'.join(stop_word_regex_list), re.IGNORECASE)
    return stop_word_pattern

def generate_candidate_keywords(sentence_list, stopword_pattern):
    phrase_list = []
    for s in sentence_list:
        tmp = re.sub(stopword_pattern, '|', s.strip())
        phrases = tmp.split("|")
        for phrase in phrases:
            phrase = phrase.strip().lower()
            if phrase != "":
                phrase_list.append(phrase)
    return phrase_list

def calculate_word_scores(phraseList):
    word_frequency = {}
    word_degree = {}
    for phrase in phraseList:
        word_list = separate_words(phrase, 0)
        word_list_length = len(word_list)
        word_list_degree = word_list_length - 1
        for word in word_list:
            word_frequency.setdefault(word, 0)
            word_frequency[word] += 1
            word_degree.setdefault(word, 0)
            word_degree[word] += word_list_degree
    for item in word_frequency:
        word_degree[item] = word_degree[item] + word_frequency[item]

    # Calculate Word scores = deg(w)/frew(w)
    word_score = {}
    for item in word_frequency:
        word_score.setdefault(item, 0)
        word_score[item] = word_degree[item] / (word_frequency[item] * 1.0)
    return word_score

def generate_candidate_keyword_scores(phrase_list, word_score):
    keyword_candidates = {}
    for phrase in phrase_list:
        keyword_candidates.setdefault(phrase, 0)
        word_list = separate_words(phrase, 0)
        candidate_score = 0
        for word in word_list:
            candidate_score += word_score[word]
        keyword_candidates[phrase] = candidate_score
    return keyword_candidates

class Rake(object):
    def __init__(self, stop_words, divide = True, delimiter = ' '):
        #lets users call predefined stopwords easily in a platform agnostic manner or use their own list
        if isinstance(stop_words, list):
            self.__stop_words_pattern = build_stop_word_regex(stop_words)
        else:
            if delimiter != ' ' && divide == False:
                #send error and stop here
                else:
                    if divide == True:
                        self.__stop_words_pattern = build_stop_word_regex(load_stop_words(stop_words,divide))
                    else:
                        self.__stop_words_pattern = build_stop_word_regex(load_stop_words_delimiter(stop_words,delimiter))

    def run(self, text):
        sentence_list = split_sentences(text)

        phrase_list = generate_candidate_keywords(sentence_list, self.__stop_words_pattern)

        word_scores = calculate_word_scores(phrase_list)

        keyword_candidates = generate_candidate_keyword_scores(phrase_list, word_scores)

        sorted_keywords = sorted(keyword_candidates.items(), key=operator.itemgetter(1), reverse=True)
        return sorted_keywords
<|endoftext|>


<commit_before>vLine = "|"
hLine = "-"
spots = dict()
spots["a"] = "a"
spots["b"] = "b"
spots["c"] = "c"
spots["d"] = "d"
spots["e"] = "e"
spots["f"] = "f"
spots["g"] = "g"
spots["h"] = "h"
spots["i"] = "i"
gameXWon = False
gameOWon = False

def firstLine():
    print(" " + spots["a"] + vLine + spots["b"] + vLine + spots["c"])
def hDivider():
    print(hLine + hLine + hLine + hLine + hLine + hLine + hLine)
def secondLine():
    print(" " + spots["d"] + vLine + spots["e"] + vLine + spots["f"])
def thirdLine():
    print(" " + spots["g"] + vLine + spots["h"] + vLine + spots["i"])
def showBoard():
    firstLine()
    hDivider()
    secondLine()
    hDivider()
    thirdLine()
def getInput(check):
    Input = input(check + ": ")
    if Input == "a":
        spots["a"] = check
    if Input == "b":
        spots["b"] = check
    if Input == "c":
        spots["c"] = check
    if Input == "d":
        spots["d"] = check
    if Input == "e":
        spots["e"] = check
    if Input == "f":
        spots["f"] = check
    if Input == "g":
        spots["g"] = check
    if Input == "h":
        spots["h"] = check
    if Input == "i":
        spots["i"] = check

def checkWin(check):
    if spots["a"] == check and spots["b"] == check and spots["c"] == check:
        return True
    if spots["d"] == check and spots["e"] == check and spots["f"] == check:
        return True
    if spots["g"] == check and spots["h"] == check and spots["i"] == check:
        return True
    if spots["a"] == check and spots["d"] == check and spots["g"] == check:
        return True
    if spots["b"] == check and spots["e"] == check and spots["h"] == check:
        return True
    if spots["c"] == check and spots["f"] == check and spots["i"] == check:
        return True
    if spots["a"] == check and spots["e"] == check and spots["i"] == check:
        return True
    if spots["c"] == check and spots["e"] == check and spots["g"] == check:
        return True
    return False

print("This is a very simple text-based tic tac toe game. You will not be playing against a friend of yours,")
print("who will work at the same computer as you. You will see a tic tac toe board like this one:")
showBoard()
print("Just type what corresponding letter you would like to land on. It will start with X and then alternate.")
while gameXWon == False and gameOWon == False:
    getInput("X")
    showBoard()
    if checkWin("X"):
        gameXWon = True
    if gameXWon == False:
        getInput("O")
        showBoard()
        if checkWin("O"):
            gameOWon = True

if gameXWon:
    print("Congratulations! X won the game!")
if gameOWon:
    print("Congratulations! O won the game!")
<commit_msg>Added comments<commit_after>## Tic Tac Toe
## Written in Python3 by Benjamin Massey
## Protected under the MIT License, see the License file for more info.

## Line variables for convenience, no other reason
vLine = "|"
hLine = "-"

## Need a set of spaces that Xs or Os can be placed upon. I am using A-I so that
## it is easy and obvious how the players will enter their moves.
spots = dict()
spots["a"] = "a"
spots["b"] = "b"
spots["c"] = "c"
spots["d"] = "d"
spots["e"] = "e"
spots["f"] = "f"
spots["g"] = "g"
spots["h"] = "h"
spots["i"] = "i"

## Set variables so we can signify when the game is won, and we use two variables
## so that we can check who it is who won.
gameXWon = False
gameOWon = False

## The following five functions are just in order to display the board.
## The first four are for separate sections of the board.
## The function showBoard is using all of the previous functions to show the
## entirety of the board that is being played on.
def firstLine():
    print(" " + spots["a"] + vLine + spots["b"] + vLine + spots["c"])
def hDivider():
    print(hLine + hLine + hLine + hLine + hLine + hLine + hLine)
def secondLine():
    print(" " + spots["d"] + vLine + spots["e"] + vLine + spots["f"])
def thirdLine():
    print(" " + spots["g"] + vLine + spots["h"] + vLine + spots["i"])
def showBoard():
    firstLine()
    hDivider()
    secondLine()
    hDivider()
    thirdLine()

## Get input to see where a player would like to place his X or O.
def getInput(check):
    Input = input(check + ": ")
    if Input == "a":
        spots["a"] = check
    if Input == "b":
        spots["b"] = check
    if Input == "c":
        spots["c"] = check
    if Input == "d":
        spots["d"] = check
    if Input == "e":
        spots["e"] = check
    if Input == "f":
        spots["f"] = check
    if Input == "g":
        spots["g"] = check
    if Input == "h":
        spots["h"] = check
    if Input == "i":
        spots["i"] = check

## The function that will see if any possible win scenario has occurred.
def checkWin(check):
    if spots["a"] == check and spots["b"] == check and spots["c"] == check:
        return True
    if spots["d"] == check and spots["e"] == check and spots["f"] == check:
        return True
    if spots["g"] == check and spots["h"] == check and spots["i"] == check:
        return True
    if spots["a"] == check and spots["d"] == check and spots["g"] == check:
        return True
    if spots["b"] == check and spots["e"] == check and spots["h"] == check:
        return True
    if spots["c"] == check and spots["f"] == check and spots["i"] == check:
        return True
    if spots["a"] == check and spots["e"] == check and spots["i"] == check:
        return True
    if spots["c"] == check and spots["e"] == check and spots["g"] == check:
        return True
    return False

## Basic introduction for the players.
print("This is a very simple text-based tic tac toe game. You will not be playing against a friend of yours,")
print("who will work at the same computer as you. You will see a tic tac toe board like this one:")
showBoard()
print("Just type what corresponding letter you would like to land on. It will start with X and then alternate.")

## The basic loop that actually runs the game. Also checks for a winner each turn.
while gameXWon == False and gameOWon == False:
    getInput("X")
    showBoard()
    if checkWin("X"):
        gameXWon = True
    if gameXWon == False:
        getInput("O")
        showBoard()
        if checkWin("O"):
            gameOWon = True

## Need to check who won the game, and tell the players who that is.
if gameXWon:
    print("Congratulations! X won the game!")
if gameOWon:
    print("Congratulations! O won the game!")
<|endoftext|>


<commit_before>import unittest
from time import sleep
from threading import Timer as TTimer

idle = "idle"
running = "running"
stopped = "stopped"
timeup = "timeup"

class NotRunningError(Exception): pass
class AlreadyRunningError(Exception): pass

class Timer(object):

    def __init__(self):
        self.state = idle

    def start(self, duration=1, whenTimeup=None):
        if self.state == idle or self.state == timeup or self.state == stopped:
            self._timer = TTimer(duration, self._whenTimeup)
            self._timer.start()
            self.state = running
            self._userWhenTimeup = whenTimeup
        elif self.state == running:
            raise AlreadyRunningError

    def _whenTimeup(self):
        self.state = timeup
        if callable(self._userWhenTimeup):
            self._userWhenTimeup()

    def stop(self):
        if self.state == running:
            self.state = stopped
        else:
            raise NotRunningError()

    def isTimeup(self):
        if self.state == timeup:
            return True
        else:
            return False

    def reset(self):
        pass

class TestTimer(unittest.TestCase):

    def assertRunning(self):
        return self.assertEqual(running, self.timer.state)

    def assertIdle(self):
        return self.assertEqual(idle, self.timer.state)

    def assertStopped(self):
        return self.assertEqual(stopped, self.timer.state)

    def whenTimeup(self):
        self.timeupCalled = True

    def setUp(self):
        self.timer = Timer()

    def test_afterInitialisation_TimerIsIdle(self):
        self.assertIdle()

    def test_stoppingWhenIdle_isANotRunningError(self):
        self.assertRaises(NotRunningError, self.timer.stop)

    def test_afterStartingFromIdle_TimerIsRunning(self):
        self.timer.start()
        self.assertRunning()

    def test_startingWhileRunning_isAAlreadyRunningError(self):
        self.timer.start(duration=3)
        self.assertRaises(AlreadyRunningError, self.timer.start)

    def test_afterStarting_timeupIsFalseWhileRunning(self):
        self.timer.start(duration=3)
        self.assertFalse(self.timer.isTimeup())

    def test_afterTimerExpires_TimeUpIsTrue(self):
        self.timer.start(duration=0.05)
        sleep(0.1)
        self.assertTrue(self.timer.isTimeup())

    def test_timerCallsBackWhenTimeExpires(self):
        self.timeupCalled = False
        self.timer.start(duration=0.05, whenTimeup=self.whenTimeup)
        sleep(0.1)
        self.assertTrue(self.timeupCalled)

    def test_startingWhenTimeupRestartsTheTimer(self):
        self.timer.start(duration=0.05)
        while not self.timer.isTimeup():
            pass
        self.timer.start()
        self.assertRunning()

    def test_stoppingWhenTimeup_isNotRunningError(self):
        self.timer.start(duration=0.05)
        while not self.timer.isTimeup():
            pass
        self.assertRaises(NotRunningError, self.timer.stop)

    def test_startingWhenStoppedRestartsTheTimer(self):
        self.timer.start()
        self.timer.stop()
        self.timer.start()
        self.assertRunning()

    def test_resettingFromIdle_DoesNothing(self):
        self.timer.reset()
        self.assertIdle()

    def test_afterStoppingARunningTimer_timerIsStopped(self):
        self.timer.start()
        self.timer.stop()
        self.assertStopped()

if __name__ == "__main__":
    unittest.main()
<commit_msg>+ Refactored production code. + Removed redundant reset method and test.<commit_after>import unittest
from time import sleep
from threading import Timer as TTimer

idle = "idle"
running = "running"
stopped = "stopped"
timeup = "timeup"

class NotRunningError(Exception): pass
class AlreadyRunningError(Exception): pass

class Timer(object):

    def __init__(self):
        self.state = idle

    def start(self, duration=1, whenTimeup=None):
        if self.state == running:
            raise AlreadyRunningError
        else:
            self._timer = TTimer(duration, self._whenTimeup)
            self._timer.start()
            self.state = running
            self._userWhenTimeup = whenTimeup

    def _whenTimeup(self):
        self.state = timeup
        if callable(self._userWhenTimeup):
            self._userWhenTimeup()

    def stop(self):
        if self.state == running:
            self.state = stopped
        else:
            raise NotRunningError()

    def isTimeup(self):
        if self.state == timeup:
            return True
        else:
            return False

class TestTimer(unittest.TestCase):

    def assertRunning(self):
        return self.assertEqual(running, self.timer.state)

    def assertIdle(self):
        return self.assertEqual(idle, self.timer.state)

    def assertStopped(self):
        return self.assertEqual(stopped, self.timer.state)

    def whenTimeup(self):
        self.timeupCalled = True

    def setUp(self):
        self.timer = Timer()

    def test_afterInitialisation_TimerIsIdle(self):
        self.assertIdle()

    def test_stoppingWhenIdle_isANotRunningError(self):
        self.assertRaises(NotRunningError, self.timer.stop)

    def test_afterStartingFromIdle_TimerIsRunning(self):
        self.timer.start()
        self.assertRunning()

    def test_startingWhileRunning_isAAlreadyRunningError(self):
        self.timer.start(duration=3)
        self.assertRaises(AlreadyRunningError, self.timer.start)

    def test_afterStarting_timeupIsFalseWhileRunning(self):
        self.timer.start(duration=3)
        self.assertFalse(self.timer.isTimeup())

    def test_afterTimerExpires_TimeUpIsTrue(self):
        self.timer.start(duration=0.05)
        sleep(0.1)
        self.assertTrue(self.timer.isTimeup())

    def test_timerCallsBackWhenTimeExpires(self):
        self.timeupCalled = False
        self.timer.start(duration=0.05, whenTimeup=self.whenTimeup)
        sleep(0.1)
        self.assertTrue(self.timeupCalled)

    def test_startingWhenTimeupRestartsTheTimer(self):
        self.timer.start(duration=0.05)
        while not self.timer.isTimeup():
            pass
        self.timer.start()
        self.assertRunning()

    def test_stoppingWhenTimeup_isNotRunningError(self):
        self.timer.start(duration=0.05)
        while not self.timer.isTimeup():
            pass
        self.assertRaises(NotRunningError, self.timer.stop)

    def test_startingWhenStoppedRestartsTheTimer(self):
        self.timer.start()
        self.timer.stop()
        self.timer.start()
        self.assertRunning()

    def test_afterStoppingARunningTimer_timerIsStopped(self):
        self.timer.start()
        self.timer.stop()
        self.assertStopped()

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


<commit_before>import unittest

import scipy

try:
    from SloppyCell.lsodar import odeintr
    _HAVE_LSODAR = True
except ImportError:
    _HAVE_LSODAR = False

# XXX: I don't understand the banded jacobian case properly, so I don't have
#      tests for it.

# This test problem is from the lsodar documentation.
def func(y, t):
    ydot = scipy.zeros(3, scipy.float_)
    ydot[0] = -0.04 * y[0] + 1e4*y[1]*y[2]
    ydot[1] = 0.04 * y[0] - 1e4*y[1]*y[2] - 3e7*y[1]**2
    ydot[2] = 3e7*y[1]**2
    return ydot

def Dfun(y, t):
    pd = scipy.zeros((3,3), scipy.float_)
    pd[0,0] = -0.04
    pd[0,1] = 1e4*y[2]
    pd[0,2] = 1e4*y[1]
    pd[1,0] = 0.04
    pd[1,1] = -1e4*y[2] - 6e7*y[1]
    pd[1,2] = -1e4*y[1]
    pd[2,1] = 6e7*y[1]
    return pd

def root_func(y, t):
    out = scipy.zeros(2, scipy.float_)
    out[0] = y[0] - 1e-4
    out[1] = y[2] - 1e-2
    return out

def func_w_args(y, t, arg1, arg2):
    ydot = scipy.zeros(1, scipy.float_)
    ydot[0] = -1
    assert arg1 == 1.2, 'Argument 1 passed incorrectly!'
    assert arg2 == 2.3, 'Argument 2 passed incorrectly1'

    return ydot

y0 = [1.0, 0, 0]
t = scipy.array([0] + [4 * 10**x for x in range(-1, 11)])

class test_lsodar(unittest.TestCase):
    def test_basic(self):
        """ Basic test of lsodar """
        y, tout, te, ye, ie = odeintr(func, y0, t,
                    rtol = 1e-4, atol = [1e-6, 1e-10, 1e-6])

        self.assertAlmostEqual(y[1][1], 3.386380e-5, 4)
        self.assertAlmostEqual(y[6][2], 9.610125e-1, 4)

    def test_Dfun(self):
        """ Test user-supplied Jacobian """
        y, tout, te, ye, ie = odeintr(func, y0, t, Dfun=Dfun,
                                      rtol=1e-4, atol=[1e-6, 1e-10, 1e-6])

        self.assertAlmostEqual(y[1][1], 3.386380e-5, 4)
        self.assertAlmostEqual(y[6][2], 9.610125e-1, 4)

    def test_full_output(self):
        """ Test full_output """
        y, tout, te, ye, ie, info_dict = odeintr(func, y0, t, Dfun=Dfun,
                                                 full_output=True, rtol=1e-4,
                                                 atol=[1e-6, 1e-10, 1e-6])

        for key, val in info_dict.items():
            if key != 'message':
                self.assertEqual(len(y) - 1, len(val))
        self.assertAlmostEqual(y[1][1], 3.386380e-5, 4)
        self.assertAlmostEqual(y[6][2], 9.610125e-1, 4)

    def test_h0(self):
        """ Test h0 """
        # XXX: This only tests that h0 doesn't screw up the integration.
        # It doesn't actually ensure that it is used, since I don't know
        #  how to test for that.
        h0 = 1.
        y, tout, te, ye, ie, info_dict = odeintr(func, y0, t, Dfun=Dfun,
                                                 full_output=True, rtol=1e-4,
                                                 atol=[1e-6, 1e-10, 1e-6],
                                                 h0 = h0)

        self.assertAlmostEqual(y[1][1], 3.386380e-5, 4)
        self.assertAlmostEqual(y[6][2], 9.610125e-1, 4)

    def test_hmax(self):
        """ Test hmax  """
        hmax = 1e8
        y, tout,te, ye, ie, info_dict = odeintr(func, y0, t, Dfun=Dfun,
                                                full_output=True, rtol=1e-4,
                                                atol=[1e-6, 1e-10, 1e-6],
                                                hmax = hmax)

        for hu in info_dict['hu']:
            assert(hu <= hmax)
        self.assertAlmostEqual(y[1][1], 3.386380e-5, 4)
        self.assertAlmostEqual(y[6][2], 9.610125e-1, 4)

    def test_hmin(self):
        """ Test hmin  """
        # XXX: Not sure how to test hmin. Integrator dies if hmin is too small,
        #      and I don't get the smallest stepsize taken in the info_dict.
        hmin = 1e-6
        y, tout, te, ye, ie, info_dict = odeintr(func, y0, t, Dfun=Dfun,
                                                 full_output=True, rtol=1e-4,
                                                 atol=[1e-6, 1e-10, 1e-6],
                                                 hmin = hmin)

        self.assertAlmostEqual(y[1][1], 3.386380e-5, 4)
        self.assertAlmostEqual(y[6][2], 9.610125e-1, 4)

    def test_ixpr(self):
        """ Test ixpr """
        # XXX: Need a different example to text ixpr. This example always uses
        #      stiff methods.
        y, tout, te, ye, ie, info_dict = odeintr(func, y0, t, Dfun=Dfun,
                                                 full_output=True, rtol=1e-4,
                                                 atol=[1e-6, 1e-10, 1e-6],
                                                 ixpr=True)

        self.assertAlmostEqual(y[1][1], 3.386380e-5, 4)
        self.assertAlmostEqual(y[6][2], 9.610125e-1, 4)

    def test_mxstep(self):
        """ Test mxstep """
        # XXX: Annoying that this is expected to print an error message,
        #      at least if I'm following odeint semantics.
        print
        mxstep = 20
        y, tout, te, ye, ie, info_dict = odeintr(func, y0, t, Dfun=Dfun,
                                                 full_output=True, rtol=1e-4,
                                                 atol=[1e-6, 1e-10, 1e-6],
                                                 mxstep=mxstep)

        assert(info_dict['nst'][0] <= mxstep)

    def test_mxhnil(self):
        """ Test mxhnil """
        # XXX: Not sure how to test this.
        mxhnil = 10
        y, tout, te, ye, ie, info_dict = odeintr(func, y0, t, Dfun=Dfun,
                                                 full_output=True, rtol=1e-4,
                                                 atol=[1e-6, 1e-10, 1e-6],
                                                 mxhnil=mxhnil)

        self.assertAlmostEqual(y[1][1], 3.386380e-5, 4)
        self.assertAlmostEqual(y[6][2], 9.610125e-1, 4)

    def test_mxordn(self):
        """ Test mxordn """
        # XXX: Again, need a non-stiff example, and there's no way I can see
        #      what order was actually used
        mxordn = 4
        y, tout, te, ye, ie, info_dict = odeintr(func, y0, t, Dfun=Dfun,
                                                 full_output=True, rtol=1e-4,
                                                 atol=[1e-6, 1e-10, 1e-6],
                                                 mxordn=mxordn)

        self.assertAlmostEqual(y[1][1], 3.386380e-5, 4)
        self.assertAlmostEqual(y[6][2], 9.610125e-1, 4)

    def test_mxords(self):
        """ Test mxords """
        # XXX: There's no way I can see what order was actually used.
        #      I could check that integration goes bad if mxords is too low,
        #      but that seems silly.
        mxords = 3
        y, tout, te, ye, ie, info_dict = odeintr(func, y0, t, Dfun=Dfun,
                                                 full_output=True, rtol=1e-4,
                                                 atol=[1e-6, 1e-10, 1e-6],
                                                 mxords=mxords)

        self.assertAlmostEqual(y[1][1], 3.386380e-5, 4)
        self.assertAlmostEqual(y[6][2], 9.610125e-1, 4)

    def test_mxstep(self):
        """ Test mxstep """
        # XXX: Annoying that this prints messages. Maybe I could capture them?
        print
        y, tout, te, ye, ie, info_dict = odeintr(func, y0, t, Dfun=Dfun,
                                                 full_output=True, rtol=1e-4,
                                                 atol=[1e-6, 1e-10, 1e-6])

    def test_noterm_roots(self):
        """ Test basic root finding """
        root_term = [0, 0]
        y, tout, t_root, y_root, i_root, info_dict = \
                odeintr(func, y0, t, Dfun=Dfun,
                        full_output=True, rtol=1e-4,
                        atol=[1e-6, 1e-10, 1e-6],
                        root_func = root_func, root_term = root_term)

        self.assertAlmostEqual(t_root[0], 2.6400e-1, 4)
        self.assertAlmostEqual(y_root[0][1], 3.470563e-5, 4)
        self.assertEqual(i_root[0], 1)
        self.assertAlmostEqual(y_root[1][1], 4.000395e-10, 4)
        # Negative number of decimal places here...
        self.assertAlmostEqual(t_root[1], 2.0745e7, -3)
        self.assertEqual(i_root[1], 0)

        out_index = tout.index(t[1])
        self.assertAlmostEqual(y[out_index][1], 3.386380e-5, 4)
        out_index = tout.index(t[6])
        self.assertAlmostEqual(y[out_index][2], 9.610125e-1, 4)

    def test_term_roots(self):
        """ Test root finding with termination """
        root_term = [1, 0]
        y, tout, t_root, y_root, i_root, info_dict = \
                odeintr(func, y0, t, Dfun=Dfun,
                        full_output=True, rtol=1e-4,
                        atol=[1e-6, 1e-10, 1e-6],
                        root_func = root_func, root_term = root_term,
                        insert_events = True)

        self.assertAlmostEqual(t_root[0], 2.6400e-1, 4)
        self.assertAlmostEqual(y_root[0][1], 3.470563e-5, 4)
        self.assertEqual(i_root[0], 1)
        self.assertAlmostEqual(y_root[1][1], 4.000395e-10, 4)
        # Negative number of decimal places here...
        self.assertAlmostEqual(t_root[1], 2.0745e7, -3)
        self.assertEqual(i_root[1], 0)
        # Check that we stopped at the proper point
        self.assertEqual(tout[-1], t_root[1])
        self.assertEqual(y[-1][0], y_root[1][0])

    def test_int_pts(self):
        """ Test intermediate output """
        y, tout, te, ye, ie = odeintr(func, y0, t,
                                      rtol = 1e-4, atol = [1e-6, 1e-10, 1e-6],
                                      int_pts = True)

        out_index = tout.index(t[1])
        self.assertAlmostEqual(y[out_index][1], 3.386380e-5, 4)
        out_index = tout.index(t[6])
        self.assertAlmostEqual(y[out_index][2], 9.610125e-1, 4)
        self.assertEqual(t[-1], tout[-1])

    def test_tcrit(self):
        """ Test critical times """
        # Really only tests that tcrit doesn't mess up integration. The
        #  success of the int_pts test suggests, however, that tcrit is
        #  working properly.
        y, tout, te, ye, ie = odeintr(func, y0, t,
                    rtol = 1e-4, atol = [1e-6, 1e-10, 1e-6],
                    tcrit = [0.2, 0.9, 1.7])

        self.assertAlmostEqual(y[1][1], 3.386380e-5, 4)
        self.assertAlmostEqual(y[6][2], 9.610125e-1, 4)

    def test_int_pts_and_tcrit(self):
        """ Test intermediate output """
        y, tout, te, ye, ie = odeintr(func, y0, t,
                                      rtol = 1e-4, atol = [1e-6, 1e-10, 1e-6],
                                      tcrit = [0.2, 0.9, 1.7],
                                      int_pts = True)

        out_index = tout.index(t[1])
        self.assertAlmostEqual(y[out_index][1], 3.386380e-5, 4)
        out_index = tout.index(t[6])
        self.assertAlmostEqual(y[out_index][2], 9.610125e-1, 4)
        self.assertEqual(t[-1], tout[-1])

    def test_first_deriv(self):
        """ Test derivative output of 1st derivative """
        y, tout, te, ye, ie, dout = odeintr(func, y0, t,
                                      rtol = 1e-4, atol = [1e-6, 1e-10, 1e-6],
                                      return_derivs = True,
                                      int_pts = True)

        out_index = tout.index(t[1])
        # This is out_index-1 because we can't return derivative information
        #  easily for the initial point.
        int_deriv = dout[out_index]
        func_deriv = func(y[out_index], tout[out_index])
        for ii in range(len(y0)):
            self.assertAlmostEqual(int_deriv[ii], func_deriv[ii], 6)

    def test_args(self):
        """ Test passing of additional function arguments """
        def func_w_args(y, t, arg1, arg2):
            assert arg1 == 1.2, 'Argument 1 passed incorrectly!'
            assert arg2 == 2.3, 'Argument 2 passed incorrectly1'
            return [-1]

        y = odeintr(func_w_args, [0], t, args = (1.2, 2.3))

no_lsodar_msg = 'lsodar not working!'
if _HAVE_LSODAR:
    suite = unittest.makeSuite(test_lsodar)
else:
    message = no_lsodar_msg

if __name__ == '__main__':
    if _HAVE_LSODAR:
        unittest.main()
    else:
        print no_lsodar_msg
<commit_msg>Removal of lsodar-related files<commit_after><|endoftext|>


<commit_before>"""Provide the RedditorListingMixin class."""
from urllib.parse import urljoin

from ....util.cache import cachedproperty
from ..generator import ListingGenerator
from .base import BaseListingMixin
from .gilded import GildedListingMixin

class RedditorListingMixin(BaseListingMixin, GildedListingMixin):
    """Adds additional methods pertaining to Redditor instances."""

    @cachedproperty
    def comments(self):
        r"""Provide an instance of :class:`.SubListing` for comment access.

        For example, to output the first line of all new comments by
        ``/u/spez`` try:

        .. code:: python

           for comment in reddit.redditor('spez').comments.new(limit=None):
               print(comment.body.split('\n', 1)[0][:79])

        """
        return SubListing(self._reddit, self._path, "comments")

    @cachedproperty
    def submissions(self):
        """Provide an instance of :class:`.SubListing` for submission access.

        For example, to output the title's of top 100 of all time submissions
        for ``/u/spez`` try:

        .. code:: python

           for submission in reddit.redditor('spez').submissions.top('all'):
               print(submission.title)

        """
        return SubListing(self._reddit, self._path, "submitted")

    def downvoted(self, **generator_kwargs):
        """Return a :class:`.ListingGenerator` for items the user has downvoted.

        May raise ``prawcore.Forbidden`` after issuing the request if the user
        is not authorized to access the list. Note that because this function
        returns a :class:`.ListingGenerator` the exception may not occur until
        sometime after this function has returned.

        Additional keyword arguments are passed in the initialization of
        :class:`.ListingGenerator`.

        """
        return ListingGenerator(
            self._reddit, urljoin(self._path, "downvoted"), **generator_kwargs
        )

    def gildings(self, **generator_kwargs):
        """Return a :class:`.ListingGenerator` for items the user has gilded.

        May raise ``prawcore.Forbidden`` after issuing the request if the user
        is not authorized to access the list. Note that because this function
        returns a :class:`.ListingGenerator` the exception may not occur until
        sometime after this function has returned.

        Additional keyword arguments are passed in the initialization of
        :class:`.ListingGenerator`.

        """
        return ListingGenerator(
            self._reddit,
            urljoin(self._path, "gilded/given"),
            **generator_kwargs
        )

    def hidden(self, **generator_kwargs):
        """Return a :class:`.ListingGenerator` for items the user has hidden.

        May raise ``prawcore.Forbidden`` after issuing the request if the user
        is not authorized to access the list. Note that because this function
        returns a :class:`.ListingGenerator` the exception may not occur until
        sometime after this function has returned.

        Additional keyword arguments are passed in the initialization of
        :class:`.ListingGenerator`.

        """
        return ListingGenerator(
            self._reddit, urljoin(self._path, "hidden"), **generator_kwargs
        )

    def saved(self, **generator_kwargs):
        """Return a :class:`.ListingGenerator` for items the user has saved.

        May raise ``prawcore.Forbidden`` after issuing the request if the user
        is not authorized to access the list. Note that because this function
        returns a :class:`.ListingGenerator` the exception may not occur until
        sometime after this function has returned.

        Additional keyword arguments are passed in the initialization of
        :class:`.ListingGenerator`.

        """
        return ListingGenerator(
            self._reddit, urljoin(self._path, "saved"), **generator_kwargs
        )

    def upvoted(self, **generator_kwargs):
        """Return a :class:`.ListingGenerator` for items the user has upvoted.

        May raise ``prawcore.Forbidden`` after issuing the request if the user
        is not authorized to access the list. Note that because this function
        returns a :class:`.ListingGenerator` the exception may not occur until
        sometime after this function has returned.

        Additional keyword arguments are passed in the initialization of
        :class:`.ListingGenerator`.

        """
        return ListingGenerator(
            self._reddit, urljoin(self._path, "upvoted"), **generator_kwargs
        )

class SubListing(BaseListingMixin):
    """Helper class for generating :class:`.ListingGenerator` objects."""

    def __init__(self, reddit, base_path, subpath):
        """Initialize a SubListing instance.

        :param reddit: An instance of :class:`.Reddit`.
        :param base_path: The path to the object up to this point.
        :param subpath: The additional path to this sublisting.

        """
        super().__init__(reddit, _data=None)
        self._listing_use_sort = True
        self._reddit = reddit
        self._path = urljoin(base_path, subpath)
<commit_msg>(#1164) Add types to listing/mixins/redditor.py (#1206)<commit_after>"""Provide the RedditorListingMixin class."""
from typing import Any, Dict, Generator, TypeVar, Union
from urllib.parse import urljoin

from ....util.cache import cachedproperty
from ..generator import ListingGenerator
from .base import BaseListingMixin
from .gilded import GildedListingMixin

Reddit = TypeVar("Reddit")

class SubListing(BaseListingMixin):
    """Helper class for generating :class:`.ListingGenerator` objects."""

    def __init__(self, reddit: Reddit, base_path: str, subpath: str):
        """Initialize a SubListing instance.

        :param reddit: An instance of :class:`.Reddit`.
        :param base_path: The path to the object up to this point.
        :param subpath: The additional path to this sublisting.

        """
        super().__init__(reddit, _data=None)
        self._listing_use_sort = True
        self._reddit = reddit
        self._path = urljoin(base_path, subpath)

class RedditorListingMixin(BaseListingMixin, GildedListingMixin):
    """Adds additional methods pertaining to Redditor instances."""

    @cachedproperty
    def comments(self) -> SubListing:
        r"""Provide an instance of :class:`.SubListing` for comment access.

        For example, to output the first line of all new comments by
        ``/u/spez`` try:

        .. code:: python

           for comment in reddit.redditor('spez').comments.new(limit=None):
               print(comment.body.split('\n', 1)[0][:79])

        """
        return SubListing(self._reddit, self._path, "comments")

    @cachedproperty
    def submissions(self) -> SubListing:
        """Provide an instance of :class:`.SubListing` for submission access.

        For example, to output the title's of top 100 of all time submissions
        for ``/u/spez`` try:

        .. code:: python

           for submission in reddit.redditor('spez').submissions.top('all'):
               print(submission.title)

        """
        return SubListing(self._reddit, self._path, "submitted")

    def downvoted(
        self, **generator_kwargs: Union[str, int, Dict[str, str]]
    ) -> Generator[Any, None, None]:
        """Return a :class:`.ListingGenerator` for items the user has downvoted.

        May raise ``prawcore.Forbidden`` after issuing the request if the user
        is not authorized to access the list. Note that because this function
        returns a :class:`.ListingGenerator` the exception may not occur until
        sometime after this function has returned.

        Additional keyword arguments are passed in the initialization of
        :class:`.ListingGenerator`.

        """
        return ListingGenerator(
            self._reddit, urljoin(self._path, "downvoted"), **generator_kwargs
        )

    def gildings(
        self, **generator_kwargs: Union[str, int, Dict[str, str]]
    ) -> Generator[Any, None, None]:
        """Return a :class:`.ListingGenerator` for items the user has gilded.

        May raise ``prawcore.Forbidden`` after issuing the request if the user
        is not authorized to access the list. Note that because this function
        returns a :class:`.ListingGenerator` the exception may not occur until
        sometime after this function has returned.

        Additional keyword arguments are passed in the initialization of
        :class:`.ListingGenerator`.

        """
        return ListingGenerator(
            self._reddit,
            urljoin(self._path, "gilded/given"),
            **generator_kwargs
        )

    def hidden(
        self, **generator_kwargs: Union[str, int, Dict[str, str]]
    ) -> Generator[Any, None, None]:
        """Return a :class:`.ListingGenerator` for items the user has hidden.

        May raise ``prawcore.Forbidden`` after issuing the request if the user
        is not authorized to access the list. Note that because this function
        returns a :class:`.ListingGenerator` the exception may not occur until
        sometime after this function has returned.

        Additional keyword arguments are passed in the initialization of
        :class:`.ListingGenerator`.

        """
        return ListingGenerator(
            self._reddit, urljoin(self._path, "hidden"), **generator_kwargs
        )

    def saved(
        self, **generator_kwargs: Union[str, int, Dict[str, str]]
    ) -> Generator[Any, None, None]:
        """Return a :class:`.ListingGenerator` for items the user has saved.

        May raise ``prawcore.Forbidden`` after issuing the request if the user
        is not authorized to access the list. Note that because this function
        returns a :class:`.ListingGenerator` the exception may not occur until
        sometime after this function has returned.

        Additional keyword arguments are passed in the initialization of
        :class:`.ListingGenerator`.

        """
        return ListingGenerator(
            self._reddit, urljoin(self._path, "saved"), **generator_kwargs
        )

    def upvoted(
        self, **generator_kwargs: Union[str, int, Dict[str, str]]
    ) -> Generator[Any, None, None]:
        """Return a :class:`.ListingGenerator` for items the user has upvoted.

        May raise ``prawcore.Forbidden`` after issuing the request if the user
        is not authorized to access the list. Note that because this function
        returns a :class:`.ListingGenerator` the exception may not occur until
        sometime after this function has returned.

        Additional keyword arguments are passed in the initialization of
        :class:`.ListingGenerator`.

        """
        return ListingGenerator(
            self._reddit, urljoin(self._path, "upvoted"), **generator_kwargs
        )
<|endoftext|>


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

"""

Copyright [1999-2016] EMBL-European Bioinformatics Institute

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.

"""

"""

	Please email comments or questions to the public Ensembl
	developers list at <http://lists.ensembl.org/mailman/listinfo/dev>.

	Questions may also be sent to the Ensembl help desk at
	<http://www.ensembl.org/Help/Contact>.

"""
import sys
import argparse
import collections
import json
import sqlite3
import re
import logging
import logging.config

import postgap
import postgap.GWAS
import postgap.Cisreg
import postgap.Reg
import postgap.EFO
import postgap.Globals
import postgap.Integration
from postgap.Utils import *

import sys
from pprint import pformat

'''
Development TODO list:

A. Datasets to integrate:
	-Cisregulatory annotations:
		PCHIC (STOPGAP Scoring: Single cell line: +1, multiple cell lines: +2)

	-Epigenetic activity:
		PhyloP (STOPGAP Scoring: FPR 0-0.6: +2, 0.6-0.85: +1,0.85-1: +0)

B. Code improvements:
	Pathways analysis (Downstream)
	Take into account population composition in LD calcs

C. Model improvements:
	Replace PICS with Bayesian model
	Fine mapping of summary data
	Tissue selection

'''

def main():
	"""

		Reads commandline parameters, prints corresponding associated genes with evidence info

	"""

	logging.config.fileConfig('configuration/logging.conf')
	logger = logging.getLogger(__name__)

	options = get_options()

	logger.info("Starting postagp with the following options:")
	logger.info(pformat(options))

	efo_iris = []

	if options.efos is not None:
		efo_iris = query_iris_for_efo_short_form_list(options.efos)

	if options.efos is None:
		efo_iris = filter(lambda X: X is not None, (postgap.EFO.suggest(disease) for disease in options.diseases))

	# Expand list of EFOs to children, concatenate, remove duplicates
	expanded_efo_iris = efo_iris + concatenate(map(postgap.EFO.children, efo_iris))

	if len(options.diseases) > 0 or len(expanded_efo_iris) > 0:
		res = postgap.Integration.diseases_to_genes(options.diseases, expanded_efo_iris, "CEPH", options.tissues)
	elif options.rsID is not None:
		res = postgap.Integration.rsIDs_to_genes(options.rsID, options.tissues)
	elif options.coords is not None:
		snp = postgap.DataModel.SNP(rsID = options.coords[0], chrom = options.coords[1], pos = int(options.coords[2]))
		if options.tissues is None:
			options.tissues = ["Whole_Blood"]
		res = postgap.Integration.ld_snps_to_genes([snp], options.tissues)

	if options.db is None:
		if options.output is None:
			output = sys.stdout
		else:
			output = open(options.output, "w")

		if options.json_output:
			formatted_results = json.dumps(objectToDict(res))
		elif options.rsID is None and options.coords is None:
			formatted_results = pretty_output(res)
		else:
			formatted_results = pretty_snp_output(res)

		output.write(formatted_results + "\n")
	else:
		db_output(options.db, res)

def query_iris_for_efo_short_form_list(efo_short_form_list):

	iri_list = []
	for efo_short_form in efo_short_form_list:
		iri_list += query_iris_for_efo_short_form(efo_short_form)

	return iri_list

def query_iris_for_efo_short_form(efo_short_form):

	server = 'http://www.ebi.ac.uk'
	ext = "/ols/api/ontologies/efo/terms?short_form=%s" % (efo_short_form)
	result = postgap.REST.get(server, ext)
	terms = result['_embedded']['terms']
	iri_terms = [ term['iri'] for term in terms ]
	return iri_terms

def get_options():
    """

        Reads commandline parameters
        Returntype:
            {
                diseases: [ string ],
                populations: [ string ],
                tissues: [ string ],
            }

    """
    parser = argparse.ArgumentParser(description='Search GWAS/Regulatory/Cis-regulatory databases for causal genes')
    parser.add_argument('--efos', nargs='*')
    parser.add_argument('--diseases', nargs='*')
    parser.add_argument('--rsID')
    parser.add_argument('--coords', nargs=3)
    # parser.add_argument('--populations', nargs='*', default=['1000GENOMES:phase_3:GBR'])
    parser.add_argument('--tissues', nargs='*')
    parser.add_argument('--output')
    parser.add_argument('--db')
    parser.add_argument('--species', nargs='*', default = 'Human')
    parser.add_argument('--database_dir', dest = 'databases', default = 'databases')
    parser.add_argument('--debug', '-g', action = 'store_true')
    parser.add_argument('--json_output', '-j', action = 'store_true')
    options = parser.parse_args()

    postgap.Globals.DATABASES_DIR = options.databases
    postgap.Globals.SPECIES = options.species
    postgap.Globals.DEBUG = postgap.Globals.DEBUG or options.debug

    assert postgap.Globals.DATABASES_DIR is not None
    assert options.rsID is None or (options.efos is None and options.diseases is None)
    assert options.rsID is not None or options.efos is not None or options.diseases is not None or options.coords is not None

    if options.diseases is None:
        options.diseases = []

    return options

def pretty_snp_output(associations):
	"""

		Prints association stats in roughly the same format as STOPGAP for a cluster of SNPs
		Args: [ GeneSNP_Association ]
		Returntype: String

	"""
	header = "\t".join(['snp_rsID', 'chrom', 'pos', 'gene_symbol', 'gene_id', 'score'] + [obj.display_name for obj in postgap.Cisreg.sources + postgap.Reg.sources])
	content = map(pretty_snp_association, associations)
	return "\n".join([header] + content) + "\n"

def pretty_snp_association(association):
	"""

		Prints association stats in roughly the same format as STOPGAP for a cluster of SNPs
		Args: GeneSNP_Association
		Returntype: String

	"""
	snp = association.snp
	gene_name = association.gene.name
	gene_id = association.gene.id
	score = association.score

	results = [snp.rsID, snp.chrom, str(snp.pos), gene_name, gene_id, str(score)]
	results += [str(association.intermediary_scores[functional_source.display_name]) for functional_source in postgap.Cisreg.sources]
	return "\t".join(results)

def pretty_output(associations):
	"""

		Prints association stats in roughly the same format as STOPGAP for a cluster of SNPs
		Args: [ GeneCluster_Association ]
		Returntype: String

	"""
	header = "\t".join(['ld_snp_rsID', 'chrom', 'pos', 'gene_symbol', 'gene_id', 'gene_chrom', 'gene_tss', 'disease_names', 'disease_efo_ids', 'score', 'rank', 'r2', 'gwas_snp_ids', 'ls_snp_is_gwas_snp', 'vep_terms'] + [source.display_name for source in postgap.GWAS.sources + postgap.Cisreg.sources + postgap.Reg.sources])
	content = map(pretty_cluster_association, associations)
	return "\n".join([header] + content)

def pretty_cluster_association(association):
	"""

		Prints association stats in roughly the same format as STOPGAP for a cluster of SNPs
		Args: GeneCluster_Association
		Returntype: String

	"""
	results = genecluster_association_table(association)
	return "\n".join("\t".join(map(unicode, row)) for row in results)

def db_output(db, associations):
	"""

		Writes association stats into DB
		Args1: string, DB name
		Args2: [ GeneCluster_Association ]

	"""
	conn = sqlite3.connect(db)
	table_sql = '''CREATE TABLE IF NOT EXISTS results (
		ld_snp_rsID TEXT,
		chrom TEXT,
		pos INT,
		gene_symbol TEXT,
		gene_id TEXT,
		gene_chrom TEXT,
		gene_tss INT,
		disease_names TEXT,
		disease_efo_ids TEXT,
		score REAL,
		gwas_snp_ids TEXT,
		ls_snp_is_gwas_snp INT,
		vep_terms TEXT,''' + ",".join([re.sub(" ", "_", source.display_name) + " INT\n" for source in postgap.GWAS.sources + postgap.Cisreg.sources + postgap.Reg.sources]) + ")"
	conn.execute(table_sql)
	map(lambda association: db_output_association(conn, association), associations)
	conn.commit()
	conn.close()

def db_output_association(conn, association):
	"""

		Prints association stats in roughly the same format as STOPGAP for a cluster of SNPs
		Arg1: SQLITE3 connection
		Arg2: GeneCluster_Association
		Returntype: String

	"""
	results = genecluster_association_table(association)
	sql_cmd = "INSERT INTO results VALUES (%s)" % ",".join("?" for item in results)
	conn.execute(sql_cmd, results)

def genecluster_association_table(association):
	"""

		Returns association stats in roughly the same format as STOPGAP for a cluster of SNPs
		Arg1: GeneCluster_Association
		Returntype: [[ string or float ]]

	"""
	gene_name = association.gene.name
	gene_id = association.gene.id
	gene_chrom = association.gene.chrom
	gene_tss = association.gene.tss
	cluster = association.cluster
	gwas_snps = cluster.gwas_snps
	gwas_snp_rsIDs = [gwas_snp.snp.rsID for gwas_snp in gwas_snps]
	disease_names = list(set(gwas_association.disease.name for gwas_snp in gwas_snps for gwas_association in gwas_snp.evidence))
	disease_efos = list(set(gwas_association.disease.efo for gwas_snp in gwas_snps for gwas_association in gwas_snp.evidence))

	gwas_scores = collections.defaultdict(lambda: collections.defaultdict(lambda: 1))
	for gwas_snp in gwas_snps:
		for gwas_association in gwas_snp.evidence:
			if gwas_association.pvalue < gwas_scores[gwas_association.source][gwas_snp.snp.rsID]:
				gwas_scores[gwas_association.source][gwas_snp.snp.rsID] = gwas_association.pvalue

	results = []
	for gene_snp_association in association.evidence:
		vep_terms = "N/A"
		for evidence in gene_snp_association.cisregulatory_evidence:
			if evidence.source == "VEP":
				vep_terms = ",".join(evidence.info['consequence_terms'])
				break

		row = [
				gene_snp_association.snp.rsID,
				gene_snp_association.snp.chrom,
				gene_snp_association.snp.pos,
				gene_name,
				gene_id,
				gene_chrom,
				gene_tss,
				",".join(disease_names),
				",".join(disease_efos),
				gene_snp_association.score,
				gene_snp_association.rank,
				association.r2,
				",".join(gwas_snp.snp.rsID for gwas_snp in gwas_snps),
				int(gene_snp_association.snp.rsID in gwas_snp_rsIDs),
				vep_terms
			]

		for gwas_source in postgap.GWAS.sources:
			row.append(",".join(str(gwas_scores[gwas_source.display_name][gwas_snp.snp.rsID]) for gwas_snp in cluster.gwas_snps))
		row += [gene_snp_association.intermediary_scores[functional_source.display_name] for functional_source in postgap.Cisreg.sources + postgap.Reg.sources]
		results.append(row)

	return results

if __name__ == "__main__":
	main()
<commit_msg>Updated output of GWAS stats, closes #79, closes #72, closes #67<commit_after>#! /usr/bin/env python

"""

Copyright [1999-2016] EMBL-European Bioinformatics Institute

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.

"""

"""

	Please email comments or questions to the public Ensembl
	developers list at <http://lists.ensembl.org/mailman/listinfo/dev>.

	Questions may also be sent to the Ensembl help desk at
	<http://www.ensembl.org/Help/Contact>.

"""
import sys
import argparse
import collections
import json
import sqlite3
import re
import logging
import logging.config

import postgap
import postgap.GWAS
import postgap.Cisreg
import postgap.Reg
import postgap.EFO
import postgap.Globals
import postgap.Integration
from postgap.Utils import *

import sys
from pprint import pformat

'''
Development TODO list:

A. Datasets to integrate:
	-Cisregulatory annotations:
		PCHIC (STOPGAP Scoring: Single cell line: +1, multiple cell lines: +2)

	-Epigenetic activity:
		PhyloP (STOPGAP Scoring: FPR 0-0.6: +2, 0.6-0.85: +1,0.85-1: +0)

B. Code improvements:
	Pathways analysis (Downstream)
	Take into account population composition in LD calcs

C. Model improvements:
	Replace PICS with Bayesian model
	Fine mapping of summary data
	Tissue selection

'''

def main():
	"""

		Reads commandline parameters, prints corresponding associated genes with evidence info

	"""

	logging.config.fileConfig('configuration/logging.conf')
	logger = logging.getLogger(__name__)

	options = get_options()

	logger.info("Starting postagp with the following options:")
	logger.info(pformat(options))

	efo_iris = []

	if options.efos is not None:
		efo_iris = query_iris_for_efo_short_form_list(options.efos)

	if options.efos is None:
		efo_iris = filter(lambda X: X is not None, (postgap.EFO.suggest(disease) for disease in options.diseases))

	# Expand list of EFOs to children, concatenate, remove duplicates
	expanded_efo_iris = efo_iris + concatenate(map(postgap.EFO.children, efo_iris))

	if len(options.diseases) > 0 or len(expanded_efo_iris) > 0:
		res = postgap.Integration.diseases_to_genes(options.diseases, expanded_efo_iris, "CEPH", options.tissues)
	elif options.rsID is not None:
		res = postgap.Integration.rsIDs_to_genes(options.rsID, options.tissues)
	elif options.coords is not None:
		snp = postgap.DataModel.SNP(rsID = options.coords[0], chrom = options.coords[1], pos = int(options.coords[2]))
		if options.tissues is None:
			options.tissues = ["Whole_Blood"]
		res = postgap.Integration.ld_snps_to_genes([snp], options.tissues)

	if options.db is None:
		if options.output is None:
			output = sys.stdout
		else:
			output = open(options.output, "w")

		if options.json_output:
			formatted_results = json.dumps(objectToDict(res))
		elif options.rsID is None and options.coords is None:
			formatted_results = pretty_output(res)
		else:
			formatted_results = pretty_snp_output(res)

		output.write(formatted_results + "\n")
	else:
		db_output(options.db, res)

def query_iris_for_efo_short_form_list(efo_short_form_list):

	iri_list = []
	for efo_short_form in efo_short_form_list:
		iri_list += query_iris_for_efo_short_form(efo_short_form)

	return iri_list

def query_iris_for_efo_short_form(efo_short_form):

	server = 'http://www.ebi.ac.uk'
	ext = "/ols/api/ontologies/efo/terms?short_form=%s" % (efo_short_form)
	result = postgap.REST.get(server, ext)
	terms = result['_embedded']['terms']
	iri_terms = [ term['iri'] for term in terms ]
	return iri_terms

def get_options():
    """

        Reads commandline parameters
        Returntype:
            {
                diseases: [ string ],
                populations: [ string ],
                tissues: [ string ],
            }

    """
    parser = argparse.ArgumentParser(description='Search GWAS/Regulatory/Cis-regulatory databases for causal genes')
    parser.add_argument('--efos', nargs='*')
    parser.add_argument('--diseases', nargs='*')
    parser.add_argument('--rsID')
    parser.add_argument('--coords', nargs=3)
    # parser.add_argument('--populations', nargs='*', default=['1000GENOMES:phase_3:GBR'])
    parser.add_argument('--tissues', nargs='*')
    parser.add_argument('--output')
    parser.add_argument('--db')
    parser.add_argument('--species', nargs='*', default = 'Human')
    parser.add_argument('--database_dir', dest = 'databases', default = 'databases')
    parser.add_argument('--debug', '-g', action = 'store_true')
    parser.add_argument('--json_output', '-j', action = 'store_true')
    options = parser.parse_args()

    postgap.Globals.DATABASES_DIR = options.databases
    postgap.Globals.SPECIES = options.species
    postgap.Globals.DEBUG = postgap.Globals.DEBUG or options.debug

    assert postgap.Globals.DATABASES_DIR is not None
    assert options.rsID is None or (options.efos is None and options.diseases is None)
    assert options.rsID is not None or options.efos is not None or options.diseases is not None or options.coords is not None

    if options.diseases is None:
        options.diseases = []

    return options

def pretty_snp_output(associations):
	"""

		Prints association stats in roughly the same format as STOPGAP for a cluster of SNPs
		Args: [ GeneSNP_Association ]
		Returntype: String

	"""
	header = "\t".join(['snp_rsID', 'chrom', 'pos', 'gene_symbol', 'gene_id', 'score'] + [obj.display_name for obj in postgap.Cisreg.sources + postgap.Reg.sources])
	content = map(pretty_snp_association, associations)
	return "\n".join([header] + content) + "\n"

def pretty_snp_association(association):
	"""

		Prints association stats in roughly the same format as STOPGAP for a cluster of SNPs
		Args: GeneSNP_Association
		Returntype: String

	"""
	snp = association.snp
	gene_name = association.gene.name
	gene_id = association.gene.id
	score = association.score

	results = [snp.rsID, snp.chrom, str(snp.pos), gene_name, gene_id, str(score)]
	results += [str(association.intermediary_scores[functional_source.display_name]) for functional_source in postgap.Cisreg.sources]
	return "\t".join(results)

def pretty_output(associations):
	"""

		Prints association stats in roughly the same format as STOPGAP for a cluster of SNPs
		Args: [ GeneCluster_Association ]
		Returntype: String

	"""
	header = "\t".join(['ld_snp_rsID', 'chrom', 'pos', 'gene_symbol', 'gene_id', 'gene_chrom', 'gene_tss', 'disease_names', 'disease_efo_ids', 'score', 'rank', 'r2', 'gwas_source', 'gwas_snp', 'gwas_pvalue', 'gwas_pmid', 'ls_snp_is_gwas_snp', 'vep_terms'] + [source.display_name for source in postgap.Cisreg.sources + postgap.Reg.sources])
	content = map(pretty_cluster_association, associations)
	return "\n".join([header] + content)

def pretty_cluster_association(association):
	"""

		Prints association stats in roughly the same format as STOPGAP for a cluster of SNPs
		Args: GeneCluster_Association
		Returntype: String

	"""
	results = genecluster_association_table(association)
	return "\n".join("\t".join(map(unicode, row)) for row in results)

def db_output(db, associations):
	"""

		Writes association stats into DB
		Args1: string, DB name
		Args2: [ GeneCluster_Association ]

	"""
	conn = sqlite3.connect(db)
	table_sql = '''CREATE TABLE IF NOT EXISTS results (
		ld_snp_rsID TEXT,
		chrom TEXT,
		pos INT,
		gene_symbol TEXT,
		gene_id TEXT,
		gene_chrom TEXT,
		gene_tss INT,
		disease_names TEXT,
		disease_efo_ids TEXT,
		score REAL,
		rank INT,
		r2 real,
		gwas_source TEXT,
		gwas_snp TEXT,
		gwas_pvalue TEXT,
		gwas_pmid TEXT,
		ls_snp_is_gwas_snp INT,
		vep_terms TEXT,''' + ",".join([re.sub(" ", "_", source.display_name) + " INT\n" for source in postgap.GWAS.sources + postgap.Cisreg.sources + postgap.Reg.sources]) + ")"
	conn.execute(table_sql)
	map(lambda association: db_output_association(conn, association), associations)
	conn.commit()
	conn.close()

def db_output_association(conn, association):
	"""

		Prints association stats in roughly the same format as STOPGAP for a cluster of SNPs
		Arg1: SQLITE3 connection
		Arg2: GeneCluster_Association
		Returntype: String

	"""
	results = genecluster_association_table(association)
	sql_cmd = "INSERT INTO results VALUES (%s)" % ",".join("?" for item in results)
	conn.execute(sql_cmd, results)

def genecluster_association_table(association):
	"""

		Returns association stats in roughly the same format as STOPGAP for a cluster of SNPs
		Arg1: GeneCluster_Association
		Returntype: [[ string or float ]]

	"""
	results = []
	for gene_snp_association in association.evidence:
		vep_terms = "N/A"
		for evidence in gene_snp_association.cisregulatory_evidence:
			if evidence.source == "VEP":
				vep_terms = ",".join(evidence.info['consequence_terms'])
				break

		for gwas_snp in association.cluster.gwas_snps:
			for gwas_association in gwas_snp.evidence:
				row = [
						gene_snp_association.snp.rsID,
						gene_snp_association.snp.chrom,
						gene_snp_association.snp.pos,
						association.gene.name,
						association.gene.id,
						association.gene.chrom,
						association.gene.tss,
						gwas_association.disease.name,
						gwas_association.disease.efo,
						gene_snp_association.score,
						gene_snp_association.rank,
						association.r2,
						gwas_association.source,
						gwas_association.snp,
						gwas_association.pvalue,
						gwas_association.study,
						int(gene_snp_association.snp.rsID == gwas_snp.snp.rsID),
						vep_terms
					]

				row += [gene_snp_association.intermediary_scores[functional_source.display_name] for functional_source in postgap.Cisreg.sources + postgap.Reg.sources]
				results.append(row)

	return results

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