# This script detects for the presence of either a BME680 sensor on the I2C bus or a Sense HAT
# The BME680 includes sensors for temperature, humidity, pressure and gas content
# The Sense HAT does not have a gas sensor, and so air quality is approximated using temperature and humidity only.

import sys
import time
import smbus
import os
import json

from bme680 import BME680
from bme280 import BME280       # import the class object for our BME280 sensor
from w1therm import W1THERM
from enviroplushat import ENVIROPLUS
from http.server import HTTPServer, BaseHTTPRequestHandler

class balenaSense():
    readfrom = 'unset'
    bus = smbus.SMBus(1)

    def __init__(self):
        # First, check for enviro plus hat (since it also has BME on 0x76)
        # try:
        #     self.bus.write_byte(0x23, 0) # test if we can connect to ADS1015
        # except IOError:
        #     print('Enviro Plus hat not found')
        # else:
        #     self.readfrom = 'enviroplus'
        #     self.sensor = ENVIROPLUS()
        #     print('Found Enviro+ Hat')

        # Next, check to see if there is a BME680 on the I2C bus
        if self.readfrom == 'unset':
            try:
                self.bus.write_byte(0x76, 0)
            except IOError:
                print('BME280 not found on 0x76, trying 0x77')
            else:
                print('BME280 found on 0x76')
                self.readfrom = 'bme680secondary'
                # self.sensor = BME680(self.readfrom)
                self.sensor = BME280(self.readfrom)  # Make the class' sensor BME280

        # If we didn't find it on 0x76, look on 0x77
        if self.readfrom == 'unset':
            try:
                self.bus.write_byte(0x77, 0)
            except IOError:
                print('BME280 not found on 0x77')
            else:
                print('BME280 found on 0x77')
                self.readfrom = 'bme680primary'
                # self.sensor = BME680(self.readfrom)   <---- OLD; working
                self.sensor = BME280(self.readfrom)     # Make the class' sensor BME280

        # If no BME680, is there a Sense HAT?
        # if self.readfrom == 'unset':
        #     try:
        #         self.bus.write_byte(0x5F, 0)
        #     except:
        #         print('Sense HAT not found')
        #     else:
        #         self.readfrom = 'sense-hat'
        #         print('Using Sense HAT for readings (no gas measurements)')
        #
        #         # Import the sense hat methods
        #         import sense_hat_air_quality
        #         from hts221 import HTS221
        #         self.sense_hat_reading = lambda: sense_hat_air_quality.get_readings(HTS221())

        # Next, check if there is a 1-wire temperature sensor (e.g. DS18B20)
        # if self.readfrom == 'unset':
        #     if os.environ.get('BALENASENSE_1WIRE_SENSOR_ID') != None:
        #         sensor_id = os.environ['BALENASENSE_1WIRE_SENSOR_ID']
        #     else:
        #         sensor_id = None
        #
        #     try:
        #         self.sensor = W1THERM(sensor_id)
        #     except:
        #         print('1-wire sensor not found')
        #     else:
        #         self.readfrom = '1-wire'
        #         print('Using 1-wire for readings (temperature only)')

        # If this is still unset, no sensors were found; quit!
        if self.readfrom == 'unset':
            print('No suitable sensors found! Exiting.')
            sys.exit()

    def sample(self):
        if self.readfrom == 'sense-hat':
            return self.apply_offsets(self.sense_hat_reading())
        else:
            return self.apply_offsets(self.sensor.get_readings(self.sensor))

    def apply_offsets(self, measurements):
        # Apply any offsets to the measurements before storing them in the database
        # if os.environ.get('BALENASENSE_TEMP_OFFSET') != None:
        #     measurements[0]['fields']['temperature'] = measurements[0]['fields']['temperature'] + float(os.environ['BALENASENSE_TEMP_OFFSET'])
        #
        # if os.environ.get('BALENASENSE_HUM_OFFSET') != None:
        #     measurements[0]['fields']['humidity'] = measurements[0]['fields']['humidity'] + float(os.environ['BALENASENSE_HUM_OFFSET'])
        #
        # if os.environ.get('BALENASENSE_ALTITUDE') != None:
        #     # if there's an altitude set (in meters), then apply a barometric pressure offset
        #     altitude = float(os.environ['BALENASENSE_ALTITUDE'])
        #     measurements[0]['fields']['pressure'] = measurements[0]['fields']['pressure'] * (1-((0.0065 * altitude) / (measurements[0]['fields']['temperature'] + (0.0065 * altitude) + 273.15))) ** -5.257

        return measurements

class balenaSenseHTTP(BaseHTTPRequestHandler):
    def _set_headers(self):
        self.send_response(200)
        self.send_header('Content-type', 'text/html')
        self.end_headers()

    def do_GET(self):
        self._set_headers()
        measurements = balenasense.sample()
        # print("##############\tHERE ARE THE MEASUREMENTS MAN\t##############")
        # print(measurements)
        # print("##############\t############################\t##############")
        print("Sending measurements in JSON")
        self.wfile.write(json.dumps(measurements[0]['fields']).encode('UTF-8'))

    def do_HEAD(self):
        self._set_headers()

# Start the server to answer requests for readings
balenasense = balenaSense()

while True:
    server_address = ('', 80)
    httpd = HTTPServer(server_address, balenaSenseHTTP)
    print('Sensor HTTP server running')
    httpd.serve_forever()


from tkinter import *
import tkinter.messagebox as messagebox

class Cal:
    # 获取表达式的优先级
    def getPriority(self, s):
        if s == "(":
            return 0
        elif s == "+" or s == "-":
            return 1
        elif s == "x" or s == "/":
            return 2
        else:
            return 3

    # 将输入的字符串转换为表达式
    def getExpression(self, input):
        expression = []
        str = ""
        for c in input:
            if c in "+-x/()":
                if str:
                    expression.append(str)
                    str = ""
                expression.append(c)
            else:
                str += c
        if str:
            expression.append(str)
        return expression

    # 得到逆波兰表达式
    def postExpression(self, expression):
        post = []
        stack = []
        for c in expression:
            if c == "(":
                stack.append(c)
            elif c == ")":
                while stack[-1] != "(":
                    post.append(stack.pop())
                stack.pop()
            else:
                if c not in "+-x/":
                    stack.append(c)
                else:
                    while stack and self.getPriority(stack[-1]) >= self.getPriority(c):
                        post.append(stack.pop())
                    stack.append(c)
        while stack:
            post.append(stack.pop())
        return post

    # 逆波兰表达式求值
    def evaluateExpression(self, input):
        expression = self.getExpression(input)
        post = self.postExpression(expression)
        stack = []
        for c in post:
            if c in "+-x/":
                num = 0
                x = int(stack.pop())
                y = int(stack.pop())
                if c == "+":
                    num = y + x
                elif c == "-":
                    num = y - x
                elif c == "x":
                    num = y*x
                else:
                    if x == 0:
                        return 0
                    else:
                        num = y//x
                stack.append(str(num))
            else:
                stack.append(c)
        if stack:
            return stack.pop()
        else:
            return 0

'''
class Buffer(object):
    def __init__(self, data):
        self.data = data
        self.offset = 0

    def peek(self):
        if self.offset >= len(self.data):
            return None
        return self.data[self.offset]

    def advance(self):
        self.offset += 1

class Token(object):
    def consume(self, buffer):
        pass

class TokenInt(Token):
    def consume(self, buffer):
        accum = ""
        while True:
            ch = buffer.peek()
            if ch is None or ch not in "0123456789":
                break
            else:
                accum += ch
                buffer.advance()

        if accum != "":
            return ("int", int(accum))
        else:
            return None

class TokenOperator(Token):
    def consume(self, buffer):
        ch = buffer.peek()
        if ch is not None and ch in "+-":
            buffer.advance()
            return ("ope", ch)
        return None

class Node(object):
    pass

class NodeInt(Node):
    def __init__(self, value):
        self.value = value

class NodeBinaryOp(Node):
    def __init__(self, kind):
        self.kind = kind
        self.left = None
        self.right = None

def operator(string):
    tokens = tokenize(string)
    node = parse(tokens)
    return evaluate(node)

def evaluate(node):
    if isinstance(node, NodeInt):
        return node.value
    else:
        return calculate(node)

def tokenize(string):
    buffer = Buffer(string)
    tk_int = TokenInt()
    tk_op = TokenOperator()
    tokens = []
    while buffer.peek():
        token = None
        for tk in (tk_int, tk_op):
            token = tk.consume(buffer)
            if token:
                print(token)
                tokens.append(token)
                break

        if not token:
            raise ValueError("Error in syntax")
    return tokens

def parse(tokens):
    if tokens[0][0] != "int":
        raise ValueError("Must start with an int")

    node = NodeInt(tokens[0][1])
    nbo = None
    last = tokens[0][0]

    for token in tokens[1:]:
        if token[0] == last:
            raise ValueError("Error in syntax")
        last = token[0]
        if token[0] == 'ope':
            nbo = NodeBinaryOp(token[1])
            nbo.left = node

        if token[0] == 'int':
            nbo.right = NodeInt(token[1])
            node = nbo
    return node

def calculate(nbo):
    if isinstance(nbo.left, NodeBinaryOp):
        leftval = calculate(nbo.left)
    else:
        leftval = nbo.left.value

    if nbo.kind == '-':
        return leftval - nbo.right.value
    elif nbo.kind == '+':
        return leftval + nbo.right.value
    else:
        raise ValueError("Wrong operator")

def operate(string):
    tokens = tokenize(string)
    node = parse(tokens)
    return evaluate(node)

if __name__ == '__main__':
    input = input('Input:')
    tokens = tokenize(input)
    node = parse(tokens)
    print(evaluate(node))
'''

class Application(Frame, Cal):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.pack()
        self.createWidgets()

    # 页面布局
    def createWidgets(self):
        # 文本框
        self.stringInput = Entry(self, width=25)
        self.stringInput.grid(row=0, column=0, columnspan=4)
        # Button
        self.alertButton = Button(self, text='CE', width=5, height=2, command=self.clearInput)
        self.alertButton.grid(row=1, column=0)

        self.alertButton = Button(self, text='C', width=5, height=2, command=self.clearInput)
        self.alertButton.grid(row=1, column=1)

        self.alertButton = Button(self, text='<-', width=5, height=2, command=self.backupInput)
        self.alertButton.grid(row=1, column=2)

        self.alertButton = Button(self, text='/', width=5, height=2, command=lambda: self.handlerOpe(str='/'))
        self.alertButton.grid(row=1, column=3)

        self.alertButton = Button(self, text='7', width=5, height=2, command=lambda: self.handlerNum(str='7'))
        self.alertButton.grid(row=2, column=0)

        self.alertButton1 = Button(self, text='8', width=5, height=2, command=lambda: self.handlerNum(str='8'))
        self.alertButton1.grid(row=2, column=1)

        self.alertButton2 = Button(self, text='9', width=5, height=2, command=lambda: self.handlerNum(str='9'))
        self.alertButton2.grid(row=2, column=2)

        self.alertButton3 = Button(self, text='x', width=5, height=2,  command=lambda: self.handlerOpe(str='x'))
        self.alertButton3.grid(row=2, column=3)

        self.alertButton4 = Button(self, text='4', width=5, height=2, command=lambda: self.handlerNum(str='4'))
        self.alertButton4.grid(row=3, column=0)

        self.alertButton5 = Button(self, text='5', width=5, height=2, command=lambda: self.handlerNum(str='5'))
        self.alertButton5.grid(row=3, column=1)

        self.alertButton6 = Button(self, text='6', width=5, height=2, command=lambda: self.handlerNum(str='6'))
        self.alertButton6.grid(row=3, column=2)

        self.alertButton7 = Button(self, text='-', width=5, height=2, command=lambda: self.handlerOpe(str='-'))
        self.alertButton7.grid(row=3, column=3)

        self.alertButton8 = Button(self, text='1', width=5, height=2, command=lambda: self.handlerNum(str='1'))
        self.alertButton8.grid(row=4, column=0)

        self.alertButton9 = Button(self, text='2', width=5, height=2, command=lambda: self.handlerNum(str='2'))
        self.alertButton9.grid(row=4, column=1)

        self.alertButton10 = Button(self, text='3', width=5, height=2, command=lambda: self.handlerNum(str='3'))
        self.alertButton10.grid(row=4, column=2)

        self.alertButton11 = Button(self, text='+', width=5, height=2, command=lambda: self.handlerOpe(str='+'))
        self.alertButton11.grid(row=4, column=3)

        self.alertButton12 = Button(self, text='0', width=5, height=2, command=lambda: self.handlerNum(str='0'))
        self.alertButton12.grid(row=5, column=0)

        self.alertButton13 = Button(self, text='(', width=5, height=2, command=lambda: self.handler(str='('))
        self.alertButton13.grid(row=5, column=1)

        self.alertButton14 = Button(self, text=')', width=5, height=2, command=lambda: self.handler(str=')'))
        self.alertButton14.grid(row=5, column=2)

        self.alertButton15 = Button(self, text='=', width=5, height=2, command=self.getResult)
        self.alertButton15.grid(row=5, column=3)
    # 相应函数

    # 获取文本框的字符串,计算表达式的值，清空文本框，显示结果，并修改flag的值
    def getResult(self):
        x = Cal()
        if self.flag == 0:
            result = self.stringInput.get()
            self.stringInput.delete(0, END)
            message = x.evaluateExpression(result)
            self.flag = 1
            self.stringInput.insert('insert', message)

    # EC和C键清空文本框
    def clearInput(self):
        self.stringInput.delete(0, END)

    # 回退一位
    def backupInput(self):
        if self.stringInput.get():
            size = len(self.stringInput.get())
            self.stringInput.delete(size-1, END)

    # 不同数字和括号按键在文本框输出
    def handlerNum(self, str):
        if self.flag == 1:
            self.stringInput.delete(0, END)
            self.flag = 0
        self.stringInput.insert('insert', str)

    # 不同的运算符按键在文本框输出
    def handlerOpe(self, str):
        self.flag = 0
        self.stringInput.insert('insert', str)

    # 是否已经显示出结果
    flag = 0

app = Application()
# 设置窗口标题:
app.master.title('Calculator')
app.master.geometry('400x300')
# 主消息循环:
app.mainloop()


from django.shortcuts import render
from django.http import HttpResponse
from django.http import JsonResponse
import blog.models
import json

from django.views.decorators.gzip import gzip_page

# Create your views here.

def index(request):
    articles=blog.models.Article.objects.all()
    return render(request,'blog/index.html',{'articles':articles})

def article_page(request,article_id):
    article = blog.models.Article.objects.get(pk=article_id)
    return render(request,'blog/article_page.html',{'article':article})

def edit_page(request,article_id):
    if str(article_id)=='0':
        return render(request,'blog/edit_page.html')
    article = blog.models.Article.objects.get(pk=article_id)
    return render(request,'blog/edit_page.html',{'article':article})

def edit_action(request,article_id,title,content):
    #title = request.POST.get('title','TITLE')
    #content = request.POST.get('content','CONTENT')
    #article_id=request.POST.get('article_id',0)
    if article_id=='0':
        blog.models.Article.objects.create(title=title,content=content)
        articles = blog.models.Article.objects.all()
        #return render(request,'blog/index.html',{'articles':articles})
        return HttpResponse(json.dumps({"status":0,"msg":"success"}))
    article=blog.models.Article.objects.get(pk=article_id)
    article.title=title
    article.content=content
    article.save()
    #return render(request,'blog/article_page.html',{'article':article})
    return HttpResponse(json.dumps({"status":0,"msg":"success"}))

def get_article(request,article_id):
    if article_id=='-1':
        articles=blog.models.Article.objects.all()
        data=[]
        for a in articles:
            article={"id":a.id,"title":a.title,"content":a.content}
            data.append(article)
        return HttpResponse(json.dumps({"code":0,"data":data}))
    article=blog.models.Article.objects.get(pk=article_id)
    data = {"code":0,"data":[{'id': article.id, 'title': article.title,'content':article.content},{'id': article.id, 'title': article.title,'content':article.content}]}
    return HttpResponse(json.dumps(data))
    #return JsonResponse({'id': article.id, 'title': article.title,'content':article.content})

def formattingData(code,data):
    return json.dumps(
        {
            "status": code,
            "content": data
        }
    )


if __name__ == '__main__' and  __package__ is None:
    import sys
    from os import path
    sys.path.append( path.dirname( path.dirname( path.abspath(__file__) ) ) )

from sklearn.model_selection import train_test_split
from models import ImageModel
from dataset_generators import ImageDataset
import utils as ut
import os
import numpy as np
from torch.utils.data import Subset, DataLoader
from torchviz import make_dot
import torch
import torch.nn as nn

MODEL_NAME = "IMAGES_MODEL"
IMAGE_SHAPE = 224
IMAGE_COL = "local_path"
LABEL_COL = "label"
LR = 0.001
DEVICE = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
BATCH_SIZE = 228
EPOCHS = 20
NUM_CLASSES = 3

data = ImageDataset(IMAGE_SHAPE, IMAGE_COL, LABEL_COL)
indices = np.arange(len(data))

train_indices, test_indices = train_test_split(indices, test_size=0.2, random_state=42)

train_data = Subset(data, train_indices)
test_data = Subset(data, test_indices)

train_loader = DataLoader(train_data, batch_size=BATCH_SIZE, shuffle=True, pin_memory=True)
test_loader = DataLoader(test_data, batch_size=BATCH_SIZE, shuffle=False, pin_memory=True)

model = ImageModel(NUM_CLASSES, IMAGE_SHAPE).to(DEVICE)

criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=LR)

print()
print(" ############# MODEL SUMMARY ############# ")
print()
print(model)
print()
print(" ############ ############# ############ ")
print()

ut.train(model, train_loader, test_loader, EPOCHS, MODEL_NAME, optimizer, criterion, DEVICE, False)

# text, _ = next(iter(train_loader))
# output = model(text.to(DEVICE))

# archi = make_dot(output, params=dict(model.named_parameters()))

# archi.save("images_model_archi", "C:/Users/MourtadaHouari/Desktop/sentiment-analysis/src/images")
# archi.render('C:/Users/MourtadaHouari/Desktop/sentiment-analysis/src/images/images_model_archi', format='png')


#! /usr/bin/env python
import csv
import itertools
from collections import defaultdict

with open("airbnb.csv", newline="") as fid:
    reader = csv.DictReader(
        itertools.islice(fid, 1, None),
        delimiter=";",
        fieldnames=(
            "ID",
            "Listing Url",
            "Scrape ID",
            "Last Scraped",
            "Name",
            "Summary",
            "Space",
            "Description",
            "Experiences Offered",
            "Neighborhood Overview",
            "Notes",
            "Transit",
            "Access",
            "Interaction",
            "House Rules",
            "Thumbnail Url",
            "Medium Url",
            "Picture Url",
            "XL Picture Url",
            "Host ID",
            "Host URL",
            "Host Name",
            "Host Since",
            "Host Location",
            "Host About",
            "Host Response Time",
            "Host Response Rate",
            "Host Acceptance Rate",
            "Host Thumbnail Url",
            "Host Picture Url",
            "Host Neighbourhood",
            "Host Listings Count",
            "Host Total Listings Count",
            "Host Verifications",
            "Street",
            "Neighbourhood",
            "Neighbourhood Cleansed",
            "Neighbourhood Group Cleansed",
            "City",
            "State",
            "Zipcode",
            "Market",
            "Smart Location",
            "Country Code",
            "Country",
            "Latitude",
            "Longitude",
            "Property Type",
            "Room Type",
            "Accommodates",
            "Bathrooms",
            "Bedrooms",
            "Beds",
            "Bed Type",
            "Amenities",
            "Square Feet",
            "Price",
            "Weekly Price",
            "Monthly Price",
            "Security Deposit",
            "Cleaning Fee",
            "Guests Included",
            "Extra People",
            "Minimum Nights",
            "Maximum Nights",
            "Calendar Updated",
            "Has Availability",
            "Availability 30",
            "Availability 60",
            "Availability 90",
            "Availability 365",
            "Calendar last Scraped",
            "Number of Reviews",
            "First Review",
            "Last Review",
            "Review Scores Rating",
            "Review Scores Accuracy",
            "Review Scores Cleanliness",
            "Review Scores Checkin",
            "Review Scores Communication",
            "Review Scores Location",
            "Review Scores Value",
            "License",
            "Jurisdiction Names",
            "Cancellation Policy",
            "Calculated host listings count",
            "Reviews per Month",
            "Geolocation",
            "Features",
        ),
    )

    listings = defaultdict(list)

    # import ipdb

    # ipdb.set_trace()
    for row in reader:
        listings[row["Country"]].append(row["ID"])

for country, ids in listings.items():
    with open(f"{country}.txt", "w") as fid:
        fid.write("\n".join(ids))


import helper
import gold

def parse_content(content):
    """ Parse the content of the file """
    words = dict()
    for line in content.split('\n'):
        word, frequency = line.split()
        words[word] = int(frequency)

    return words

def make_tree(words):
    trie_structure = dict()
    for key, value in words.items():
        current_loc = trie_structure
        for character in key:
            if character not in current_loc.keys():
                current_loc[character] = dict()

            current_loc = current_loc[character]
        current_loc["$" + key] = value

    return trie_structure

def get_leaves(node):
    """ Get the leaves from a given tree """
    leaf_nodes = list()
    search_list = [node]

    while search_list:
        search_node = search_list.pop()
        for key in search_node.keys():
            if key.startswith("$"):
                leaf_nodes.append((key.replace("$", ""), search_node[key]))
            else:
                search_list.append(search_node[key])

    return leaf_nodes

def predict(tree, numbers):
    """ Predict the value from the input numbers """
    output_predictions = list()
    search_list = [tree]
    t9 = {"2": "abc", "3": "def", "4": "ghi", "5": "jkl", "6": "mno", "7": "pqrs", "8": "tuv", "9": "wxyz"}

    for number in numbers:
        length = len(search_list)

        for _ in range(length):
            node = search_list.pop(0)
            for key in node.keys():
                if key in t9[number]:
                    search_list.append(node[key])

    for node in search_list:
        output_predictions.extend(get_leaves(node))

    output_predictions.sort(key=lambda x: x[1], reverse=True)
    return output_predictions

if __name__ == '__main__':
    content = helper.read_content(filename='ngrams-10k.txt')

    # When you've finished implementing a part, remove the `gold.` prefix to check your own code.

    # PART 1: Parsing a string into a dictionary.
    words = parse_content(content)

    # PART 2: Building a trie from a collection of words.
    tree = make_tree(words)

    while True:
        # PART 3: Predict words that could follow
        numbers = helper.ask_for_numbers()
        predictions = predict(tree, numbers)

        if not predictions:
            print('No words were found that match those numbers. :(')
        else:
            for prediction, frequency in predictions[:10]:
                print(prediction, frequency)

        response = input('Want to go again? [y/N] ')
        again = response and response[0] in ('y', 'Y')
        if not again:
            break

Lessons Exercises/T9 Predictive Text/T9.py


'''This spider is used to crawl and save spot
    value of equites like SBIN, YESBANK,
    etc. You need to provide SYMBOL, STARTDATE,
    & ENDDATE as argument to this spider.
'''

import sys
from os import path
sys.path.append( path.dirname( path.dirname( path.abspath(__file__) ) ) )

import datetime
import scrapy, scraper
from scrapy.loader import ItemLoader
from scrapy.selector import Selector
from scrapy.loader.processors import Join, MapCompose
from scraper.items import kHistoricEquityItem
from scraper import utility

class kHistoricEquitySpider(scrapy.Spider):

    name = 'kHistoricEquitySpider'
    allowed_domains = ['www.nseindia.com']

    def __init__(self, symbol, startDate, endDate, databaseTableName, coldStart, * args, **kwargs):
        super(kHistoricEquitySpider, self).__init__(*args, **kwargs)
        self.symbol = symbol
        self.startDate = startDate
        self.endDate = endDate
        self.databaseTableName = databaseTableName + symbol
        self.coldStart = coldStart

        # working_url = https://www.nseindia.com/products/dynaContent/common/productsSymbolMapping.jsp?symbol=sbin&segmentLink=3&symbolCount=1&series=EQ&dateRange=+&fromDate=01-01-2018&toDate=01-03-2018&dataType=PRICEVOLUMEDELIVERABLE
        # url = https://www.nseindia.com/products/dynaContent/common/productsSymbolMapping.jsp?symbol={symbol}&segmentLink=3&symbolCount=1&series=EQ&dateRange=+&fromDate={startDate}&toDate={endDate}&dataType=PRICEVOLUMEDELIVERABLE
        # Referer: https://www.nseindia.com/products/content/equities/equities/eq_security.htm

    def start_requests(self):
        scrapy.Spider.start_requests(self)

        symbolCount = 1
        try:
            symbolCount = utility.SYMBOL_COUNT[self.symbol]
        except Exception:
            # do nothing
            symbolCount = 1

        endDate = datetime.datetime.now()
        startDate = endDate - datetime.timedelta(days=3)

        if (self.coldStart == True):
            startDate =  datetime.datetime.strptime(self.startDate, "%d-%b-%Y")
            endDate = datetime.datetime.strptime(self.endDate, "%d-%b-%Y")

        while (endDate > startDate):

            tempEndDate = startDate + datetime.timedelta(days=90)

            startDateStr = startDate.strftime("%d-%m-%Y")
            tempEndDatStr = tempEndDate.strftime("%d-%m-%Y")

            forceBreak = False
            if tempEndDate >= datetime.datetime.now():
                tempEndDate = datetime.datetime.now() - datetime.timedelta(days=1)
                forceBreak = True

            url = "https://www.nseindia.com/products/dynaContent/common/productsSymbolMapping.jsp?symbol=%s&segmentLink=3&symbolCount=%s&series=EQ&dateRange=+&fromDate=%s&toDate=%s&dataType=PRICEVOLUMEDELIVERABLE" %(self.symbol.lower(), symbolCount, startDateStr, tempEndDatStr)

            yield scrapy.Request(url,self.parse, headers={'Referer': 'https://www.nseindia.com/products/content/equities/equities/eq_security.htm'})

            startDate = tempEndDate
            if forceBreak:
                break

    # used to save respective xpath value with their item,
    # later we can iterate through it.
    item_fields = {
                'Date':'./td[3]//text()',
                'Open':'./td[5]//text()',
                'High':'./td[6]//text()',
                'Low':'./td[7]//text()',
                'Close':'./td[9]//text()',
                'SharesTraded':'./td[11]//text()',
                'Turnover':'./td[12]//text()'
                }

    def parse(self, response):

        # checking for response, wheather it contains records or not
        if (response.xpath("//tr[2]//text()").extract()[0].find("No Records") < 0):
            # Storing all selector except top 2(which contains waste data)
            res = response.xpath('//tr[position()>2]')
            # val will iterate from 0 to (last -1), because last res item is waste
            for val in res[0:len(res)]:
                loader = ItemLoader(kHistoricEquityItem(), val)
                loader.default_input_processor = MapCompose(unicode.strip)
                loader.default_output_processor = Join()

                for item, xpath in self.item_fields.iteritems():
                    loader.add_xpath(item, xpath)
                loader.add_value('DatabaseTableName', unicode(self.databaseTableName))
                yield loader.load_item()


__author__ = 'marko'

import numpy as np
from random import randint
from skimage.feature import hessian_matrix
from skimage.morphology import disk
from skimage.filters.rank import entropy
from preprocess import Preprocess
import cv2

class ImageSample(object):
    '''Image wrapper class that is used for samples extraction from images'''

    def __init__(self, img=None, path=None, block_size=5):
        if path and not img:
            img = cv2.imread(path)

        #img = Preprocess().blur_image(img)
        self.block_size = block_size
        self.img_rgb = img.copy()
        self.img_hsv = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
        #self.img_ycbcr = cv2.cvtColor(img, cv2.COLOR_RGB2YCrCb)
        self.img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
        self.height, self.width, _ = img.shape
        self.Hxx, self.Hxy, self.Hyy = hessian_matrix(self.img_gray)
        #vector, self.hog = hog(self.img_gray, orientations=8, pixels_per_cell=(3, 3),
        #            cells_per_block=(1, 1), visualise=True)

        neighours = disk(25)
        self.entropy = entropy(self.img_gray, neighours)

    def to_samples(self, n='all'):
        """
        Extract samples from image.
        :param n: number of samples per image
        :return: list of samples
        """
        samples = []
        add_sample = lambda fts: samples.append(fts) if fts else None

        if n == 'all':
            for i in xrange(self.height):
                for j in xrange(self.width):
                    add_sample(self._get_features(i, j))
        else:
            cnt = 0
            while len(samples) < n:
                i, j = randint(0, self.height-1), randint(0, self.width-1)
                add_sample(self._get_features(i, j))

        return samples

    def _get_features(self, i, j):
        """
        Calculates vector of features for pixel given with i,j coordinate
        :param i:
        :param j:
        :return:
        """
        features = []
        margin = self.block_size/2

        for ii in xrange(i-margin, i+margin):
            for jj in xrange(j-margin, j+margin):
                if ii < 0 or jj < 0 or ii >= self.height or jj >= self.width:
                    return None
                else:
                    features.extend(self.img_rgb[ii, jj])
                    features.extend(self.img_hsv[ii, jj])
                    #features.extend(self.img_ycbcr[ii, jj])

                    features.append(self.Hxx[ii, jj])
                    features.append(self.Hxy[ii, jj])
                    features.append(self.Hyy[ii, jj])
                    features.append(self.entropy[ii, jj])

                    #features.append(self.hog[ii, jj])

        return features

    # def find_vegetation(self, classifier):
    #     """
    #     Extract vegetation mask from image using given classifier
    #     :param classifier:
    #     :return:
    #     """
    #     result = np.zeros((self.height, self.width))
    #     samples = []
    #     ij = []
    #
    #     for i in xrange(self.height):
    #         for j in xrange(self.width):
    #             fts = self._get_features(i, j)
    #             if fts:
    #                 samples.append(fts)
    #                 ij.append((i, j))
    #
    #     classified = classifier.predict(samples)
    #
    #     for point, label in zip(ij, classified):
    #         result[point] = label
    #
    #     return result
    def find_vegetation(self, classifier):
        """
        Extract vegetation mask from image using given classifier
        :param classifier:
        :return:
        """
        result = np.zeros((self.height, self.width))
        samples = []
        ij = []
        limit = 20000

        for i in xrange(self.height):
            for j in xrange(self.width):
                fts = self._get_features(i, j)
                if fts:
                    samples.append(fts)
                    ij.append((i, j))

                if len(samples) > limit:
                    classified = classifier.predict(samples)
                    for point, label in zip(ij, classified):
                        result[point] = label

                    samples = []
                    ij = []

        classified = classifier.predict(samples)
        for point, label in zip(ij, classified):
            result[point] = label

        samples = []
        ij = []
        return result

    def find_vegetation_slow(self, classifier):
        """
        Extract vegetation mask from image using given classifier
        :param classifier:
        :return:
        """
        result = np.zeros((self.height, self.width))
        for i in xrange(self.height):
            for j in xrange(self.width):
                fts = self._get_features(i, j)
                if fts:
                    result[i, j] = classifier.predict(fts)

        return result


# Type a program that will calculate the BMI
# For that purpose, the users needs to input their weight and height
# For example, w= 80 & height = 1.75, the output will be 26

# Input block
height = float(input("Enter your height in [m]: \n"))
weight = float(input("Enter your weight in [kg]: \n"))

# Operations block
bmiCalc = weight/(height*height)
bmiCalc = round(bmiCalc,0)

# Output block
print(f"Your BMI is: {bmiCalc}")


# Deep Learning
#
# Assignment 3
#
# Previously in 2_fullyconnected.ipynb, you trained a logistic regression and a neural network model.
#
# The goal of this assignment is to explore regularization techniques.

# These are all the modules we'll be using later. Make sure you can import them
# before proceeding further.
from __future__ import print_function
import numpy as np
import tensorflow as tf
from six.moves import cPickle as pickle
from copy import copy

# First reload the data we generated in `1_notmnist.ipynb`.

pickle_file = 'notMNIST.pickle'

with open(pickle_file, 'rb') as f:
    save = pickle.load(f)
    train_dataset = save['train_dataset']
    train_labels = save['train_labels']
    valid_dataset = save['valid_dataset']
    valid_labels = save['valid_labels']
    test_dataset = save['test_dataset']
    test_labels = save['test_labels']
    del save  # hint to help gc free up memory
    print('Training set', train_dataset.shape, train_labels.shape)
    print('Validation set', valid_dataset.shape, valid_labels.shape)
    print('Test set', test_dataset.shape, test_labels.shape)

# Reformat into a shape that's more adapted to the models we're going to train:
# - data as a flat matrix,
# - labels as float 1-hot encodings.

image_size = 28
num_labels = 10

def reformat(dataset, labels):
    dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32)
    # Map 1 to [0.0, 1.0, 0.0 ...], 2 to [0.0, 0.0, 1.0 ...]
    labels = (np.arange(num_labels) == labels[:,None]).astype(np.float32)
    return dataset, labels

train_dataset, train_labels = reformat(train_dataset, train_labels)
valid_dataset, valid_labels = reformat(valid_dataset, valid_labels)
test_dataset, test_labels = reformat(test_dataset, test_labels)
print('Training set', train_dataset.shape, train_labels.shape)
print('Validation set', valid_dataset.shape, valid_labels.shape)
print('Test set', test_dataset.shape, test_labels.shape)

def accuracy(predictions, labels):
    return (100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1))
            / predictions.shape[0])

# ---
# Problem 1
# ---------
#
# Introduce and tune L2 regularization for both logistic and neural network models. Remember that L2 amounts to adding
# a penalty on the norm of the weights to the loss. In TensorFlow, you can compute the L2 loss for a tensor `t` using
# `nn.l2_loss(t)`. The right amount of regularization should improve your validation / test accuracy.
#
# ---

batch_size = 128
num_steps = 5000
hidden_layers = 1028
learning_rate = 0.5
l2_constant = 0.001

# graph = tf.Graph()
# with graph.as_default():
#     train = tf.placeholder(tf.float32, [batch_size, image_size * image_size])
#     labels = tf.placeholder(tf.float32, [batch_size, num_labels])
#     valid = tf.constant(valid_dataset)
#     test = tf.constant(test_dataset)
#
#     W = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels], mean=0, stddev=0.5))
#     bias = tf.Variable(tf.zeros([num_labels, ]))
#
#     logits = tf.matmul(train, W) + bias
#     loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=labels, logits=logits)) +\
#         l2_constant * tf.nn.l2_loss(W)
#
#     optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)
#
#     prediction = tf.nn.softmax(logits)
#     valid_prediction = tf.nn.softmax(tf.matmul(valid, W) + bias)
#     test_prediction = tf.nn.softmax(tf.matmul(test, W) + bias)

graph = tf.Graph()
with graph.as_default():
    train = tf.placeholder(tf.float32, [batch_size, image_size * image_size])
    labels = tf.placeholder(tf.float32, [batch_size, num_labels])
    valid = tf.constant(valid_dataset)
    test = tf.constant(test_dataset)

    W1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_layers], mean=0, stddev=0.5))
    bias1 = tf.Variable(tf.zeros([hidden_layers, ]))
    W2 = tf.Variable(tf.truncated_normal([hidden_layers, num_labels], mean=0, stddev=0.5))
    bias2 = tf.Variable(tf.zeros([num_labels, ]))

    layer1_out = tf.nn.relu(tf.matmul(train, W1) + bias1)
    logits = tf.matmul(layer1_out, W2) + bias2
    loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=labels, logits=logits)) +\
        l2_constant * (tf.nn.l2_loss(W1) + tf.nn.l2_loss(W2))

    optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)

    prediction = tf.nn.softmax(logits)
    valid_hidden = tf.nn.relu(tf.matmul(valid, W1) + bias1)
    valid_prediction = tf.nn.softmax(tf.matmul(valid_hidden, W2) + bias2)
    test_hidden = tf.nn.relu(tf.matmul(test, W1) + bias1)
    test_prediction = tf.nn.softmax(tf.matmul(test_hidden, W2) + bias2)

# with tf.Session(graph=graph) as session:
#     tf.global_variables_initializer().run()
#     for step in range(num_steps):
#         offset = (step * batch_size) % (train_labels.shape[0] - batch_size)
#
#         batch_data = train_dataset[offset:(offset + batch_size), :]
#         batch_labels = train_labels[offset:(offset + batch_size), :]
#
#         _, l, preds = session.run([optimizer, loss, prediction], feed_dict={train: batch_data, labels: batch_labels})
#         # _, loss = session.run([optimizer, loss], feed_dict={train: batch_data, labels: batch_labels})
#         if step % (num_steps/20) == 0:
#             print('Minibatch loss: {}'.format(l))
#             print('Minibatch valid accuracy: {}'.format(accuracy(valid_prediction.eval(), valid_labels)))
#     print('Test accuracy: {}'.format(accuracy(test_prediction.eval(), test_labels)))

# ---
# Problem 2
# ---------
# Let's demonstrate an extreme case of overfitting. Restrict your training data to just a few batches. What happens?
#
# ---

batch_size = 128
num_steps = 500
hidden_layers = 1028
learning_rate = 0.5
l2_constant = 0.001

graph = tf.Graph()
with graph.as_default():
    train = tf.placeholder(tf.float32, [batch_size, image_size * image_size])
    labels = tf.placeholder(tf.float32, [batch_size, num_labels])
    valid = tf.constant(valid_dataset)
    test = tf.constant(test_dataset)

    W1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_layers], mean=0, stddev=0.5))
    bias1 = tf.Variable(tf.zeros([hidden_layers, ]))
    W2 = tf.Variable(tf.truncated_normal([hidden_layers, num_labels], mean=0, stddev=0.5))
    bias2 = tf.Variable(tf.zeros([num_labels, ]))

    layer1_out = tf.nn.relu(tf.matmul(train, W1) + bias1)
    logits = tf.matmul(layer1_out, W2) + bias2
    loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=labels, logits=logits)) +\
        l2_constant * (tf.nn.l2_loss(W1) + tf.nn.l2_loss(W2))

    optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)

    prediction = tf.nn.softmax(logits)
    valid_hidden = tf.nn.relu(tf.matmul(valid, W1) + bias1)
    valid_prediction = tf.nn.softmax(tf.matmul(valid_hidden, W2) + bias2)
    test_hidden = tf.nn.relu(tf.matmul(test, W1) + bias1)
    test_prediction = tf.nn.softmax(tf.matmul(test_hidden, W2) + bias2)

# with tf.Session(graph=graph) as session:
#     tf.global_variables_initializer().run()
#     for step in range(num_steps):
#         offset = 128 * (num_steps % 4)
#
#         batch_data = train_dataset[offset:(offset + batch_size), :]
#         batch_labels = train_labels[offset:(offset + batch_size), :]
#
#         _, l, preds = session.run([optimizer, loss, prediction], feed_dict={train: batch_data, labels: batch_labels})
#         # _, loss = session.run([optimizer, loss], feed_dict={train: batch_data, labels: batch_labels})
#         if step % (num_steps/20) == 0:
#             print('Minibatch loss: {}'.format(l))
#             print('Minibatch accuracy: {}'.format(accuracy(preds, batch_labels)))
#             print('Minibatch valid accuracy: {}'.format(accuracy(valid_prediction.eval(), valid_labels)))
#     print('Test accuracy: {}'.format(accuracy(test_prediction.eval(), test_labels)))

# ---
# Problem 3
# ---------
# Introduce Dropout on the hidden layer of the neural network. Remember: Dropout should only be introduced during
# training, not evaluation, otherwise your evaluation results would be stochastic as well. TensorFlow provides
# `nn.dropout()` for that, but you have to make sure it's only inserted during training.
#
# What happens to our extreme overfitting case?
#
# ---

batch_size = 128
num_steps = 5000
hidden_layers = 1028
learning_rate = 0.5
l2_constant = 0.001
drop_prob = 0.9

import time
start = time.time()

graph = tf.Graph()
with graph.as_default():
    train = tf.placeholder(tf.float32, [batch_size, image_size * image_size])
    labels = tf.placeholder(tf.float32, [batch_size, num_labels])
    valid = tf.constant(valid_dataset)
    test = tf.constant(test_dataset)

    W1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_layers], mean=0, stddev=0.5))
    bias1 = tf.Variable(tf.zeros([hidden_layers, ]))
    W2 = tf.Variable(tf.truncated_normal([hidden_layers, num_labels], mean=0, stddev=0.5))
    bias2 = tf.Variable(tf.zeros([num_labels, ]))

    layer1_out = tf.nn.relu(tf.nn.dropout(tf.matmul(train, W1), drop_prob) + bias1)
    logits = tf.nn.dropout(tf.matmul(layer1_out, W2), drop_prob) + bias2
    loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=labels, logits=logits)) +\
        l2_constant * (tf.nn.l2_loss(W1) + tf.nn.l2_loss(W2))

    optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)

    batch_hidden = tf.nn.relu(tf.matmul(train, W1) + bias1)
    batch_prediction = tf.nn.softmax(tf.matmul(batch_hidden, W2) + bias2)
    valid_hidden = tf.nn.relu(tf.matmul(valid, W1) + bias1)
    valid_prediction = tf.nn.softmax(tf.matmul(valid_hidden, W2) + bias2)
    test_hidden = tf.nn.relu(tf.matmul(test, W1) + bias1)
    test_prediction = tf.nn.softmax(tf.matmul(test_hidden, W2) + bias2)

with tf.Session(graph=graph) as session:
    tf.global_variables_initializer().run()
    for step in range(num_steps):
        offset = (step * batch_size) % (train_labels.shape[0] - batch_size)

        batch_data = train_dataset[offset:(offset + batch_size), :]
        batch_labels = train_labels[offset:(offset + batch_size), :]

        _, l = session.run([optimizer, loss], feed_dict={train: batch_data, labels: batch_labels})
        if step % (num_steps/10) == 0:
            print('Minibatch loss: {}'.format(l))
            print('Validation accuracy: {}'.format(accuracy(valid_prediction.eval(), valid_labels)))
    print('Test accuracy: {}'.format(accuracy(test_prediction.eval(), test_labels)))

print('Processed in {} minutes'.format(round((time.time() - start)/60, 2)))

# 94.05 test:
batch_size = 128
num_steps = 5000
hidden_layers = 1028
learning_rate = 0.5
l2_constant = 0.001
drop_prob = 0.9

# ---
# Problem 4
# ---------
#
# Try to get the best performance you can using a multi-layer model! The best reported test accuracy using a deep
# network is [97.1%]
# (http://yaroslavvb.blogspot.com/2011/09/notmnist-dataset.html?showComment=1391023266211#c8758720086795711595).
#
# One avenue you can explore is to add multiple layers.
#
# Another one is to use learning rate decay:
#
#     global_step = tf.Variable(0)  # count the number of steps taken.
#     learning_rate = tf.train.exponential_decay(0.5, global_step, ...)
#     optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step)
#
#  ---

batch_size = 128
num_steps = 5000
hidden_layers = [256]
learning_rate = 0.5
l2_constant = 0.001
drop_prob = 0.9

def evaluate_network(hidden_layers, batch_size=128, num_steps=5000, learning_rate=0.5, l2_constant=0.001, drop_prob=0.9,
                     store_results=20):
    batch_error = []
    valid_error = []
    graph = tf.Graph()
    with graph.as_default():
        train = tf.placeholder(tf.float32, [batch_size, image_size * image_size])
        train_layers = [train]
        labels = tf.placeholder(tf.float32, [batch_size, num_labels])
        valid = tf.constant(valid_dataset)
        test = tf.constant(test_dataset)

        weight_sizes = [image_size * image_size] + hidden_layers + [num_labels]
        W = [tf.Variable(tf.truncated_normal([size_in, size_out], mean=0, stddev=0.5))
             for size_in, size_out in zip(weight_sizes[:-1], weight_sizes[1:])]
        bias_sizes = hidden_layers + [num_labels]
        bias = [tf.Variable(tf.zeros([size, ])) for size in bias_sizes]

        valid_layer = valid
        test_layer = test
        for i in range(len(W)):
            if i < (len(W) - 1):
                train_layer = tf.nn.relu(tf.nn.dropout(tf.matmul(train_layers[i], W[i]), drop_prob) + bias[i])
                train_layers = train_layers + [train_layer]
                valid_layer = tf.nn.relu(tf.matmul(valid_layer, W[i]) + bias[i])
                test_layer = tf.nn.relu(tf.matmul(test_layer, W[i]) + bias[i])
            else:
                train_layer = tf.nn.dropout(tf.matmul(train_layers[i], W[i]), drop_prob) + bias[i]
                train_layers = train_layers + [train_layer]
                valid_layer = tf.matmul(valid_layer, W[i]) + bias[i]
                test_layer = tf.matmul(test_layer, W[i]) + bias[i]
        loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=labels, logits=train_layers[-1])) +\
            l2_constant * sum(tf.nn.l2_loss(W_i) for W_i in W)

        optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)

        batch_prediction = tf.nn.softmax(train_layers[-1])
        valid_prediction = tf.nn.softmax(valid_layer)
        test_prediction = tf.nn.softmax(test_layer)

    with tf.Session(graph=graph) as session:
        tf.global_variables_initializer().run()
        for step in range(num_steps):
            offset = (step * batch_size) % (train_labels.shape[0] - batch_size)

            batch_data = train_dataset[offset:(offset + batch_size), :]
            batch_labels = train_labels[offset:(offset + batch_size), :]

            _, l, preds = session.run([optimizer, loss, batch_prediction],
                                      feed_dict={train: batch_data, labels: batch_labels})
            if step % (num_steps/store_results) == 0:
                # print('Minibatch loss: {}'.format(l))
                # print('Validation accuracy: {}'.format(accuracy(valid_prediction.eval(), valid_labels)))
                batch_error += [accuracy(preds, batch_labels)]
                valid_error += [accuracy(valid_prediction.eval(), valid_labels)]
        test_accuracy = accuracy(test_prediction.eval(), test_labels)
        print('Test accuracy: {}'.format(test_accuracy))

    return batch_error, valid_error, test_accuracy

# Test Loss: 94.4
batch_size = 128; num_steps = 5000; hidden_layers = [1028]; learning_rate = 0.5; l2_constant = 0.001; drop_prob = 0.9

layers = [
    [128], [256], [512], [1024], [2048],
    [128, 128], [128*2, 128], [128*3, 128],
    [256, 256], [256*2, 256], [256*3, 256],
    [512, 512], [512 * 2, 512], [512 * 3, 512],
    [2048, 1024, 512, 256]
]

params = [{'hidden_layers': l, 'learning_rate': 0.5 / (2**(len(l) - 1))} for l in layers]
smaller_lr = [{'hidden_layers': x['hidden_layers'], 'learning_rate': x['learning_rate'] / 5} for x in params]
params = params + smaller_lr

test_error = map(lambda x: evaluate_network(**x), params)

[x[2] for x in test_error]


#Faça um programa que leia a largura e a altura de uma parede
#em metros ,calcule a sua area e a quantidade de tinta necessaria
#para pinta-la, sabendo que cada litro de tinta pinta uma area
#de 2m²

largura = float(input("Digite a largura :"))
altura =  float(input("Digite a altura :"))
area = largura * altura
tinta = area / 2
print("Sua parede tema dimesão de {}x{} e sua area é de {} ,vai ser preciso de {}L de tinta para pintar a parede.".format(largura,altura,area,tinta))


from sklearn.datasets import fetch_20newsgroups  # 导入fetch_20newsgroups函数，用于获取新闻数据集
from sklearn.naive_bayes import MultinomialNB  # 导入MultinomialNB朴素贝叶斯分类器
from sklearn.feature_extraction.text import TfidfTransformer  # 导入TfidfTransformer，用于计算TF-IDF特征
from sklearn.feature_extraction.text import CountVectorizer  # 导入CountVectorizer，用于将文本转换为词频向量

category_map = {'talk.politics.misc': 'Politics',  # 定义新闻类别的映射关系
                'rec.autos': 'Autos',
                'rec.sport.hockey': 'Hockey',
                'sci.electronics': 'Electronics',
                'sci.med': 'Medicine'}

training_data = fetch_20newsgroups(subset='train',  # 获取训练数据集
                                   categories=category_map.keys(),
                                   shuffle=True,
                                   random_state=5)

count_vectorizer = CountVectorizer()  # 创建CountVectorizer对象，用于将文本转换为词频向量
train_tc = count_vectorizer.fit_transform(training_data.data)  # 对训练数据进行词频向量化

tfidf = TfidfTransformer()  # 创建TfidfTransformer对象，用于计算TF-IDF特征
train_tfidf = tfidf.fit_transform(train_tc)  # 对词频向量进行TF-IDF转换

input_data = [  # 待分类的输入数据
    'Be sure to take medicine when you are ill',
    'The player made a mistake in passing the ball',
    'Be sure to fasten your seat belt when you drive a car',
    'Sensing technology has been well applied in this device'
]

classifier = MultinomialNB().fit(train_tfidf, training_data.target)  # 创建并训练MultinomialNB分类器

input_tc = count_vectorizer.transform(input_data)  # 对输入数据进行词频向量化

input_tfidf = tfidf.transform(input_tc)  # 对词频向量进行TF-IDF转换

predictions = classifier.predict(input_tfidf)  # 对输入数据进行分类预测

for sent, category in zip(input_data, predictions):  # 遍历输入数据和预测结果
    print('\nInput sentence:', sent, '\nPredicted category:', \
          category_map[training_data.target_names[category]])  # 打印输入句子和预测的类别名称


import cv2
img1=cv2.imread('1.jpg')
img2=cv2.imread('2.jpg')
#使用SIFT算法获取图像特征的关键点和描述符
sift=cv2.xfeatures2d.SIFT_create()
kp1,des1=sift.detectAndCompute(img1,None)
kp2,des2=sift.detectAndCompute(img2,None)

#定义FLANN匹配器
indexParams=dict(algorithm=0,trees=10)
searchParams=dict(checks=50)
flann=cv2.FlannBasedMatcher(indexParams,searchParams)
#使用KNN算法实现图像匹配，并对匹配结果排序
matches=flann.knnMatch(des1,des2,k=2)
matches=sorted(matches,key=lambda x:x[0].distance)

#去除错误匹配，0.5是系数，系数大小不同，匹配的结果页不同
goodMatches=[]
for m,n in matches:
    if m.distance<0.5*n.distance:
        goodMatches.append(m)

#获取某个点的坐标位置
#index是获取匹配结果的中位数
index=int(len(goodMatches)/2)
#queryIdx是目标图像的描述符索引
x,y=kp1[goodMatches[index].queryIdx].pt
#将坐标位置勾画在2.png图片上，并显示
cv2.rectangle(img1,(int(x),int(y)),(int(x)+5,int(y)+5),(0,255,0),2)
cv2.imshow('baofeng',img1)
cv2.waitKey()


#!/usr/bin/env python2.7

import random
import time

rand = lambda: random.randint(0, 255)

def flash_randomly(leds, times=100):
    for _ in xrange(times):
        leds[0:len(leds)] = [(rand(), rand(), rand()) for _ in xrange(len(leds))]
        leds.update()
        time.sleep(0.02)

if __name__ == '__main__':
    import sys
    from lightsculpture import LEDs

    num_rods = int(sys.argv[1]) if len(sys.argv) >= 2 else 14
    times = int(sys.argv[2]) if len(sys.argv) >= 3 else 100

    flash_randomly(LEDs(num=num_rods), times=times)


import numpy as npy
import cvxpy as cvx
import matplotlib as mpl
mpl.use('QT4Agg')
import matplotlib.pyplot as plt

def p_fcn(u):
    tmp = npy.zeros(u.shape)
    for idx in npy.arange(u.size):
        if u[idx] <= -1:
            tmp[idx] = float("-inf")
        elif u[idx] <= 8:
            tmp[idx] = 11 + u[idx] - 6 * npy.sqrt(1+u[idx])
        else:
            tmp[idx] = 1
    return tmp

N = 100
u = npy.linspace(-2, 10, num = N)
p = p_fcn(u)
plt.plot(u, p, label = 'p*(u)')
xli = plt.gca().get_xlim();
plt.plot(0, 5, marker = 'o', label = 'p*(0)')
plt.xlabel('u')
plt.legend()
plt.show()


#! /usr/bin/python3

import pygame
import random
import gpiozero
import RPi.GPIO as GPIO
import functions
import time

button3 = 22
GPIO.setup(button3, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)

def define_image(image):
	global electronic_images
	image1 = image + '.png'
	image1 = pygame.image.load(image1).convert_alpha()
	image1 = pygame.transform.scale(image1,(100,100))

	electronic_images.append(image1)

def buttonColors():
	global button1Color, button2Color, button3Color
	button1Color = (0,191,255)
	button2Color = (30,144,255)
	button3Color = (65,105,225)

class Button:
    def __init__(self, text, width, height, pos, color):
        self.top_rect = pygame.Rect(pos,(width,height))
        self.top_color = color
        self.text_surf = gui_font.render(text, 0,(210,255,100))
        self.text_rect = self.text_surf.get_rect(center = self.top_rect.center)

    def draw(self):
        action = False

        mouse_pos = pygame.mouse.get_pos()

        if self.top_rect.collidepoint(mouse_pos):
            if pygame.mouse.get_pressed()[0] == 1:
                action = True

        pygame.draw.rect(screen,self.top_color, self.top_rect, 16)
        screen.blit(self.text_surf, self.text_rect)

        return action

pygame.init()
#screen = pygame.display.set_mode((800,480))
screen = pygame.display.set_mode((800,480), pygame.FULLSCREEN)
clock = pygame.time.Clock()

x = 0
picked = 0
question = 0

white = (255, 255, 255)
red = (255, 51, 58)
button1Color = (0,191,255)
button2Color = (30,144,255)
button3Color = (65,105,225)

buttonNumber = 0

electronic_images = []

global end, ts
end = 0
ts = 0

font = pygame.font.Font('freesansbold.ttf', 100)
gui_font = pygame.font.Font(None,30)

electronics = ['Andorra',
				'Argentina',
				'Armenia',
				'Afghanistan',
				'Australia',
				'Bolivia',
				'Brazil',
				'Belgium',
				'Canada',
				'Cambodia',
				'China',
				'Congo',
				'Cyprus',
				'Denmark',
				'Djibouti',
				'Equador',
				'Egypt',
				'Finland',
				'Figi',
				'France',
				'Georgia',
				'Ghana',
				'Greece',
				'Haiti',
				'Hungary',
				'Honduras',
				'Iceland',
				'Iran',
				'Ireland',
				'Italy',
				'India',
				'Japan',
				'Jordan',
				'Jamaica',
				'Kenya',
				'Kazakhstan',
				'Laos',
				'Liberia',
				'Madagascar',
				'Malta',
				'Mexico',
				'Malawi',
				'Nigeria',
				'Nepal',
				'Oman',
				'Pakistan',
				'Portugal',
				'Panama',
				'Qatar',
				'Romania',
				'Rwanda',
				'Scotland',
				'Spain',
				'Syria',
				'Serbia',
				'Thailand',
				'Tanzania',
				'Uganda',
				'Uruguay',
				'Ukraine',
				'Venezuela',
				'Vietnam',
				'Vanuatu',
				'Yemen',
				'Zambia',
				'Zimbabwe'
				]

for element in electronics:
	image = element.lower()
	define_image(image)

while True:

	buttonOne = Button('', 250, 480, (15,0), button1Color)
	buttonTwo = Button('', 245, 480, (280,0), button2Color)
	buttonThree = Button('',250, 480, (540, 0), button3Color)

	for event in pygame.event.get():
	    if event.type == pygame.QUIT:
	        pygame.quit()
	        sys.exit()
	    if event.type == pygame.KEYDOWN:
	        if event.key == pygame.K_ESCAPE:
	            pygame.quit()
	            sys.exit()

	if GPIO.input(button3) == 1:
	    pygame.quit()
	    sys.exit()

	if question == 0:
		countries = len(electronics)-1
		picked = random.randint(0,countries)
		numbers = [*range(0, countries+1, 1)]

		numbers.remove(picked)
		second = random.choice(numbers)
		numbers.remove(second)
		third = random.choice(numbers)
		question += 1
		all_choice = [picked, second, third]
		random.shuffle(all_choice)
		index = 0
		for element in all_choice:
			if element == picked:
				buttonNumber = index
			index += 1

	textSurface = font.render(electronics[picked], True, (32,178,170),(173, 216, 230))
	textRect = textSurface.get_rect()
	textRect.center = (400, 100)

	screen.fill((173, 216, 230))

	x = 100
	for element in all_choice:
		screen.blit(electronic_images[element], (0+x,250))
		x += 250

	if buttonOne.draw():
		ts = time.time()
		if (ts - end) > 1:
			if buttonNumber == 0:
				question = 0
				buttonColors()
				end = time.time()
			else:
				button1Color = (255,0,0)

	if buttonTwo.draw():
		ts = time.time()
		if (ts - end) > 1:
		    if buttonNumber == 1:
		        question = 0
		        buttonColors()
		        end = time.time()
		    else:
		    	button2Color = (255,0,0)

	if buttonThree.draw():
		ts = time.time()
		if (ts - end) > 1:
		    if buttonNumber == 2:
		        question = 0
		        buttonColors()
		        end = time.time()
		    else:
		    	button3Color = (255,0,0)

	screen.blit(textSurface, textRect)
	pygame.display.update()
	clock.tick(10)


from advent_of_code import BaseSolution
from typing import List
from .instruction import Instruction
from .sample import Sample
from .device import Device
from .opcode_map import OpcodeMap

class Solution(BaseSolution):
    samples: List[Sample]
    instructions: List[Instruction]

    def setup(self):
        (input_file, ) = self.parameters()
        lines: List[str] = list(self.read_input(input_file))

        self.samples = []
        self.instructions = []

        while len(lines) > 0:
            section = lines.pop(0)
            if section.startswith('Before'):
                before_parts = section.split(': ')[1][1:-1].split(', ')
                before = Device([int(x) for x in before_parts])
                ins = Instruction(*[int(x) for x in lines.pop(0).split(' ')])
                after_parts = lines.pop(0).split(':  ')[1][1:-1].split(', ')
                after = Device([int(x) for x in after_parts])
                self.samples.append(Sample(before, after, ins))
                continue

            ins = Instruction(*[int(x) for x in section.split(' ')])
            self.instructions.append(ins)

    def part1(self):
        samples_with_more_than_two_matches: List[Sample] = []
        for s in self.samples:
            if s.test() > 2:
                samples_with_more_than_two_matches.append(s)
        return len(samples_with_more_than_two_matches)

    def part2(self):
        for s in self.samples:
            s.train()

        final_device = Device()
        for ins in self.instructions:
            OpcodeMap.execute(final_device, ins)

        return final_device.registers[0]