import csv
import os
import pandas as pd
import numpy as np
##import indicatorsData
##import Doji
##import getPrice
##import longBody
##import getTrend
import logging
from datetime import datetime, timedelta
import matplotlib
import matplotlib.dates as mdates
import matplotlib.pyplot as plt
from matplotlib import dates, ticker
from datetime import datetime , timedelta
from mpl_finance import candlestick_ohlc
matplotlib.use('TkAgg')
##from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
import matplotlib.animation as animation
from matplotlib import style
style.use('ggplot')
'''
Too High/Low RSI on 5 min candle and
last candle: Very long and more than double average volume for that day.
Basically I need to find out GREED BUYING...
last 5 min body twice that of wicks

'''

try:
    stocksAll = []
####    logging.basicConfig(level=logging.ERROR)
##    time_log = datetime.now().strftime('MILLION_%H_%M_%d_%m_%Y.log')
##    logging.basicConfig(filename = time_log ,level=logging.INFO)
    def save_fig(df_stock_5min_temp, filename):
        df_stock_5min_temp['TIMESTR'] = [str(i).replace('9', '09') for i in df_stock_5min_temp['TIME'].values]
        df_stock_5min_temp['TIME_'] = [str(i)[:2] + ':' + str(i)[2:] for i in df_stock_5min_temp['TIMESTR'].values]
        df_stock_5min_temp['DATETIME'] = df_stock_5min_temp['DATE'].map(str) + ' '+df_stock_5min_temp['TIME_']
        df_stock_5min_temp['DATETIME'] = pd.to_datetime(df_stock_5min_temp['DATETIME'])

        df_stock_5min_temp["dateCopy"] = df_stock_5min_temp['DATETIME']
        df_stock_5min_temp["MPLDates"] = df_stock_5min_temp["dateCopy"].apply(lambda date: mdates.date2num(date.to_pydatetime()))
        del df_stock_5min_temp["dateCopy"]
        ##                        print(df_stock_5min_temp)
        allDates = df_stock_5min_temp["MPLDates"].tolist()
        dates1 = np.asarray(allDates)
        ##        print(dates)
        ##        quotes = quotes.astype(float)

        Volume = df_stock_5min_temp['TIME'].apply(float).tolist()

        volume = np.asarray(Volume)
        ##    ax = plt.subplots()
        ax = plt.subplot2grid((6,4), (0,0), rowspan = 5, colspan = 4)
        av = plt.subplot2grid((6,4), (5,0), rowspan = 1, colspan = 4, sharex = ax)
        av.fill_between(allDates, 0, volume,facecolor='green', alpha=0.5)
        ##        av.bar(allDates,volume,align='center')
        ax.xaxis.set_major_formatter(mdates.DateFormatter('%d/%m/%Y %H:%M'))
        ax.xaxis.set_major_locator(ticker.MaxNLocator(10))
        candlestick_ohlc(ax, df_stock_5min_temp[["MPLDates","OPEN","HIGH","LOW","CLOSE"]].values, width = 0.5 , colorup = 'g', colordown = 'r', alpha = 0.8)
        ax.xaxis_date()
        ax.autoscale_view()
        ##        plt.setp(av.get_xticklabels(), rotation=45, horizontalalignment='right')
        plt.setp(ax.get_xticklabels(), visible = False) ## this removes x - axis
        plt.setp(av.get_xticklabels(), visible = False)

        ##        plt.subplots_adjust(hspace=.0)
        plt.tight_layout()
##        plt.show()
        plt.savefig('figure/{}'.format(filename), bbox_inches='tight')

    def record_winLoss(nrdf,check_df,symbol):
        global NRfound, win, loss
        nro = float(nrdf['OPEN'].values)
        nrc = float(nrdf['CLOSE'].values)
        nrh = float(nrdf['HIGH'].values)
        nrl = float(nrdf['LOW'].values)

        chko = float(check_df['OPEN'].values)
        chkc = float(check_df['CLOSE'].values)
        chkh = float(check_df['HIGH'].values)
        chkl = float(check_df['LOW'].values)

        if (chkl > nrl and chkh > nrh) or (chkh < nrh and chkl < nrl):
            print('Win on',check_df['DATE'].values, 'for', symbol )
            win += 1
        else:
            print('Loss on',check_df['DATE'].values,  'for', symbol )
            loss += 1

    def calculate_rsi_df(df_stock,rsi_period):
        delta = df_stock['CLOSE'].diff()
        dUp, dDown = delta.copy(), delta.copy()
        dUp[dUp < 0] = 0
        dDown[dDown > 0] = 0
        RolUp = pd.rolling_mean(dUp, rsi_period)
        RolDown = pd.rolling_mean(dDown,rsi_period).abs()
        RS = RolUp / RolDown
        rsi= 100.0 - (100.0 / (1.0 + RS))
        df_stock['RSI'] = rsi
##        return df_stock['RSI'].tail(1).values
        return df_stock

    def NR_BACKTST(companies, days):
        global NRfound, win, loss
        for symbol in companies:
            df_stock = pd.read_csv('../SYMBOL/STOCK_{}.csv'.format(symbol))
##            df_stock = calculate_rsi_df(df_stock,14)
##            for i in [50,20,9]:
##               df_stock['MA{}'.format(i)] =  df_stock['CLOSE'].rolling(window=i).mean()

##            print(df_stock.tail(5))
            df_stock['RANGE'] = df_stock['HIGH'] - df_stock['LOW']
            for i in range(0,days):
                check_df = df_stock.tail(1)
##                print(check_df)
                df_stock.drop(df_stock.tail(1).index,inplace=True)
                stockdf = df_stock.tail(21) ##NR21 NR7

                NRdf = df_stock.tail(1)
                stockdf.drop(stockdf.tail(1).index,inplace=True)

##                print(NRdf)

##                stockdf.drop(stockdf.tail(1).index,inplace=True)
                sixRange = stockdf['RANGE'].values
    ##            print(sixRange)
                if all(i > NRdf['RANGE'].values for i in sixRange) and stockdf['HIGH'].tail(1).values > NRdf['HIGH'].values and\
                   stockdf['LOW'].tail(1).values < NRdf['LOW'].values:

##                    print('NR candle found on',NRdf['DATE'].values,' for', symbol )
                    print(symbol,NRdf['DATE'].values)
##                    NRfound += 1
##                    record_winLoss(NRdf, check_df,symbol)

##                print('---------------------------------------')

    def get_ob_os_stocks(companies, flg = True):
        if flg == True:
            ob = []
            os = []
            for symbol in companies:
                df_stock = pd.read_csv('../SYMBOL/STOCK_{}.csv'.format(symbol))
                df_stock = calculate_rsi_df(df_stock,9)
                if df_stock.tail(1)['RSI'].values > 80:
                    ob.append(symbol)
                elif df_stock.tail(1)['RSI'].values < 20:
                    os.append(symbol)
            print('OVERBOUGHT LIST = ', ob)
            print('OVERSOLD LIST = ', os)
        else:
            ob = []
            os = []
            for symbol in companies:
                df_stock = pd.read_csv('../SYMBOL/STOCK_{}.csv'.format(symbol))
                df_stock = calculate_rsi_df(df_stock,9)
                if df_stock.tail(1)['RSI'].values > 75:
                    ob.append(symbol)
                elif df_stock.tail(1)['RSI'].values <= 25:
                    os.append(symbol)
            print('UP LIST = ', len(ob))
            print('DOWN LIST = ', len(os))

    def get_3IIIB_stocks(companies, flg = True):
        global stocksAll
        _IIIB = []
        for symbol in companies:
            df_stock = pd.read_csv('../SYMBOL/STOCK_{}.csv'.format(symbol))
            df_lastThree = df_stock.tail(3)
##            print(df_lastThree.iloc[0]['HIGH'])
            if df_lastThree.iloc[0]['HIGH'] > df_lastThree.iloc[1]['HIGH']\
               and df_lastThree.iloc[0]['LOW'] < df_lastThree.iloc[1]['LOW']\
               and df_lastThree.iloc[1]['HIGH'] > df_lastThree.iloc[2]['HIGH']\
               and df_lastThree.iloc[1]['LOW'] < df_lastThree.iloc[2]['LOW']:
                _IIIB.append(symbol)
        print('3 INSIDE BARS,', _IIIB)

    def get_DMA_stocks(companies, flg = True):
        global stocksAll
        if flg == True:
            _DMA50 = []
            _DMA100 = []
            for symbol in companies:
                df_stock = pd.read_csv('../SYMBOL/STOCK_{}.csv'.format(symbol))
                for i in [100,50]:
                    df_stock['MA{}'.format(i)] =  df_stock['CLOSE'].rolling(window=i).mean()

                closep = df_stock.tail(1)['CLOSE'].values
                ma100 = df_stock.tail(1)['MA100'].values
                ma50 = df_stock.tail(1)['MA50'].values

                UL = closep + ((closep * (1))/100)
                LL = closep - ((closep * (1))/100)
                if ma100 > LL and ma100 < UL:
                    _DMA100.append(symbol)
                if ma50 > LL and ma50 < UL:
                    _DMA50.append(symbol)
            print('STOCK NEAR 5ODMA', _DMA50)
            print('STOCK NEAR 100DMA', _DMA100)
        else:
            bullish = []
            bearish = []
            _j = 5
            for symbol in companies:
                df_stock = pd.read_csv('../SYMBOL/STOCK_{}.csv'.format(symbol))
                for i in [_j]:
                    df_stock['MA'] =  df_stock['CLOSE'].rolling(window=i).mean()
                closep = df_stock.tail(1)['CLOSE'].values
                ma = df_stock.tail(1)['MA'].values
                if closep > ma:
                    bullish.append(symbol)
                elif closep < ma:
                    bearish.append(symbol)
            print('MA UP list', len(bullish))
            print('MA DOWN list',len(bearish))

    def get_NR_list(companies, day):
        global stocksAll
        NR = []
        for symbol in companies:
            stockdf = pd.DataFrame()
            df_stock = pd.read_csv('../SYMBOL/STOCK_{}.csv'.format(symbol))
            stockdf = df_stock.tail(day)
            ## get aVERAGE Volume for todays
            stockdf['RANGE'] = stockdf['HIGH'] - stockdf['LOW']
            lastdaydf = stockdf.tail(1)
            stockdf.drop(stockdf.tail(1).index,inplace=True)
            sixRange = stockdf['RANGE'].values
##            print(sixRange)
            if all(i > lastdaydf['RANGE'].values for i in sixRange) and stockdf['HIGH'].tail(1).values > lastdaydf['HIGH'].values and stockdf['LOW'].tail(1).values < lastdaydf['LOW'].values:
                NR.append(symbol)

        return NR

    def getUPDOWNTrendingStock(companies):
        global stocksAll
        stocks_to_buy=[]
        stocks_to_sell=[]
        for stock in companies:
            try:
                df_Trendstock = pd.read_csv('../SYMBOL/STOCK_{}.csv'.format(stock))
                df_Trendstock = calculate_rsi_df(df_Trendstock,14)
                for i_ma in [50,20,9,5,3]:
                    df_Trendstock['MA{}'.format(i_ma)] =  df_Trendstock['CLOSE'].rolling(window=i_ma).mean()
                PPDH         = df_Trendstock.tail(3)['HIGH'].values[0]
                PPDL         = df_Trendstock.tail(3)['LOW'].values[0]
                PDH         = df_Trendstock.tail(2)['HIGH'].values[0]
                PDL         = df_Trendstock.tail(2)['LOW'].values[0]
                VOL_PD      = df_Trendstock.tail(2)['VOLUME'].values[0]
                VOL         = df_Trendstock.tail(1)['VOLUME'].values[0]
                MA50        = df_Trendstock.tail(1)['MA50'].values[0]
                MA20        = df_Trendstock.tail(1)['MA20'].values[0]
                MA9         = df_Trendstock.tail(1)['MA9'].values[0]
                MA5         = df_Trendstock.tail(1)['MA5'].values[0]
                MA3         = df_Trendstock.tail(1)['MA5'].values[0]
                close_    = df_Trendstock.tail(1)['CLOSE'].values[0]
                RSI         = df_Trendstock.tail(1)['RSI'].values[0]
                ## PDClose_ > float(MA50) and PDClose_ < float(MA50)and
                if close_ > float(MA9) and  close_ > float(MA5) and close_ > PDH and VOL > VOL_PD:
                    stocks_to_buy.append(stock)
                elif close_ < float(MA9) and  close_ < float(MA5) and close_ < PDL and VOL > VOL_PD:
                    stocks_to_sell.append(stock)
            except Exception as e:
                print('error ', e)
        print('BUY', stocks_to_buy)
        print('SELL', stocks_to_sell)

    def getUPDOWNTrendingStockBacktest(companies):
        stocks_to_buy=[]
        stocks_to_sell=[]
        win = 0
        lost = 0

        for stock in companies:
            try:
                winstk = 0
                loststk = 0
                df_Trendstock = pd.read_csv('../SYMBOL/STOCK_{}.csv'.format(stock))
                #df_Trendstock = calculate_rsi_df(df_Trendstock,14)

                for i_ma in [50,20,9,5]:
                    df_Trendstock['MA{}'.format(i_ma)] =  df_Trendstock['CLOSE'].rolling(window=i_ma).mean()
                df_Trendstock = df_Trendstock.dropna()
                for i in range(0,  len(df_Trendstock.index)-3):
                    df_ = df_Trendstock.iloc[i:i+3]
                    #df_['TIME'] = ['915','920','925']
                    PDH         = float(df_.iloc[0]['HIGH'])
                    PDL         = float(df_.iloc[0]['LOW'])
                    PDCLOSE     = float(df_.iloc[0]['CLOSE'])
                    MA50PD      = float(df_.iloc[0]['MA50'])
                    MA5PD      = float(df_.iloc[0]['MA5'])
                    MA9PD      = float(df_.iloc[0]['MA9'])
                    MA20PD      = float(df_.iloc[0]['MA20'])
                    VOL_PD      = float(df_.iloc[0]['VOLUME'])
                    VOL         = float(df_.iloc[1]['VOLUME'])
                    MA50        = float(df_.iloc[1]['MA50'])
                    MA5        = float(df_.iloc[1]['MA5'])
                    MA20        = float(df_.iloc[1]['MA20'])
                    MA9         = float(df_.iloc[1]['MA9'])
                    close_      = float(df_.iloc[1]['CLOSE'])
                    low_      = float(df_.iloc[1]['LOW'])
                    high_      = float(df_.iloc[1]['HIGH'])
                    #RSI         = float(df_.iloc[1]['RSI'])
                    open_chk    = float(df_.iloc[2]['OPEN'])
                    close_chk    = float(df_.iloc[2]['CLOSE'])
                    high_chk    = float(df_.iloc[2]['HIGH'])
                    low_chk    = float(df_.iloc[2]['LOW'])
                    ## PDClose_ > float(MA50) and PDClose_ < float(MA50)and
                    if close_ > MA5 and close_ > MA9 and close_ > PDH:
                        if low_chk > low_ and close_chk > close_:
                            winstk +=1
                        elif low_chk < low_:
                            loststk += 1
                        #save_fig(df_,'long_{}_{}.png'.format(stock,df_.iloc[1]['DATE']))

                        #stocks_to_buy.append(stock)
                    elif close_ < MA5 and close_ < MA9 and close_ < PDL:
                        if high_chk < high_ and close_chk > close_:
                            winstk += 1
                        elif high_chk > high_:
                            loststk +=1
                        #save_fig(df_,'short{}_{}.png'.format(stock,df_.iloc[1]['DATE']))
                        #stocks_to_sell.append(stock)
                win = win + winstk
                lost = lost + loststk
                print('{} win:{} lost:{} totalTrades {}'.format(stock, winstk, loststk, int(winstk+ loststk)))
            except Exception as e:
                print('error ', e)
        print('total trades:{}, win:{}, lost:{}'.format(int(win+lost), win, lost))
##        print('BUY', stocks_to_buy)
##        print('SELL', stocks_to_sell)

##--------------------------------------------------------------##
    with open('EQUITY STOCKS.csv') as csvfile:
        readcsv = csv.reader(csvfile, delimiter=',')
        companies = []
        for row in readcsv:
            comp = row[0]
            companies.append(comp)

##    print('GETTING NR STOCKS')
##    print('NR7',get_NR_list(companies,7))
##    print('NR21',get_NR_list(companies,21))
##    get_ob_os_stocks(companies)
##    get_3IIIB_stocks(companies)
##    get_DMA_stocks(companies)
##
##
##    print('')
##    print('')
##    print('Getting Bullish/bearish List for probable next day move.')
##    get_ob_os_stocks(companies, False)
##    get_DMA_stocks(companies, False)

    print('----------------------')
    print('-----------GETTING DAILY UP DOWN TRENDING-----------')
    getUPDOWNTrendingStock(companies)

except Exception as e:
        print(e)


# vim: set et nosi ai ts=2 sts=2 sw=2:
# coding: utf-8
from __future__ import absolute_import, print_function, unicode_literals
import collections
import functools
import operator
import itertools

import six

from .decoration import Decorator

__all__ = ['add_prev_next', 'build_index', 'build_multi_index', 'materialize_slices', 'reverse_slices', 'find_contained_slices', 'convert_slices', 'reverse_pointers']

def _attrsetter(attr):
  if attr is None:
    fn = lambda obj, val: None
  else:
    def fn(obj, val):
      setattr(obj, attr, val)
      return val

  # Set a default value like any other value
  fn.default = fn
  return fn

def _attrappender(attr):
  if attr is None:
    fn = lambda obj, val: None
    fn.default = fn
    return fn

  def fn(obj, val):
    try:
      getattr(obj, attr).append(val)
    except AttributeError:
      setattr(obj, attr, [val])
    return getattr(obj, attr)

  # Do not set a default value, just initialise the list
  def default_fn(obj, val):
    setattr(obj, attr, [])
  fn.default = default_fn
  return fn

def _attrgetter(attr):
  if callable(attr):
    return attr
  return operator.attrgetter(attr)

def _storegetter(store):
  """
  Allows decorator arguments referring to a store of objects either to be a
  named attribute of a document, or a function which returns a list of objects
  given a document.
  """
  if callable(store):
    return store
  else:
    return operator.attrgetter(store)

def _aggregator(mode, attr):
  if mode is None or mode == 'last':
    return _attrsetter(attr)
  elif mode == 'first':
    def fn(obj, val):
      try:
        return getattr(obj, attr)
      except AttributeError:
        pass
      setattr(obj, attr, val)
      return val
    return fn
  elif mode == 'append':
    return _attrappender(attr)
  elif mode == 'min':
    fn = min
    default = float('inf')
  elif mode == 'max':
    fn = max
    default = float('-inf')
  elif mode == 'add':
    fn = operator.add
    default = 0
  elif mode == 'mul':
    fn = operator.mul
    default = 1
  else:
    raise ValueError('Unknown aggregate type {0}'.format(mode))

  def agg(obj, val):
    prev = getattr(obj, attr, default)
    val = fn(prev, val)
    setattr(obj, attr, val)
    return val
  return agg

class add_prev_next(Decorator):
  """
  Adds prev and next pointers or None where N/A. Also may add index.
  """
  def __init__(self, store, prev_attr='prev', next_attr='next', index_attr=None):
    super(add_prev_next, self).__init__(self._build_key(store, prev_attr, next_attr, index_attr))
    self.get_store = _storegetter(store)
    self.set_prev = _attrsetter(prev_attr)
    self.set_next = _attrsetter(next_attr)
    self.set_index = _attrsetter(index_attr)
    self._set_affected_fields((store, prev_attr), (store, next_attr), (store, index_attr))

  def decorate(self, doc):
    prev = None
    for i, item in enumerate(self.get_store(doc)):
      self.set_prev(item, prev)
      self.set_index(item, i)
      if prev is not None:
        self.set_next(prev, item)
      prev = item
    if prev is not None:
      self.set_next(prev, None)

class build_index(Decorator):
  """
  Constructs an index over a selected field (key_attr) from a given object
  store, and stores it on the document at index_attr. By default the index
  maps from a keys to single object. If the extracted key is iterable and
  is not a string or tuple, multiple keys will be mapped for the object.

  Setting by_index=True will map to the object's offset in the given store
  instead of the object itself. The construct and add_entry arguments may define
  an arbitrary indexing structure.
  """
  def __init__(self, store, key_attr, index_attr, construct=dict, add_entry='__setitem__', by_index=False):
    super(build_index, self).__init__(self._build_key(index_attr, key_attr, store, construct, add_entry, by_index))
    self.get_objects = _storegetter(store)
    self.set_index = _attrsetter(index_attr)
    self.get_key = _attrgetter(key_attr)
    self.construct_index = construct
    self.get_add_entry = _attrgetter(add_entry)
    self.by_index = by_index
    self._set_affected_fields(index_attr)

  def decorate(self, doc):
    res = self.construct_index()
    self.set_index(doc, res)
    add_entry = self.get_add_entry(res)

    for i, obj in enumerate(self.get_objects(doc)):
      val = i if self.by_index else obj
      try:
        keys = self.get_key(obj)
      except AttributeError:
        # Is this correct behaviour?
        # Or should users ensure all objects in the store have the appropriate attribute?
        # WARNING: This behaviour may hide true AttributeErrors
        continue
      if isinstance(keys, (six.binary_type, six.text_type, tuple)) or not hasattr(keys, '__iter__'):
        keys = (keys,)
      for key in keys:
        add_entry(key, val)

class build_multi_index(build_index):
  def __init__(self, *args, **kwargs):
    kwargs['construct'] = functools.partial(collections.defaultdict, set)
    kwargs['add_entry'] = lambda index: lambda key, val: index[key].add(val)
    super(build_multi_index, self).__init__(*args, **kwargs)

class materialize_slices(Decorator):
  """
  Decorates entries in the source_store with deref_attr, the list of elements
  in target_store corresponding to the slice_attr value.
  """
  def __init__(self, source_store, target_store, slice_attr, deref_attr):
    super(materialize_slices, self).__init__(self._build_key(source_store, target_store, slice_attr, deref_attr))
    self.get_source_store = _storegetter(source_store)
    self.get_target_store = _storegetter(target_store)
    self.slice_attr = slice_attr
    self.deref_attr = deref_attr
    self._set_affected_fields((source_store, deref_attr))

  def decorate(self, doc):
    store = self.get_target_store(doc)
    for obj in self.get_source_store(doc):
      span = getattr(obj, self.slice_attr)
      if span is not None:
        setattr(obj, self.deref_attr, store[span])

class reverse_slices(Decorator):
  """
  Where objects in source_store point (through slice_attr) to slices over
  objects in target_store, this decorates the target_store objects with any or
  all of: a pointer to a target_store object, its offset within the slice
  range (counting from left and/or right), a tuple of all the above.

  If slices are not mutually exclusive, each attribute will be a list whose
  items correspond to source annotations.

  >>> from schwa import dr
  >>> class Token(dr.Ann):
  ...   norm = dr.Text()
  >>> class NounPhrase(dr.Ann):
  ...   span = dr.Slice(Token)
  ...   def __repr__(self):
  ...     return '<NP {}:{}>'.format(self.span.start, self.span.stop)
  >>> class Doc(dr.Doc):
  ...   tokens = dr.Store(Token)
  ...   nps = dr.Store(NounPhrase)
  ...
  >>> def make_doc():
  ...   doc = Doc()
  ...   for norm in 'The cat sat on the mat in the yard .'.split():
  ...     doc.tokens.create(norm=norm)
  ...   for start, stop in [(0, 2), (4, 9), (7, 9)]:
  ...     doc.nps.create(span=slice(start, stop))
  ...   return doc
  >>>
  >>> # With mark_outside=False, the attribute is set iff covered by a NounPhrase:
  >>> doc = make_doc()
  >>> reverse_slices('nps', 'tokens', 'span', 'np', mark_outside=False)(doc)
  >>> [hasattr(tok, 'np') for tok in doc.tokens]
  [True, True, False, False, True, True, True, True, True, False]
  >>> # some_token.np points to the covering NounPhrase
  >>> doc.tokens[0].norm, doc.tokens[0].np
  ('The', <NP 0:2>)
  >>> # with mutex=True, only one covering slice is recorded
  >>> doc.tokens[8].norm, doc.tokens[8].np
  ('yard', <NP 7:9>)
  >>>
  >>> # With mark_outside=True, the attribute is None unless covered by a NounPhrase:
  >>> doc = make_doc()
  >>> reverse_slices('nps', 'tokens', 'span', 'np', mark_outside=True)(doc)
  >>> [tok.np is not None for tok in doc.tokens]
  [True, True, False, False, True, True, True, True, True, False]
  >>>
  >>> # With mutex=False, a list of covering NPs is stored, and mark_outside stores an empty list
  >>> doc = make_doc()
  >>> reverse_slices('nps', 'tokens', 'span', 'nps', mutex=False, mark_outside=True)(doc)
  >>> doc.tokens[0].nps
  [<NP 0:2>]
  >>> doc.tokens[2].nps
  []
  >>> doc.tokens[8].nps
  [<NP 4:9>, <NP 7:9>]
  >>>
  >>> # With offset_attr or roffset_attr the position of the covered token is stored
  >>> # This allows us to know if the token is at the start or end of a phrase, for instance
  >>> doc = make_doc()
  >>> reverse_slices('nps', 'tokens', 'span', 'np',
  ...                offset_attr='np_offset', roffset_attr='np_roffset',
  ...                all_attr='np_data', mutex=False, mark_outside=True)(doc)
  >>> [tok.np_offset for tok in doc.tokens]
  [[0], [1], [], [], [0], [1], [2], [3, 0], [4, 1], []]
  >>> [tok.np_roffset for tok in doc.tokens]
  [[1], [0], [], [], [4], [3], [2], [1, 1], [0, 0], []]
  >>> # The all_attr stores a tuple of pointer and offset information
  >>> doc.tokens[8].np_data
  [(<NP 4:9>, 4, 0), (<NP 7:9>, 1, 0)]
  """

  def __init__(self, source_store, target_store, slice_attr, pointer_attr=None, offset_attr=None, roffset_attr=None, all_attr=None, mutex=True, mark_outside=False):
    super(reverse_slices, self).__init__(self._build_key(source_store, target_store, slice_attr, pointer_attr, offset_attr, roffset_attr, all_attr, mutex, mark_outside))
    self.get_source_store = _storegetter(source_store)
    self.get_target_store = _storegetter(target_store)
    self.slice_attr = slice_attr
    if mutex:
      setter = _attrsetter
    else:
      setter = _attrappender
    self.set_pointer = setter(pointer_attr)
    self.set_offset = setter(offset_attr)
    self.set_roffset = setter(roffset_attr)
    self.set_all = setter(all_attr)
    self.mark_outside = mark_outside
    self._set_affected_fields((target_store, pointer_attr), (target_store, offset_attr), (target_store, roffset_attr), (target_store, all_attr))

  def decorate(self, doc):
    target_items = self.get_target_store(doc)

    if self.mark_outside:
      for target in target_items:
        self.set_pointer.default(target, None)
        self.set_offset.default(target, None)
        self.set_roffset.default(target, None)
        self.set_all.default(target, (None, None, None))

    for source in self.get_source_store(doc):
      span = getattr(source, self.slice_attr)
      if span is None:
        continue
      n = span.stop - span.start
      for i, target in enumerate(target_items[span]):
        self.set_pointer(target, source)
        self.set_offset(target, i)
        roffset = n - i - 1
        self.set_roffset(target, roffset)
        self.set_all(target, (source, i, roffset))

class find_contained_slices(Decorator):
  """
  Adds collection_attr to each containing_store object O, being a list of the
  objects in contained_store with a contained_slice that is a sub-span of O's
  containing_slice value.

  If contained_store (or contained_slice) is None, the value is copied from
  containing_store (containing_slice). In this case, each O will not be
  included in its contained objects collection.
  """
  def __init__(self, containing_store, containing_slice, contained_store=None, contained_slice=None, collection_attr=None):
    if not collection_attr:
      raise ValueError('collection_attr must be a non-empty string')
    super(find_contained_slices, self).__init__(self._build_key(containing_store, containing_slice, contained_store, contained_slice, collection_attr))
    self.get_containing_store = _storegetter(containing_store)
    self.get_containing_slice = operator.attrgetter(containing_slice)
    self.get_contained_store = _storegetter(contained_store or containing_store)
    self.get_contained_slice = operator.attrgetter(contained_slice or containing_slice)
    self.set_collection = _attrsetter(collection_attr)
    self.get_collection = operator.attrgetter(collection_attr)
    self._set_affected_fields((containing_store, collection_attr))

  def _gen_tuples(self, store, get_slice, group):
    for obj in store:
      sl = get_slice(obj)
      if not sl:
        continue
      yield sl.start, -sl.stop, group, obj

  def decorate(self, doc, CONTAINING_GROUP=0, CONTAINED_GROUP=1):
    containing_tuples = self._gen_tuples(self.get_containing_store(doc), self.get_containing_slice, CONTAINING_GROUP)
    contained_tuples = self._gen_tuples(self.get_contained_store(doc), self.get_contained_slice, CONTAINED_GROUP)
    open_containers = []
    for tup in sorted(itertools.chain(containing_tuples, contained_tuples)):
      start, stop, group, obj = tup
      while open_containers and start >= open_containers[-1][1]:
        open_containers.pop()

      if group == CONTAINING_GROUP:
        collection = []
        self.set_collection(obj, collection)
        open_containers.append((start, -stop, obj, collection))
      else:
        for cstart, cstop, container, collection in open_containers:
          if container != obj:
            collection.append(obj)

class convert_slices(Decorator):
  """
  Augments source_store objects with new_slice_attr, whose value corresponds to
  source_slice_attr over the finer granularity indicated by target_slice_attr.

  For example, if Sentence.span is a Slice over Token objects, and Token.span
  indicates a Slice over raw text, this decorator could be used to augment
  Sentence objects with Slice attributes over raw text.
  """
  def __init__(self, source_store, target_store, source_slice_attr, target_slice_attr, new_slice_attr):
    super(convert_slices, self).__init__(self._build_key(source_store, target_store, source_slice_attr, target_slice_attr, new_slice_attr))
    self.get_source_store = _storegetter(source_store)
    self.get_target_store = _storegetter(target_store)
    self.get_source_slice = operator.attrgetter(source_slice_attr)
    self.get_target_slice = operator.attrgetter(target_slice_attr)
    self.set_new_slice = _attrsetter(new_slice_attr)
    self._set_affected_fields((source_store, new_slice_attr))

  def decorate(self, doc):
    targets = self.get_target_store(doc)
    for source in self.get_source_store(doc):
      source_slice = self.get_source_slice(source)
      try:
        start = source_slice.start
        stop = source_slice.stop
      except AttributeError:
        self.set_new_slice(source, None)
        continue

      try:
        start = self.get_target_slice(targets[start])
      except IndexError:
        # Have seen case where start == stop == len(targets)
        self.set_new_slice(source, None)
      stop = self.get_target_slice(targets[stop - 1])
      try:
        self.set_new_slice(source, slice(start.start, stop.stop))
      except AttributeError:
        self.set_new_slice(source, None)

class reverse_pointers(Decorator):
  """
  Where objects in source_store point (through pointer_attr) to objects in
  target_store, this decorates the target objects with a rev_attr attribute
  pointing back to the source object.
  """

  def __init__(self, source_store, target_store, pointer_attr, rev_attr, mutex=True, mark_outside=False):
    super(reverse_pointers, self).__init__(self._build_key(source_store, target_store, pointer_attr, rev_attr, mutex, mark_outside))
    self.get_source_store = _storegetter(source_store)
    self.get_target_store = _storegetter(target_store)
    self.pointer_attr = pointer_attr
    if mutex:
      setter = _attrsetter
    else:
      setter = _attrappender
    self.set_rev = setter(rev_attr)
    self.mark_outside = mark_outside
    self._set_affected_fields((target_store, rev_attr))

  def decorate(self, doc):
    if self.mark_outside:
      for target in self.get_target_store(doc):
        self.set_rev.default(target, None)

    for source in self.get_source_store(doc):
      target = getattr(source, self.pointer_attr)
      if target is None:
        continue
      if isinstance(target, list):
        for target_item in target:
          self.set_rev(target_item, source)
      else:
        self.set_rev(target, source)

class mark_depth(Decorator):
  """
  In a graph structure, mark the depth and height of each node, counted from root and leaf, respectively.

  Arguments:
  - store: the store of nodes on which depth and height attrs are set (required for undo)
  - get_starts: a function or attribute on the doc that returns the starting nodes for a depth-first search
  - child_attr: a function or attribute for traversing from one node to its immediate descendants
  - depth_attr: the attribute to set on each node indicating its distance from a start node
  - height_attr: the attribute to set on each node indicating its distance from an end node (one without children)
  - depth_aggregate: one of ('last', 'min', 'max'). 'last' adopts the last value in right traversal.

  """
  def __init__(self, store, get_starts, child_attr, depth_attr=None, height_attr=None, depth_aggregate=None, height_aggregate='max', max_depth=None):
    super(mark_depth, self).__init__(self._build_key(get_starts, child_attr, depth_attr, height_attr, depth_aggregate, height_aggregate, max_depth))
    self.get_starts = _storegetter(get_starts)
    self.get_children = _attrgetter(child_attr)
    self.set_depth = _aggregator(depth_aggregate, depth_attr)
    self.set_height = _aggregator(height_aggregate, height_attr)
    self.max_depth = max_depth
    self._set_affected_fields((store, depth_attr), (store, height_attr))

  def decorate(self, doc):
    for obj in self.get_starts(doc):
      self._decorate(obj, 0)

  def _decorate(self, obj, depth):
    self.set_depth(obj, depth)
    children = self.get_children(obj)
    if not children or depth == self.max_depth:
      return self.set_height(obj, 0)
    if not isinstance(children, (list, tuple)):
      children = [children]
    for child in children:
      ch_height = self._decorate(child, depth + 1)
      height = self.set_height(obj, ch_height + 1)
    return height

# Get min-height can be done more efficiently with breadth-first search


#coding:utf-8
#
# Project IA
# Group: Andreza Queiroz,
#	 Carlos Interaminense,
#	 Maria Daniela
#
#Author: Carlos Interaminense

from __future__ import print_function, division
import nltk
import os
import random
from collections import Counter
from nltk import word_tokenize, WordNetLemmatizer
from nltk.corpus import stopwords
from nltk import NaiveBayesClassifier, classify
import sys
import csv

stoplist = stopwords.words('english')

# Função para realizar um preprocessamento dos emails, retirando o plural das palavras por exemplo.
# Com isso, "examples" e "example" são consideradas as mesmas palavras.
def preprocess(sentence):
    lemmatizer = WordNetLemmatizer()
    return [lemmatizer.lemmatize(word.lower()) for word in word_tokenize(unicode(sentence, errors='ignore'))]

# Computa a frequencia das palavras dos emails.
def get_features(text, setting):
    if setting=='bow':
        return {word: count for word, count in Counter(preprocess(text)).items() if not word in stoplist}
    else:
        return {word: True for word in preprocess(text) if not word in stoplist}

def zeros_list(n):
	l = [0 for i in range(n)]

	return l

def read_file_csv(file_name_csv):
	l = []
	with open(file_name_csv, 'rb') as f:
	    reader = csv.reader(f)
	    l = list(reader)
	return l

args = sys.argv

path_email = args[1]

f = open(path_email, 'r')

email = f.read()

f.close()

file_name_csv = args[2]
l_aux = read_file_csv(file_name_csv)
list_words = l_aux[0]
logs = l_aux[1]

n_words = int(args[3])

hist = zeros_list(n_words)

email = preprocess(email)

for i in range(n_words):
	hist[i] = (email.count(list_words[i]) ) * float(logs[i])

hist.append("email")

#gambiarra...
hists = [hist, hist]

with open('hist_email_demo.csv', "wb") as f:
	    writer = csv.writer(f)
	    wr = csv.writer(f, quoting=csv.QUOTE_ALL)
	    writer.writerows(hists)

c = 'python csv2arff.py hist_email_demo.csv'
os.system(c)


import base64
from email.mime.text import MIMEText

from googleapiclient import errors

from apiauth.authenticate import Authenticator

class ComposeUtils:

    def __init__(self, access_token, user_agent):
        authenticator = Authenticator()
        self.__service = authenticator.get_service(access_token, user_agent)

    def create_message(self, sender, to, subject, message_text):
        message = MIMEText(message_text)
        message['to'] = to
        message['from'] = sender
        message['subject'] = subject
        return {'raw': base64.urlsafe_b64encode(message.as_bytes()).decode('utf-8')}

    def send_message(self, user_id, message):
        try:
            message = (self.__service.users().messages().send(userId=user_id, body=message)
                       .execute())
            print('Message Id: %s' % message['id'])
            return message
        except errors.HttpError as error:
            print('An error occurred: %s' % error)


import unittest
from common import HTMLTestRunner_cn
#用例路径
casepath=("E:\\web_auto\\test_case")
rule=("test*.py")
discover=unittest.defaultTestLoader.discover(start_dir=casepath,pattern=rule)
reportpath=("E:\\web_auto\\report\\"+"report.html")
fp=open(reportpath,"wb")
runner=HTMLTestRunner_cn.HTMLTestRunner(stream=fp,title="超级完美无敌美丽的测试报告",description="主要是用来测"
                                                                               "试百度搜索和上海屹通的登录界面")
runner.run(discover)

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


n, l, r = map(int, input().split())
import copy
country = [list(map(int, input().split())) for _ in range(n)]
visit = [[0] * n for _ in range(n)]
from collections import deque

direction = [(1, 0), (-1, 0), (0, 1), (0, -1)]

def search():
    start_queue = deque()
    for y in range(n):
        for x in range(n):
            for dy, dx in [(1, 0), (0, 1)]:
                ny, nx = y + dy, x + dx
                if 0<=ny<n and 0<=nx<n:
                    diff = abs(country[y][x] - country[ny][nx])
                    if l <= diff <= r:
                        start_queue.append((ny, nx))
    return start_queue

def bfs(start):
    global country
    visit = [[0] * n for _ in range(n)]
    queue = deque([start])
    xy = deque([start])
    val = deque([country[start[0]][start[1]]])
    visit[start[0]][start[1]] = 1
    while queue:
        y, x = queue.popleft()

        for dy, dx in direction:
            ny, nx = y + dy, x + dx

            if 0 <= ny < n and 0 <= nx < n and visit[ny][nx] == 0 and l <= abs(country[y][x] - country[ny][nx]) <= r:
                visit[ny][nx] = 1
                xy.append((ny, nx))
                queue.append((ny, nx))
                val.append(country[ny][nx])

    avg = int(sum(val) / len(val))

    return xy,avg

start_queue = search()
days = 0
while start_queue:
    queue = start_queue
    xy = []
    avgs = []
    while queue:
        y, x = queue.popleft()
        used_queue, avg = bfs((y, x))
        xy.append(used_queue)
        avgs.append(avg)

        queue = deque(set(queue) - set(used_queue))

    for _, avg in zip(xy, avgs):
        for y, x in _:
            country[y][x] = avg
    days += 1
    start_queue = search()

print(days)


import numpy as np
import pandas as pd
from sklearn.compose import ColumnTransformer
from sklearn.impute import SimpleImputer
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import OneHotEncoder
from data.column_mappings import column_mappings
from data.value_mappings import mappings

class DataProcessor:
    """
    Class for processing data for machine learning.

    :param data_path: path to the data file.
    :param data: the data loaded from the file.
    :param X_train_preprocessed: preprocessed training data.
    :param X_val_preprocessed: preprocessed validation data.
    :param y_train: target values for the training data.
    :param y_val: target values for the validation data.
    """

    def __init__(self, data_path='data/train.csv'):
        """
        Initialize DataProcessor with path to the data file.

        :param data_path: path to the data file.
        """
        self.data_path = data_path
        self.data = self.load_data()
        self.X_train_preprocessed = None
        self.X_val_preprocessed = None
        self.y_train = None
        self.y_val = None

    def load_data(self):
        """
        Load and preprocess the data from the file.

        :return: The loaded and preprocessed data.
        """
        data = pd.read_csv(self.data_path)
        data.rename(columns=column_mappings, inplace=True)
        data.replace(mappings, inplace=True)
        return data

    def preprocess_data(self):
        """
        Preprocess the data, creating preprocessed training and validation data and targets.
        """
        preprocessor = self.create_preprocessor()
        self.split_data(preprocessor)

    def create_preprocessor(self):
        """
        Create a preprocessor for the data.

        :return: preprocessor for the data.
        """
        num_features = self.data.select_dtypes(include=[np.number]).drop('SalePrice', axis=1).columns
        cat_features = self.data.select_dtypes(include=[object]).columns

        num_pipeline = Pipeline(steps=[
            ('num_imputer', SimpleImputer(strategy='mean'))
        ])

        cat_pipeline = Pipeline(steps=[
            ('cat_imputer', SimpleImputer(strategy='most_frequent')),
            ('onehot', OneHotEncoder(handle_unknown='ignore'))
        ])

        transformers = [
            ('num', num_pipeline, num_features),
            ('cat', cat_pipeline, cat_features)
        ]

        return ColumnTransformer(transformers=transformers)

    def split_data(self, preprocessor):
        """
        Split the data into training and validation sets, and preprocess them.

        :param preprocessor: preprocessor for the data.
        """
        X = self.data.drop('SalePrice', axis=1)
        y = self.data['SalePrice']

        X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)

        self.X_train_preprocessed = preprocessor.fit_transform(X_train)
        self.X_val_preprocessed = preprocessor.transform(X_val)
        self.y_train = y_train
        self.y_val = y_val


#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time    : 2018/12/5 15:49
# @Author  : hedengfeng
# @Site    :
# @File    : aei_time_test.py
# @Software: EAI_test
# @description:
import time
import serial

ser = serial.Serial(
    port='/dev/ttyUSB0',
    baudrate=115200,
    parity=serial.PARITY_NONE,
    stopbits=serial.STOPBITS_ONE,
    bytesize=serial.EIGHTBITS,
    timeout=1
)

time.sleep(1)

ser.write('e\r'.encode())
e_return = ser.readline().decode()
print('begin:',  e_return, time.asctime(), time.time())

ser.write('z 30 -30;\r'.encode())
z_return = ser.readline().decode()
print('z_return:', z_return, time.asctime(), time.time())

while True:
    ser.write('e\r'.encode())
    e_return = ser.readline().decode()
    print('e_return:', e_return, time.asctime(), time.time())