# Question 14:
# A robot moves in a plane starting from the original point (0,0). The
# robot can move toward UP, DOWN, LEFT and RIGHT with a given steps.
# The trace of robot movement is shown as the following:
# UP 5
# DOWN 3
# LEFT 3
# RIGHT 2
# ¡
# The numbers after the direction are steps. Please write a program to
# compute the distance from current position after a sequence of
# movement and original point. If the distance is a float, then just
# print the nearest integer.
# Example:
# If the following tuples are given as input to the program:
# UP 5
# DOWN 3
# LEFT 3
# RIGHT 2
# Then, the output of the program should be:
# 2
# Hints:
# In case of input data being supplied to the question, it should be
# assumed to be a console input.

position = {'x': 0, 'y': 0}

while True:
    line = input('> ')
    if not line:
        break

    direction, step = line.split()
    if direction == 'up':
        position['y'] += int(step)
    elif direction == 'down':
        position['y'] -= int(step)
    elif direction == 'right':
        position['x'] += int(step)
    elif direction == 'left':
        position['x'] -= int(step)

print(int(round((position['x']**2 + position['y']**2)**0.5)))


#!/usr/bin/env python

import ja_sentence as ja
from gensim.models import word2vec
import sys

sentences = list(ja.convert_to_ja_sentences_from_files(sys.argv[1:]))

model = word2vec.Word2Vec(sentences, size=100, min_count=3, window=3, iter=30)

#def out(wv, path):
#  wv.save(path)
#  for k in wv.vocab:
#    print(k + " " + " ".join([str(v) for v in wv.word_vec(k)]))

vocab = model.wv.vocab
while True:
  text = input('>> ')

  if not (text in vocab):
    continue

  result = model.most_similar(positive=[text])

  for pair in result:
    word = pair[0]
    distance = pair[1]
    print(word, distance)


from sys import argv
from os.path import exists

script, from_file, to_file = argv

print ("Copying from %s to %s" % (from_file, to_file))

# We could do these two on one line, how?
in_file = open(from_file)
indate = in_file.read()

print ("The input file is %d bytes long" % len(indate))

print ("Does the output file exist? %r" % exists(to_file))

print ("Ready, hit RETURN to continue, CRTL-C to abort.")
input()

out_file = open(to_file, 'w')
out_file.write(indate)

# Do all of these one line.
# open(to_file, 'w').write(open(from_file).read())

print ("Alright, all done.")

out_file.close()
in_file.close()


import numpy as np
import cv2
import time
import warnings
def sideBySide(img1, img2, destName):
    dims1 = img1.shape
    dims2 = img2.shape
    if len(dims1) < len(dims2):
        temp = np.dstack((img1,img1,img1))
        toWrite = np.hstack((temp,img2))
    elif len(dims1) > len(dims2):
        temp = np.dstack((img2,img2,img2))
        toWrite = np.hstack((img1, temp))
    else:
        toWrite = np.hstack((img1, img2))
    cv2.imwrite(destName, toWrite)

def maxContour(contours):
    maxCont = None
    maxIdx = -1
    maxLen = -1 #cv2.arcLength(maxCont, False)
    for i, c in enumerate(contours):
        nextLen = cv2.arcLength(c, False)
        if nextLen > maxLen:
            maxCont = c
            maxIdx = i
            maxLen = nextLen
    return maxCont,maxIdx

image = cv2.imread('Test1.jpg')
ylen,xlen,d = image.shape
trimTop =ylen/2
trimRight = xlen
lines = []

#while len(lines) != 1 and (trimTop < ylen and trimRight > xlen/2):
# temp = image
# r,g,b = cv2.split(temp)
# subtract = r-g-b
# cv2.imwrite('r.jpg',r)
# cv2.imwrite('g.jpg',g)
# cv2.imwrite('b.jpg',g)
# cv2.imwrite('subtract.jpg', subtract)
# thresh = cv2.inRange(subtract, 0, 80)
# cv2.imwrite('whitewash.jpg',thresh)
# #thresh = cv2.merge((r,thresh,b))
# #thresh = cv2.medianBlur(thresh, 21)
# cont, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# h,s,v = cv2.split(temp)
# #cv2.drawContours(temp, contours, -1, (255,0,0), 3)
# canny = cv2.Canny(thresh, 100,200)
# minLineLength = 70
# maxLineGap = 60
# lines = cv2.HoughLinesP(canny,rho=1,theta=np.pi/180,threshold=10,minLineLength=minLineLength,maxLineGap=maxLineGap)
# cont, contours, hierarchy = cv2.findContours(canny, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# cv2.drawContours(temp, contours, 4, (255,0,0), 3)
# #print len(contours)
# cv2.imwrite('tresh.jpg', thresh)
# cv2.imwrite('canny.jpg',canny)
#

############################
#Attempt via thresholding on center pixels
###################################
imageHSV = cv2.cvtColor(cv2.medianBlur(image,3), cv2.COLOR_RGB2HSV)
h,s,v = cv2.split(imageHSV)
xBox = range(xlen/2-5,xlen/2+5)
yBox = range(ylen-10, ylen)
hmean = np.mean(h[yBox,xBox])
smean = np.mean(s[yBox,xBox])
vmean = np.mean(v[yBox,xBox])

epsH = 20
epsS = 20
epsV = 50
threshH = cv2.inRange(h,hmean-epsH, hmean+epsH)
threshS = cv2.inRange(s,smean-epsS, smean+epsS)
threshV = cv2.inRange(v,vmean-epsV, vmean+epsV)
combined = (threshH) & (threshS)
combined = cv2.medianBlur(combined,5)
combined = cv2.Canny(combined,100,200)
cv2.imwrite('hThresh.jpg',threshH)
cv2.imwrite('sThresh.jpg',threshS)
cv2.imwrite('vThresh.jpg',threshV)
cv2.imwrite('combined.jpg',combined)
cont, contours, hierarchy = cv2.findContours(combined[:,xlen/2:], cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
c, i = maxContour(contours)
temp = np.zeros_like(image)
row,cols = combined.shape[:2]
[vx,vy,x,y] = cv2.fitLine(c, cv2.DIST_L2,0,0.01,0.01)
lefty = int((-x*vy/vx) + y)
righty = int(((cols-x)*vy/vx)+y)
cv2.line(image,(cols-1+xlen/2,righty),(0+xlen/2,lefty),(0,255,0),2)
#cv2.drawContours(temp, contours, -1, (255,0,0), 3)

cv2.drawContours(temp, contours, i, (255,0,0), 3)
cv2.imwrite('combinedCont.jpg',temp)

# print len(contours[319])
# longest = contours.index(max(contours, key=len))
# print len(contours)
# temp = image
# cv2.drawContours(temp, longest, -1, (255,0,0), 3)
# cv2.imwrite('temp.jpg', temp)
temp2 = np.zeros_like(image)
cv2.drawContours(temp2, contours, i, (255,0,0), 3)
sideBySide(image, temp, "HSV.jpg")

# low_black = np.array([50, 50, 50]) #if value is
# high_black = np.array([100, 255, 255])
# mask = cv2.inRange(img, low_black, high_black)
# cv2.imwrite('mask.jpg', mask)
# blurMask = cv2.GaussianBlur(mask, (7,7), 3)
# cv2.imwrite('blurMask.jpg', blurMask)
# cropped = mask[300:,500:]
# cv2.imwrite('maskCrop.jpg', cropped)
# cv2.imwrite('cannyCrop.jpg', cv2.Canny(cropped, 80,20))
# #maskRGB = cv2.cvtColor(mask, cv2.COLOR_HSV2RGB)
# #cv2.imwrite('maskRGB.jpg', maskRGB)


# encoding=utf-8

import unicodecsv as csv
import json
import urllib2
import re
from sources import functions
from sefaria.model import *

def read_csv(filename):

    data = []
    with open(filename) as data_file:
        names = csv.DictReader(data_file, encoding='utf-8')

        for line in names:
            data.append(line)
    return data

def add_mitzva(mitzva, index):
    """
    Create an ArrayMapNode for an individual Mitzva
    :param mitzva: Dictionary with keys "English" and "Hebrew" for corresponding language names.
    :param index: Index of the mitzva to be called by Ref or url
    :return: Assembled ArrayMapNode
    """

    # check if English was added
    if re.search(u'English Translation Here', mitzva['English']):
        mitzva['English'] = u'Mitzva {}'.format(index)

    node = ArrayMapNode()
    node.wholeRef = 'Minchat Chinukh.{}'.format(index)
    node.depth = 0
    node.add_title(mitzva['English'], 'en', True)
    node.add_title(mitzva['Hebrew'], 'he', True)

    return node

def add_parsha(parsha_name, children, mitzva_node_list):
    """
    Creates a SchemaNode for a parsha with all ArrayMapNodes for mitzvot properly appended
    :param parsha_name: Name of the Parsha
    :param children: Integer or range (i.e. 3-8)
    :param mitzva_node_list: A list of ArrayMapNodes from which to select children for this Node.
    :return: SchemaNode
    """

    # determine if node has one child or many
    if children.find('-') >= 0:
        edges = [int(x) for x in children.split('-')]
        child_nodes = mitzva_node_list[edges[0]-1:edges[1]]

    else:
        child_nodes = [mitzva_node_list[int(children)-1]]

    node = SchemaNode()
    node.add_shared_term(parsha_name)
    for child in child_nodes:
        node.append(child)

    return node

def construct_alt_struct(parsha_file, mitzva_file):

    mitzvot, parashot = read_csv(mitzva_file), read_csv(parsha_file)
    mitzva_nodes= []
    root = SchemaNode()

    for index, mitzva in enumerate(mitzvot):
        mitzva_nodes.append(add_mitzva(mitzva, index+1))

    for parsha in parashot:
        root.append(add_parsha(parsha['Parsha'], parsha['Includes'], mitzva_nodes))

    return root

s = construct_alt_struct('Chinukh_by_Parsha.csv', 'Chinukh Mitzva names.csv')
data = {
        'Parsha': s.serialize()
}

raw = urllib2.urlopen('http://www.sefaria.org/api/v2/raw/index/Sefer_HaChinukh')
index = json.load(raw)
index['alt_structs'] = data
index['default_struct'] = 'Parsha'

functions.post_index(index)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
Deal with polynomials.
Comming soon:
    init by interpolation formula;
    Bernoulli summation algorithm - p.sum()
"""

__author__ = 'Clarence Zhuo'

class Poly(object):
    """
    Doctest:

    >>> Poly(0)
    0
    >>> Poly()
    x
    >>> Poly(1,2,3)
    x^2 + 2x + 3
    >>> Poly(1,-1,-1)
    x^2 - x - 1
    >>> Poly(1,-4,-10)
    x^2 - 4x - 10
    >>> Poly(-1,-4,-10)
    -x^2 - 4x - 10
    >>> Poly(-1,-4,0)
    -x^2 - 4x
    >>> Poly(-1,0,-90)
    -x^2 - 90
    >>> Poly(lambda i: -i*i, 3)
    -9x^3 - 4x^2 - x

    >>> p = Poly(1,-2,3)
    >>> q = Poly(9,0,-1,3)
    >>> p+q
    9x^3 + x^2 - 3x + 6
    >>> p-q
    -9x^3 + x^2 - x

    >>> p = Poly(1,1)
    >>> p**2
    x^2 + 2x + 1
    >>> p
    x + 1
    >>> q = Poly(1,-1)
    >>> p*q
    x^2 - 1

    >>> p = Poly(1,-3,2)
    >>> p(5)
    12
    >>> p
    x^2 - 3x + 2
    >>> p(2)
    0
    >>> Poly(-1, 0, -2, 1).diff()
    -3x^2 - 2
    """
    def __init__(self, *args, sym='x', reverse=True):
        """
        Poly() -> polynomial x
        Poly(Poly p) -> a copy of p
        Poly(Iterable) -> init from Iterable
        Poly(callable f, n) -> f(n) x^n + ... + f(1) x + f(0)
        Poly(an, ..., a1, a0) -> an x^n + ... + a1 x + a0
        """
        from collections import Iterable
        self._sym = sym

        argc = len(args)
        if argc == 0:
            self._coef = [0, 1] # polynomial x
        elif argc == 1 and isinstance(args[0], Poly):
            self._coef = list(args[0]._coef)
        elif argc == 1 and isinstance(args[0], Iterable):
            self._coef = list(args[0])
        elif argc == 2 and callable(args[0]):
            if not isinstance(args[1], int) or args[1] < 0:
                raise TypeError('The 2nd argument (degree) '
                                'must be a non-negative int.')
            self._coef = [args[0](i) for i in range(args[1]+1)]
        else:
            if reverse:
                self._coef = list(reversed(args))
            else:
                self._coef = list(args)

        # invariant: len(self._coef) >= 1

        self.rmzero()

    # specials-------------------------------------------------

    def __getitem__(self, key):
        """
        Returns Cn, the coefficient of item x^n.
        """
        if not isinstance(key, int):
            raise TypeError('Key must be an int.')
        elif key < -len(self._coef):
            raise IndexError('Index out of range.')
        elif key > len(self._coef)-1:
            return 0 # higher degree coefficient == 0
        else:
            return self._coef[key]

    def __setitem__(self, key, value):
        if not isinstance(key, int):
            raise TypeError('Key must be an int.')
        elif key < -len(self._coef):
            raise IndexError('Index out of range.')

        while key > self.deg():
            self._coef.append(0)
        self._coef[key] = value

    def __str__(self):

        n = len(self._coef)-1
        ret = ''

        # reversed iteration
        for k in range(n, -1, -1):

            # ignore those with coef == 0
            # except for zero polynomial
            if self[k] == 0 and n != 0:
                continue

            # 'sign'
            # process highest degree item specially
            # self[k] is the kth degree coefficient
            if k == n:
                sign = '-' if self[k] < 0 else ''
            else:
                sign = ' - ' if self[k] < 0 else ' + '

            # 'coefficient'
            abs_coef = abs(self[k])
            if abs_coef == 1 and k != 0:
                coef = ''
            else:
                coef = str(abs_coef)

            # 'symbol and its exponent'
            if k == 0:
                expo = ''
            elif k == 1:
                expo = self._sym
            else:
                expo = self._sym + '^' + str(k)

            ret += sign + coef + expo

        return ret

    __repr__ = __str__

    def __call__(self, v):
        """
        Poly(v) -> result

        Assign a value to polynomial, using Horner's rule.
        """
        ret = self[-1]
        for c in self._coef[-2::-1]:
            ret = ret * v + c
        return ret

    def __eq__(self, other):
        try:
            other = Poly(other)
        except Exception:
            return False
        # list will compare lexicographically
        return self._coef == other._coef

    def __ne__(self, other):
        return not self == other

    def __pos__(self):
        return self

    def __neg__(self):
        return Poly(-c for c in self._coef)

    def __add__(self, other):
        return _do_operation(self, other, '+')

    def __radd__(self, other):
        return _do_operation(other, self, '+')

    def __sub__(self, other):
        return _do_operation(self, other, '-')

    def __rsub__(self, other):
        return _do_operation(other, self, '-')

    def __mul__(self, other):
        return _do_operation(self, other, '*')

    def __rmul__(self, other):
        return _do_operation(other, self, '*')

    def __divmod__(self, other):
        return _do_operation(self, other, '/%')

    def __floordiv__(self, other):
        return _do_operation(self, other, '//')

    def __mod__(self, other):
        return _do_operation(self, other, '%')

    def __pow__(self, other):
        if not isinstance(other, int) or other < 0:
            raise TypeError('arg2 for __pow__() must be '
                            'non-negative integer!')
        ret = Poly(1)
        for i in range(other):
            ret *= self
        return ret

    # other methods--------------------------------------------

    def deg(self):
        """
        Poly.deg() -> non-negative int

        Returns the degree of self. Degree of zero is -1.
        """
        if self.iszero():
            return -1
        else:
            return len(self._coef)-1

    def sym(self, c=None):
        """
        Poly.sym() -> str
        Poly.sym(c) -> None

        Reset the variable name for self.
        """
        if c == None:
            return self._sym
        self._sym = c

    def iszero(self):
        """Returns True iff self == zero polynomial."""
        return len(self._coef) == 1 and self[0] == 0

    def rmzero(self):
        """Remove extra zeros."""
        while len(self._coef) > 1 and self._coef[-1] == 0:
            self._coef.pop()

    def diff(self):
        """Derivate of polynomial."""
        if len(self._coef) == 1:
            return Poly(0)
        return Poly(self[i] * i for i in range(1, len(self._coef)))

# class ends---------------------------------------------------

def _do_operation(lhs, rhs, method):
    """helper function for method __add__, __radd__, __sub__ and so on"""
    try:
        if not isinstance(lhs, Poly):
            lhs = Poly(lhs)
        if not isinstance(rhs, Poly):
            rhs = Poly(rhs)
    except Exception:
        return NotImplemented

    if method == '+' or method == '-':
        if method == '+':
            func = lambda x,y: x+y
        else:
            func = lambda x,y: x-y
        length = max(len(lhs._coef), len(rhs._coef))
        return Poly(func(lhs[i], rhs[i]) for i in range(length))

    elif method == '*':
        if lhs.iszero() or rhs.iszero():
            return Poly(0)
        # 0 <= i < len(lhs._coef)
        # 0 <= j-i < len(rhs._coef) <---> j-len(rhs._coef) < i <= j
        # hence max(0, j-len(rhs)+1) <= i < min(j+1, len(lhs._coef))
        lenl, lenr = len(lhs._coef), len(rhs._coef)
        return Poly(sum(lhs[i] * rhs[j-i] for i in range(max(0, j-lenr+1),\
                min(j+1, lenl))) for j in range(lenl+lenr) )

    elif method == '/%':
        pass
    elif method == '//':
        pass
    elif method == '%':
        pass
    else:
        return NotImplemented

if __name__ == '__main__':
    import doctest
    doctest.testmod()


import requests
from bs4 import BeautifulSoup
import re

r = requests.get('https://divar.ir/s/mashhad')

res = r.text

soup = BeautifulSoup(res, 'html.parser')

all_price = soup.find_all('div', attrs={'class': 'kt-post-card__title'})
all_name = soup.find_all('div', attrs={'class': 'kt-post-card__top-description kt-post-card-description'})
__price__name__ = zip(all_price, all_name)
for price_is, name_is in __price__name__:
    name = re.sub(r'\s+', ' ', price_is.text).strip()
    price = re.sub(r'\s+', ' ', name_is.text).strip()
    if price != 'توافقی':
        print(name, 'قيمت :%s' % price)


import time

def sum_of_n(n):
	start = time.time()
	the_sum = 0
	for i in range(1, n+1):
		the_sum = the_sum+1
	end = time.time()
	return the_sum,(end-start)

print("Exercicio 1 TP:")
print(sum_of_n(1000))

def exercicio1(n):
	start = time.time()
	res = ((n*(n+1))/2)
	end = time.time()
	return res,(end-start)

print('Exercicio 2 TP:')
print(exercicio1(0))
print(exercicio1(10))
print(exercicio1(100))
print(exercicio1(1000))

def exercicio2(n):
	start = time.time()
	soma = 0
	for i in range(1,n+1):
		soma = soma + 1
	end = time.time()
	return soma,end-start

import numpy as mp

def exercicio3():
	m = mp.array([[1,2,3],[4,5,6]],[])
	vec = mp.zeros(3)
	for i in range(m.shape[0]):
		vec[i]=matrix[i][0]
		for j in range(matrix.shape[i]):
			if matrix[i][j] > vec[i]:
				vec[i] = matrix[i][j]
	return vec

def exercicio4():
	matrix = mp.array([1,3,5],[3,2,1],[5,1,6])
	soma = 0
	for i in range(matrix.shape[0]):
		for j in range(i):
			soma += matrix[i][j]*2
		soma+= matrix[i][i]
	return soma

printf(exercicio4())


#!/usr/bin/env python
import sys
import json
import uuid
import logging
import asyncio
import traceback
from typing import Any
from typing import Dict
from typing import Tuple

__all__ = ['Server', 'Client', 'Crate']

logging.basicConfig(
    level=logging.DEBUG,
    format='[%(levelname)-5s][%(asctime)s][crate] %(message)s',
    stream=sys.stderr)

class Server(asyncio.Protocol):
    def __init__(self, app, loop):
        self.app = app
        self.loop = loop

        self.log = logging.getLogger('server')

        super().__init__()

    def _jsonify(self, **kwargs):
        return json.dumps(kwargs).encode('utf-8')

    def connection_made(self, transport):
        self.transport = transport
        self.address = transport.get_extra_info('peername')
        self.log.debug(
            'Connection accepted from {}:{}'.format(*self.address))

    def data_received(self, data):
        message = json.loads(data.decode())
        task_name = message.get('task')
        task_args = message.get('args', [])
        task_kwargs = message.get('kwargs', {})

        # If data received contains a task name
        if task_name:
            # Retrieve task method in registrar
            task_func = self.app._registrar.get(task_name)

            # If no method has been retrieved, this is an unregistered task
            if not task_func:
                self.log.info(
                    'Unregistered "{}" task received'.format(task_name))
                self.transport.write(self._jsonify(
                    success=False, message='Unregistered task.'))
                return

            task_uuid = str(uuid.uuid4())
            self.log.info('(task:{}) Running task...'.format(task_uuid))

            try:
                # Send task coroutine to loop
                self.loop.create_task(task_func(*task_args, **task_kwargs))
                self.log.info('(task:{}) Task sent.'.format(task_uuid))
                # Send to client task uuid
                self.transport.write(
                    self._jsonify(success=True, message=task_uuid))
            except Exception as err:
                self.log.info(
                    '(task:{}) Error while running task...\n{}'.format(
                        task_uuid, traceback.format_exc()))
                # Send to client task uuid and traceback
                self.transport.write(
                    self._jsonify(
                        success=False, error=str(err),
                        message=task_uuid, traceback=traceback.format_exc()))
        else:
            self.log.info('Invalid message received: {}'.format(data))

class Client(asyncio.Protocol):
    def __init__(self, message: Dict, loop: asyncio.AbstractEventLoop):
        super().__init__()
        self.message = message
        self.loop = loop
        self.result = {}

        self.log = logging.getLogger('client')

    def connection_made(self, transport):
        self.transport = transport
        self.address = transport.get_extra_info('peername')
        self.log.debug('Connecting to {}:{}'.format(*self.address))

        data = json.dumps(self.message)
        transport.write(data.encode('utf-8'))

        self.log.debug('Sending {!r}'.format(data))
        if transport.can_write_eof():
            transport.write_eof()

    def data_received(self, data):
        self.log.debug('Received {!r}'.format(data))
        self.result = json.loads(data.decode('utf-8'))

    def connection_lost(self, exc):
        self.log.debug('Server closed connection')
        self.transport.close()
        self.loop.stop()
        super().connection_lost(exc)

class Crate:
    def __init__(self, host: str = 'localhost', port: int = 6666):
        self.host = host
        self.port = port
        self.log = logging.getLogger('crate')

        self._registrar = {}
        self._loop = asyncio.get_event_loop()

    def __del__(self):
        if self._loop:
            self._loop.close()

    def run(self):
        self.log.info('Listening on {}:{}...'.format(self.host, self.port))

        factory = self._loop.create_server(
            lambda: Server(self, self._loop), self.host, self.port)
        server = self._loop.run_until_complete(factory)
        try:
            self._loop.run_forever()
        finally:
            self.log.info('Closing server')
            server.close()
            self._loop.run_until_complete(server.wait_closed())
            self.log.debug('Closing event loop')
            self._loop.close()

    def task(self, *args: Any, **kwargs: Any):
        def delay(app, name, *args, **kwargs):
            return app.send_task(name, args, kwargs)

        method = args[0]
        method.delay = lambda *args, **kwargs: delay(
            self, method.__name__, *args, **kwargs)
        self._registrar[method.__name__] = method

        return method

    def send_task(
            self, name: str, args: Tuple = tuple(), kwargs: Dict = {}) -> Dict:

        coroutine = self._loop.create_connection(lambda: Client(
            {'task': name, 'args': args, 'kwargs': kwargs}, self._loop),
            self.host, self.port)

        transport, client = self._loop.run_until_complete(coroutine)
        self._loop.run_forever()

        return client.result


#!/usr/bin/python

import logging, os

# Raspberry Pi SPI Port and Device
spi_port = 0
spi_dev = 0

# Pin # for relay connected to heating element
he_pin = 26

# Default goal temperature
#set_temp = 221.
set_temp = 203.
set_temp_tolerance = 2.
cold_state = 100
warm_state = 200

pid_history_length = 10
temp_history_length = 5

startup_sleep_time = 1

# Default sleep/wake times
sched_enabled = True
sched_disabled_op = "wakeup"
#sched_disabled_op = "gotosleep"
weekday_wake_time = '07:30'
weekday_sleep_time = '10:30'
weekend_wake_time = '10:00'
weekend_sleep_time = '12:30'
schedule_sample_time = 1

# Main loop sample rate in seconds
sample_time = 0.5
# Number of seconds until machine changes between hot & cold states.
num_secs_for_state_change = 60*15
num_secs_for_state_constant = 60*1

# Number of times to allow RuntimeError("short circuit to ground")
temp_reading_errors = 100
gpio_errors = 100
server_hc_errors = 100
slack_send_message_retry = 100
slack_send_message_fail_sleep = 1

# PID Proportional, Integral, and Derivative values
Pc = 3.4
Ic = 0.3
Dc = 40.0
#Pc = 8
#Ic = 0.2
#Dc = 60

Pw = 2.9
Iw = 0.3
Dw = 40.0
#Pw = 8
#Iw = 0.2
#Dw = 60

#Web/REST Server Options
port = 8080

#Slack channel to monitor
#slack_channel_type="public_channel"
slack_channel_type="im"
#slack_channel_name="general"
slack_channel_name="matthewfaw"
slack_sample_time=5

# Logging info
log_level = logging.INFO
log_maxbytes = int(1e7)
#log_level = logging.DEBUG
basedir=os.path.dirname(os.path.realpath(__file__))
log_files = {
        "main": "silvia-pi.log",
        "pid": "pid.log",
        "dummy": "dummy.log",
        "he": "heat_exchange.log",
        "schedule": "scheduler.log",
        "server": "webserver.log",
        "slack": "slack.log",
        "watchdog": "watchdog.log",
        }


#!/usr/bin/python
# -*- coding: utf8 -*-
# author              : Brian Ernst
# python_version      : 2.7.6 and 3.4.0
# =================================

# TODO: setup batches before pushing to threads and use p4 --parallel
# http://www.perforce.com/perforce/r14.2/manuals/cmdref/p4_sync.html

from p4Helper import *

import time, traceback

#==============================================================
class P4SyncMissing:
    def run( self, args ):
        start = time.clock()

        fail_if_no_p4()

         #http://docs.python.org/library/optparse.html
        parser = optparse.OptionParser( )

        parser.add_option( "-d", "--dir", dest="directory", help="Desired directory to crawl.", default=None )
        parser.add_option( "-t", "--threads", dest="thread_count", help="Number of threads to crawl your drive and poll p4.", default=12 )
        parser.add_option( "-q", "--quiet", action="store_true", dest="quiet", help="This overrides verbose", default=False )
        parser.add_option( "-v", "--verbose", action="store_true", dest="verbose", default=False )

        ( options, args ) = parser.parse_args( args )

        directory = normpath( options.directory if options.directory is not None else os.getcwd( ) )

        with Console( auto_flush_time=1 ) as c:
            with P4Workspace( directory ):
                if not options.quiet:
                    c.writeflush( "Retreiving missing files..." )
                    c.writeflush( " Setting up threads..." )

                # Setup threading
                WRK = enum( 'SHUTDOWN', 'SYNC' )

                def shutdown( data ):
                    return False
                def sync( files ):
                    files_len = len(files)
                    files_flat = ' '.join('"' + p4FriendlyPath( f ) + '"' for f in files)

                    if options.verbose:
                        files_len = len(files)
                        if files_len > 1:
                            c.write( "  Syncing batch of " + str(len(files)) + " ...")
                            for f in files:
                                c.write( "   " + os.path.relpath( f, directory ) )
                        else:
                            for f in files:
                                c.write( "   Syncing " + os.path.relpath( f, directory ) + " ..." )

                    ret = -1
                    count = 0
                    while ret != 0 and count < 2:
                        ret = try_call_process( "p4 sync -f " + files_flat )
                        count += 1
                        if ret != 0 and not options.quiet:
                            c.write("Failed, trying again to sync " + files_flat)
                    if ret != 0:
                        if not options.quiet:
                            c.write("Failed to sync " + files_flat)
                    else:
                        if not options.quiet:
                            files_len = len(files)
                            if files_len > 1:
                                c.write( "  Synced batch of " + str(len(files)) )
                            for f in files:
                                c.write( "   Synced " + os.path.relpath( f, directory ) )
                    return True

                commands = {
                    WRK.SHUTDOWN : shutdown,
                    WRK.SYNC : sync
                }

                threads = [ ]
                thread_count = options.thread_count if options.thread_count > 0 else multiprocessing.cpu_count( ) + options.thread_count

                count = 0
                total = 0
                self.queue = multiprocessing.JoinableQueue( )

                for i in range( thread_count ):
                    t = Worker( c, self.queue, commands )
                    t.daemon = True
                    threads.append( t )
                    t.start( )

                c.writeflush( "  Done." )

                make_drive_upper = True if os.name == 'nt' or sys.platform == 'cygwin' else False

                command = "p4 fstat ..."

                if not options.quiet:
                    c.writeflush( " Checking files in depot, this may take some time for large depots..." )

                proc = subprocess.Popen( command.split( ), stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=directory )

                clientFile_tag = "... clientFile "
                headAction_tag = "... headAction "
                headType_tag   = "... headType "

                # http://www.perforce.com/perforce/r12.1/manuals/cmdref/fstat.html
                accepted_actions = [ 'add', 'edit', 'branch', 'move/add', 'move\\add', 'integrate', 'import', 'archive' ] #currently not checked
                rejected_actions = [ 'delete', 'move/delete', 'move\\delete', 'purge' ]
                file_type_binary = 'binary+l'
                file_type_text   = 'text'

                client_file    = None
                file_action    = None
                file_type      = None
                file_type_last = None

                # todo: use fewer threads, increase bucket size and use p4 threading
                class Bucket:
                    def __init__(self, limit):
                        self.queue = []
                        self.queue_size = 0
                        self.queue_limit = limit
                    def append(self,obj):
                        self.queue.append(obj)
                        self.queue_size += 1
                    def is_full(self):
                        return self.queue_size >= self.queue_limit

                self.buckets             = {}
                self.buckets[file_type_text]   = Bucket(10)
                self.buckets[file_type_binary] = Bucket(2)

                def push_queued(bucket):
                    if bucket.queue_size == 0:
                        return
                    if options.verbose:
                        for f in bucket.queue:
                            c.write( "  Checking " + os.path.relpath( f, directory ) )
                    self.queue.put( ( WRK.SYNC, bucket.queue ) )
                    bucket.queue = []
                    bucket.queue_size = 0

                for line in proc.stdout:
                    line = get_str_from_process_stdout( line )

                    #push work when finding out type
                    if client_file and file_action is not None and line.startswith( headType_tag ):

                        file_type = normpath( line[ len( headType_tag ) : ].strip( ) )
                        if file_type == file_type_text:
                            self.buckets[file_type_text].append(client_file)
                        else:
                            self.buckets[file_type_binary].append(client_file)
                        count += 1

                        #check sizes and push
                        for b in self.buckets.values():
                            if b.is_full():
                                push_queued(b)

                    elif client_file and line.startswith( headAction_tag ):
                        file_action = normpath( line[ len( headAction_tag ) : ].strip( ) )
                        if any(file_action == a for a in rejected_actions):
                            file_action = None
                        else:
                            total += 1
                            if os.path.exists( client_file ):
                                file_action = None

                    elif line.startswith( clientFile_tag ):
                        client_file = line[ len( clientFile_tag ) : ].strip( )
                        if make_drive_upper:
                            drive, path = splitdrive( client_file )
                            client_file = ''.join( [ drive.upper( ), path ] )

                    elif len(line.rstrip()) == 0:
                        client_file = None

                for b in self.buckets.values():
                    push_queued(b)
                proc.wait( )

                for line in proc.stderr:
                    if "no such file" in line:
                        continue
                    #raise Exception(line)
                    c.write(line)#log as error

                if not options.quiet:
                    c.writeflush( "  Done. Checked " + str(total) + " file(s)." )
                    c.writeflush( " Queued " + str(count) + " file(s), now waiting for threads..." )

                for i in range( thread_count ):
                    self.queue.put( ( WRK.SHUTDOWN, None ) )

                for t in threads:
                    t.join( )

                if not options.quiet:
                    print( "  Done." )

        if not options.quiet:
            end = time.clock()
            delta = end - start
            output = " Done. Finished in " + str(delta) + "s"
            print( output )

if __name__ == "__main__":
    try:
        P4SyncMissing().run(sys.argv)
    except:
        print( "\nUnexpected error!" )
        traceback.print_exc( file = sys.stdout )


import sys
import math
import copy
import random
import time

class CSP:

    def __init__(self, num_variables, num_domains, num_contraints, constraints):

        self.attempts = 0
        self.limit = 20

        self.num_variables = int(num_variables)
        self.constraints = constraints
        self.domain = [v for v in range(int(num_domains))]

        self.variables = []
        for i in range(0, int(self.num_variables)):
            self.variables.append(i)

        self.adjacents = {key: [] for key in range(0, int(self.num_variables))}
        for c in self.constraints:
            self.adjacents[int(c[0])].append(int(c[1]))
            self.adjacents[int(c[1])].append(int(c[0]))
        for c in self.constraints:
            self.adjacents[int(c[0])].append(int(c[1]))
            self.adjacents[int(c[1])].append(int(c[0]))

# Randomly chose a variable from the set of conflicted variables
def getRandomConflictingVariable(assignment, csp):
    #if the limit crossed for this random assignment, return any variable at random
    if csp.attempts > csp.limit:
        return random.randrange(0, csp.num_variables)

    #list of the conflicting variables
    conflicting_variables = []
    #check conflicting assignments and append the corresponding variables to the list
    for v in range(0, csp.num_variables):
        for neighbour in csp.adjacents[v]:
            if assignment[neighbour] == assignment[v]:
                conflicting_variables.append(v)
                break
    #radomly return one of the conflicting variables
    return random.choice(conflicting_variables)

#Finds the value val for var that minimizes conflicts
def getLeastConflictingValue(var, assignment, csp):

    if csp.attempts > csp.limit:
        csp.attempts = 0
        domain_siz = len(csp.domain)
        least_conflict_value = csp.domain[random.randrange(0, domain_siz)]
        return least_conflict_value

    conflicts = []
    least_conflict_count = float('inf')
    for val in csp.domain:
        conf_count = 0
        #try this new assignment by assigning var = val
        new_assignment = copy.deepcopy(assignment)
        new_assignment[var] = val

        #Count the number of conflicts if we assign val to this variable.
        for arc in csp.constraints:
            if new_assignment[int(arc[0])] == new_assignment[int(arc[1])]:
                conf_count = conf_count + 1
        #append this count to the conflicts list
        conflicts.append(conf_count)

        #Keep a check on the minimum conflict count
        if conf_count < least_conflict_count:
            least_conflict_count = conf_count

    #Gather all the values that gave the least_conflict_count and randomly return one of those
    possible_values = [
        i for i, count in enumerate(conflicts) if count == least_conflict_count
    ]
    return random.choice(possible_values)

steps = 0

def minConflicts(assignment, csp):
    global steps

    for var in csp.variables:
        val = random.randrange(0, len(csp.domain))
        assignment[var] = val

    random_restart = 0
    while random_restart < 100:
        steps += 1

        ##check if this random assignment is the actual solution, if yes, return the assignment
        res = True
        for arc in csp.constraints:
            if assignment[int(arc[0])] == assignment[int(arc[1])]:
                res = False

        if res == True:
            return assignment

        # if we have crossed the maximum limit of trying different values for this assignment, do a random restart
        if (csp.attempts > csp.limit):
            for var in csp.variables:
                val = random.randrange(0, len(csp.domain))
                assignment[var] = val
                random_restart += 1
            csp.attempts = 0

        csp.attempts += 1
        #We did not find a solution yet, pick a random variable from amongst the conflicting variables
        #This will be the most resctricted variable
        variable = getRandomConflictingVariable(assignment, csp)
        # Now pick a value to assign to this variable
        # This will be the least conflicting value.
        value = getLeastConflictingValue(variable, assignment, csp)
        assignment[variable] = value
    return False

if __name__ == '__main__':
    input_file = ""
    output_file = ""
    if len(sys.argv) == 3:
        input_file = sys.argv[1]
        output_file = sys.argv[2]
    else:
        print("Please enter all valid arguments")
        print("python3 dfsb.py <input_file> <output_file>")
        sys.exit(1)

    print('Input file = ' + input_file)
    print('Output_file = ' + output_file)

    fin = open(input_file, 'r')
    line = fin.readline().strip()
    top_row = line.split('\t')
    num_variables, num_constraints, num_domains = top_row[0], top_row[
        1], top_row[2]
    #print("Domains " + num_domains)
    constraints = []
    for i in range(int(num_constraints)):
        line = fin.readline().strip()
        constraints.append(line.split('\t'))

    #Create a CSP object that describes the problem
    csp = CSP(num_variables, num_domains, num_constraints, constraints)

    final_res = {}
    #Start the timer
    start_time = time.time() * 1000
    res = minConflicts(final_res, csp)
    #Record the current time
    end_time = time.time() * 1000
    print("Time taken(ms) = " + str(end_time - start_time))

    fout = open(output_file, 'w')
    if res == False:
        print("No answer")
        fout.write("No answer")
    else:
        print("\n Possible valid assignment is: \n")
        print(final_res)
        print("\nSteps = " + str(steps))

    #Print the output in the file

    for color in final_res:
        fout.write(str(final_res[color]))
        fout.write('\n')


#importing data sets

#This file does two tasks
"""
1. Given humidity predict temperature
2. Given humidity predict apparent temperature
"""
import numpy as np;
import matplotlib.pyplot as plt;
import pandas as pd;

#impoting dataset
dataset=pd.read_csv('weatherHistory.csv')
humidity=dataset.iloc[0:10000,5:6].values
temperature=dataset.iloc[0:10000:,3:4].values
apparentTemperature=dataset.iloc[0:10000:,4:5].values

#replace missing data with mean using imputer library
from sklearn.preprocessing import Imputer
imputerHumidity=Imputer(missing_values='NaN',strategy='mean',axis=0)
imputerHumidity=imputerHumidity.fit(humidity)
humidity=imputerHumidity.transform(humidity)

imputerTemperature=Imputer(missing_values='NaN',strategy='mean',axis=0)
imputerTemperature=imputerTemperature.fit(temperature)
temperature=imputerTemperature.transform(temperature)

imputerappTemp=Imputer(missing_values='NaN',strategy='mean',axis=0)
imputerappTemp=imputerappTemp.fit(apparentTemperature)
apparentTemperature=imputerappTemp.transform(apparentTemperature)

#split the data into training and testing
from sklearn.cross_validation import train_test_split
humidity_train,humidity_test,temperature_train,temperature_test=train_test_split(humidity,temperature,test_size=0.3,random_state=0)
humidity_train,humidity_test,apptemp_train,apptemp_test=train_test_split(humidity,apparentTemperature,test_size=0.3,random_state=0)
humidity_train=humidity_train
humidity_test=humidity_test
temperature_train=temperature_train.flatten()
temperature_test=temperature_test.flatten()
apptemp_train=apptemp_train.flatten()
apptemp_test=apptemp_test.flatten()

#Task 1 To predict Temperature given Humidity
from LinearRegression import LinearRegression
lr1=LinearRegression(humidity_train,temperature_train)
print(lr1.computeCostFunction())
theta1=lr1.returnTheta()
theta1,cost_history1,theta_history1=lr1.performGradientDescent(10000,0.01)
temperature_predict,temperature_error=lr1.predict(humidity_test,temperature_test)
temperature_pred_normal,error_temp_normal=lr1.predictUsingNormalEquation(humidity_test,temperature_test)

plt.scatter(humidity_test,temperature_test)
plt.plot(humidity_test,temperature_pred_normal,'r')
plt.title('Humidity vs Temperature using normal equation')
plt.xlabel('Humidity')
plt.ylabel('Temperature')
plt.show()

plt.scatter(humidity_test,temperature_test)
plt.plot(humidity_test,temperature_predict,'r')
plt.title('Humidity vs Temperature using gradient descent')
plt.xlabel('Humidity')
plt.ylabel('Temperature')
plt.show()

lr2=LinearRegression(humidity_train,apptemp_train)
print(lr2.computeCostFunction())
theta2=lr2.returnTheta()
theta2,cost_history2,theta_history2=lr2.performGradientDescent(100000,0.01)
apptemp_predict,apptemp_error=lr2.predict(humidity_test,apptemp_test)
apptemp_pred_normal,error_apptemp_normal=lr2.predictUsingNormalEquation(humidity_test,apptemp_test)

plt.scatter(humidity_test,apptemp_test)
plt.plot(humidity_test,apptemp_pred_normal,'r')
plt.title('Humidity vs Apparent Temperature using normal equation')
plt.xlabel('Humidity')
plt.ylabel('Apparent Temperature')
plt.show()

plt.scatter(humidity_test,temperature_test)
plt.plot(humidity_test,temperature_predict,'r')
plt.title('Humidity vs Apparent Temperature using gradient descent')
plt.xlabel('Humidity')
plt.ylabel('Apparent Temperature')
plt.show()


import os
import time

BaseDir = os.path.dirname(os.path.abspath(__file__))

class History:
    def __init__(self, path=BaseDir + os.sep + 'history'):
        self.path = path
        self.max = 20
        self.check_file()

    def check_file(self):
        if not os.path.exists(self.path):
            open(self.path, 'w').close()

    def get_now_time(self):
        localtime = time.localtime()
        now_time = str(localtime.tm_mon) + "-" + str(localtime.tm_mday) + ' ' + ':'.join(
            [str(localtime.tm_hour), str(localtime.tm_min), str(localtime.tm_sec)])
        return now_time

    def put(self, movie_name):
        self.check_file()
        with open(self.path, 'r+') as f:
            context = f.read()
            f.seek(0,0)
            f.write(self.get_now_time() + " " + movie_name + '\n')
            f.write(context)

    def show(self, page):
        self.check_file()
        need = ''
        start = page * self.max
        end = start + self.max
        with open(self.path, 'r') as f:
            for line_num,line in enumerate(f):
                if line_num >= start and line_num < end and line!='\n':
                    need += line
                elif line_num > end:
                    break
        return need.rstrip('\n')

if __name__ == '__main__':
    history = History()


from .disk import Disk
from .exceptions import NotAnOverlay
from .exceptions import VirshError
from .exceptions import UnknownGuestError
from .util import eprint

class Guest:

    """ Guest system (virtual machine)
    """
    exclude = None  # Disks to be excluded

    def __init__(self, name, remote_host=None):
        """ The option remote_host can be used to create partially remote
        Guests. Such a guest provides some remote actions such as:
            * remote blockcommits
            * remote shutdown
        """
        import subprocess
        self.name = name
        self.remote_host = remote_host
        try:
            subprocess.check_output(['virsh', 'domblklist', name])
        except subprocess.CalledProcessError:
            raise UnknownGuestError(self.name)

    def shutdown(self):
        """ Turns the guest off.
        """
        import subprocess
        virsh_cmd = []
        if self.remote_host:
            virsh_cmd += ['ssh',
                          '%s' % self.remote_host.name
                          ]
        virsh_cmd += ['virsh',
                      'shutdown',
                      '%s' % self.name]
        try:
            subprocess.check_output(virsh_cmd,
                                    stderr=subprocess.STDOUT
                                    ).decode('utf-8')
        except subprocess.CalledProcessError as e:
            raise UnknownGuestError(e.output)

    def disk_images_list(self):  # FIXME rename to disk_list
        """  Returns all not excluded disks of the guest.
        The parameter remote_host creates partially remote disks that
        provide remote blockcommits.
        """
        import subprocess
        import re
        disks = []
        virsh_cmd = ['virsh',
                     'domblklist',
                     self.name]
        try:
            output = subprocess.check_output(virsh_cmd).decode('utf-8')
            output = output.splitlines()
        except subprocess.CalledProcessError:
            eprint("Virsh call raised an error")
            raise UnknownGuestError()
        output.pop(0)   # eliminating header & footer
        output.pop(0)
        output.pop()
        for dline in output:
            disk, path = dline.split()
            if re.match(Guest.exclude, disk):
                continue
            try:
                disks.append(Disk(self, disk, path,
                                  remote_host=self.remote_host)
                             )
            except NotAnOverlay:
                pass    # Skip if there is no overlay
        return disks

    def overlays(self):
        """ Returns all overlays from new to old, including the base image.
        """
        all_files = []
        for disk in self.disk_images_list():
            all_files += disk.overlays()
        return all_files

    def snapshot_all(self):
        """ Creates a new overlay based snapshot for every disk. Returns a list
        of all stable overlays.
        """
        if not self.active():
            print('Host %s is inactive, skipping overlay creation.' %
                  self.name)
            return
        for disk in self.disk_images_list():
            disk.create_next_overlay()

    def active(self):
        """ If the guest is active. If a guest is inactive vmbak is unable to
        do blockcommits.
        """
        import subprocess
        virsh_cmd = ['virsh',
                     'list'
                     ]
        try:
            guest_list = subprocess.check_output(virsh_cmd).decode('utf-8')
        except subprocess.CalledSubprocessError:
            raise VirshError()
        return self.name in guest_list

    def clear_snapshots(self):
        """ In virsh there are even disk-only snapshots registered, because
        blockcommit is not supported by virsh 0.9.8 these snapshots have to be
        removed manually.
        """
        pass

    def grap_xml(self):
        """ Returns the xml code that defines the guest.
        """
        import subprocess
        cmd = ['virsh',
               'dumpxml',
               '%s' % self.name,
               ]
        try:
            xml = subprocess.check_output(cmd).decode('utf-8')
        except subprocess.CalledProcessError:
                raise VirshError()
        return xml


nontype = {'broths': ['chicken stock',
            'fish stock',
            'beef stock',
            'lamb broth',
            'beef broth',
            'chicken broth',
            'chicken bouillon',
            'beef bouillon',
            'veal bouillon',
            'prawn stock',
            'veal stock',
            'lamb stock'],
 'butter': ['butter', 'margarine'],
 'cheese': ['process sharp cheddar cheese singles',
            'processed american cheese',
            'processed cheese',
            'processed cheese food',
            'american cheese',
            'asiago cheese',
            'brie cheese',
            'cheddar monterey jack cheese blend',
            'cheddar monterey cheese blend',
            'cheddar cheese',
            'cheddar flavored soy cheese',
            'cheddar or colby jack cheese',
            'cheddar/monterey jack cheese blend',
            'colby cheese',
            'cream cheese',
            'emmentaler cheese',
            'gouda cheese',
            'gruyere cheese',
            'havarti cheese',
            'italian cheese blend',
            'jarlsberg cheese',
            'locatella cheese',
            'medium cheddar cheese',
            'mexican cheese',
            'mexican cheese blend',
            'mexican processed cheese food',
            'mizithra cheese',
            'monterey jack cheese',
            'muenster cheese',
            'parmesan cheese',
            'parmesan cheese flavored bread',
            'parmesan cheese or to taste',
            'parmesan cheese to taste',
            'parmigiano reggiano cheese',
            'pepper jack cheese',
            'red leicester cheese',
            'reggiano parmesan cheese',
            'romano cheese',
            'roquefort cheese',
            'stilton cheese crumbed',
            'swiss gruyere cheese',
            'bite size cheddar cheese crackers',
            'blue cheese',
            'blue cheese dressing',
            'broccoli cheese flavored rice mix',
            'cheese curds',
            'cheese filled tortellini',
            'cheese flavored butter crackers',
            'cheese flavored crackers',
            'cheese garlic croutons',
            'cheese garlic dressing',
            'cheese ravioli',
            'cheese spread',
            'cheese tortellini',
            'chive flavored cottage cheese',
            'chive onion cream cheese',
            'cotija cheese',
            'cottage cheese',
            'cottage cheese whipped',
            'cream cheese',
            'cream cheese frosting',
            'curd cottage cheese',
            'extra sharp cheddar cheese',
            'extra sharp white cheddar cheese',
            'fat free american cheese',
            'fat free parmesan cheese',
            'fat free cream cheese',
            'fat free mozzarella cheese',
            'fat free strawberry cream cheese',
            'feta cheese',
            'feta or cotija cheese',
            'fontina cheese',
            'four cheese alfredo sauce',
            'four cheese blend',
            'garlic cheese spread',
            'garlic flavored processed cheese',
            'garlic herb cheese spread',
            'garlic herb flavored cheese spread',
            'garlic parmesan cheese sauce',
            'goat cheese',
            'gorgonzola cheese',
            'herb garlic flavored cream cheese',
            'instant cheddar cheese sauce mix',
            'jalapeno cheese dip',
            'jalapeno cream cheese',
            'jalapeno processed cheese spread',
            'low fat cottage cheese',
            'low fat mozzarella cheese',
            'low fat ricotta cheese',
            'lowfat monterey jack cheese',
            'lowfat cream cheese',
            'macaroni cheese',
            'mascarpone cheese',
            'mild cheddar cheese',
            'mozzarella cheese',
            'mozzarella cheese sticks',
            'mozzarella cheese thick',
            'nacho cheese dip',
            'non fat cottage cheese',
            'nonfat cottage cheese',
            'nonfat mozzarella cheese',
            'nonfat ricotta cheese',
            'part skim mozzarella cheese',
            'part skim ricotta cheese',
            'pepper jack cheese',
            'pimento cheese',
            'pimento cheese food',
            'pimento cheese spread',
            'potato cheese filled pierogies',
            'process cheese food',
            'processed cheese slices',
            'processed cheese spread',
            'processed sharp cheddar cheese',
            'provolone cheese',
            'rancherito cheese',
            'reduced calorie monterey jack cheese',
            'reduced fat cheddar cheese',
            'reduced fat colby cheese',
            'reduced fat swiss cheese',
            'reduced fat processed cheese food',
            'ricotta cheese',
            'sharp cheddar cheese',
            'sharp processed cheese food',
            'sharp processed cheese spread',
            'shaved parmesan cheese',
            'smoked cheddar cheese',
            'smoked gouda cheese',
            'smoked mozzarella cheese',
            'stilton cheese',
            'strawberry flavored cream cheese',
            'tomato basil feta cheese',
            'wedge brie cheese',
            'wheel brie cheese',
            'whipped cream cheese',
            'white cheddar cheese',
            'white cheddar macaroni cheese mix',
            'whole milk ricotta cheese',
            'abondance fermlere herve mons',
            'american cheese',
            'american cheese or your choice',
            'american cheese slices',
            'american processed cheese',
            'appenzeller',
            'babybel cheese',
            'bergenost',
            'blue cheese',
            'blue cheese, maytag',
            'maytag blue cheese',
            'borough market stilton',
            'brick cheese',
            'brie cheese',
            'cabot',
            'cahill\xe2\x80\x99s porter',
            'camembert cheese',
            'campode montalban',
            'casein',
            'cheddar cheese',
            'cheshire cheese',
            'colby cheese',
            'colby-jack cheese',
            'colby jack cheese',
            'colby monterey jack cheese',
            'colby longhorn cheese',
            'cordobes',
            'cottage cheese, full fat',
            'full fat cottage cheese',
            'cottage cheese',
            'cottage cheese, trim',
            'trim cottage cheese',
            'cougar gold cheddar',
            'cream cheese',
            'cup cheese',
            'drunken goat cheese',
            'dublinger',
            'edam cheese',
            'emmenthal',
            'emmenthal swiss cheese',
            'english derby with sage',
            'farmer cheese',
            'feta cheese',
            'fiscalim sand joaquin',
            'goat cheese',
            'gorgonzola cheese',
            'gouda cheese',
            'government cheese',
            'gran queso',
            'gruyere cheese',
            'half & half',
            'havarti cheese',
            'herve mons pave de nord',
            'herve mons tomme des bois noir',
            'jack, monterey',
            'jarlsberg cheese',
            'kefalotyri cheese',
            'kefir',
            'kerrygold invernia',
            'le gruye\xe2\x80\x99re switzerland',
            'liederkranz cheese',
            'limburger cheese',
            'manchego 1 year',
            'manchego 8 mo. raw milk',
            'manchego cheese',
            'mascarpone cheese',
            'mimolette',
            'montasio',
            'monterey jack cheese',
            'mozzarella cheese',
            'mozzarella',
            'muenster cheese',
            'naked goat',
            'neufchatel cheese',
            'paneer cheese',
            'parmesan cheese',
            'parmesan',
            'pastore sini',
            'pecorino cheese',
            'pepperjack cheese',
            'pimento cheese',
            'pinconning cheese',
            'plave',
            'port de salut cheese',
            'provel cheese',
            'provolone cheese',
            'provolone',
            'quark cheese',
            'ricotta cheese',
            'romanian urda',
            'romano cheese',
            'roquefort cheese',
            'roquefort',
            'salad dressings, creamy',
            'sheep milk cheeses',
            'sour cream',
            'spray cheese',
            'stilton cheese',
            'string cheese',
            'swiss cheese',
            'teleme cheese',
            'tete de moine',
            'ubriaco del plave po-si',
            'uniekaas reserve',
            'uniekaas robusto',
            'velveeta',
            'whey protein powder',
            'white cheeses',
            'yellow cheeses',
            'cheese',
            'abbaye de belloc',
            'abbaye de citeaux',
            'abbaye du mont des cats',
            'abertam',
            'abondance',
            'acapella',
            'ackawi',
            'acorn',
            'adelost',
            'affidelice au chablis',
            "afuega'l pitu",
            'airag',
            'airedale',
            'aisy cendre',
            'allgauer emmentaler',
            'alverca',
            'ambert',
            'american cheese',
            'ami du chambertin',
            'anejo enchilado',
            'anneau du vic-bilh',
            'anthoriro',
            'appenzell',
            'aragon',
            'ardi gasna',
            'ardrahan',
            'armenian string',
            'aromes au gene de marc',
            'asadero',
            'asiago',
            'aubisque pyrenees',
            'autun',
            'avaxtskyr',
            'baby swiss',
            'babybel',
            'baguette laonnaise',
            'bakers',
            'baladi',
            'balaton',
            'bandal',
            'banon',
            "barry's bay cheddar",
            'basing',
            'basket cheese',
            'bath cheese',
            'bavarian bergkase',
            'baylough',
            'beaufort',
            'beauvoorde',
            'beenleigh blue',
            'beer cheese',
            'bel paese',
            'bergader',
            'bergere bleue',
            'berkswell',
            'bethmale des pyr\xc3\xa9n\xc3\xa9es',
            'bethmale of the pyrenees',
            'beyaz peynir',
            'bierkase',
            'bishop kennedy',
            'blarney',
            "bleu d'auvergne",
            'bleu de gex',
            'bleu de laqueuille',
            'bleu de septmoncel',
            'bleu de termignon alpage',
            'bleu des causses',
            'blue',
            'blue castello',
            'blue of termignon',
            'blue rathgore',
            'blue vein (australian)',
            'blue vein cheeses',
            'bocconcini',
            'bocconcini (australian)',
            'boeren leidenkaas',
            'bonchester',
            'bosworth',
            'bougon',
            'boule du roves',
            "boulette d'avesnes",
            'boursault',
            'boursin',
            'bouyssou',
            'bra',
            'braudostur',
            'breakfast cheese',
            'brebis du lavort',
            'brebis du lochois',
            'brebis du puyfaucon',
            'bresse bleu',
            'brick',
            'brie',
            'brie au poivre',
            'brie de meaux',
            'brie de melun',
            'brie with pepper',
            'brillat-savarin',
            'brin',
            "brin d' amour",
            "brin d'amour",
            'brinza (burduf brinza)',
            'briquette de brebis',
            'briquette du forez',
            'broccio',
            'broccio demi-affine',
            'brousse du rove',
            'bruder basil',
            'brusselae kaas (fromage de bruxelles)',
            'bryndza',
            "buchette d'anjou",
            'buffalo',
            'burgos',
            'butte',
            'butterkase',
            'button (innes)',
            'buxton blue',
            'cabecou',
            'caboc',
            'cabrales',
            'cachaille',
            'caciocavallo',
            'caciotta',
            'caerphilly',
            'cairnsmore',
            'calenzana',
            'cambazola',
            'camembert de normandie',
            'canadian cheddar',
            'canestrato',
            'cantal',
            'caprice des dieux',
            'capricorn goat',
            'capriole banon',
            'caravane',
            "carre de l'est",
            'casciotta di urbino',
            'cashel blue',
            'castellano',
            'castelleno',
            'castelmagno',
            'castelo branco',
            'castigliano',
            'cathelain',
            'celtic promise',
            "cendre d'olivet",
            'cerney',
            'chabichou',
            'chabichou du poitou',
            'chabis de gatine',
            'chaource',
            'charolais',
            'chaumes',
            'cheddar',
            'cheddar clothbound',
            'cheshire',
            'chevres',
            'chevrotin des aravis',
            'chontaleno',
            'civray',
            'coeur de camembert au calvados',
            'coeur de chevre',
            'cojack',
            'colby',
            'colby-jack',
            'cold pack',
            'comte',
            'coolea',
            'cooleney',
            'coquetdale',
            'corleggy',
            'cornish pepper',
            'cotherstone',
            'cotija',
            'cottage',
            'cougar gold',
            'coulommiers',
            'coverdale',
            'crayeux de roncq',
            'cream cheese',
            'cream havarti',
            'crema agria',
            'crema mexicana',
            'creme fraiche',
            'crescenza',
            'croghan',
            'crottin de chavignol',
            'crottin du chavignol',
            'crowdie',
            'crowley',
            'cuajada',
            'curd',
            'cure nantais',
            'curworthy',
            'cwmtawe pecorino',
            'cypress grove chevre',
            'danablu (danish blue)',
            'danbo',
            'danish fontina',
            'daralagjazsky',
            'dauphin',
            'delice des fiouves',
            'denhany dorset drum',
            'derby',
            'dessertnyj belyj',
            'devon blue',
            'devon garland',
            'dolcelatte',
            'doolin',
            'doppelrhamstufel',
            'dorset blue vinney',
            'double gloucester',
            'double worcester',
            'dreux a la feuille',
            'dry jack',
            'duddleswell',
            'dunbarra',
            'dunlop',
            'dunsyre blue',
            'duroblando',
            'durrus',
            'dutch mimolette (commissiekaas)',
            'edam',
            'edelpilz',
            'emental grand cru',
            'emlett',
            'emmental',
            'epoisses de bourgogne',
            'esbareich',
            'esrom',
            'etorki',
            'evansdale farmhouse brie',
            "evora de l'alentejo",
            'exmoor blue',
            'explorateur',
            'farmer',
            'feta',
            'feta (australian)',
            'figue',
            'filetta',
            'fin-de-siecle',
            'finlandia swiss',
            'finn',
            'fiore sardo',
            'fleur du maquis',
            'flor de guia',
            'flower marie',
            'folded',
            'folded cheese with mint',
            'fondant de brebis',
            'fontainebleau',
            'fontal',
            "fontina val d'aosta",
            'formaggio di capra',
            'fougerus',
            'four herb gouda',
            "fourme d' ambert",
            'fourme de haute loire',
            'fourme de montbrison',
            'fresh jack',
            'fresh mozzarella',
            'fresh ricotta',
            'fresh truffles',
            'fribourgeois',
            'friesekaas',
            'friesian',
            'friesla',
            'frinault',
            'fromage a raclette',
            'fromage corse',
            'fromage de montagne de savoie',
            'fromage frais',
            'fruit cream cheese',
            'frying cheese',
            'fynbo',
            'gabriel',
            'galette du paludier',
            'galette lyonnaise',
            "galloway goat's milk gems",
            'gammelost',
            "gaperon a l'ail",
            'garrotxa',
            'gastanberra',
            'geitost',
            'gippsland blue',
            'gjetost',
            'gloucester',
            'golden cross',
            'gorgonzola',
            'gornyaltajski',
            'gospel green',
            'gouda',
            'goutu',
            'gowrie',
            'grabetto',
            'graddost',
            'grafton village cheddar',
            'grana',
            'grana padano',
            'grand vatel',
            "grataron d' areches",
            'gratte-paille',
            'graviera',
            'greuilh',
            'greve',
            'gris de lille',
            'gruyere',
            'gubbeen',
            'guerbigny',
            'halloumi',
            'halloumy (australian)',
            'haloumi-style cheese',
            'harbourne blue',
            'havarti',
            'heidi gruyere',
            'hereford hop',
            'herrgardsost',
            'herriot farmhouse',
            'herve',
            'hipi iti',
            'hubbardston blue cow',
            'humboldt fog',
            'hushallsost',
            'iberico',
            'idaho goatster',
            'idiazabal',
            'il boschetto al tartufo',
            "ile d'yeu",
            'isle of mull',
            'jarlsberg',
            'jermi tortes',
            'jibneh arabieh',
            'jindi brie',
            'jubilee blue',
            'juustoleipa',
            'kadchgall',
            'kaseri',
            'kashta',
            'kefalotyri',
            'kenafa',
            'kernhem',
            'kervella affine',
            'kikorangi',
            'king island cape wickham brie',
            'king river gold',
            'klosterkaese',
            'knockalara',
            'kugelkase',
            "l'aveyronnais",
            "l'ecir de l'aubrac",
            'la taupiniere',
            'la vache qui rit',
            'laguiole',
            'lairobell',
            'lajta',
            'lanark blue',
            'lancashire',
            'langres',
            'lappi',
            'laruns',
            'lavistown',
            'le brin',
            'le fium orbo',
            'le lacandou',
            'le roule',
            'leafield',
            'lebbene',
            'leerdammer',
            'leicester',
            'leyden',
            'limburger',
            'lincolnshire poacher',
            "lingot saint bousquet d'orb",
            'liptauer',
            'little rydings',
            'livarot',
            'llanboidy',
            'llanglofan farmhouse',
            'loch arthur farmhouse',
            'loddiswell avondale',
            'longhorn',
            'lou palou',
            'lou pevre',
            'lyonnais',
            'maasdam',
            'macconais',
            'mahoe aged gouda',
            'mahon',
            'malvern',
            'mamirolle',
            'manchego',
            'manouri',
            'manur',
            'marble cheddar',
            'marbled cheeses',
            'maredsous',
            'margotin',
            'maribo',
            'maroilles',
            'mascares',
            'mascarpone',
            'mascarpone (australian)',
            'mascarpone torta',
            'matocq',
            'maytag blue',
            'meira',
            'menallack farmhouse',
            'menonita',
            'meredith blue',
            'mesost',
            'metton (cancoillotte)',
            'meyer vintage gouda',
            'mihalic peynir',
            'milleens',
            'mimolette',
            'mine-gabhar',
            'mini baby bells',
            'mixte',
            'molbo',
            'monastery cheeses',
            'mondseer',
            "mont d'or lyonnais",
            'montasio',
            'monterey jack',
            'monterey jack dry',
            'morbier',
            'morbier cru de montagne',
            'mothais a la feuille',
            'mozzarella',
            'mozzarella (australian)',
            'mozzarella di bufala',
            'mozzarella fresh, in water',
            'mozzarella rolls',
            'muenster',
            'munster',
            'murol',
            'mycella',
            'myzithra',
            'naboulsi',
            'nantais',
            'neufchatel',
            'neufchatel (australian)',
            'niolo',
            'nokkelost',
            'northumberland',
            'oaxaca',
            'olde york',
            'olivet au foin',
            'olivet bleu',
            'olivet cendre',
            'orkney extra mature cheddar',
            'orla',
            'oschtjepka',
            'ossau fermier',
            'ossau-iraty',
            'oszczypek',
            'oxford blue',
            "p'tit berrichon",
            'palet de babligny',
            'paneer',
            'panela',
            'pannerone',
            'pant ys gawn',
            'parmesan',
            'parmigiano reggiano',
            "pas de l'escalette",
            'passendale',
            'pasteurized processed',
            'pate de fromage',
            'patefine fort',
            "pave d'affinois",
            "pave d'auge",
            'pave de chirac',
            'pave du berry',
            'pecorino',
            'pecorino in walnut leaves',
            'pecorino romano',
            'peekskill pyramid',
            'pelardon des cevennes',
            'pelardon des corbieres',
            'penamellera',
            'penbryn',
            'pencarreg',
            'pepper jack',
            'perail de brebis',
            'petit morin',
            'petit pardou',
            'petit-suisse',
            'picodon de chevre',
            'picos de europa',
            'pinconning',
            'piora',
            'pithtviers au foin',
            'plateau de herve',
            'plymouth cheese',
            'podhalanski',
            "poivre d'ane",
            'polkolbin',
            "pont l'eveque",
            'port nicholson',
            'port-salut',
            'postel',
            'pouligny-saint-pierre',
            'pourly',
            'prastost',
            'pressato',
            'prince-jean',
            'processed cheddar',
            'provel',
            'provolone',
            'provolone (australian)',
            'pyengana cheddar',
            'pyramide',
            'quark',
            'quark (australian)',
            'quartirolo lombardo',
            'quatre-vents',
            'quercy petit',
            'queso blanco',
            'queso blanco con frutas --pina y mango',
            'queso de murcia',
            'queso del montsec',
            'queso del tietar',
            'queso fresco',
            'queso fresco (adobera)',
            'queso iberico',
            'queso jalapeno',
            'queso majorero',
            'queso media luna',
            'queso para frier',
            'queso quesadilla',
            'rabacal',
            'raclette',
            'ragusano',
            'raschera',
            'reblochon',
            'red leicester',
            'regal de la dombes',
            'reggianito',
            'remedou',
            'requeson',
            'richelieu',
            'ricotta',
            'ricotta (australian)',
            'ricotta salata',
            'ridder',
            'rigotte',
            'rocamadour',
            'rollot',
            'romano',
            'romans part dieu',
            'roncal',
            'roquefort',
            'roule',
            'rouleau de beaulieu',
            'royalp tilsit',
            'rubens',
            'rustinu',
            'saaland pfarr',
            'saanenkaese',
            'saga',
            'sage derby',
            'sainte maure',
            'saint-marcellin',
            'saint-nectaire',
            'saint-paulin',
            'salers',
            'samso',
            'san simon',
            'sancerre',
            'sap sago',
            'sardo',
            'sardo egyptian',
            'sbrinz',
            'scamorza',
            'schabzieger',
            'schloss',
            'selles sur cher',
            'selva',
            'serat',
            'seriously strong cheddar',
            'serra da estrela',
            'sharpam',
            'shelburne cheddar',
            'shropshire blue',
            'siraz',
            'sirene',
            'smoked gouda',
            'somerset brie',
            'sonoma jack',
            'sottocenare al tartufo',
            'soumaintrain',
            'sourire lozerien',
            'spenwood',
            'sraffordshire organic',
            'st. agur blue cheese',
            'stilton',
            'stinking bishop',
            'string',
            'sussex slipcote',
            'sveciaost',
            'swaledale',
            'sweet style swiss',
            'swiss',
            'syrian (armenian string)',
            'tala',
            'taleggio',
            'tamie',
            'tasmania highland chevre log',
            'taupiniere',
            'teifi',
            'telemea',
            'testouri',
            'tete de moine',
            'tetilla',
            'texas goat cheese',
            'tibet',
            'tillamook cheddar',
            'tilsit',
            'timboon brie',
            'toma',
            'tomme brulee',
            "tomme d'abondance",
            'tomme de chevre',
            'tomme de romans',
            'tomme de savoie',
            'tomme des chouans',
            'tommes',
            'torta del casar',
            'toscanello',
            "touree de l'aubier",
            'tourmalet',
            'trappe (veritable)',
            'trois cornes de vendee',
            'tronchon',
            'trou du cru',
            'truffe',
            'tupi',
            'turunmaa',
            'tymsboro',
            'tyn grug',
            'tyning',
            'ubriaco',
            'ulloa',
            'vacherin-fribourgeois',
            'valencay',
            'vasterbottenost',
            'venaco',
            'vendomois',
            'vieux corse',
            'vignotte',
            'vulscombe',
            'waimata farmhouse blue',
            'washed rind cheese (australian)',
            'waterloo',
            'weichkaese',
            'wellington',
            'wensleydale',
            'white stilton',
            'whitestone farmhouse',
            'wigmore',
            'woodside cabecou',
            'xynotyro',
            'yarg cornish',
            'yarra valley pyramid',
            'yorkshire blue',
            'zamorano',
            'zanetti grana padano',
            'zanetti parmigiano reggiano'],
 'eggs': ['eggs',
          'egg',
          'egg whites',
          'egg yolks',
          'fried egg',
          'poached egg',
          'liquid eggs',
          'liquid egg',
          'liquid egg whites'],
 'meat': ['beef',
          'beef stew',
          'beef brisket',
          'corned beef',
          'beef tongue',
          'beef sirloin',
          'beef top sirloin',
          'beef sirloin',
          'beef heart',
          'steaks',
          'calf',
          'bison',
          'buffalo',
          'ground beef',
          'beef chuck',
          'steak',
          'horse',
          'veal ',
          'lamb',
          'pork',
          'bacon',
          'bacon bits',
          'turkey bacon',
          'pork belly',
          'pork loin',
          'pork rib',
          'ham',
          'rump',
          'roast',
          'porterhouse',
          'poultry',
          'chicken',
          'chicken breast',
          'chicken thigh',
          'chicken feet',
          'duck',
          'goose',
          'quail',
          'squab',
          'turkey ',
          'snipe',
          'pork',
          'ribs',
          'pork chop',
          'tenderloin ',
          'ham',
          'pancetta',
          'prosciutto',
          'sausage',
          'italian sausage',
          'chorizo',
          'ribs',
          'rib',
          'rib tips',
          'ribeye steak',
          'ribeye',
          'rib roast',
          'rabbit',
          'venison',
          'boar',
          'roast',
          'shoulder',
          'sweetbread',
          'liver',
          'kidney',
          'tongue',
          'duck',
          'goose',
          'bear',
          'goat',
          'quail',
          'pheasant',
          'pigeon',
          'squab',
          'chicken',
          'turkey',
          'snipe',
          'mutton',
          'fish',
          'salmon',
          'catfish',
          'shrimp',
          'tuna',
          'capon',
          'tendon',
          'tripe',
          'oyster',
          'halibut',
          'fish',
          'squirrel',
          'rib',
          'brisket',
          'ribeye',
          'tenderloin',
          'sirloin',
          'filet mignon',
          'flank',
          'flank steak',
          'frog',
          'frankfurter',
          'spam',
          'reindeer',
          'moose',
          'elk',
          'turtle',
          'intestines',
          'caribou',
          'snake',
          'meatloaf',
          'ox tail',
          'ostrich',
          'squab',
          'goose liver',
          'pastrami'],
 'milk': ['milk',
          'whole milk',
          '2% milk',
          'cow-skim milk',
          'cow-whole milk',
          'skim milk',
          'ghee',
          'buttermilk milk',
          'buttermilk',
          'goat milk'],
 'saltysauces': ['fish sauce', 'oyster sauce'],
 'sauces': ['sour cream', 'mayonnaise', 'whipped cream'],
 'honey': ['honey'],
 'seafood': ['calimari',
             'shrimp',
             'oyster',
             'clam',
             'prawns',
             'lobster',
             'crayfish',
             'mussels',
             'octopus'],
 'yogurts': ['yogurt',
             'greek yogurt',
             'cow yogurt',
             'flavored yogurt',
             'low-fat yogurt',
             'ice cream']}

rtype = {'broths': ['vegetable broth'],
 'butter': ['vegetable oil'],
 'cheese': ['tofu gouda',
            'cashew brie',
            'almond cheese',
            'cashew mozzarella',
            'cashew cheese',
            'aquafaba cheese',
            'aquafaba',
            'soy cheese',
            'tapioca cheese'],
 'eggs': ['flegg',
          'chia seeds',
          'silken tofu',
          'applesauce',
          'mashed banana',
          'tofu'],
 'meat': ['seitan',
          'tofu',
          'tempeh',
          'beans',
          'chickpeas',
          'mushrooms',
          'jackfruit',
          'eggplant',
          'lentils',
          'cauliflower'],
 'milk': ['soymilk',
          'soy milk',
          'rice milk',
          'oat milk',
          'coconut milk',
          'almond milk',
          'hemp milk'],
 'saltysauces': ['vegan fish sauce', 'vegan oyster sauce'],
 'sauces': ['coco whip',
            'rice whip',
            'soy whip',
            'vegan sour cream',
            'vegan mayo',
            'veganaise'],
 'seafood': ['eggplant', 'tofu', 'mashed chickpeas'],
 'honey': ['maple syrup'],
 'yogurts': ['soy yogurt',
             'rice yogurt',
             'coconut milk yogurt',
             'almond milk yogurt',
             'arrowroot yogurt',
             'cashew milk yogurt',
             'rice cream',
             'soy ice cream',
             'cashew milk ice cream',
             'coconut ice cream',
             'almond milk ice cream']}


import json
import sys
import re
import urllib

from bs4 import BeautifulSoup

def contractAsJson(filename):
#yfs_l84_aapl
#span id="yfs_l84_aapl"
#98.94
    soup = BeautifulSoup (open(filename))

    keywords = soup.find("meta",{'name':'keywords'})
    content = keywords.attrs['content']
    symbolre = re.search(r'(\w+)',content)
    mysymbol = symbolre.group()
    priceLabel = "yfs_l84_" + mysymbol.lower()
    priceTag = soup.find('span', {'id': priceLabel})
    price = priceTag.contents[0]
    priceInt = float(price)

    overDict = {}
    overDict["currPrice"] = priceInt
    jsonObjen = json.dumps({"currPrice":priceInt})
    jsonObjde = json.loads(jsonObjen)

    expiration = soup.find_all("td")
    urlList =[]
    for td in expiration:
        view = td.next_element
        if(view == "View By Expiration: "):
            urls = td.findAll('a')

            for url in urls:
                if(url.find("strong",text='Expand to Straddle View...')):
                    urlList.append(url)
                for month in ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep', "Oct",'Nov','Dec']:
                    if(month in url.next_element):
                        urlList.append(url)
    hrefList = []
    for url in urlList:
        sub = url.attrs['href']
        index = sub.find('m=')
        sub = sub[:index] +'amp;' + sub[index:]
        hrefList.append("http://finance.yahoo.com" + sub)

    overDict["dateUrls"] = hrefList
    jsonObjen += json.dumps({"dateUrls":hrefList})
    x = 0
    table = soup.find("td",{'class': "yfnc_h"})
    tablep = table.parent
    tablepp = tablep.parent
    optList = []
    num = 0
    for child in tablepp.children:
        aOpt = optQ()
        num = 0
        # print child.name
        for td in child.children:
            if td.attrs["class"][0] == "yfnc_h" or td.attrs["class"][0] == "yfnc_tabledata1":
                # print td.name
                if num == 0:
                    a = td.next_element
                    strong = a.next_element
                    strike = strong.next_element
                    aOpt.Strike = strike
                elif num == 1:
                    a = td.next_element
                    content = a.next_element
                    search = re.search("(\d)",content)
                    group1 = search.group(1)
                    if group1 == "7":
                        search = re.search("([a-zA-z]+\d)",content)
                        aOpt.Symbol = search.group(1)
                    else:
                        search = re.search("([a-zA-z]+)",content)
                        aOpt.Symbol = search.group(1)
                    search = re.search("(\d)",content)
                    group1 = search.group(1)
                    if group1 == "7":
                        search = re.search("\d(\d+)",content)
                        date = search.group(1)
                    else:
                        search = re.search("(\d+)",content)
                        date = search.group(1)
                    aOpt.Date = date
                    search = re.search("[a-zA-z]+\d+([a-zA-Z])",content)
                    aOpt.Type = search.group(1)
                elif num == 2:
                    if(td.next_element == "N/A"):
                        aOpt.Last = td.next_element
                    else:
                        a = td.find("b")
                        aOpt.Last = a.next_element
                elif num == 3:
                    b = td.find("b")
                    aOpt.Change = " " + b.next_element
                elif num == 4:
                    aOpt.Bid = td.next_element
                elif num == 5:
                    aOpt.Ask = td.next_element
                elif num == 6:
                    aOpt.Vol = td.next_element
                elif num == 7:
                    aOpt.Open = td.next_element

                if num == 7:
                    optList.append(aOpt)
            num +=1

    table = soup.find_all("table",{"class":"yfnc_datamodoutline1"})
    tabled = table[1].find("td",{'class': "yfnc_tabledata1"})
    tablep = tabled.parent
    tablepp = tablep.parent

    num = 0
    for child in tablepp.children:
        aOpt = optQ()
        num = 0
        # print child.name
        for td in child.children:
            if td.attrs["class"][0] == "yfnc_h" or td.attrs["class"][0] == "yfnc_tabledata1":
                # print td.name
                if num == 0:
                    a = td.next_element
                    strong = a.next_element
                    strike = strong.next_element
                    aOpt.Strike = strike
                elif num == 1:
                    a = td.next_element
                    content = a.next_element
                    search = re.search("(\d)",content)
                    group1 = search.group(1)
                    if group1 == "7":
                        search = re.search("([a-zA-z]+\d)",content)
                        aOpt.Symbol = search.group(1)
                    else:
                        search = re.search("([a-zA-z]+)",content)
                        aOpt.Symbol = search.group(1)
                    search = re.search("(\d)",content)
                    group1 = search.group(1)
                    if group1 == "7":
                        search = re.search("\d(\d+)",content)
                        date = search.group(1)
                    else:
                        search = re.search("(\d+)",content)
                        date = search.group(1)
                    aOpt.Date = date
                    search = re.search("[a-zA-z]+\d+([a-zA-Z])",content)
                    aOpt.Type = search.group(1)
                elif num == 2:
                    if(td.next_element == "N/A"):
                        aOpt.Last = td.next_element
                    else:
                        a = td.find("b")
                        aOpt.Last = a.next_element
                elif num == 3:
                    b = td.find("b")
                    aOpt.Change = " " + b.next_element
                elif num == 4:
                    aOpt.Bid = td.next_element
                elif num == 5:
                    aOpt.Ask = td.next_element
                elif num == 6:
                    aOpt.Vol = td.next_element
                elif num == 7:
                    aOpt.Open = td.next_element

                if num == 7:
                    optList.append(aOpt)
            num +=1

    optList.sort(comparator)
    optDictList = []
    for opt in optList:
        dict = {}
        dict["Ask"] = opt.Ask
        dict["Bid"] = opt.Bid
        dict["Change"] = opt.Change
        dict["Date"] = opt.Date
        dict["Last"] = opt.Last
        dict["Open"] = opt.Open
        dict["Strike"] = opt.Strike
        dict["Symbol"] = opt.Symbol
        dict["Type"] = opt.Type
        dict["Vol"] = opt.Vol
        optDictList.append(dict)

    overDict["optionQuotes"] = optDictList
    jsonObjFinal = json.dumps(overDict,sort_keys=True,indent = 4)

    # jsonQuoteData = "[]"
    # return jsonQuoteData
    return jsonObjFinal

def comparator(x, y):
    openX = int(x.Open.replace(",",""))
    openY = int(y.Open.replace(",",""))
    return openY - openX

class optQ:

    def __init__(self):
        self.Ask = ""
        self.Bid = ""
        self.Change = ""
        self.Date = ""
        self.Last = ""
        self.Open = ""
        self.Strike = ""
        self.Symbol = ""
        self.Type = ""
        self.Vol = ""

    def toJson(self):
        return "Ask = " + self.Ask


#!/usr/bin/python3
"""Singly Linked-List

Classes to create a singly-linked list

"""

class Node:
    """a single node in a singly-linked list

    Each node holds a piece of data and a reference to the next node.

    """

    @property
    def data(self):
        """int: the data stored in this node

        The setter validates the type

        Returns:
            the private data attribute

        """

        return self.__data

    @data.setter
    def data(self, val):
        if not isinstance(val, int):
            raise TypeError('data must be an integer')
        self.__data = val

    @property
    def next_node(self):
        """Node: reference to the next node

        The setter validates that the value is either a Node or None

        Returns:
            the private next node attribute

        """

        return self.__next_node

    @next_node.setter
    def next_node(self, val):
        if val is not None and not isinstance(val, Node):
            raise TypeError('next_node must be a Node object')
        self.__next_node = val

    def __init__(self, data, next_node=None):
        """Create a new Node object

        Data must be given to put in this node, but it doesn't have to have a
        next node yet.

        Args:
            data (int): data to store in this node
            next_node (Node): the next node in the list

        Raises:
            TypeError: an argument has the wrong type for its attribute

        """

        self.data = data
        self.next_node = next_node

class SinglyLinkedList:
    """The linked list itself, with reference to the first node

    Does not contain a collection of nodes, they refer to each other.

    """

    def __init__(self):
        """Create an empty linked list

        These lists are always sorted

        """

        self.__head = None

    def __str__(self):
        """Show each piece of data in the list

        Returns:
            str: each integer in the list, separated by a line break

        """

        node = self.__head
        data = []
        while node is not None:
            data.append(node.data)
            node = node.next_node
        return '\n'.join('{:d}'.format(i) for i in data)

    def sorted_insert(self, value):
        """Insert a new value at the sorted position

        These lists are sorted smallest to largest

        Args:
            value (int): value to add to list

        """

        if self.__head is None:
            self.__head = Node(value, None)
            return
        node = self.__head
        previous = None
        while node is not None and node.data <= value:
            previous = node
            node = node.next_node
        node = Node(value, node)
        if previous is not None:
            previous.next_node = node
        else:
            self.__head = node


import os
import logging
from multiprocessing.dummy import Pool

from .config import Config
from .event import EventBus
from .plugin import PluginManager

class Joseph:
    default_config = {
        'AUTO_UPDATE': False,
        'APP_ROOT': os.path.dirname(os.path.dirname(__file__)),
        'CONFIG_FILE': 'joseph.cfg',
        'DEFAULT_PROCESSES': 4,
        'LOG_LEVEL': 'INFO',
        'PLUGIN_FOLDER': 'plugins',
        'UPDATE_CHANNEL': 'stable',
    }
    bus = None
    plugin_manager = None

    def __init__(self, **kwargs):
        self._started = False
        self.config = Config(self.default_config['APP_ROOT'], self.get_logger('config'), defaults=self.default_config)
        self.pool = Pool(self.config['DEFAULT_PROCESSES'])

        for key, item in kwargs.items():
            setattr(self, key, item)

    def main(self, **kwargs):
        """ Main program loop """
        try:
            while True:
                if not self._started:
                    self.startup(kwargs)

        except KeyboardInterrupt:
            self.shutdown()

    def startup(self, kwargs={}):
        """ Start up procedure """
        self.config.from_file(self.config['CONFIG_FILE'])

        self.bus = EventBus(self.pool, self.get_logger('event_bus'))
        self.plugin_manager = PluginManager(self.pool, self.config['PLUGIN_FOLDER'], self.get_logger('plugin_manager'))
        self.plugin_manager.discover_and_run()

        self._started = True
        self.bus.dispatch('joseph:started')

    def shutdown(self):
        """ Shutdown procedure """
        self.bus.dispatch('joseph:shutdown')
        self.pool.close()
        self.pool.join()

    def get_logger(self, name):
        return logging.getLogger('joseph.' + name)


import unittest
import zserio

from testutils import getZserioApi

class BoolParamChoiceTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.api = getZserioApi(__file__, "choice_types.zs").bool_param_choice

    def testSelectorConstructor(self):
        selector = True
        boolParamChoice = self.api.BoolParamChoice(selector)
        self.assertEqual(selector, boolParamChoice.getSelector())

    def testFromReader(self):
        selector = False
        value = 234
        writer = zserio.BitStreamWriter()
        BoolParamChoiceTest._writeBoolParamChoiceToStream(writer, selector, value)
        reader = zserio.BitStreamReader(writer.getByteArray())
        boolParamChoice = self.api.BoolParamChoice.fromReader(reader, selector)
        self.assertEqual(selector, boolParamChoice.getSelector())
        self.assertEqual(value, boolParamChoice.getGrey())

    def testEq(self):
        enumParamChoice1 = self.api.BoolParamChoice(True)
        enumParamChoice2 = self.api.BoolParamChoice(True)
        self.assertTrue(enumParamChoice1 == enumParamChoice2)

        value = 99
        enumParamChoice1.setBlack(value)
        self.assertFalse(enumParamChoice1 == enumParamChoice2)

        enumParamChoice2.setBlack(value)
        self.assertTrue(enumParamChoice1 == enumParamChoice2)

        diffValue = value + 1
        enumParamChoice2.setBlack(diffValue)
        self.assertFalse(enumParamChoice1 == enumParamChoice2)

    def testHash(self):
        enumParamChoice1 = self.api.BoolParamChoice(True)
        enumParamChoice2 = self.api.BoolParamChoice(True)
        self.assertEqual(hash(enumParamChoice1), hash(enumParamChoice2))

        value = 99
        enumParamChoice1.setBlack(value)
        self.assertTrue(hash(enumParamChoice1) != hash(enumParamChoice2))

        enumParamChoice2.setBlack(value)
        self.assertEqual(hash(enumParamChoice1), hash(enumParamChoice2))

        diffValue = value + 1
        enumParamChoice2.setBlack(diffValue)
        self.assertTrue(hash(enumParamChoice1) != hash(enumParamChoice2))

    def testGetSelector(self):
        selector = True
        boolParamChoice = self.api.BoolParamChoice(selector)
        self.assertEqual(selector, boolParamChoice.getSelector())

    def testGetSetBlack(self):
        boolParamChoice = self.api.BoolParamChoice(True)
        value = 99
        boolParamChoice.setBlack(value)
        self.assertEqual(value, boolParamChoice.getBlack())

    def testGetSetGrey(self):
        boolParamChoice = self.api.BoolParamChoice(False)
        value = 234
        boolParamChoice.setGrey(value)
        self.assertEqual(value, boolParamChoice.getGrey())

    def testBitSizeOf(self):
        boolParamChoice = self.api.BoolParamChoice(True)
        self.assertEqual(8, boolParamChoice.bitSizeOf())

        boolParamChoice = self.api.BoolParamChoice(False)
        self.assertEqual(16, boolParamChoice.bitSizeOf())

    def testInitializeOffsets(self):
        boolParamChoice = self.api.BoolParamChoice(True)
        bitPosition = 1
        self.assertEqual(9, boolParamChoice.initializeOffsets(bitPosition))

        boolParamChoice = self.api.BoolParamChoice(False)
        self.assertEqual(17, boolParamChoice.initializeOffsets(bitPosition))

    def testReadWrite(self):
        selector = True
        boolParamChoice = self.api.BoolParamChoice(selector)
        byteValue = 99
        boolParamChoice.setBlack(byteValue)
        writer = zserio.BitStreamWriter()
        boolParamChoice.write(writer)
        readBoolParamChoice = self.api.BoolParamChoice(selector)
        reader = zserio.BitStreamReader(writer.getByteArray())
        readBoolParamChoice.read(reader)
        self.assertEqual(byteValue, readBoolParamChoice.getBlack())
        self.assertEqual(boolParamChoice, readBoolParamChoice)

        selector = False
        boolParamChoice = self.api.BoolParamChoice(selector)
        shortValue = 234
        boolParamChoice.setGrey(shortValue)
        writer = zserio.BitStreamWriter()
        boolParamChoice.write(writer)
        readBoolParamChoice = self.api.BoolParamChoice(selector)
        reader = zserio.BitStreamReader(writer.getByteArray())
        readBoolParamChoice.read(reader)
        self.assertEqual(shortValue, readBoolParamChoice.getGrey())
        self.assertEqual(boolParamChoice, readBoolParamChoice)

    @staticmethod
    def _writeBoolParamChoiceToStream(writer, selector, value):
        if selector:
            writer.writeSignedBits(value, 8)
        else:
            writer.writeSignedBits(value, 16)