import socket
import ssl

# TODO 发送请求  访问网站  页面获取  保存 浏览器解析

def parsed_url(url):

    # 检查协议
    protocol = 'http'
    if url[:7] == 'http://':
        u = url.split('://')[1]
    elif url[:8] == 'https://':
        protocol = 'https'
        u = url.split('://')[1]
    else:
        # '://' 定位 然后取第一个 / 的位置来切片
        u = url

    # 检查默认 path
    i = u.find('/')
    if i == -1:
        host = u
        path = '/'
    else:
        host = u[:i]
        path = u[i:]

    # 检查端口
    port_dict = {
        'http': 80,
        'https': 443,
    }
    # 默认端口
    port = port_dict[protocol]
    if host.find(':') != -1:
        h = host.split(':')
        host = h[0]
        port = int(h[1])

    return protocol, host, port, path

def socket_by_protocol(protocol):
    if protocol == "http":
        s = socket.socket()
    elif protocol == "https":
        s = ssl.wrap_socket(socket.socket())
    return s

def response_by_socket(s):
    response = b''
    buffer_size = 1024
    while True:
        r = s.recv(buffer_size)
        if len(r) == 0:
            break
        else:
            response = response + r

    response = response.decode('utf-8')
    return response

def parsed_response(response):
    header, body = response.split("\r\n\r\n", 1)
    h = header.split("\r\n")
    status_code = h[0].split()[1]
    status_code = int(status_code)

    headers = {}
    for line in h[1:]:
        k, v = line.split(': ')
        headers[k] = v
    return status_code, headers, body

def creatRequest(path, host):
    request = 'GET {} HTTP/1.1\r\nHost:{}\r\nConnection: close\r\n\r\n'.format(path, host)
    encoding = 'utf-8'
    request = request.encode(encoding)
    return request

def get(url):
    protocol, host, port, path = parsed_url(url)

    s = socket_by_protocol(protocol)
    s.connect((host, port))

    request = creatRequest(path, host)
    s.send(request)

    response = response_by_socket(s)

    status_code, headers, body = parsed_response(response)

    if status_code in [301, 302]:
        url = headers['Location']
        # print("网页重定向了")
        return get(url)

    return status_code, headers, body

def main():
    url = 'http://movie.douban.com/top250'
    status_code, headers, body = get(url)

main()


# Function tells number of digits at ten's place and at one's place.
def NumberOfDigits(integer):
    ones=0
    ten=0
    if integer >9 and integer <100:
        while integer > 9:
            ones+= integer%10
            integer=integer//10
            ten+=integer
    else:
        ones= integer
    return "Ten's: {}\nOne's: {}".format(ten,ones)
print(NumberOfDigits(10))

# Function to form integer from number of digits.
def Integer_from_number_of_digits(tens, ones):
    integer = (tens*10)+ones
    return integer
print(Integer_from_number_of_digits(2,2))

2 Python Programming Examples on Mathematical Expressions/11 Number of digits at ten's and one's place of the integer.py


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

import torch

from torch import nn
from torch.autograd import Variable

data_csv = pd.read_csv('F:/data_test/data.csv', usecols=[1])
plt.plot(data_csv)

plt.show()

# 数据预处理
data_csv = data_csv.dropna()  # 删除缺失数据
dataset = data_csv.values
dataset = dataset.astype('float32')  # 转换数据类型
max_value = np.max(dataset)
min_value = np.min(dataset)
scalar = max_value - min_value
dataset = list(map(lambda x: x / scalar, dataset))  # 数据归一化?

# 用前两个月的流量预测当月流量 所以look_back=2
def create_dataset(dataset, look_back=2):
    dataX, dataY = [], []
    for i in range(len(dataset) - look_back):
        a = dataset[i:(i + look_back)]  # 选两个 当前的和下一个
        dataX.append(a)
        dataY.append(dataset[i + look_back])  # 从第三个数据开始
    return np.array(dataX), np.array(dataY)

# 创建好输入输出
data_X, data_Y = create_dataset(dataset)

# 划分训练集和测试集，70% 作为训练集
train_size = int(len(data_X) * 0.7)
test_size = len(data_X) - train_size
train_X = data_X[:train_size]
train_Y = data_Y[:train_size]
test_X = data_X[train_size:]
test_Y = data_Y[train_size:]

train_X = train_X.reshape(-1, 1, 2)  # 后两维固定为1,2，第一维自动确定
train_Y = train_Y.reshape(-1, 1, 1)
test_X = test_X.reshape(-1, 1, 2)

train_x = torch.from_numpy(train_X)  # 转为tensor形式
train_y = torch.from_numpy(train_Y)
test_x = torch.from_numpy(test_X)

# print('trainx{}\ntrainy{}\ntestx{}'.format(train_x,train_y,test_x))

# 定义模型
class lstm_reg(nn.Module):
    def __init__(self, input_size, hidden_size, output_size=1, num_layers=2):
        super(lstm_reg, self).__init__()  # 调用父类(超类)的一个方法

        self.rnn = nn.LSTM(input_size, hidden_size, num_layers)  # rnn
        self.reg = nn.Linear(hidden_size, output_size)  # 回归

    def forward(self, x):
        x, _ = self.rnn(x)  # (seq, batch, hidden)
        s, b, h = x.shape
        x = x.view(s * b, h)  # view改变维度；转换成线性层的输入格式，因为nn.Linear 不接受三维的输入，所以我们先将前两维合并在一起
        x = self.reg(x)  # 经过线性层
        x = x.view(s, b, -1)  # 将前两维分开，最后输出结果
        return x

net = lstm_reg(2, 4)

criterion = nn.MSELoss()  # 均方根损失函数
optimizer = torch.optim.Adam(net.parameters(), lr=1e-2)  # 优化步骤

# 开始训练
for e in range(1000):
    var_x = Variable(train_x)
    var_y = Variable(train_y)
    # 前向传播
    out = net(var_x)
    loss = criterion(out, var_y)
    # 反向传播
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    if (e + 1) % 100 == 0:  # 每 100 次输出结果
        print('Epoch: {}, Loss: {:.5f}'.format(e + 1, loss.item()))

net = net.eval()  # 转换成测试模式

data_X = data_X.reshape(-1, 1, 2)
data_X = torch.from_numpy(data_X)
var_data = Variable(data_X)
pred_test = net(var_data)  # 测试集的预测结果

# 改变输出的格式
pred_test = pred_test.view(-1).data.numpy()

# 画出实际结果和预测的结果
plt.plot(pred_test, 'r', label='prediction')
plt.plot(dataset, 'b', label='real')
plt.legend(loc='best')


#!/usr/bin/python
"""
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).
The robot can only move either down or right at any point in time.
The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
#62
"""
def uniquePaths(m, n):
    rslt = [x[:] for x in [[0]*(m+1)]*(n+1)]

    for i in range(1, n+1):
        for j in range(1, m+1):
            if i == 1 and j == 1:
                rslt[1][1] = 1
            else:
                rslt[i][j] = rslt[i-1][j] + rslt[i][j-1]

    return rslt[-1][-1]

def test1():
    print(uniquePaths(4, 3))

if __name__ == '__main__':
    test1()


from datetime import datetime

from django.test import TestCase
from freezegun import freeze_time
from rest_framework.reverse import reverse
from rest_framework.test import APIClient

from phonecall.models import PhoneCall

class TestCreatePhoneCall(TestCase):

    def setUp(self):
        self.client = APIClient()

    def test_create_phone_call_start(self):
        data = {
            "type": "start",
            "timestamp": "2018-12-30T12:00:00",
            "call_id": "008",
            "source": 19982065330,
            "destination": 19982055500
        }
        response = self.client.post(reverse('phonecall-list'), data, format='json')
        assert response.status_code == 201
        assert PhoneCall.objects.exists()
        assert PhoneCall.objects.first().type == 'start'
        assert PhoneCall.objects.first().call_id == '008'

    def test_create_phone_call_end(self):
        PhoneCall.objects.create(type='start', timestamp=datetime(2018, 12, 30, 12, 0, 0), call_id='008',
                                 source=19982065330, destination=19982055500)
        data = {
            "type": "end",
            "timestamp": "2018-12-30T12:55:00",
            "call_id": "008"
        }
        response = self.client.post(reverse('phonecall-list'), data, format='json')
        assert response.status_code == 201
        assert PhoneCall.objects.count() == 2
        assert PhoneCall.objects.last().type == 'end'
        assert PhoneCall.objects.last().price > 0
        assert PhoneCall.objects.last().duration.seconds == 55 * 60

    def test_create_phone_call_end_without_start(self):
        data = {
            "type": "end",
            "timestamp": "2018-12-30T12:55:00",
            "call_id": "008"
        }
        response = self.client.post(reverse('phonecall-list'), data, format='json')
        assert response.status_code == 404
        assert not PhoneCall.objects.exists()

class TestPhoneCallBill(TestCase):

    def setUp(self):
        PhoneCall.objects.create(type='start', timestamp=datetime(2018, 10, 30, 12, 0, 0), call_id='008',
                                 source=19982065330, destination=19982055500)
        PhoneCall.objects.create(type='end', timestamp=datetime(2018, 10, 30, 12, 5, 0), call_id='008')

    @freeze_time("2018-11-05")
    def test_phonecall_bill(self):
        response = self.client.get(reverse('phonecalls-bill-list'), {'source': 19982065330})
        assert response.data['total_price'] == 'R$ 0,81'
        assert response.data['calls'][0]['destination'] == 19982055500
        assert response.data['calls'][0]['call_duration'] == '0h5m0s'

    @freeze_time("2018-12-05")
    def test_phonecall_bill_with_date(self):
        response = self.client.get(reverse('phonecalls-bill-list'),
                                   {'source': 19982065330, 'month': 10, 'year': 2018})
        assert response.data['total_price'] == 'R$ 0,81'
        assert response.data['calls'][0]['destination'] == 19982055500
        assert response.data['calls'][0]['call_duration'] == '0h5m0s'

    @freeze_time("2018-12-05")
    def test_phonecall_bill_with_no_results(self):
        response = self.client.get(reverse('phonecalls-bill-list'), {'source': 19982065330, 'month': 9, 'year': 2018})
        assert not response.data['calls']

    def test_phonecall_bill_with_invalid_source(self):
        response = self.client.get(reverse('phonecalls-bill-list'), {'source': '1998206533', 'month': 9, 'year': 2018})
        assert response.status_code == 400
        assert response.json()[0] == 'Invalid Source value.'

    def test_phonecall_bill_with_invalid_date(self):
        response = self.client.get(reverse('phonecalls-bill-list'),
                                   {'source': 19982065330, 'month': 14, 'year': 2018})
        assert response.status_code == 400
        assert response.json()[0] == 'Invalid month/year value.'


from django import forms
from django.conf import settings
from .models import Product
from .barcode import MyBarcodeDrawing

class ProductForm(forms.ModelForm):
    class Meta:
        model = Product
        exclude = ('barcode_image', 'date_from', 'date_to')

    def save(self, commit=True):
        instance = super(ProductForm, self).save(commit=False)
        bid = self.cleaned_data['barcode_id']
        output = "%s/product/barcodes/" % (settings.MEDIA_ROOT)
        output = MyBarcodeDrawing(bid).save(formats=['png'],outDir=output,fnRoot=bid)
        instance.barcode_image = 'product/barcodes/%s.png' % (bid)
        if commit:
            instance.save()
        return instance


from setuptools import setup, find_packages

with open('README.md', 'r') as fh:
    long_description = fh.read()

setup(
    # Package Info
    name='revgraph',
    author='shhoalex',
    version='0.0.1',
    description='A toy deep learning library built using numpy as its only dependency.',
    long_description=long_description,
    long_description_content_type='text/markdown',
    project_urls={
        'Source Code': 'https://github.com/shhoalex/revgraph',
        'Documentation': 'https://shhoalex.github.io/revgraph'
    },
    classifiers=[
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.6',
        'Programming Language :: Python :: 3.7',
        'Programming Language :: Python :: 3.8',
        'Intended Audience :: Education',
        'Topic :: Scientific/Engineering :: Artificial Intelligence',
        'Topic :: Software Development :: Libraries',
        'License :: OSI Approved :: MIT License',
        'Operating System :: OS Independent',
    ],

    # Dependencies
    install_requires=['numpy>=1.18.2', 'dill>=0.3.2'],
    python_requires='>=3.6',

    # Exporting the following packages
    packages=find_packages(),
    package_data={
        # Also export the sample datasets
        'revgraph.datasets': ['data/*/*']
    }
)


import urllib2, sys, re
import xlwt
import requests, json
import operator
from operator import attrgetter
from bs4 import BeautifulSoup
import urllib
import time

TOTAL_NO_OF_PRODUCT_PER_CATEGORY = 2000
ALCHEMY_API_KEY = "" #Alchemy api key
DATA = []	# Main array in which product will be populated
hdr = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.17 (KHTML, like Gecko)  Chrome/24.0.1312.57 Safari/537.17'}

# Function takes categegoy url and populate product into DATA array in required format
def populateTopProductsByCategory(categoryUrl, categoryName):
	print("Getting items for " + categoryName + " form url:" + categoryUrl)
	req = urllib2.Request(categoryUrl,headers=hdr)
	text = urllib2.urlopen(req).read()
	soup = BeautifulSoup(text, "html.parser")
	data = soup.findAll('h2', attrs={'class':'bc-big-row-title'})
	if not data:
		data = soup.findAll('div', attrs= {'class' : 'bc-navy-cate-inner'})
	noOfItemForEachCategory = TOTAL_NO_OF_PRODUCT_PER_CATEGORY/len(data)
	for li in data:
		link = li.find('a', href=True)
		if(link['href']):
			populateProductsFromURL(link['href'], noOfItemForEachCategory, categoryName)

# Function takes sub-categegoy url, limit(upto how many products should br included) and populate product into DATA array in required format
def populateProductsFromURL(url, limit, categoryName):
	print(url + "\n")
	page = 1
	req = urllib2.Request(getValidUrl(url), headers=hdr)
	text = urllib2.urlopen(req).read()
	soup = BeautifulSoup(text, "html.parser")
	div = soup.find('div', attrs = {'class' : 'ui-pagination-navi'})
	nextPageUrl = div.findAll('a', href=True)
	while (limit > 0):
		if(page!=1):
			req = urllib2.Request(getValidUrl(url), headers=hdr)
			text = urllib2.urlopen(req).read()
			soup = BeautifulSoup(text, "html.parser")
		productLinks = soup.findAll('a',attrs={'class':'product'}, href=True)
		for productLink in productLinks:
			if(limit == 0):
				break;
			populateItemDetail(productLink['href'], categoryName)
			limit -= 1
		page += 1
		url = nextPageUrl[page - 1]['href']

# create object for one product
def populateItemDetail(url, categoryName):
	print(url)
	if(url):
		try :
			req = urllib2.Request(getValidUrl(url), headers=hdr)
			text = urllib2.urlopen(req).read()
			soup = BeautifulSoup(text, "html.parser")
			name = str(soup.find('h1', attrs = {'class' : 'product-name'}).text.encode('utf8').strip())
			price = float(getOnePrice(soup.find('span', attrs = {'id' : 'j-sku-price'}).text.encode('utf8').strip()).encode('utf8').strip())
			pictures = getImageUrlArray(soup.find('ul', attrs = {'id' : 'j-image-thumb-list'}))
			description = getDescription(soup.find('ul', attrs = {'class' : 'product-property-list'}))
			feedback = getFeedback(soup.find('iframe')['thesrc'])
			DATA.append({"name" : name, "price" : price, "pictures" : pictures, "description" : description, "category" : categoryName, "rating" : feedback['rating'], "reviewAnalysis" : feedback["reviewAnalysis"]})
			print(str(len(DATA)) + " Items Done")
		except :
			print("Unable to get data retry in 1 second")
			time.sleep(1)
			req = urllib2.Request("http://www.aliexpress.com/category/44/consumer-electronics.html?spm=2114.01010108.2.3.Bn0rkU",headers=hdr)
			text = urllib2.urlopen(req).read()
			soup = BeautifulSoup(text, "html.parser")
			populateItemDetail(url, categoryName)

# Return object containing rating, reviews, and response from alchemy api after sentimentents analysis
def getFeedback(url):
	# reviews = []
	reqData = {}
	if(url):
		req = urllib2.Request(getValidUrl(url), headers=hdr)
		text = urllib2.urlopen(req).read()
		soup = BeautifulSoup(text, "html.parser")
		rating = getRating(soup.findAll('span', attrs = {'class' : 'star-view'}))
		# feedbackItem = soup.findAll('dt', attrs = { 'class' : 'buyer-feedback'})
		# for feedback in feedbackItem:
		# 	message1 = feedback.find('span')
		# 	message2 = ""
		# 	if(message1):
		# 		message2 = message1.get_text()
		# 	reviews.append(message2.encode("utf-8"))
		if(ALCHEMY_API_KEY != ""):
			r = requests.post("https://gateway-a.watsonplatform.net/calls/html/HTMLGetTextSentiment",data= { "apikey" : ALCHEMY_API_KEY, "html" : soup.text, "outputMode" : "json" }).text
			response = json.loads(r)
			reqData = response['docSentiment']
	return {"rating": rating, "reviewAnalysis" : reqData}

# Create array for product description
def getDescription(descriptionUl):
	des = []
	if(descriptionUl):
		for li in descriptionUl.findAll('li'):
			des.append({'title' : str(li.find('span', attrs = {'class' : 'propery-title'}).text.encode('utf8').strip()), 'value' : str(li.find('span', attrs = {'class' : 'propery-des'}).text.encode('utf8').strip()) })
	return des

# Calculate average rating from first page of review(contains 10 reviews in each page)
def getRating(ratingViews):
	if(ratingViews):
		totalRating = 0
		for rate in ratingViews:
			totalRating += getRatingByImageWidth(rate.find('span')['style'])
		return totalRating/len(ratingViews)
	else:
		return 0

# Get rating based on width of start images
def getRatingByImageWidth(x):
    return {
        'width:100%': 5,
        'width:80%': 4,
        'width:60%': 3,
        'width:40%': 2,
        'width:20%': 1,
    }[x]

# Append http in url
def getValidUrl(url):
	return "http:" + url

# get price from string of different format like "2,334 - 1,555"
def getOnePrice(price):
	price = price.replace(",", "")
	if "-" in price:
		index = price.index('-')
		return price[:index]
	else :
		return price

# get images of product inside array
def getImageUrlArray(imageUl):
	images = []
	if(imageUl):
		for li in imageUl.findAll('li'):
			images.append(str(li.find('img', {"src":True})['src']))
	return images

def startScrapping():
	populateTopProductsByCategory("http://www.aliexpress.com/category/44/consumer-electronics.html?spm=2114.01010108.2.3.Bn0rkU", "Consumer Electronics")
	populateTopProductsByCategory("http://www.aliexpress.com/category/1509/jewelry-accessories.html?spm=2114.20020108.1.70.OEnrwm", "Jewelry & Accessories")
	populateTopProductsByCategory("http://www.aliexpress.com/category/66/health-beauty.html?spm=2114.20020108.1.152.0LclhE", "Health and Beauty")
	print("Done scrapping sorting now\n")
	DATA.sort(key = lambda x: x['price'],reverse=True)
	print("Writing data in json format\n")
	with open('aliexpress.txt', 'w') as outfile:
		json.dump(DATA, outfile)
	print("Data written in json format in aliexpress.txt\n")

startScrapping()


#!/usr/bin/env python3

# metal.py
#
# One of the main roles of a compiler is taking high-level programs
# such as what you might write in C or Python and reducing them to
# instructions that can execute on actual hardware.
#
# This file implements a very tiny simulated CPU in the form of a
# Python program.  Although simulated, this CPU mimics the behavior of
# a real CPU.  There are registers for performing simple mathematical
# calculations, memory operations for loading/storing values, and
# control flow instructions for branching and gotos.
#
# See the end of this file for some exercises.
#
# The machine has 8 registers (R0, R1, ..., R7) that hold 32-bit
# integer values.  Register R0 is hardwired to always contains the
# value 0. Register R7 is initialized to the highest valid memory
# address.   A special register PC holds the memory location of the
# next instruction that will execute.
#
# The machine understands the following instructions--which
# are encoded as tuples:
#
#   ('ADD', 'Ra', 'Rb', 'Rd')       ; Rd = Ra + Rb
#   ('SUB', 'Ra', 'Rb', 'Rd')       ; Rd = Ra - Rb
#   ('AND', 'Ra', 'Rb', 'Rd')       ; Rd = Ra & Rb (bitwise-and)
#   ('OR', 'Ra', 'Rb', 'Rd')        ; Rd = Ra | Rb (bitwise-or)
#   ('XOR', 'Ra', 'Rb', 'Rd')       ; Rd = Ra ^ Rb (bitwise-xor)
#   ('SHL', 'Ra', 'Rb', 'Rd')       ; Rd = Ra << Rb (left bit-shift)
#   ('SHR', 'Ra', 'Rb', 'Rd')       ; Rd = Ra >> Rb (right bit-shift)
#   ('CONST', value, 'Rd')          ; Rd = value
#   ('LOAD', 'Rs', 'Rd', offset)    ; Rd = MEMORY[Rs + offset]
#   ('STORE', 'Rs', 'Rd', offset)   ; MEMORY[Rd + offset] = Rs
#   ('JMP', 'Rd', offset)           ; PC = Rd + offset
#   ('BZ', 'Rt', offset)            ; if Rt == 0: PC = PC + offset
#   ('HALT,)                        ; Halts machine
#
# In the the above instructions 'Rx' means some register number such
# as 'R0', 'R1', etc.  All memory instructions take their address from
# register plus an offset that's encoded as part of the instruction.

MASK    = 0xffffffff
PRINT   = 'PRINT'
ADD     = 'ADD'
SUB     = 'SUB'
AND     = 'AND'
OR      = 'OR'
XOR     = 'XOR'
SHL     = 'SHL'
SHR     = 'SHR'
CONST   = 'CONST'
LOAD    = 'LOAD'
STORE   = 'STORE'
JMP     = 'JMP'
BZ      = 'BZ'
HALT    = 'HALT'
R0      = 'R0'
R1      = 'R1'
R2      = 'R2'
R3      = 'R3'
R4      = 'R4'
R5      = 'R5'
R6      = 'R6'
R7      = 'R7'

class Metal:
    def run(self, memory):
        '''
        Run a program. memory is a Python list containing the program
        instructions and other data.  Upon startup, all registers
        are initialized to 0.  R7 is initialized with the highest valid
        memory index (len(memory) - 1).
        '''
        self.pc = 0
        self.registers = { f'R{d}':0 for d in range(8) }
        self.memory = memory
        self.registers['R7'] = len(memory) - 1
        self.running = True
        while self.running:
            op, *args = self.memory[self.pc]
            self.pc += 1
            getattr(self, op)(*args)
            self.registers['R0'] = 0    # R0 is always 0
        return

    def ADD(self, ra, rb, rd):
        self.registers[rd] = (self.registers[ra] + self.registers[rb]) & MASK

    def SUB(self, ra, rb, rd):
        self.registers[rd] = (self.registers[ra] - self.registers[rb]) & MASK

    def AND(self, ra, rb, rd):
        self.registers[rd] = (self.registers[ra] & self.registers[rb]) & MASK

    def OR(self, ra, rb, rd):
        self.registers[rd] = (self.registers[ra] | self.registers[rb]) & MASK

    def XOR(self, ra, rb, rd):
        self.registers[rd] = (self.registers[ra] ^ self.registers[rb]) & MASK

    def SHL(self, ra, rb, rd):
        self.registers[rd] = (self.registers[ra] << self.registers[rb]) & MASK

    def SHR(self, ra, rb, rd):
        self.registers[rd] = (self.registers[ra] >> self.registers[rb]) & MASK

    def CONST(self, value, rd):
        self.registers[rd] = value & MASK

    def LOAD(self, rs, rd, offset):
        self.registers[rd] = (self.memory[self.registers[rs]+offset]) & MASK

    def STORE(self, rs, rd, offset):
        self.memory[self.registers[rd]+offset] = self.registers[rs]

    def JMP(self, rd, offset):
        self.pc = self.registers[rd] + offset

    def BZ(self, rt, offset):
        if not self.registers[rt]:
            self.pc += offset

    def PRINT(self, rx):
        print(f'{rx} = {self.registers[rx]}')

    def HALT(self):
        self.running = False

if __name__ == '__main__':
    machine = Metal()

    # ----------------------------------------------------------------------
    # Problem 1:  Computers
    #
    # The CPU of a computer executes low-level instructions.  Using the
    # Metal instruction set above, show how you would compute 2 + 3 - 4.

    prog1 = [ # Instructions here
              ('CONST', 2, 'R1'),
              ('CONST', 3, 'R2'),
              # More instructions here
              # ...
              # Save the result. Replace 'R0' with whatever register holds the result.
              ('STORE', 'R0', 'R7', 0),
              ('HALT',),
              0            # Store the result here (note: R7 points here)
              ]

    prog1 = [
        (CONST, 2, R1),
        (CONST, 3, R2),
        (ADD, R1, R2, R3),
        (CONST, 4, R4),
        (SUB, R3, R4, R5),
        (STORE, R5, R7, 0),
        (HALT, ),
        0,
    ]

    machine.run(prog1)
    print('Program 1 Result:', prog1[-1], '(Should be 1)')

    # ----------------------------------------------------------------------
    # Problem 2: Computation
    #
    # Write a Metal program that computes 3 * 7.
    #
    # Note: The machine doesn't implement multiplication. So, you need
    # to figure out how to do it.

    prog2 = [ # Instructions here
              ('CONST', 3, 'R1'),
              ('CONST', 7, 'R2'),
              # ...
              ('HALT',),
              0           # Store result here
            ]

    prog2 = [
        (CONST, 3, R1),
        (CONST, 7, R2),
        (CONST, 0, R3),
        (CONST, 1, R4),
        (ADD, R2, R3, R3),
        (SUB, R1, R4, R1),
        (BZ, R1, 1),
        (JMP, R0, 4),
        (STORE, R3, R7, 0),
        (HALT,),
        0,
    ]

    machine.run(prog2)
    print('Program 2 Result:', prog2[-1], '(Should be 21)')

    # ----------------------------------------------------------------------
    # Problem 3: Abstraction
    #
    # Write a Python function mul(x, y) that computes x * y on Metal.
    # This function, should abstract details away--you're not supposed to
    # worry about how it works.  Just call mul(x, y).  Naturally, you
    # are NOT allowed to use the Python * operator.  Only use the provided
    # Metal instructions.

    def mul(x, y):
        prog = [ # Instructions here
                 # ...

                 ('HALT',),
                 0       # Result
        ]

        prog = [
            (CONST, x, R1),
            (CONST, y, R2),
            (CONST, 0, R3),
            (CONST, 1, R4),
            (ADD, R2, R3, R3),
            (SUB, R1, R4, R1),
            (BZ, R1, 1),
            (JMP, R0, 4),
            (STORE, R3, R7, 0),
            (HALT,),
            0,
        ]
        machine.run(prog)
        return prog[-1]

    print(f'Problem 3: 3 * 9 = {mul(3, 9)}. (Should be 27)')

    # ----------------------------------------------------------------------
    # Optional challenge:
    #
    # What is the fastest algorithm for computing mul(x, y)?
    # Katsuba Algorithm


from typing import Any, Sequence

import openai
import tiktoken
from azure.search.documents import SearchClient
from azure.search.documents.models import QueryType
from approaches.approach import Approach
from text import nonewlines

class ChatReadRetrieveReadApproach(Approach):
    # Chat roles
    SYSTEM = "system"
    USER = "user"
    ASSISTANT = "assistant"

    """
    Simple retrieve-then-read implementation, using the Cognitive Search and OpenAI APIs directly. It first retrieves
    top documents from search, then constructs a prompt with them, and then uses OpenAI to generate an completion
    (answer) with that prompt.
    """
    system_message_chat_conversation = """Assistant helps the company employees with their healthcare plan questions, and questions about the employee handbook. Be brief in your answers.
Answer ONLY with the facts listed in the list of sources below. If there isn't enough information below, say you don't know. Do not generate answers that don't use the sources below. If asking a clarifying question to the user would help, ask the question.
For tabular information return it as an html table. Do not return markdown format.
Each source has a name followed by colon and the actual information, always include the source name for each fact you use in the response. Use square brackets to reference the source, e.g. [info1.txt]. Don't combine sources, list each source separately, e.g. [info1.txt][info2.pdf].
{follow_up_questions_prompt}
{injected_prompt}
"""
    follow_up_questions_prompt_content = """Generate three very brief follow-up questions that the user would likely ask next about their healthcare plan and employee handbook.
    Use double angle brackets to reference the questions, e.g. <<Are there exclusions for prescriptions?>>.
    Try not to repeat questions that have already been asked.
    Only generate questions and do not generate any text before or after the questions, such as 'Next Questions'"""

    query_prompt_template = """Below is a history of the conversation so far, and a new question asked by the user that needs to be answered by searching in a knowledge base about employee healthcare plans and the employee handbook.
    Generate a search query based on the conversation and the new question.
    Do not include cited source filenames and document names e.g info.txt or doc.pdf in the search query terms.
    Do not include any text inside [] or <<>> in the search query terms.
    If the question is not in English, translate the question to English before generating the search query.

Chat History:
{chat_history}

Question:
{question}

Search query:
"""

    def __init__(self, search_client: SearchClient, chatgpt_deployment: str, chatgpt_model: str, gpt_deployment: str, sourcepage_field: str, content_field: str):
        self.search_client = search_client
        self.chatgpt_deployment = chatgpt_deployment
        self.chatgpt_model = chatgpt_model
        self.gpt_deployment = gpt_deployment
        self.sourcepage_field = sourcepage_field
        self.content_field = content_field

    def run(self, history: Sequence[dict[str, str]], overrides: dict[str, Any]) -> Any:
        use_semantic_captions = True if overrides.get("semantic_captions") else False
        top = overrides.get("top") or 3
        exclude_category = overrides.get("exclude_category") or None
        filter = "category ne '{}'".format(exclude_category.replace("'", "''")) if exclude_category else None

        # STEP 1: Generate an optimized keyword search query based on the chat history and the last question
        prompt = self.query_prompt_template.format(chat_history=self.get_chat_history_as_text(history, include_last_turn=False), question=history[-1]["user"])
        completion = openai.Completion.create(
            engine=self.gpt_deployment,
            prompt=prompt,
            temperature=0.0,
            max_tokens=32,
            n=1,
            stop=["\n"])
        q = completion.choices[0].text

        # STEP 2: Retrieve relevant documents from the search index with the GPT optimized query
        if overrides.get("semantic_ranker"):
            r = self.search_client.search(q,
                                          filter=filter,
                                          query_type=QueryType.SEMANTIC,
                                          query_language="en-us",
                                          query_speller="lexicon",
                                          semantic_configuration_name="default",
                                          top=top,
                                          query_caption="extractive|highlight-false" if use_semantic_captions else None)
        else:
            r = self.search_client.search(q, filter=filter, top=top)
        if use_semantic_captions:
            results = [doc[self.sourcepage_field] + ": " + nonewlines(" . ".join([c.text for c in doc['@search.captions']])) for doc in r]
        else:
            results = [doc[self.sourcepage_field] + ": " + nonewlines(doc[self.content_field]) for doc in r]
        content = "\n".join(results)

        follow_up_questions_prompt = self.follow_up_questions_prompt_content if overrides.get("suggest_followup_questions") else ""

        # Allow client to replace the entire prompt, or to inject into the exiting prompt using >>>
        prompt_override = overrides.get("prompt_template")
        messages = self.get_messages_from_history(prompt_override=prompt_override, follow_up_questions_prompt=follow_up_questions_prompt,history=history, sources=content)

        # STEP 3: Generate a contextual and content specific answer using the search results and chat history
        chat_completion = openai.ChatCompletion.create(
            deployment_id=self.chatgpt_deployment,
            model=self.chatgpt_model,
            messages=messages,
            temperature=overrides.get("temperature") or 0.7,
            max_tokens=1024,
            n=1)

        chat_content = chat_completion.choices[0].message.content

        msg_to_display = '\n\n'.join([str(message) for message in messages])

        return {"data_points": results, "answer": chat_content, "thoughts": f"Searched for:
{q}

Conversations:
" + msg_to_display.replace('\n', '
')}

    def get_chat_history_as_text(self, history: Sequence[dict[str, str]], include_last_turn: bool=True, approx_max_tokens: int=1000) -> str:
        history_text = ""
        for h in reversed(history if include_last_turn else history[:-1]):
            history_text = """<|im_start|>user""" + "\n" + h["user"] + "\n" + """<|im_end|>""" + "\n" + """<|im_start|>assistant""" + "\n" + (h.get("bot", "") + """<|im_end|>""" if h.get("bot") else "") + "\n" + history_text
            if len(history_text) > approx_max_tokens*4:
                break
        return history_text

    def get_messages_from_history(self, prompt_override, follow_up_questions_prompt, history: Sequence[dict[str, str]], sources: str, approx_max_tokens: int = 1000) -> []:
        '''
        Generate messages needed for chat Completion api
        '''
        messages = []
        token_count = 0
        if prompt_override is None:
            system_message = self.system_message_chat_conversation.format(injected_prompt="", follow_up_questions_prompt=follow_up_questions_prompt)
        elif prompt_override.startswith(">>>"):
            system_message = self.system_message_chat_conversation.format(injected_prompt=prompt_override[3:] + "\n", follow_up_questions_prompt=follow_up_questions_prompt)
        else:
            system_message = prompt_override.format(follow_up_questions_prompt=follow_up_questions_prompt)

        messages.append({"role":self.SYSTEM, "content": system_message})
        token_count += self.num_tokens_from_messages(messages[-1], self.chatgpt_model)

        # latest conversation
        user_content = history[-1]["user"] + " \nSources:" + sources
        messages.append({"role": self.USER, "content": user_content})
        token_count += token_count + self.num_tokens_from_messages(messages[-1], self.chatgpt_model)

        '''
        Enqueue in reverse order
        if limit exceeds truncate old messages
        leaving system message behind
        Keep track of token count for each conversation
        If token count exceeds limit, break
        '''
        for h in reversed(history[:-1]):
            if h.get("bot"):
                messages.insert(1, {"role": self.ASSISTANT, "content" : h.get("bot")})
                token_count += self.num_tokens_from_messages(messages[1], self.chatgpt_model)
            messages.insert(1, {"role": self.USER, "content" : h.get("user")})
            token_count += self.num_tokens_from_messages(messages[1], self.chatgpt_model)
            if token_count > approx_max_tokens*4:
                break
        return messages

    def num_tokens_from_messages(self, message: dict[str,str], model: str) -> int:
        """
        Calculate the number of tokens required to encode a message.
        Args:
            message (dict): The message to encode, represented as a dictionary.
            model (str): The name of the model to use for encoding.
        Returns:
            int: The total number of tokens required to encode the message.
        Example:
            message = {'role': 'user', 'content': 'Hello, how are you?'}
            model = 'gpt-3.5-turbo'
            num_tokens_from_messages(message, model)
            output: 11
        """
        encoding = tiktoken.encoding_for_model(self.get_oai_chatmodel_tiktok(model))
        num_tokens = 0
        num_tokens += 2  # For "role" and "content" keys
        for key, value in message.items():
            num_tokens += len(encoding.encode(value))
        return num_tokens

    def get_oai_chatmodel_tiktok(self, aoaimodel: str):
        if aoaimodel == "" or aoaimodel is None:
            raise Exception("Expected AOAI chatGPT model name")

        return "gpt-3.5-turbo" if aoaimodel == "gpt-35-turbo" else aoaimodel


import torch
import os
import glob
from PIL import Image

class Hotdog_NotHotdog(torch.utils.data.Dataset):
    def __init__(self, train, transform, data_path='hotdog_nothotdog'):
        'Initialization'
        self.transform = transform
        data_path = os.path.join(data_path, 'train' if train else 'test')
        image_classes = [os.path.split(d)[1] for d in glob.glob(data_path +'/*') if os.path.isdir(d)]
        image_classes.sort()
        self.name_to_label = {c: id for id, c in enumerate(image_classes)}
        self.image_paths = glob.glob(data_path + '/*/*.jpg')

    def __len__(self):
        'Returns the total number of samples'
        return len(self.image_paths)

    def __getitem__(self, idx):
        'Generates one sample of data'
        image_path = self.image_paths[idx]

        image = Image.open(image_path)
        c = os.path.split(os.path.split(image_path)[0])[1]
        y = self.name_to_label[c]
        X = self.transform(image)
        return X, y


# Copyright 2015-2016 Canonical, Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import logging
import yaml

from bundleplacer.charmstore_api import CharmStoreID

log = logging.getLogger('bundleplacer')

class Service:
    def __init__(self, service_name, charm_source, summary_future,
                 constraints, depends, conflicts,
                 allowed_assignment_types, num_units, options,
                 allow_multi_units, subordinate, required, relations,
                 placement_spec):
        self.service_name = service_name
        self.charm_source = charm_source
        self.csid = CharmStoreID(charm_source)
        self.charm_name = self.csid.name
        self.summary_future = summary_future
        self._summary = "Loading summary…"
        self.constraints = constraints
        self.depends = depends
        self.conflicts = conflicts
        self.allowed_assignment_types = allowed_assignment_types
        self.num_units = num_units
        self.options = options
        self.allow_multi_units = allow_multi_units
        self.subordinate = subordinate
        self.is_core = required
        self.isolate = True if not subordinate else False
        self.relations = relations
        self.placement_spec = placement_spec
        self.resources = None

    @property
    def summary(self):
        if self.summary_future and self.summary_future.done():
            self._summary = self.summary_future.result()
        return self._summary

    @property
    def display_name(self):
        return "{} ({})".format(self.service_name,
                                self.charm_source)

    def _format_scope_directive(self, placement):
        if ':' in placement:
            scope, directive = placement.split(':')
            if scope == 'lxc':
                scope = 'lxd'
            return {"scope": scope, "directive":
                    str(directive)}
        else:
            # Assume that the placement is to a machine number
            scope = '#'
            directive = placement
            return {"scope": scope, "directive":
                    str(directive)}

    def _prepare_placement(self, placement):
        new_placements = []
        if isinstance(placement, list):
            for p in placement:
                new_placements.append(self._format_scope_directive(p))
        else:
            new_placements.append(self._format_scope_directive(placement))

        return new_placements

    def as_deployargs(self):
        rd = {"charm-url": self.csid.as_str(),
              "application": self.service_name,
              "num-units": self.num_units,
              "constraints": self.constraints}

        if self.resources:
            rd['resources'] = self.resources

        if len(self.options) > 0:
            config_dict = {self.service_name: self.options}
            config_yaml = yaml.dump(config_dict, default_flow_style=False)
            rd["config-yaml"] = config_yaml

        if self.placement_spec:
            specs = self._prepare_placement(self.placement_spec)
            rd["placement"] = specs

        return rd

    def required_num_units(self):
        return self.num_units

    def __repr__(self):
        return "<Service {}>".format(self.service_name)

    def __eq__(self, other):
        me = self.charm_source + self.service_name
        them = other.charm_source + other.service_name
        return me == them

    def __hash__(self):
        """We assume that we won't instantiate multiple Charm objects for the
        same class that have different properties.
        """
        return hash(self.charm_source + self.service_name)


# Régression Linéaire Simple
#une seule variable indépendante et une seule variable dépendante dans la Régression Linéaire Simple
# Importer les librairies
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.metrics import mean_squared_error, r2_score

# Importer le dataset
dataset = pd.read_csv('Salary_Data.csv')
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, -1].values

# Gérer les données manquantes
# dans cet exemple il n’y a pas de données manquantes

# Gérer les variables catégoriques
# dans cet exemple il n’y a pas de variables catégoriques

# Diviser le dataset entre le Training set et le Test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.33, random_state = 0)

# Construction du modèle
#on importe la classe LinearRegression du module linear_model de sklearn
from sklearn.linear_model import LinearRegression
#on crée un objet regressor de cette classe
regressor = LinearRegression()
#nous allons lier cet objet à notre training test composé de X_train et y_train
#c’est la méthode fit qui lie le training set à l’objet.
regressor.fit(X_train, y_train)

# Faire de nouvelles prédictions
# on va donner en paramètre l'ensemble des 10 valeurs de variables indépendantes X_test
# on peut voir les résultats en cliquant sur l'objet y_pred dans la zone explorateur
# de variables et la comparer avec y_test qui contient les salaires réels y_test
y_pred = regressor.predict(X_test)
#pour prédire un salaire d'un employé qui n'existe pas dans notre data set à travers le nombre
#d'années d’expérience.
#exemple suivant : prédire le salaire de quelqu'un avec 15 ans d'expérience
#[[15]] avec des crochets car c'est une matrice.
print(int(regressor.predict([[15]])))

# Explained variance score: 1 is perfect prediction
print('R2 score: %.2f' % r2_score(y_test,y_pred))
print(regressor.score(X_test,y_test))
# Visualiser les résultats
#nous allons créer un graphe pour visualiser nos points d'observation et les points de prédiction
# nous allons utiliser le nodule pyplot de la librairie matplotlib par son raccourcis plt
# en premier lieu on va placer nos points d'observation sur le graphe avec la fonction scatter
#cette fonction demande comme paramètres l'axe des abscisses qui contient X_test et l'axe
#des ordonnées qui contient y_test
#puisque c’est un graphe, on aura besoin des couleurs pour distinguer les points d'observation
# des points de prédiction. On choisit le rouge pour les points d'observation
plt.scatter(X_test, y_test, color = 'red')
# pour les points de prédictions, nous n'allons pas placer les points de prédictions
#mais nous allons tracer une ligne droite
#pour tracer une ligne droite ou une courbe on utilise la fonction plot
#on indique les abscisses des points de la courbe "X_train" et les ordonnées des points de la
#courbe "regressor.predict(X_train)"
# on ne met pas y_train dans les ordonnées des points de la courbe car y_train contient les
# valeurs réelles
plt.plot(X_train, regressor.predict(X_train), color = 'blue')
plt.title('Salaire vs Expérience')
plt.xlabel('Expérience')
plt.ylabel('Salaire')
plt.show()
# nous remarquons qu'il y a une corrélation linéaire entre les salaires et les nb d'années
# d'expérience donc un modèle de régression linéaire est tout à fait approprie a ce problème
# on remarque qu'il y a seulement 3 points d'observation qui sont un petit peu loin de notre
# droite, par contre les autres on voit qu’il une excellente prédiction, pour le cas des
# points d'observation qui se sont situées sur la droite.

from sklearn.model_selection import cross_val_score
clf = LinearRegression()

scores = cross_val_score(clf, X, y, cv=3)
print(scores.mean())


from django import forms

class LocationAForm(forms.Form):
    loc_a = forms.CharField(label='')
    loc_a.widget.attrs.update({'id': 'search_termA','align': 'center', 'placeholder':'Enter your address'})

class LocationBForm(forms.Form):
    loc_b = forms.CharField(label='')
    loc_b.widget.attrs.update({'id': 'search_termB','align': 'center', 'placeholder':'Enter other address'})

class KeywordForm(forms.Form):
    keyword = forms.CharField(label='', required=False)
    keyword.widget.attrs.update({'align': 'center', 'placeholder':'FIND tacos, cafe, donut, sushi, etc.'})


# The guess API is already defined for you.
# @param num, your guess
# @return -1 if my number is lower, 1 if my number is higher, otherwise return 0
# def guess(num: int) -> int:

class Solution:
    def guessNumber(self, n: int) -> int:
        l, r = 1, n
        while l <= r:
            m = (l + r) >> 1
            k = guess(m)
            if k == -1:
                r = m - 1
            elif k == 1:
                l = m + 1
            else:
                return m
        return l


import json
import logging
import random
from cdtcommon.cdtdiagnostics import DIAGNOSTICS
from cdtcommon.cdtembed import Embed

import aiohttp
import discord
from redbot.core import checks, commands
from redbot.core.config import Config

log = logging.getLogger("red.CollectorDevTeam.dadjokes")
CDT_LOGO = "https://raw.githubusercontent.com/CollectorDevTeam/assets/master/data/cdt_logo.png"

class DadJokes(commands.Cog):
    """Random dad jokes from icanhazdadjoke.com"""

    def __init__(self, bot):
        self.bot = bot
        self.diagnostics = DIAGNOSTICS(self.bot)
        self.dadjoke_images = [
            "https://cdn.discordapp.com/attachments/391330316662341632/865819301326880768/dadjoke5.png",
            "https://cdn.discordapp.com/attachments/391330316662341632/865819305693675541/dadjoke6.png",
            "https://cdn.discordapp.com/attachments/391330316662341632/865819308731138109/dadjoke7.png",
            "https://cdn.discordapp.com/attachments/391330316662341632/865822723858104390/dadjoke8.png",
            "https://cdn.discordapp.com/attachments/391330316662341632/865822729675603988/dadjoke9.png"

        ]
        self.session = aiohttp.ClientSession()

    def cog_unload(self):
        self.bot.loop.create_task(self.session.close())

    @commands.command(
        aliases=(
            "joke",
            "dadjokes",
            "jokes",
        ),
    )
    async def dadjoke(self, ctx):
        """Gets a random dad joke."""
        author = ctx.message.author
        joke = await self.get_joke()
        image = random.choice(self.dadjoke_images)
        kwargs = {"content": f"{image}\n\n{joke}"}
        #if await ctx.embed_requested():
        data = await Embed.create(ctx, title="CollectorVerse Dad Jokes:sparkles:", description=joke, image=image, footer_text="Dad Jokes | CollectorDevTeam")
        await ctx.send(embed=data)

    async def get_joke(self):
        api = "https://icanhazdadjoke.com/slack"
        joke = None
        while joke is None:
            async with self.session.get(api) as response:
                result = await response.json()
                attachments = result["attachments"][0]
                joke = attachments["text"]
        return joke

def setup(bot):
    n = DadJokes(bot)
    bot.add_cog(n)


from keras.layers import Input, LSTM, Embedding, Dense, Bidirectional
from keras.models import Model

def architector(num_decoder_tokens, latent_dim=50):

    # Encoder

    encoder_inputs = Input(shape=(None,))
    enc_emb =  Embedding(124, latent_dim, mask_zero = True)(encoder_inputs)
    encoder_lstm = Bidirectional(LSTM(latent_dim, return_state=True))
    _, forward_h, forward_c, backward_h, backward_c = encoder_lstm(enc_emb)
    encoder_states = [forward_h, forward_c, backward_h, backward_c]

    # Decoder
    # using `encoder_states` as initial state.

    decoder_inputs = Input(shape=(None,))
    dec_emb_layer = Embedding(num_decoder_tokens, latent_dim, mask_zero = True)
    dec_emb = dec_emb_layer(decoder_inputs)
    decoder_lstm = Bidirectional(LSTM(latent_dim, return_sequences=True, return_state=True))
    decoder_outputs, _, _, _, _ = decoder_lstm(dec_emb, initial_state=encoder_states)
    decoder_dense = Dense(num_decoder_tokens, activation='softmax')
    decoder_outputs = decoder_dense(decoder_outputs)

    # Model

    enc_dec_model = Model([encoder_inputs, decoder_inputs], decoder_outputs)

    # Encode the input sequence to get the "thought vectors"
    encoder_model = Model(encoder_inputs, encoder_states)

    # Decoder setup
    # Below tensors will hold the states of the previous time step
    decoder_state_input_h = Input(shape=(latent_dim,))
    decoder_state_input_c = Input(shape=(latent_dim,))
    decoder_state_input_h1 = Input(shape=(latent_dim,))
    decoder_state_input_c1 = Input(shape=(latent_dim,))

    decoder_states_inputs = [decoder_state_input_h, decoder_state_input_c,
                             decoder_state_input_h1, decoder_state_input_c1]

    dec_emb2 = dec_emb_layer(decoder_inputs)  # Get the embeddings of the decoder sequence

    # To predict the next word in the sequence, set the initial states to the states from the previous time step
    decoder_outputs2, forward_h2, forward_c2, \
                    backward_h2, backward_c2 = decoder_lstm(dec_emb2, initial_state=decoder_states_inputs)
    decoder_states2 = [forward_h2, forward_c2, backward_h2, backward_c2]
    decoder_outputs2 = decoder_dense(
        decoder_outputs2)  # A dense softmax layer to generate prob dist. over the target vocabulary

    # Final decoder model
    decoder_model = Model(
        [decoder_inputs] + decoder_states_inputs,
        [decoder_outputs2] + decoder_states2)

    return enc_dec_model, encoder_model, decoder_model


# This code is part of Qiskit.
#
# (C) Copyright IBM 2021.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

"""Resonance analysis class."""

from typing import List, Union

import numpy as np

import qiskit_experiments.curve_analysis as curve

class ResonanceAnalysis(curve.CurveAnalysis):
    r"""A class to analyze a resonance, typically seen as a peak.

    Overview
        This analysis takes only single series. This series is fit by the Gaussian function.

    Fit Model
        The fit is based on the following Gaussian function.

        .. math::

            F(x) = a \exp(-(x-f)^2/(2\sigma^2)) + b

    Fit Parameters
        - :math:`a`: Peak height.
        - :math:`b`: Base line.
        - :math:`f`: Center frequency. This is the fit parameter of main interest.
        - :math:`\sigma`: Standard deviation of Gaussian function.

    Initial Guesses
        - :math:`a`: Calculated by :func:`~qiskit_experiments.curve_analysis.guess.max_height`.
        - :math:`b`: Calculated by :func:`~qiskit_experiments.curve_analysis.guess.\
          constant_spectral_offset`.
        - :math:`f`: Frequency at max height position calculated by
          :func:`~qiskit_experiments.curve_analysis.guess.max_height`.
        - :math:`\sigma`: Calculated from FWHM of peak :math:`w`
          such that :math:`w / \sqrt{8} \ln{2}`, where FWHM is calculated by
          :func:`~qiskit_experiments.curve_analysis.guess.full_width_half_max`.

    Bounds
        - :math:`a`: [-2, 2] scaled with maximum signal value.
        - :math:`b`: [-1, 1] scaled with maximum signal value.
        - :math:`f`: [min(x), max(x)] of frequency scan range.
        - :math:`\sigma`: [0, :math:`\Delta x`] where :math:`\Delta x`
          represents frequency scan range.

    """

    __series__ = [
        curve.SeriesDef(
            fit_func=lambda x, a, sigma, freq, b: curve.fit_function.gaussian(
                x, amp=a, sigma=sigma, x0=freq, baseline=b
            ),
            plot_color="blue",
            model_description=r"a \exp(-(x-f)^2/(2\sigma^2)) + b",
        )
    ]

    def _generate_fit_guesses(
        self, user_opt: curve.FitOptions
    ) -> Union[curve.FitOptions, List[curve.FitOptions]]:
        """Compute the initial guesses.

        Args:
            user_opt: Fit options filled with user provided guess and bounds.

        Returns:
            List of fit options that are passed to the fitter function.
        """
        curve_data = self._data()
        max_abs_y, _ = curve.guess.max_height(curve_data.y, absolute=True)

        user_opt.bounds.set_if_empty(
            a=(-2 * max_abs_y, 2 * max_abs_y),
            sigma=(0, np.ptp(curve_data.x)),
            freq=(min(curve_data.x), max(curve_data.x)),
            b=(-max_abs_y, max_abs_y),
        )
        user_opt.p0.set_if_empty(b=curve.guess.constant_spectral_offset(curve_data.y))

        y_ = curve_data.y - user_opt.p0["b"]

        _, peak_idx = curve.guess.max_height(y_, absolute=True)
        fwhm = curve.guess.full_width_half_max(curve_data.x, y_, peak_idx)

        user_opt.p0.set_if_empty(
            a=curve_data.y[peak_idx] - user_opt.p0["b"],
            freq=curve_data.x[peak_idx],
            sigma=fwhm / np.sqrt(8 * np.log(2)),
        )

        return user_opt

    def _evaluate_quality(self, fit_data: curve.FitData) -> Union[str, None]:
        """Algorithmic criteria for whether the fit is good or bad.

        A good fit has:
            - a reduced chi-squared less than 3,
            - a peak within the scanned frequency range,
            - a standard deviation that is not larger than the scanned frequency range,
            - a standard deviation that is wider than the smallest frequency increment,
            - a signal-to-noise ratio, defined as the amplitude of the peak divided by the
              square root of the median y-value less the fit offset, greater than a
              threshold of two, and
            - a standard error on the sigma of the Gaussian that is smaller than the sigma.
        """
        curve_data = self._data()

        max_freq = np.max(curve_data.x)
        min_freq = np.min(curve_data.x)
        freq_increment = np.mean(np.diff(curve_data.x))

        fit_a = fit_data.fitval("a").value
        fit_b = fit_data.fitval("b").value
        fit_freq = fit_data.fitval("freq").value
        fit_sigma = fit_data.fitval("sigma").value
        fit_sigma_err = fit_data.fitval("sigma").stderr

        snr = abs(fit_a) / np.sqrt(abs(np.median(curve_data.y) - fit_b))
        fit_width_ratio = fit_sigma / (max_freq - min_freq)

        criteria = [
            min_freq <= fit_freq <= max_freq,
            1.5 * freq_increment < fit_sigma,
            fit_width_ratio < 0.25,
            fit_data.reduced_chisq < 3,
            (fit_sigma_err is None or fit_sigma_err < fit_sigma),
            snr > 2,
        ]

        if all(criteria):
            return "good"

        return "bad"


#coding:utf8
import pandas as pd
import matplotlib.pyplot as plt
from graphtools import *
from collections import Counter
# from visualization import *

def main():
	# get a set of data points
    # data = scio.loadmat('./datasets/can383.mat',header=None, names=['x', 'y'])
    df = pd.read_csv('./datasets/Wholescale.csv', delimiter=' ', header=None)
    # create knn graph
    k=30;cluster_number=2
    rho, dismatrix,k_neighbors = Rho_Calculate(df, k)
    Delta,nneigh=Delta_Calculate(rho, dismatrix)
    graph=Creat_graph(Delta,nneigh,df)
	#split graph
    L_subgraph,L_outlier=part_graph_1(graph)
    print(len(L_subgraph))
    print(len(L_outlier))
    Sub_clu_num=len(L_subgraph)
    df['cluster'] = 0;cn=1;flag=0;ind=1
    num_subgraph=range(Sub_clu_num)
	#calculate connectivity
    con=connectivity_cal(L_subgraph,k_neighbors,dismatrix)
	#merge sub-graph
    while True:
        con,df,cn,flag,L_subgraph,num_subgraph=merge_graph(con,df,L_subgraph,cn,num_subgraph)
        if flag==1 or len(Counter(num_subgraph))==cluster_number :
            break
    print(len(Counter(num_subgraph)))
    num=Counter(num_subgraph).items()
    target=[i[0] for i in num if i[1]==1]
    for i in range(len(target)):
        g=target[i]
        li=L_subgraph[g].nodes()
        df.ix[li, 'cluster'] = cn
        cn=cn+1
    L_outlier=[i.nodes() for i in L_outlier]
    L_outlier=[i[0] for i in L_outlier]
    df.ix[L_outlier, 'cluster']=cn
    temp=df['cluster']
    sort_class=Counter(temp).items()
    for i in range(len(sort_class)):
        index=[a for a in range(len(df)) if df.ix[a,'cluster'] == sort_class[i][0]]
        df.ix[index, 'cluster']=ind
        ind=ind+1
    # df.plot(kind='scatter', c=df['cluster'], cmap='gist_rainbow', x='x', y='y')
    label=pd.read_csv('./datasets/Wholescale.csv',delimiter=' ',header=None)
    print(NMI(np.array(label[0]),df['cluster'].values.T))
    plt.show()
if __name__ == "__main__":
	main()


from flask import Flask, request, jsonify
from flask_cors import CORS
from hackerman.ui.output import msg, color
import json

class Chatroom(object):
    def __init__(self, messages=[], backup_number=10, backup_file="messages.json", load_backup=True):
        self.messages = []
        self.backup_n = backup_number
        self.backup_f = backup_file
        self.counter = 0 # for backup_n
        if load_backup:
            self.messages = self.read_backup()

    def read_backup(self):
        try:
            d = json.loads(open(self.backup_f,"r").read())
        except (FileNotFoundError, PermissionError):
            print(
                msg.alert("Error opening backupfile [%s]" % self.backup_f)
            )
            f = open(self.backup_f,"w") # create file
            f.write(json.dumps([])) # empty list
            f.close()
            d = []
        return d
    def write_backup(self, msgs):
        with open(self.backup_f, "w") as f:
            f.write(json.dumps(msgs))
    def update_backup(self, newmsgs):
        old = self.read_backup()
        [old.append(i) for i in newmsgs]
        self.write_backup(old)

    def build_app(self):
        app = Flask(__name__)
        CORS(app)
        @app.route("/")
        def app_main():
            return jsonify(self.messages)
        @app.route("/send", methods=['GET','POST'])
        def app_send():
            inp = request.args if request.method == "GET" else request.form
            if (not "user" in inp) or (not "msg" in inp):
                return "error - expected argument: " + ("user" if not "user" in inp else "msg")
            self.messages.append({'user':inp['user'],'msg':inp['msg']})
            self.counter += 1
            if self.counter >= self.backup_n:
                print(msg.info("Updating backup"))
                self.update_backup(self.messages)
                self.counter = 0
            return "ok"
        @app.route("/client")
        def app_client():
            return open("client.html","r").read()
        return app
    def run_app(self, app, addr=['127.0.0.1',5000]):
        app.run(host=addr[0], port=addr[1])

    def run(self, addr=['127.0.0.1',5000]):
        app = self.build_app()
        self.run_app(app, addr=addr)

if __name__ == "__main__":
    mchat = Chatroom()
    mchat.run(addr=['0.0.0.0',8080])


cities = [
    'Absecon',
    'Adelphia',
    'Allamuchy',
    'Allendale',
    'Allenhurst',
    'Allentown',
    'Allenwood',
    'Alloway',
    'Alpine',
    'Andover',
    'Annandale',
    'Asbury',
    'Asbury Park',
    'Atco',
    'Atlantic City',
    'Atlantic Highlands',
    'Audubon',
    'Augusta',
    'Avalon',
    'Avenel',
    'Avon By The Sea',
    'Baptistown',
    'Barnegat',
    'Barnegat Light',
    'Barrington',
    'Basking Ridge',
    'Bass River',
    'Bayonne',
    'Bayville',
    'Beach Haven',
    'Beachwood',
    'Bedminster',
    'Belford',
    'Belle Mead',
    'Belleville',
    'Bellmawr',
    'Belmar',
    'Belvidere',
    'Bergenfield',
    'Berkeley',
    'Berkeley Heights',
    'Berlin',
    'Bernards',
    'Bernardsville',
    'Beverly',
    'Birmingham',
    'Blackwood',
    'Blairstown',
    'Blawenburg',
    'Bloomfield',
    'Bloomingdale',
    'Bloomsbury',
    'Bogota',
    'Boonton',
    'Bordentown',
    'Bound Brook',
    'Bradley Beach',
    'Branchville',
    'Brick',
    'Bridgeport',
    'Bridgeton',
    'Bridgewater',
    'Brielle',
    'Brigantine',
    'Broadway',
    'Brookside',
    'Browns Mills',
    'Budd Lake',
    'Buena',
    'Burlington',
    'Butler',
    'Buttzville',
    'Caldwell',
    'Califon',
    'Camden',
    'Cape May',
    'Cape May Court House',
    'Cape May Point',
    'Carlstadt',
    'Carteret',
    'Cedar Brook',
    'Cedar Grove',
    'Cedar Knolls',
    'Cedarville',
    'Changewater',
    'Chatham',
    'Chatsworth',
    'Cherry Hill',
    'Chester',
    'Clark',
    'Clarksboro',
    'Clarksburg',
    'Clayton',
    'Clementon',
    'Cliffside Park',
    'Cliffwood',
    'Clifton',
    'Clinton',
    'Closter',
    'Collingswood',
    'Cologne',
    'Colonia',
    'Colts Neck',
    'Columbia',
    'Columbus',
    'Cookstown',
    'Cranbury',
    'Cranford',
    'Cream Ridge',
    'Cresskill',
    'Crosswicks',
    'Dayton',
    'Deal',
    'Deepwater',
    'Deerfield Street',
    'Delaware',
    'Delmont',
    'Demarest',
    'Dennisville',
    'Denville',
    'Dividing Creek',
    'Dorchester',
    'Dorothy',
    'Dover',
    'Downe',
    'Dumont',
    'Dunellen',
    'East Amwell',
    'East Brunswick',
    'East Hanover',
    'East Orange',
    'East Rutherford',
    'Eatontown',
    'Edgewater',
    'Edison',
    'Egg Harbor City',
    'Egg Harbor Township',
    'Elizabeth',
    'Elmer',
    'Elmwood Park',
    'Elwood',
    'Elwood-Magnolia',
    'Emerson',
    'Englewood',
    'Englewood Cliffs',
    'Englishtown',
    'Essex Fells',
    'Estell Manor',
    'Ewan',
    'Fair Haven',
    'Fair Lawn',
    'Fairfield',
    'Fairton',
    'Fairview',
    'Fanwood',
    'Far Hills',
    'Farmingdale',
    'Flagtown',
    'Flanders',
    'Flemington',
    'Florence',
    'Florham Park',
    'Fords',
    'Forked River',
    'Fort Lee',
    'Fort Monmouth',
    'Fortescue',
    'Franklin',
    'Franklin Lakes',
    'Franklin Park',
    'Franklinville',
    'Freehold',
    'Frenchtown',
    'Garfield',
    'Garwood',
    'Gibbsboro',
    'Gibbstown',
    'Gillette',
    'Gladstone',
    'Glassboro',
    'Glasser',
    'Glen Gardner',
    'Glen Ridge',
    'Glen Rock',
    'Glendora',
    'Glenwood',
    'Gloucester City',
    'Goshen',
    'Great Meadows',
    'Great Meadows-Vienna',
    'Green Creek',
    'Green Village',
    'Greendell',
    'Greenwich',
    'Grenloch',
    'Hackensack',
    'Hackettstown',
    'Haddon Heights',
    'Haddonfield',
    'Hainesport',
    'Haledon',
    'Hamburg',
    'Hammonton',
    'Hampton',
    'Hancocks Bridge',
    'Hardyston',
    'Harrington Park',
    'Harrison',
    'Harrisonville',
    'Hasbrouck Heights',
    'Haskell',
    'Haworth',
    'Hawthorne',
    'Hazlet',
    'Heislerville',
    'Helmetta',
    'Hewitt',
    'Hibernia',
    'High Bridge',
    'Highland Lakes',
    'Highland Park',
    'Highlands',
    'Hightstown',
    'Hillsdale',
    'Hillside',
    'Ho Ho Kus',
    'Ho-Ho-Kus',
    'Hoboken',
    'Holmdel',
    'Hopatcong',
    'Hope',
    'Hopewell',
    'Howell',
    'Imlaystown',
    'Ironia',
    'Irvington',
    'Iselin',
    'Island Heights',
    'Jackson',
    'Jamesburg',
    'Jersey City',
    'Jobstown',
    'Johnsonburg',
    'Juliustown',
    'Keansburg',
    'Kearny',
    'Keasbey',
    'Kendall Park',
    'Kenilworth',
    'Kenvil',
    'Keyport',
    'Kingston',
    'Lafayette',
    'Lake Hiawatha',
    'Lake Hopatcong',
    'Lakehurst',
    'Lakewood',
    'Lambertville',
    'Landing',
    'Landisville',
    'Lanoka Harbor',
    'Lavallette',
    'Lawnside',
    'Layton',
    'Lebanon',
    'Ledgewood',
    'Leeds Point',
    'Leesburg',
    'Leonardo',
    'Leonia',
    'Liberty Corner',
    'Lincoln Park',
    'Lincroft',
    'Linden',
    'Linwood',
    'Little Falls',
    'Little Ferry',
    'Little Silver',
    'Little York',
    'Livingston',
    'Lodi',
    'Long Branch',
    'Long Valley',
    'Longport',
    'Lumberton',
    'Lyndhurst',
    'Lyons',
    'Madison',
    'Magnolia',
    'Mahwah',
    'Malaga',
    'Manahawkin',
    'Manasquan',
    'Mantoloking',
    'Mantua',
    'Manville',
    'Maple Shade',
    'Maplewood',
    'Margate City',
    'Marlboro',
    'Marlton',
    'Marmora',
    'Martinsville',
    'Matawan',
    'Maurice River',
    'Mauricetown',
    'Mays Landing',
    'Maywood',
    'McAfee',
    'Medford',
    'Mendham',
    'Merchantville',
    'Metuchen',
    'Mickleton',
    'Middlesex',
    'Middletown',
    'Middleville',
    'Midland Park',
    'Milford',
    'Millburn',
    'Millington',
    'Millstone',
    'Milltown',
    'Millville',
    'Milmay',
    'Mine Hill',
    'Minotola',
    'Mizpah',
    'Monmouth Beach',
    'Monmouth Junction',
    'Monroeville',
    'Montague',
    'Montclair',
    'Montvale',
    'Montville',
    'Moonachie',
    'Moorestown',
    'Morganville',
    'Morris Plains',
    'Morristown',
    'Mount Arlington',
    'Mount Ephraim',
    'Mount Freedom',
    'Mount Holly',
    'Mount Laurel',
    'Mount Royal',
    'Mount Tabor',
    'Mountain Lakes',
    'Mountainside',
    'Mullica Hill',
    'National Park',
    'Navesink',
    'Neptune',
    'Neshanic Station',
    'Netcong',
    'New Brunswick',
    'New Egypt',
    'New Gretna',
    'New Lisbon',
    'New Milford',
    'New Providence',
    'New Vernon',
    'New York',
    'Newark',
    'Newfield',
    'Newfoundland',
    'Newport',
    'Newton',
    'Newtonville',
    'Norma',
    'Normandy Beach',
    'North Arlington',
    'North Bergen',
    'North Brunswick',
    'Northfield',
    'Northvale',
    'Norwood',
    'Nutley',
    'Oak Ridge',
    'Oakhurst',
    'Oakland',
    'Oaklyn',
    'Ocean City',
    'Ocean Gate',
    'Ocean Grove',
    'Ocean View',
    'Oceanport',
    'Oceanville',
    'Ogdensburg',
    'Old Bridge',
    'Oldwick',
    'Oradell',
    'Orange',
    'Oxford',
    'Palisades Park',
    'Palmyra',
    'Paramus',
    'Park Ridge',
    'Parlin',
    'Parsippany',
    'Passaic',
    'Paterson',
    'Paulsboro',
    'Peapack',
    'Peapack and Gladstone',
    'Pedricktown',
    'Pemberton',
    'Pennington',
    'Penns Grove',
    'Pennsauken',
    'Pennsville',
    'Pequannock',
    'Perrineville',
    'Perth Amboy',
    'Phillipsburg',
    'Picatinny Arsenal',
    'Pine Beach',
    'Pine Brook',
    'Piscataway',
    'Pitman',
    'Pittstown',
    'Plainfield',
    'Plainsboro',
    'Pleasantville',
    'Pluckemin',
    'Point Pleasant Beach',
    'Pomona',
    'Pompton Lakes',
    'Pompton Plains',
    'Port Elizabeth',
    'Port Monmouth',
    'Port Murray',
    'Port Norris',
    'Port Reading',
    'Port Republic',
    'Pottersville',
    'Princeton',
    'Princeton Junction',
    'Quakertown',
    'Quinton',
    'Rahway',
    'Ramsey',
    'Rancocas',
    'Randolph',
    'Raritan',
    'Readington',
    'Red Bank',
    'Richland',
    'Richwood',
    'Ridgefield',
    'Ridgefield Park',
    'Ridgewood',
    'Ringoes',
    'Ringwood',
    'Rio Grande',
    'River Edge',
    'Riverdale',
    'Riverside',
    'Riverton',
    'Rochelle Park',
    'Rockaway',
    'Rocky Hill',
    'Roebling',
    'Roosevelt',
    'Roseland',
    'Roselle',
    'Roselle Park',
    'Rosemont',
    'Rosenhayn',
    'Rumson',
    'Runnemede',
    'Rutherford',
    'Saddle Brook',
    'Saddle River',
    'Salem',
    'Sayreville',
    'Schooleys Mountain',
    'Scotch Plains',
    'Sea Girt',
    'Sea Isle City',
    'Seaside Heights',
    'Seaside Park',
    'Secaucus',
    'Sergeantsville',
    'Sewaren',
    'Sewell',
    'Shiloh',
    'Short Hills',
    'Shrewsbury',
    'Sicklerville',
    'Skillman',
    'Somerdale',
    'Somers Point',
    'Somerset',
    'Somerville',
    'South Amboy',
    'South Bound Brook',
    'South Dennis',
    'South Hackensack',
    'South Orange',
    'South Plainfield',
    'South River',
    'South Seaville',
    'Sparta',
    'Spotswood',
    'Spring Lake',
    'Springfield',
    'Stanhope',
    'Stanton',
    'Stewartsville',
    'Stillwater',
    'Stirling',
    'Stockholm',
    'Stockton',
    'Stone Harbor',
    'Stratford',
    'Strathmere',
    'Succasunna',
    'Summit',
    'Sussex',
    'Swartswood',
    'Swedesboro',
    'Teaneck',
    'Tenafly',
    'Tennent',
    'Teterboro',
    'Thorofare',
    'Three Bridges',
    'Titusville',
    'Toms River',
    'Totowa',
    'Towaco',
    'Tranquility',
    'Trenton',
    'Tuckahoe',
    'Tuckerton',
    'Union',
    'Union City',
    'Vauxhall',
    'Ventnor City',
    'Vernon',
    'Verona',
    'Vienna',
    'Villas',
    'Vincentown',
    'Vineland',
    'Voorhees',
    'Waldwick',
    'Wallington',
    'Wallpack Center',
    'Wanaque',
    'Waretown',
    'Warren',
    'Washington',
    'Watchung',
    'Waterford Works',
    'Wayne',
    'Wenonah',
    'West Berlin',
    'West Creek',
    'West Long Branch',
    'West Milford',
    'West New York',
    'West Orange',
    'West Paterson',
    'Westfield',
    'Westville',
    'Westwood',
    'Wharton',
    'Whippany',
    'Whitehouse',
    'Whitehouse Station',
    'Whitesboro',
    'Whiting',
    'Wickatunk',
    'Wildwood',
    'Williamstown',
    'Willingboro',
    'Windsor',
    'Winslow',
    'Wood Ridge',
    'Woodbine',
    'Woodbridge',
    'Woodbury',
    'Woodbury Heights',
    'Woodcliff Lake',
    'Woodstown',
    'Wrightstown',
    'Wyckoff'
]


import os
import subprocess

import requests
from constants import (
    BUMPVERSION_CFG,
    COMMIT_TYPE,
    CURRENT_BRANCH,
    GH_AUTH_HEADERS,
    PROJECT_GIT_DIR,
    PROJECT_ROOT,
    REPO_NAME,
    REPO_OWNER,
)

from scenario_player import __version__

CI_CONFIG_DIR = os.environ["CI_CONFIG_DIR"]

print(f"Bumping branch {CURRENT_BRANCH}..")

if COMMIT_TYPE == "VERSION_BUMP":
    print("This is already a version bump - skip bumping.")
    exit()

def get_last_tag():
    return subprocess.run(
        "git describe --tags".split(" "), check=True, stdout=subprocess.PIPE
    ).stdout.decode("UTF-8")

part = ""
bump_release_type = None
if CURRENT_BRANCH in ("dev", "release"):
    part = "iteration"
    # Make sure we set the correct release type.
    if CURRENT_BRANCH == "dev" and "dev" not in __version__:
        bump_release_type = "dev"
    elif CURRENT_BRANCH == "release" and "rc" not in __version__:
        bump_release_type = "rc"

elif CURRENT_BRANCH == "master":
    # Get all commits of the release branch that was merged. If no release
    # branch exists, we assume this was a hotfix merge.
    resp = requests.get(
        f"https://api.github.com/repos/{REPO_OWNER}/{REPO_NAME}/branches", headers=GH_AUTH_HEADERS
    )
    branches = [branch["name"] for branch in resp.json()]
    print("Available branches: ", branches)
    part = "patch"

    if "release" in branches and COMMIT_TYPE == "RELEASE":
        print("Found a 'release' branch, checking for feature commits..")
        process_output = subprocess.run(
            f"git --git-dir={PROJECT_GIT_DIR} log {CURRENT_BRANCH}~1..origin/release --format=%s".split(
                " "
            ),
            check=True,
            stdout=subprocess.PIPE,
        )
        stdout_decoded = process_output.stdout.decode("UTF-8")
        for line in stdout_decoded.split("\n"):
            if "FEAT" in line:
                print("Feature commit detected:\n{line}\nChanging bump type to 'minor'..")
                part = "minor"
                break

print(f"Bumping part {part}..")

if bump_release_type:
    r = subprocess.run(
        f"bumpversion --config-file={BUMPVERSION_CFG} --current-version={__version__} release_type".split(
            " "
        ),
        check=True,
        stdout=subprocess.PIPE,
    )
else:
    r = subprocess.run(
        f"bumpversion --config-file={BUMPVERSION_CFG} --current-version={__version__} {part}".split(
            " "
        ),
        check=True,
        stdout=subprocess.PIPE,
    )

print("Push Bump commit..")
subprocess.run(
    f"git --git-dir={PROJECT_GIT_DIR} push --set-upstream origin {CURRENT_BRANCH}".split(" "),
    check=True,
)

print("Push Bump tag..")
subprocess.run(f"git --git-dir={PROJECT_GIT_DIR} push -u --tags origin".split(" "), check=True)


"""Build nodes to intersect a ray from a transform to a plane defined by a second transform.

Based on tutorial from Marco D'ambros:
https://vimeo.com/59896276

"""

from brenmy.build import bmCmdsBuild

class BmPlaneIntersectorBuild(bmCmdsBuild.BmCmdsBuildBase):
    def __init__(self, name, plane_transform, ray_transform):
        super(BmPlaneIntersectorBuild, self).__init__(name)

        self._plane_transform = plane_transform
        self._ray_transform = ray_transform

        self._create_output_transform = True
        self._attribute_node = None
        self._ray_vector = [0,0,1]
        self._plane_normal = [0, 1, 0]

    def create_output_transform(self):
        return self._create_output_transform

    def set_create_output_transform(self, value):
        self._create_output_transform = int(value)

    def plane_normal(self):
        return self._plane_normal

    def set_plane_normal(self, value):
        """TODO check value"""
        self._plane_normal = value

    def ray_vector(self):
        return self._ray_vector

    def set_ray_vector(self, value):
        """TODO check value"""
        self._ray_vector = value

    def attribute_node(self):
        return self._attribute_node

    def set_attribute_node(self, value):
        self._attribute_node = value

    def build_output_transform(self):
        pass

    def _create_nodes(self):
        # create nodes
        self._origin_decomp = self.create_node("origin", "decomposeMatrix")
        self._plane_decomp = self.create_node("plane", "decomposeMatrix")

        self._origin_ray_vector_product = self.create_node("originRay", "vectorProduct")
        self._plane_vector_product = self.create_node("planeNormal", "vectorProduct")

        # vector from origin to plane transform
        self._origin_plane_vector = self.create_node("originPlaneVector", "plusMinusAverage")

        # dot product between plane normal and origin plane vector
        self._op_pn_dot_product = self.create_node("opPnDot", "vectorProduct")

        # dot product between ray vector and plane normal
        self._ray_plane_dot_product = self.create_node("rayPlaneDot", "vectorProduct")

        # check if ray and plane are parallel to avoid dividing by zero
        self._angle_check_condition = self.create_node("angleCheck", "condition")

        # divide above dot products to give us the distance from origin to plane along ray
        self._ray_dot_divide = self.create_node("rayDotDivide", "multiplyDivide")

        # multiply normalized ray with divide result
        self._intersect_mult = self.create_node("intersectMult", "multiplyDivide")

        # offset intersect result by origin
        self._intersect_offset = self.create_node("intersectOffset", "plusMinusAverage")

    def _set_node_values(self):
        self.set_attr(self._origin_ray_vector_product, "input1", *self._ray_vector)
        self.set_attr(self._origin_ray_vector_product, "normalizeOutput", True)
        self.set_attr(self._origin_ray_vector_product, "operation", 3) # vecto matrix mult

        self.set_attr(self._plane_vector_product, "input1", *self._plane_normal)
        self.set_attr(self._plane_vector_product, "normalizeOutput", True)
        self.set_attr(self._plane_vector_product, "operation", 3)  # vecto matrix mult

        self.set_attr(self._origin_plane_vector, "operation", 2) # subtract

        self.set_attr(self._angle_check_condition, "operation", 0) # equal
        self.set_attr(self._angle_check_condition, "colorIfTrue", 1, 0, 0)
        self.set_attr(self._angle_check_condition, "colorIfFalse", 2, 0, 0)

    def _connect_nodes(self):
        self.connect_attr(self._ray_transform, "worldMatrix[0]", self._origin_decomp, "inputMatrix")
        self.connect_attr(self._ray_transform, "worldMatrix[0]", self._origin_ray_vector_product, "matrix")

        self.connect_attr(self._plane_transform, "worldMatrix[0]", self._plane_decomp, "inputMatrix")
        self.connect_attr(self._plane_transform, "worldMatrix[0]", self._plane_vector_product, "matrix")

        self.connect_attr(self._plane_decomp, "outputTranslate", self._origin_plane_vector, "input3D[0]")
        self.connect_attr(self._origin_decomp, "outputTranslate", self._origin_plane_vector, "input3D[1]")

        self.connect_attr(self._origin_plane_vector, "output3D", self._op_pn_dot_product, "input1")
        self.connect_attr(self._plane_vector_product, "output", self._op_pn_dot_product, "input2")

        self.connect_attr(self._origin_ray_vector_product, "output", self._ray_plane_dot_product, "input1")
        self.connect_attr(self._plane_vector_product, "output", self._ray_plane_dot_product, "input2")

        self.connect_attr(self._ray_plane_dot_product, "outputX", self._angle_check_condition, "firstTerm")

        self.connect_attr(self._angle_check_condition, "outColorR", self._ray_dot_divide, "operation")
        self.connect_attr(self._op_pn_dot_product, "output", self._ray_dot_divide, "input1")
        self.connect_attr(self._ray_plane_dot_product, "output", self._ray_dot_divide, "input2")

        self.connect_attr(self._origin_ray_vector_product, "output", self._intersect_mult, "input1")
        self.connect_attr(self._ray_dot_divide, "output", self._intersect_mult, "input2")

        self.connect_attr(self._origin_decomp, "outputTranslate", self._intersect_offset, "input3D[0]")
        self.connect_attr(self._intersect_mult, "output", self._intersect_offset, "input3D[1]")

    def build(self):
        res = super(BmPlaneIntersectorBuild, self).build()
        return res


import math
import random
import time

def exercise_1():
    def is_prime(number):
        if number is 1:
            return "complex"

        if number % 2 is 0:
            return "complex"

        end_condition = int(math.sqrt(number)) + 1

        for i in range(3, end_condition, 2):
            if number % i is 0:
                return "complex"
        else:
            return "prime"

    def mersenne_number(number) -> float:
        return 2 ** number - 1

    for i in range(1, 32):
        mersenne = mersenne_number(i)
        print(mersenne, "is " + is_prime(mersenne))

def exercise_2():
    def printCenter(text, width=80):
        print(text.center(width))

    printCenter("Zażółć gęślą jaźń")
    printCenter("Zażółć gęślą jaźń", 70)

def exercise_3():
    def printFx(fx, x_start=-1, x_stop=1, number_of_intervals=10):
        intervals = ((x_stop - x_start) / number_of_intervals)
        for x in range(x_start * 10, x_stop * 10 + 1, int(10 * intervals)):
            x /= 10
            print(x, "-->", fx(x))

    printFx(lambda a: math.sin(a))

def exercise_4():
    def k4():
        return random.randint(1, 4)

    def k8():
        return random.randint(1, 8)

    def k10():
        return random.randint(1, 10)

    def k20():
        return random.randint(1, 20)

    def getDice(text):
        if text == "k4":
            return k4()
        elif text == "k8":
            return k8()
        elif text == "k10":
            return k10()
        elif text == "k20":
            return k20()
        else:
            return 0

    def rollDice(text):
        numbers = text.split("k")
        fun = "k" + numbers[1]
        sum = 0
        if numbers[0].__contains__("e"):
            numb = numbers[0].split("e")
            number = int(numb[0]) * 10 ** int(numb[1])
        else:
            number = int(numbers[0])

        for times in range(0, number):
            sum += getDice(fun)

        return sum

    print("1k4 -->", rollDice("1k4"))
    print("2k10 -->", rollDice("2k10"))
    print("4k8 -->", rollDice("4k8"))
    print("10k4 -->", rollDice("10k4"))
    print("100k4 -->", rollDice("100k4"))
    print("1k7 -->", rollDice("1k7"))
    print("0k4 -->", rollDice("0k4"))
    print("1e5k4 -->", rollDice("1e5k4"))

def exercise_5():
    def credits(*args, **keywords):
        '''
        Funkcja przyjmuje argumenty zwykle "args" oraz kluczowe "keywords"
        Funkcja wypisuje co sekunde tekst zawarty w argumentach.
        Najpierw argumenty zwykle.
        Potem argumenty kluczowe.
        '''

        for string in args:
            print(string.center(100))
            time.sleep(1)

        for key in keywords:
            print((key + ": " + keywords.get(key)).center(100))
            time.sleep(1)

    credits("Cudowniejszy program pokazowy",
            "Powrot Zlego Markiza Cul-de-Sac",
            Scenariusz="Autor",
            Scenografia="Autor",
            Podziekowania="Python")

if __name__ == "__main__":
    print("Zadanie 1")
    exercise_1()
    print("\nZadanie 2")
    exercise_2()
    print("\nZadanie 3")
    exercise_3()
    print("\nZadanie 4")
    exercise_4()
    print("\nZadanie 5")
    exercise_5()


# -*- coding:utf-8 -*-
"""
@contact: adonis_wu@outlook.com
@time: 2019/4/11
"""
__author__ = '🍊 adowu 🍊'
import tensorflow as tf

# -*- coding:utf-8 -*-
"""
@contact: adonis_wu@outlook.com
@time: 2019/4/10
"""
__author__ = '🍊 adowu 🍊'
import tensorflow as tf
import json
import logging
import os

class AdoInput(object):
    def __init__(self, params: dict):
        self.log = logging.getLogger('ado-qa')

        self.p_word_len = params.get('p_word_len')
        self.q_word_len = params.get('q_word_len')

        self.max_char_len = params.get('max_char_len')
        self.params = params
        self.root = params.get('root')
        self.file_pattern = params.get('file_pattern')
        self.batch_size = params.get('batch_size')

        self.pad_ids = params.get('pad_ids')
        self.unk_ids = params.get('unk_ids')

        self.batch_size = params.get('batch_size')

    def get_data_dir(self, mode):

        if mode == tf.estimator.ModeKeys.TRAIN:
            sub = 'train'

        elif mode == tf.estimator.ModeKeys.EVAL:
            sub = 'eval'

        else:
            sub = 'test'

        return os.path.join(self.root, sub, self.file_pattern)

    def input_fn(self, mode, vocabs_words: dict = None, vocabs_chars: dict = None):
        files = self.get_data_dir(mode)

        filenames = tf.gfile.Glob(files)

        ds = tf.data.TextLineDataset(filenames)

        def word2id(sequences: list) -> tuple:
            """将 word 和char 均转为 ids"""
            words = [vocabs_words.get(each, self.unk_ids) for each in sequences]
            chars = [[vocabs_chars.get(char, self.unk_ids) for char in each] for each in sequences]
            return words, chars

        def pad(words: list, chars: list, max_length: int) -> tuple:
            """pad 或者 truncate 字段，并返回 mask 信息"""
            curr_word_length = len(words)
            if curr_word_length <= max_length:
                word_padded = words + [self.pad_ids] * (max_length - curr_word_length)
                mask = [True] * curr_word_length + [False] * (max_length - curr_word_length)
                chars_padded = []

                for each in chars:
                    curr_length = len(each)
                    if curr_length <= self.max_char_len:
                        chars_padded.append(each + [self.pad_ids] * (self.max_char_len - curr_length))
                    else:
                        chars_padded.append(each[:self.max_char_len])

                for i in range(max_length - curr_word_length):
                    chars_padded.append([self.pad_ids] * self.max_char_len)

            else:
                word_padded = words[:max_length]
                mask = [True] * max_length
                chars_padded = []
                for each in chars:
                    curr_length = len(each)
                    if curr_length <= self.max_char_len:
                        chars_padded.append(each + [self.pad_ids] * (self.max_char_len - curr_length))
                    else:
                        chars_padded.append(each[:self.max_char_len])
                chars_padded = chars_padded[:max_length]

            return word_padded, mask, chars_padded

        def parse(raw):
            wrong = (0, 0, False, 0, 0, False, 0, 0)
            try:
                raw = raw.decode('utf-8')
                sample = json.loads(raw)
            except json.decoder.JSONDecodeError:
                return wrong

            if mode == tf.estimator.ModeKeys.TRAIN:
                if len(sample['answer_spans']) == 0:
                    return wrong

                if sample['answer_spans'][0][1] >= self.p_word_len:
                    return wrong

            words, chars = word2id(sample['segmented_question'])
            q_words_inputs, q_mask, q_chars_inputs = pad(words, chars, self.q_word_len)
            try:
                doc_index = sample['answer_docs'][0]
            except IndexError:
                return wrong

            if mode != tf.estimator.ModeKeys.PREDICT:
                #   和 baseline 中对于每一个doc 都生成一条数据不一样，一个问题只有一组数据
                doc = sample['documents'][doc_index]
                most_related_para = doc['most_related_para']
                paragraphs = doc['segmented_paragraphs'][most_related_para]
                words, chars = word2id(paragraphs)
                p_words_inputs, p_mask, p_chars_inputs = pad(words, chars, self.p_word_len)
                start = sample['answer_spans'][0][0]
                end = sample['answer_spans'][0][1]
                print(end)
                return q_chars_inputs, q_words_inputs, q_mask, p_chars_inputs, p_words_inputs, p_mask, start, end
            else:
                #   对于预测数据， 按照生成时候的逻辑只选取得分最高的那条数据作为paragraphs。
                paras = []
                question_tokens = sample['segmented_question']

                for doc in sample['documents']:
                    #   在预测的时候，是没有 most_related_para 字段，所以根据共有词来排序 paragraphs
                    for paragraphs_tokens in doc['segmented_paragraphs']:
                        common_with_question = Counter(paragraphs_tokens) & Counter(question_tokens)
                        correct_preds = sum(common_with_question.values())
                        recall = 0 if correct_preds == 0 else float(correct_preds) / len(question_tokens)
                        paras.append((paragraphs_tokens, recall, len(paragraphs_tokens)))

                paras.sort(key=lambda x: (-x[1], x[2]))
                paragraphs_tokens = paras[0]
                words, chars = word2id(paragraphs_tokens)
                p_words_inputs, p_mask, p_chars_inputs = pad(words, chars, self.p_word_len)

            return q_chars_inputs, q_words_inputs, q_mask, p_chars_inputs, p_words_inputs, p_mask

        if mode != tf.estimator.ModeKeys.PREDICT:

            ds = ds \
                .repeat(None if mode == tf.estimator.ModeKeys.TRAIN else 1) \
                .shuffle(buffer_size=self.batch_size * 10) \
                .map(lambda line: tf.py_function(
                parse,
                inp=[line],
                Tout=[tf.int64,
                      tf.int64,
                      tf.bool,
                      tf.int64,
                      tf.int64,
                      tf.bool,
                      tf.int64,
                      tf.int64]
            )
                     ) \
                .map(
                lambda q_chars_inputs, q_words_inputs, q_mask, p_chars_inputs, p_words_inputs, p_mask, start, end: (
                    {'q_chars_inputs': tf.reshape(q_chars_inputs, [self.q_word_len, self.max_char_len]),
                     'q_words_inputs': tf.reshape(q_words_inputs, [self.q_word_len]),
                     'q_mask': tf.reshape(q_mask, [self.q_word_len]),
                     'p_chars_inputs': tf.reshape(p_chars_inputs, [self.p_word_len, self.max_char_len]),
                     'p_words_inputs': tf.reshape(p_words_inputs, [self.p_word_len]),
                     'p_mask': tf.reshape(p_mask, [self.p_word_len])},

                    {'start': tf.reshape(start, []),
                     'end': tf.reshape(end, [])}
                )) \
                .batch(self.batch_size) \
                .prefetch(buffer_size=self.batch_size * 10)
        else:
            ds = ds \
                .repeat(1) \
                .shuffle(buffer_size=self.batch_size * 10) \
                .map(lambda line: tf.py_function(parse, inp=[line],
                                                 Tout=[tf.int64, tf.int64, tf.bool, tf.int64, tf.int64, tf.bool])) \
                .map(lambda sample: ({
                {'q_chars_inputs': tf.reshape(sample[0], [self.q_word_len, self.max_char_len]),
                 'q_words_inputs': tf.reshape(sample[0], [self.q_word_len]),
                 'q_mask': tf.reshape(sample[0], [self.q_word_len]),
                 'p_chars_inputs': tf.reshape(sample[0], [self.p_word_len, self.max_char_len]),
                 'p_words_inputs': tf.reshape(sample[0], [self.p_word_len]),
                 'p_mask': tf.reshape(sample[0], [self._word_len])}
            })) \
                .batch(self.batch_size) \
                .prefetch(buffer_size=self.batch_size * 10)

        return ds

def input_test():
    params = {}
    qa = AdoInput(params)
    ds = qa.input_fn(mode=tf.estimator.ModeKeys.TRAIN)

    iterator = ds.make_one_shot_iterator()
    with tf.Session() as sess:
        x = iterator.get_next()
        for i in range(1):
            print(sess.run(x))