from django.shortcuts import get_object_or_404, render
from .models import Product, Brand
from .forms import ProductForm
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
brand = [
    {
        'name': 'vst',
    }
]

def index(request):
    return render(request, "PhoneSaleApp/index.html")

# input: id (param), output: render (request, url, json), JSON: y het model

# chi tiết sản phẩm
def detail(request, product_id):
    pro = Product.objects.get(idProduct = product_id)
    return render(request, "PhoneSaleApp/ProductDetail.html", {"product": pro})

# đăng tải bài đăng mới
def post_product(request):
    if request.method == "GET":
        form = ProductForm()
        context = {"edit": False, "title": "Đăng sản phẩm mới", "button": "Đăng tin"}
        return render(request, "PhoneSaleApp/PostProducts.html", context )
    if request.method == "POST":
        data = {
         "idProduct": request.POST['idProduct'] ,
         "name": request.POST['name'] ,
         "description": request.POST['description'],
         "price": request.POST['price'],
         "numberRemain": request.POST['numberRemain'],
         "avatar": request.FILES['avatar']}
        idBrandProduct = Brand.objects.get(pk=request.POST['brand'])
        pro = Product.objects.create(**data, idBrandProduct = idBrandProduct )
        return render(request, "PhoneSaleApp/index.html")

# update bài đăng
def update_product(request, product_id):
    pro = Product.objects.get(idProduct = product_id)
    if request.method == "GET":
        content = {"edit": True, "title": "Chỉnh sửa bài đăng", "button": "Cập nhập", "product": pro}
        return render(request, "PhoneSaleApp/PostProducts.html", content)
    if request.method == "POST":
        pro.name = request.POST['name']
        pro.description = request.POST['description']
        pro.price = request.POST['price']
        pro.numberRemain = request.POST['numberRemain']
        pro.avatar = request.FILES['avatar']
        pro.idBrandProduct = Brand.objects.get(pk=request.POST['brand'])
        pro.save()
        return render(request, "PhoneSaleApp/index.html")

@csrf_exempt
def delete_product(request, product_id):
    pro = Product.objects.get(idProduct = product_id)
    if request.method == "DELETE":
        pro.delete()
        return JsonResponse({"message":'success'})

# post san pham moi: input: model san pham, output: json{success: true}
# update san pham: input: nhu tren, output nhu tren
# delete: input: id (param), output: json{success: true or false}
# list brand: input: idBrand (param), output: json{model Brand}
# Validation: input: model San Pham, check gia, ten
# gia: >10k, must have, int
# ten san pham: must have >=10 char, unique


from flask import Flask, render_template, request, redirect, url_for
import sqlite3
from flask_sqlalchemy import SQLAlchemy

'''
Red underlines? Install the required packages first:
Open the Terminal in PyCharm (bottom left).

On Windows type:
python -m pip install -r requirements.txt

On MacOS type:
pip3 install -r requirements.txt

This will install the packages from requirements.txt for this project.
'''

app = Flask(__name__)

app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///new-books-collection.db'
db = SQLAlchemy(app)

@app.route('/')
def home():
    all_books = Book.query.all()

    return render_template("index.html",all_books=all_books)

class Book(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    title = db.Column(db.String(255), nullable=False)
    author = db.Column(db.String(255), nullable=False)
    rating = db.Column(db.Float, nullable=False)

with app.app_context():
    db.create_all()

@app.route("/add", methods=["GET","POST"])
def add():
    if request.method == "POST":
        with app.app_context():
            new_book = Book(title=request.form["title"], author=request.form["author"], rating=request.form["rating"])
            db.session.add(new_book)
            db.session.commit()

        return redirect( url_for('home') )

    return render_template("add.html")

@app.route("/edit/<int:book_id>", methods = ["GET","POST"])
def edit_rating(book_id):
    book = db.get_or_404(Book, book_id)
    if request.method == "POST":
        new_rating = request.form.get("new_rating")
        book.rating = float(new_rating)
        db.session.commit()
        return redirect( url_for("home") )

    return render_template("edit.html",book=book)

@app.route("/delete")
def delete_book():
    book_id = request.args.get("book_id")
    book = db.get_or_404(Book, book_id)

    db.session.delete(book)
    db.session.commit()

    return redirect( url_for("home"))

# Create the database schema within the Flask app context

# Create a new entry in the books table

if __name__ == "__main__":
    app.run(debug=True)


import pandas as pd

alumnos = {
    "nombre": ["Juan","Maria","Pedro","Miguel"],
    "edad": [20,19,22,18],
    "carrera":["IN","C","NI","IN"],
    "promedio":[90,85,70,100]
}
df_alumnos = pd.DataFrame(alumnos)

#Busquedas <>
#and - &
#or |
#neg ~
# Tecnica 1. Filtrado de datos
c1=df_alumnos.promedio>80
data_c1= df_alumnos[c1]
print(data_c1)
print("\n")
c2= df_alumnos.promedio>80 & (df_alumnos.carrera== "IN")
data_c2= df_alumnos[c2]
print(data_c2)

print("\n")

#Tecnica2. Busqueda por query
query1_c1= df_alumnos.query("promedio > 80")
print(query1_c1)

query2_c2 = df_alumnos.query("promedio>80 and carrera =='IN'")
print(query2_c2)
#Otra forma
condicion="promedio>80 and carrera =='IN'"
query2_c2 = df_alumnos.query(condicion)


import tensorflow as tf
from models import DCGAN

# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    BATCH_SIZE = 256
    EPOCHS = 50
    NOISE_DIM = 100
    NUMBER_OF_PREDS = 16
    BUFFER_SIZE = 60000

    # We don't have a test. Unsupervised learning
    (train_images, train_labels), (_, _) = tf.keras.datasets.mnist.load_data()

    # Reshaping from 60k x 28 x 28 into 60k x 28 x 28 x 1 because conv2D needs for each image three dims
    train_images = train_images.reshape(train_images.shape[0], 28, 28, 1).astype('float32')

    # Normalizing between -1 and 1 (that's why we have tanh at the end of the generator)
    train_images = (train_images - 127.5) / 127.5

    # tf dataset from the array
    train_dataset = tf.data.Dataset.from_tensor_slices(train_images).shuffle(BUFFER_SIZE).batch(BATCH_SIZE)

    # Defining the model
    GAN = DCGAN(
        image_size=[28, 28, 1],
        latent_dim=NOISE_DIM,
        batch_size=BATCH_SIZE,
        checkpoint_dir='./checkpoints'
    )

    GAN.train(train_dataset, EPOCHS)


import os
import sys
import datetime

import cfg
import db

def set_standard_config():
    global board_count, board_components_count_max
    global mech_number_of_components
    global mech_num_assemblies, mech_assy_components_count_max
    global mech_num_level
    global top_code_count, top_code_components_count_max
    global spare_part_count, spare_part_components_count_max
    global prototype_count, changes_count

    # number of boards
    board_count = 40
    # the board components count is
    # board_components_count_max / 10 + rnd % board_components_count_max
    board_components_count_max = 200

    # number of mechanical components
    mech_number_of_components = 1000

    # number of subassemblies
    mech_num_assemblies = 200
    # the assembly components count is
    # mech_assy_components_count_max / 10 + rnd % mech_assy_components_count_max
    mech_assy_components_count_max = 20
    # maximum depth of bom
    mech_num_level = 7

    # number of top code
    top_code_count = 120
    # the top code components count is
    # top_code_components_count_max / 10 + rnd % top_code_components_count_max
    top_code_components_count_max = 12

    # number of spare parts
    spare_part_count = 120
    # the spare part components count is
    # spare_part_components_count_max / 10 + rnd % spare_part_components_count_max
    spare_part_components_count_max = 12

    # number of prototypes
    prototype_count = 200

    # number of revisions
    changes_count = 200

def set_advance_config():
    global board_count, board_components_count_max
    global mech_number_of_components
    global mech_num_assemblies, mech_assy_components_count_max
    global mech_num_level
    global top_code_count, top_code_components_count_max
    global spare_part_count, spare_part_components_count_max
    global prototype_count, changes_count

    # number of boards
    board_count = 600
    # the board components count is
    # board_components_count_max / 10 + rnd % board_components_count_max
    board_components_count_max = 200

    # number of mechanical components
    mech_number_of_components = 9000

    # number of subassemblies
    mech_num_assemblies = 9000
    # the assembly components count is
    # mech_assy_components_count_max / 10 + rnd % mech_assy_components_count_max
    mech_assy_components_count_max = 20 # 30 ->3500
    # maximum depth of bom
    mech_num_level = 10

    # number of top code
    top_code_count = 800
    # the top code components count is
    # top_code_components_count_max / 10 + rnd % top_code_components_count_max
    top_code_components_count_max = 12

    # number of spare parts
    spare_part_count = 800
    # the spare part components count is
    # spare_part_components_count_max / 10 + rnd % spare_part_components_count_max
    spare_part_components_count_max = 12

    # number of prototypes
    prototype_count = 200

    # number of revisions
    changes_count = 200

    # number of revisions
    changes_count = 3000

cfg.init()

date0 = "2000-01-01"

class MyRandom:
    def __init__(self, seed = 3):
        self._seed = seed
    def get(self):
        # glibc (used by GCC) -- from wikipedia https://en.wikipedia.org/wiki/Linear_congruential_generator
        n = (self._seed *1103515245 +  12345) % (2<<31)
        self._seed = n
        return n

rnd = MyRandom()

dbname="database.sqlite"

def insert_code(c, descr, code, ver='0', iter_=0, default_unit='NR',
                gval1='', gval2='', date=None, date_to=None, drawings=[],
                gval3='', gval4=''):

    if date is None:
        date = date0
    if date_to is None:
        date_to = db.days_to_iso(db.end_of_the_world)

    c.execute("INSERT INTO items(code) VALUES (?)", (
        code,)
    )
    c.execute("SELECT MAX(id) FROM items")
    code_id = c.fetchone()[0]

    c.execute("""INSERT INTO item_revisions(
        descr, code_id, ver,
        iter, default_unit,
        gval1, gval2, gval3, gval4,
        date_from, date_from_days,
        date_to, date_to_days) VALUES (
        ?, ?, ?,
        ?, ?,
        ?, ?, ?, ?,
        ?, ?,
        ?, ?)""",
        (descr, code_id, ver,
         iter_, default_unit,
         gval1, gval2, gval3, gval4,
         date, db.iso_to_days(date),
         date_to, db.iso_to_days(date_to))
    )

    c.execute("SELECT MAX(id) FROM item_revisions")
    rev_id = c.fetchone()[0]

    if len(drawings) > 0:
        for fn in drawings:
            c.execute("""INSERT INTO drawings(
                            code, revision_id, filename, fullpath
                         ) VALUES ( ?, ?, ?, ? )
                      """, (code, rev_id, os.path.basename(fn), os.path.abspath(fn))
            )

    return rev_id, code_id

# create screws
def insert_screws(c):
    cnt=1
    for l in [3, 4, 5, 6, 8, 10, 12, 14, 16, 20, 15, 30, 35]:
        for d in [2, 3, 4, 5, 6, 8, 10, 12]:

            insert_code(c, "SCREW M%dX%d"%(d, l), "710%03d"%(cnt), 0,
                    0, "NR", "M%dX%d"%(d, l), "SCREWS SUPPLIER")
            cnt += 1

# create washers
def insert_washer(c):
    cnt=1
    for d in [2, 3, 4, 5, 6, 8, 10, 12]:
        insert_code(c, "WASHER D%d"%(d), "720%03d"%(cnt), 0,
                0, "NR", "W D%d"%(d, ), "WASHERS SUPPLIER")
        cnt += 1

# create elastic washers
def insert_elastic_washer(c):
    cnt=1
    for d in [2, 3, 4, 5, 6, 8, 10, 12]:
        insert_code(c, "ELASTIC WASHER D%d"%(d, ), "730%03d"%(cnt), 0,
                0, "NR", "EW D%d"%(d, ), "WASHERS SUPPLIER")
        cnt += 1

# create resistors
def insert_resistor(c):
    # from https://www.eeweb.com/tools/resistor-tables/
    # 0.1%, 0.25%, and 0.5% Resistor Table (E192)
    table="""
100 	101 	102 	104 	105 	106 	107 	109 	110 	111 	113 	114
115 	117 	118 	120 	121 	123 	124 	126 	127 	129 	130 	132
133 	135 	137 	138 	140 	142 	143 	145 	147 	149 	150 	152
154 	156 	158 	160 	162 	164 	165 	167 	169 	172 	174 	176
178 	180 	182 	184 	187 	189 	191 	193 	196 	198 	200 	203
205 	208 	210 	213 	215 	218 	221 	223 	226 	229 	232 	234
237 	240 	243 	246 	249 	252 	255 	258 	261 	264 	267 	271
274 	277 	280 	284 	287 	291 	294 	298 	301 	305 	309 	312
316 	320 	324 	328 	332 	336 	340 	344 	348 	352 	357 	361
365 	370 	374 	379 	383 	388 	392 	397 	402 	407 	412 	417
422 	427 	432 	437 	442 	448 	453 	459 	464 	470 	475 	481
487 	493 	499 	505 	511 	517 	523 	530 	536 	542 	549 	556
562 	569 	576 	583 	590 	597 	604 	612 	619 	626 	634 	642
649 	657 	665 	673 	681 	690 	698 	706 	715 	723 	732 	741
750 	759 	768 	777 	787 	796 	806 	816 	825 	835 	845 	856
866 	876 	887 	898 	909 	920 	931 	942 	953 	965 	976 	988
"""
    rows = table.split("\n")
    values = []
    for row in rows:
        row = row.strip()
        if len(row) == 0:
            continue
        values.extend(map(float, row.split("\t")))

    cnt = 1
    for r in values:
        for (k, suffix) in [(100, ""), (10, ""), (1, ""),
                    (100, "k"), (10, "k"), (1, "k"), (100, "M")]:
            insert_code(c,
                    "RESISTOR %.2f%s"%(r / k, suffix), "51%04d"%(cnt), 0,
                    0, "NR", "R%.2f%s"%(r / k, suffix), "RESISTORS SUPPLIER")
            cnt += 1

# create capacitors
def insert_capacitor(c):
# from https://www.rfcafe.com/references/electrical/capacitor-values.htm
#pF 	pF 	pF 	    pF 	    µF 	    µF 	    µF 	    µF 	    µF 	µF  	µF
    table="""
1.0 	10 	100 	1000 	0.01 	0.1 	1.0 	10 	100 	1000 	10,000
1.1 	11 	110 	1100
1.2 	12 	120 	1200
1.3 	13 	130 	1300
1.5 	15 	150 	1500 	0.015 	0.15 	1.5 	15 	150 	1500
1.6 	16 	160 	1600
1.8 	18 	180 	1800
2.0 	20 	200 	2000
2.2 	22 	220 	2200 	0.022 	0.22 	2.2 	22 	220 	2200
2.4 	24 	240 	2400
2.7 	27 	270 	2700
3.0 	30 	300 	3000
3.3 	33 	330 	3300 	0.033 	0.33 	3.3 	33 	330 	3300
3.6 	36 	360 	3600
3.9 	39 	390 	3900
4.3 	43 	430 	4300
4.7 	47 	470 	4700 	0.047 	0.47 	4.7 	47 	470 	4700
5.1 	51 	510 	5100
5.6 	56 	560 	5600
6.2 	62 	620 	6200
6.8 	68 	680 	6800 	0.068 	0.68 	6.8 	68 	680 	6800
7.5 	75 	750 	7500
8.2 	82 	820 	8200
9.1 	91 	910 	9100"""

    cnt = 1
    for row in table.split("\n"):
        row = row.strip()
        if len(row) == 0:
            continue
        col = 0
        for v in row.split("\t"):
            if col > 3:
                suffix = "uF"
            else:
                suffix = "pf"
            v = v.strip()

            insert_code(c, "CAPACITOR %s%s"%(v, suffix), "520%03d"%(cnt), 0,
                    0, "NR", "C%s%s"%(v, suffix), "CAPACITORS SUPPLIER"
            )
            cnt += 1
            col += 1

# create microprocessors
def insert_microprocessor(c):
# from https://en.wikipedia.org/wiki/Microprocessor_chronology
    table = """
1971 	4004 	Intel 	740 kHz 	4 	10 μm 	1 	2,250 	pMOS 	[1]
1972 	PPS-25 	Fairchild 	400 kHz 	4 	  	2 		pMOS 	[2][a]
1972 	μPD700 	NEC 	  	4 	  	1 			[3]
1972 	8008 	Intel 	500 kHz 	8 	10 μm 	1 	3,500 	pMOS
1972 	PPS-4 	Rockwell 	200 kHz 	4 	  	1 		pMOS 	[4][5]
1973 	μCOM-4 	NEC 	2 MHz 	4 	7.5 μm 	1 	2,500 	NMOS 	[6][7][3][1]
1973 	TLCS-12 	Toshiba 	1 MHz 	12 	6 μm 	1 	2,800 silicon gates 	pMOS 	[8][9][1]
1973 	Mini-D 	Burroughs 	1 MHz 	8 	  	1 		pMOS 	[10]
1974 	IMP-8 	National 	715 kHz 	8 	  	3 		pMOS 	[8]
1974 	8080 	Intel 	2 MHz 	8 	6 μm 	1 	6,000 	NMOS
1974 	μCOM-8 	NEC 	2 MHz 	8 	  	1 		NMOS 	[3][1]
1974 	5065 	Mostek 	1.4 MHz 	8 	  	1 		pMOS 	[11]
1974 	μCOM-16 	NEC 	2 MHz 	16 	  	2 		NMOS 	[3][1]
1974 	IMP-4 	National 	500 kHz 	4 	  	3 		pMOS 	[8]
1974 	4040 	Intel 	740 kHz 	4 	10 μm 	1 	3,000 	pMOS
1974 	6800 	Motorola 	1 MHz 	8 	- 	1 	4,100 	NMOS 	[8]
1974 	TMS 1000 	Texas Instruments 	400 kHz 	4 	8 μm 	1 	8,000
1974 	PACE 	National 	  	16 	  	1 		pMOS 	[12][13]
1974 	ISP-8A/500 (SC/MP) 	National 	1 MHz 	8 	  	1 		pMOS
1975 	6100 	Intersil 	4 MHz 	12 	- 	1 	4,000 	CMOS 	[14][15]
1975 	TLCS-12A 	Toshiba 	1.2 MHz 	12 	- 	1 		pMOS 	[1]
1975 	2650 	Signetics 	1.2 MHz 	8 	  	1 		NMOS 	[8]
1975 	PPS-8 	Rockwell 	256 kHz 	8 	  	1 		pMOS 	[8]
1975 	F-8 	Fairchild 	2 MHz 	8 	  	1 		NMOS 	[8]
1975 	CDP 1801 	RCA 	2 MHz 	8 	5 μm 	2 	5,000 	CMOS 	[16][17]
1975 	6502 	MOS Technology 	1 MHz 	8 	- 	1 	3,510 	NMOS (dynamic)
1975 	IMP-16 	National 	715 kHz 	16 	  	5 		pMOS 	[18][1][19]
1975 	PFL-16A (MN 1610) 	Panafacom 	2 MHz 	16 	- 	1 		NMOS 	[1]
1975 	BPC 	Hewlett Packard 	10 MHz 	16 	- 	1 	6,000 (+ ROM) 	NMOS 	[20][21]
1975 	MCP-1600 	Western Digital 	3.3 MHz 	16 	- 	3 		NMOS
1975 	CP1600 	General Instrument 	3.3 MHz 	16 	  	1 		NMOS 	[12][22][23][1]
1976 	CDP 1802 	RCA 	6.4 MHz 	8 	  	1 		CMOS 	[24][25]
1976 	Z-80 	Zilog 	2.5 MHz 	8 	4 μm 	1 	8,500 	NMOS
1976 	TMS9900 	Texas Instruments 	3.3 MHz 	16 	- 	1 	8,000
1976 	8x300 	Signetics 	8 MHz 	8 	  	1 		Bipolar 	[26][27]
1977 	Bellmac-8 (WE212) 	Bell Labs 	2.0 MHz 	8 	5 μm 	1 	7,000 	CMOS
1977 	8085 	Intel 	3.0 MHz 	8 	3 μm 	1 	6,500
1977 	MC14500B 	Motorola 	1.0 MHz 	1 		1 		CMOS
1978 	6809 	Motorola 	1 MHz 	8 	5 μm 	1 	9,000
1978 	8086 	Intel 	5 MHz 	16 	3 μm 	1 	29,000
1978 	6801 	Motorola 	- 	8 	5 μm 	1 	35,000
1979 	Z8000 	Zilog 	- 	16 	- 	1 	17,500
1979 	8088 	Intel 	5 MHz 	8/16[b] 	3 μm 	1 	29,000 	NMOS (HMOS)
1979 	68000 	Motorola 	8 MHz
"""

    cnt = 1
    for row in table.split("\n"):
        row = row.strip()
        if len(row) == 0:
            continue

        (ucode, manufacturer, clock) = row.split("\t")[1:4]

        insert_code(c,
                "MICROPROCESSOR %s %s %s"%(ucode, manufacturer, clock),
                "530%03d"%(cnt), 0,
                0, "NR", "%s AT %s"%(ucode, clock), manufacturer
        )
        cnt += 1

def get_max_by_code(c, pattern):
    c.execute("""SELECT MAX(id)
                 FROM items
                 WHERE code LIKE ?""",
                 (pattern,))
    return c.fetchone()[0]

def get_min_by_code(c, pattern):
    c.execute("""SELECT MIN(id)
                 FROM items
                 WHERE code LIKE ?""",
                 (pattern,))
    return c.fetchone()[0]

def make_assembly(c, ass_rid, children):
    try:
        a = children[0][0]
    except:
        children =[[x, ] for x in children]

    for row in children:
        if len(row) == 1:
            row = list(row) + [1, '', '', '']
        else:
            row = list(row) + ['', '', '']
            row = row[:5]
        child_id, qty, gaval1, gaval2, gaval3 = row
        c.execute("""INSERT INTO assemblies (
                        unit, child_id, revision_id,
                        qty, each, ref,
                        gaval1, gaval2, gaval3
                     ) VALUES (
                        ?, ?, ?,
                        ?, ?, ?,
                        ?, ?, ?
                     )""", (
                        "NR", child_id, ass_rid,
                        qty, 1, '//',
                        gaval1, gaval2, gaval3
                     )
        )

# create boards with drawings
def insert_board(c):

    min_id = get_min_by_code(c, '5%')
    max_id = get_max_by_code(c, '5%')

    # TODO build revision

    # make 15 boards
    for cnt in range(1, board_count):

        ncomponents = rnd.get() % board_components_count_max + \
            board_components_count_max / 10

        code = "61%04d"%(cnt)

        fn1 = "documents/boards/%s_(el)_rev0.txt"%(code)
        open(fn1, "w").write("Type: board drawing\nCode: %s\nNr components: %d\n"%(
            code, ncomponents))

        board_id, _ = insert_code(c,
                "BOARD %d"%(cnt), code, 0,
                0, "NR", "BOARD %d"%(cnt), "BOARDS MANUFACTURER %d"%(cnt %3, ),
                drawings = [fn1]
        )

        # create the board assy
        components_table = []

        #resistor
        did = set()
        for i in range(rnd.get() % 120 + 5):
            while True:
                cid = rnd.get() % (max_id - min_id) + min_id
                if not cid in did:
                    break
            did.add(cid)

            make_assembly(c, board_id, [(cid, (i * cnt % 10) + 1),])

# create mechanical componets with drawings
def insert_mechanical_components(c):
    cnt=1
    for d in range(mech_number_of_components):
        code = "81%04d"%(cnt)

        fn1 = "documents/drawings/%s_(drw)_rev0.txt"%(code)
        open(fn1, "w").write("Type: mechanical drawing\nCode:%s\n"%(
            code, ))

        insert_code(c,
                "MECHANICAL COMPONENT NR.%d"%(d, ), code, 0,
                0, "NR", "", "MECHANICAL SUPPLIER %d"%(d % 4,),
                drawings = [fn1]
        )

        cnt += 1

# create assemblies with drawings and assembling procedure
def insert_mechanical_assemblies(c):

    boards_min_id = get_min_by_code(c, '7%')
    mech_comp_max_id = get_max_by_code(c, '81%')

    # TODO build revision

    # make 120 assemblies
    for cnt in range(mech_num_assemblies):

        if (cnt % int(mech_num_assemblies / mech_num_level)) == 0:
            assy_max_id = get_max_by_code(c, '8%')

        # each components between [boards_min_id, old_mech_max_id] are a
        # valid child

        ncomponents = rnd.get() % mech_assy_components_count_max + \
            mech_assy_components_count_max // 10

        code = "82%04d"%(cnt)

        fn1 = "documents/drawings/%s_(drw)_rev0.txt"%(code)
        open(fn1, "w").write("Type: mechanical drawing\nCode: %s\nNr components: %d\n"%(
            code, ncomponents))
        fn2 = "documents/assembling-procedures/%s_(asm)_rev0.txt"%(code)
        open(fn2, "w").write("Type: assembling procedure\nCode: %s\nNr components: %d\n"%(
            code, ncomponents))

        mech_id, _ = insert_code(c,
                "MECHANICAL ASSEMBLIES %d - LEVEL %d"%(cnt,
                    cnt/(mech_num_assemblies / mech_num_level) + 1), code, 0,
                0, "NR", "", "INTERNAL SUPPLIER",
                drawings = [fn1, fn2]
        )

        did =set()
        for i in range(ncomponents):
            if (i % 2) or (assy_max_id - mech_comp_max_id) < 10:
                while True:
                    cid = (rnd.get() %
                        (mech_comp_max_id - boards_min_id) + boards_min_id)
                    if not cid in did:
                        break
            else:
                # in the begin we have few assemblies
                n = 10
                while n > 0:
                    cid = (rnd.get() %
                        (assy_max_id - mech_comp_max_id) + mech_comp_max_id)
                    if not cid in did:
                        break
                    n -= 1
                if cid in did:
                    continue

            did.add(cid)

            make_assembly(c, mech_id, ((cid, (i * cnt % 10) + 1),))

# create top assemblies with packaging procedure
def insert_top_codes(c):

    mech_min = get_min_by_code(c,'82%')
    mech_max = get_max_by_code(c,'82%')

    # TODO build revision

    for cnt in range(top_code_count):

        ncomponents = rnd.get() % top_code_components_count_max + \
            top_code_components_count_max // 10

        code = "100%03d"%(cnt)

        fn1 = "documents/packaging-procedure/%s_rev0.txt"%(code)
        open(fn1, "w").write("Type: packaging procedure\nCode: %s\nNr components: %d\n"%(
            code, ncomponents))

        top_id, _ = insert_code(c,
                "TOP ASSEMBLY %d"%(cnt, ), code, 0,
                0, "NR", "", "INTERNAL SUPPLIER",
                drawings = [fn1]
        )

        #up to 127 sub assemblies
        did = set()
        for i in range(ncomponents):
            while True:
                cid = (rnd.get() %
                    (mech_max - mech_min + 1) + mech_min)
                if not cid in did:
                    break
            did.add(cid)

            make_assembly(c, top_id, ((cid, (i * cnt % 10) + 1),))

# create spare part with packaging procedure
def insert_spare_parts(c):

    mech_min = get_min_by_code(c,'6%')
    mech_max = get_max_by_code(c,'81%')
    # TODO build revision

    # make 120 top code
    for cnt in range(spare_part_count):

        ncomponents = rnd.get() % spare_part_components_count_max + \
            spare_part_components_count_max // 10

        code = "101%03d"%(cnt)

        fn1 = "documents/packaging-procedure/%s_packaging.txt"%(code)
        open(fn1, "w").write("Type: packaging procedure\nCode: %s\nNr components: %d\n"%(
            code, ncomponents))

        top_id, _ = insert_code(c,
            "SPARE PART %d"%(cnt, ), code, 0,
            0, "NR", "", "INTERNAL SUPPLIER",
            drawings = [fn1]
        )

        ts = datetime.date.fromisoformat(date0).toordinal()
        did = set()
        for i in range(ncomponents):
            while True:
                cid = (rnd.get() %
                    (mech_max - mech_min + 1) + mech_min)
                if not cid in did:
                    break
            did.add(cid)

            make_assembly(c, top_id, ((cid, (i * cnt % 10) + 1),))

def revise_code(c, old_rid, new_date):

        c.execute("""
            SELECT code_id, MAX(iter)
            FROM item_revisions
            WHERE code_id = (
                SELECT code_id
                FROM item_revisions
                WHERE id = ?
            )
            GROUP BY code_id
            """, (old_rid, ))
        (code_id, old_iter) = c.fetchone()

        c.execute("""
            SELECT id, date_from_days, ver
            FROM item_revisions
            WHERE code_id = ?
            AND iter = ?
            """, (code_id, old_iter))

        (latest_rid, old_date_from_days, rev) = c.fetchone()

        #print("Code, ver, iter=", code, ver, iter_)
        if rev == '0':
            rev = 'A'
        else:
            rev = chr(ord(rev)+1)

        new_date_from_days = new_date
        new_date_from = db.days_to_iso(new_date_from_days)

        if new_date_from_days == db.prototype_date:
            new_iter = db.prototype_iter
        else:
            new_iter = old_iter + 1

        c.execute("""
                INSERT INTO item_revisions(
                    code_id,
                    date_from,
                    date_from_days,
                    date_to,
                    ver,
                    iter,
                    note,
                    descr,
                    default_unit,
                    gval1, gval2, gval3, gval4, gval5, gval6, gval7, gval8
                ) SELECT
                    code_id,
                    ?,
                    ?,
                    '',
                    ?,
                    ?,
                    note,
                    descr,
                    default_unit,
                    gval1, gval2, gval3, gval4, gval5, gval6, gval7, gval8
                FROM item_revisions
                WHERE id = ?
            """, (new_date_from, new_date_from_days, rev, new_iter, old_rid))

        old_date_to_days = new_date_from_days-1
        old_date_to = db.days_to_iso(old_date_to_days)
        c.execute("""
            UPDATE item_revisions
            SET date_to=?, date_to_days=?
            WHERE id= ?
        """, (old_date_to, old_date_to_days, latest_rid))

        c.execute("""SELECT MAX(id) FROM item_revisions""")
        new_rid = c.fetchone()[0]

        return (new_rid, rev)

def revise_assembly(c, old_rid, new_date):

    (new_rid, rev) = revise_code(c, old_rid, new_date)

    c.execute("""
        INSERT INTO assemblies(
                    unit, child_id,
                    revision_id,
                    qty, each, ref
        ) SELECT unit, child_id,
                    ?,
                    qty, each, ref
        FROM assemblies
        WHERE revision_id=?
    """, (new_rid, old_rid))

    # TODO: make some changes to the assembly
    return (new_rid, rev)

def change_code(c, code_id, new_date):
    # fetch the latest code revision
    c.execute("""SELECT MAX(iter)
                 FROM item_revisions
                 WHERE code_id=?""", (code_id,))
    (iter_,) = c.fetchone()
    c.execute("""SELECT id
                 FROM item_revisions
                 WHERE code_id=?
                   AND iter=?""", (code_id, iter_))
    (rev_id,) = c.fetchone()
    c.execute("""SELECT code
                 FROM items
                 WHERE id=?""", (code_id,))
    (code,) = c.fetchone()

    if code.startswith("81"):
        # no assembly
        new_id, new_rev = revise_code(c, rev_id, new_date)
        fn1 = "documents/drawings/%s_(drw)_rev%s.txt"%(code, new_rev)
        open(fn1, "w").write("Type: mechanical drawing\nCode:%s\n"%(
            code, ))
        c.execute("""INSERT INTO drawings(
                code, revision_id, filename, fullpath
            ) VALUES ( ?, ?, ?, ? )
                """, (code, new_id, os.path.basename(fn1), os.path.abspath(fn1))
        )

    elif code.startswith("82"):
            # assembly
            new_id, new_rev = revise_assembly(c, rev_id, new_date)
            fn1 = "documents/drawings/%s_(drw)_rev%s.txt"%(code, new_rev)
            open(fn1, "w").write("Type: mechanical drawing\nCode:%s\n"%(
                code, ))
            c.execute("""INSERT INTO drawings(
                    code, revision_id, filename, fullpath
                ) VALUES ( ?, ?, ?, ? )
                    """, (code, new_id, os.path.basename(fn1), os.path.abspath(fn1))
            )
            fn2 = "documents/assembling-procedures/%s_(asm)_rev%s.txt"%(code, new_rev)
            open(fn2, "w").write("Type: assembling procedure\nCode:%s\n"%(
                code, ))
            c.execute("""INSERT INTO drawings(
                    code, revision_id, filename, fullpath
                ) VALUES ( ?, ?, ?, ? )
                    """, (code, new_id, os.path.basename(fn2), os.path.abspath(fn2))
            )

    elif code.startswith("6"):
            new_id, new_rev = revise_assembly(c, rev_id, new_date)
            fn1 = "documents/boards/%s_(el)_rev%s.txt"%(code, new_rev)
            open(fn1, "w").write("Type: board drawing\nCode: %s\n"%(
                code,))
            c.execute("""INSERT INTO drawings(
                    code, revision_id, filename, fullpath
                ) VALUES ( ?, ?, ?, ? )
                    """, (code, new_id, os.path.basename(fn1), os.path.abspath(fn1))
            )

    elif code[0] in "57":
        new_id, new_rev = revise_code(c, rev_id, new_date)
    elif code.startswith("1"):
        print("WARNING: code ", code, " is ignore")
        return
        # topcode
        # update the packaging procedure
        # update the assembly
    else:
        # ??
        print("Code=", code)
        assert(False)

# make some changes
def make_changes(c):
    global date0

    min_id = get_min_by_code(c, '6%')
    max_id = get_max_by_code(c, '82%')

    print("Make few changes")

    new_date = db.iso_to_days(date0)
    # to be coherent with test plan
    if changes_count  == 200:
        dtinc = 10
        new_date = db.iso_to_days(date0)
    else:
        dtinc = 22.0 * 365 / changes_count
        new_date = db.iso_to_days(date0) * 1.0
    for cnt in range(changes_count):

        new_date += dtinc

        code_id = rnd.get() % (max_id - min_id + 1) + min_id

        change_code(c, code_id, int(new_date))

def make_prototype(c):

    min_id = get_min_by_code(c, '5%')
    max_id = get_max_by_code(c, '82%')

    code_ids = set()
    for cnt in range(prototype_count):
        code_id = rnd.get() % (max_id - min_id + 1) + min_id
        code_ids.add(code_id)

    print("Make few prototypes")

    for code_id in code_ids:
        change_code(c, code_id, db.prototype_date)

def xrmdir(path):
        if os.path.isdir(path):
            for fn in os.listdir(path):
                ffn = os.path.join(path, fn)
                xrmdir(ffn)
            os.rmdir(path)
        elif os.path.exists(path):
            os.unlink(path)

# insert non ascii string
def insert_unicode_code(c):
    s="UTF8 TEST HELLO WORLD - 你好世界"
    insert_code(c, s, "TEST-HELLOWORLD", 0,
                0, "NR", "", "")
    insert_code(c, "all lowercase", "TEST-alllowercase", 0,
                0, "NR", "gval1 lower", "gval2 lower")
    insert_code(c, "ALL UPPER CASE", "TEST-ALLUPPERCASE", 0,
                0, "NR", "GVAL1 UPPER", "GVAL2 UPPER")
    insert_code(c, "DEGREE SYMBOL '°'", "TEST-DEGREE", 0,
                0, "NR", "", "")
    insert_code(c, "A-ACUTE 'à'", "TEST-AACUTE", 0,
                0, "NR", "", "")

    code = "TEST-LONG_FN"
    fn = "documents/drawings/%s_(drw)_%s_rev%s.txt"%(code, 'xX' * 20, 0)
    open(fn, "w").write("Long filename\n")
    _, id_long_fn = insert_code(c, "LONG FILENAME CODE", code, 0,
                0, "NR", "", "", drawings=[fn])

    code = "TEST-NORMAL-CODE"
    fn = "documents/drawings/%s_(drw)_rev%s.txt"%(code, 0)
    open(fn, "w").write("Normal filename\n")
    _, id_normal = insert_code(c, "NORMAL CODE", code, 0,
                0, "NR", "", "", drawings=[fn])

    code = "TEST-MISSING-FN"
    fn = "documents/drawings/%s_(drw)_rev%s.txt"%(code, 0)
    _, id_missing_file = insert_code(c, "MISSING FILE", code, 0,
                0, "NR", "", "", drawings=[fn])

    code = "TEST-FILE-TOO-BIG"
    fn = "documents/drawings/%s_(drw)_rev%s.txt"%(code, 0)
    with open(fn, "w") as f:
        f.seek(1024*1024*1024*10);
        f.write(" ");
    _, id_file_too_long = insert_code(c, "FILE TOO BIG", code, 0,
                0, "NR", "", "", drawings=[fn])

    code = "TEST-ASSY-TO-EXPORT"
    arid, _ = insert_code(c, "TEST ASSY TO EXPORT", code, 0,
                0, "NR", "", "")
    make_assembly(c, arid,
        (id_long_fn, id_missing_file, id_file_too_long, id_normal))

def insert_codes_with_date(c):

    aaid, _ = insert_code(c, "TEST-ASS-A", "TEST-ASS-A", 0,
                0, "NR", date='2020-01-01', date_to='2021-01-01')
    _, caid = insert_code(c, "TEST-A", "TEST-A", 0,
                0, "NR", date='2020-01-01')
    _, cbid = insert_code(c, "TEST-B", "TEST-B", 0,
                0, "NR", date='2020-01-01', date_to='2021-01-01')

    make_assembly(c, aaid, [caid, cbid])

def insert_codes_with_loop(c):

    car, cai = insert_code(c, "TEST-LOOP-A", "TEST-LOOP-A")
    cbr, cbi = insert_code(c, "TEST-LOOP-B", "TEST-LOOP-B")
    ccr, cci = insert_code(c, "TEST-LOOP-C", "TEST-LOOP-C")

    make_assembly(c, car,((cbi, 1),))
    make_assembly(c, cbr,((cci, 1),))
    make_assembly(c, ccr,((cai, 1),))

def insert_codes_with_color(c):
    car, _ = insert_code(c, "TEST COLOR A", "TEST-COLOR-A",
                   gval1='COLOR', gval2='A')
    _, cbi = insert_code(c, "TEST COLOR B", "TEST-COLOR-B",
                   gval1='COLOR', gval2='B')
    _, cci = insert_code(c, "TEST COLOR C", "TEST-COLOR-C",
                   gval1='COLOR', gval2='C')
    _, cdi = insert_code(c, "TEST COLOR D", "TEST-COLOR-D",
                   gval1='COLOR', gval2='D')
    _, cei = insert_code(c, "TEST COLOR E", "TEST-COLOR-E",
                   gval1='COLOR', gval2='E')
    cfr, cfi = insert_code(c, "TEST COLOR F", "TEST-COLOR-F",
                   gval1='COLOR', gval2='F')
    _, cgi = insert_code(c, "TEST COLOR G", "TEST-COLOR-G",
                   gval1='COLOR', gval2='G')
    make_assembly(c, car,(
        (cbi, 1, "COLOR", "M"),
        (cci, 1, "COLOR", "N"),
        (cdi, 1, "COLOR", "O"),
        (cei, 10, "COLOR", "P"),
        (cfi, 10, "COLOR", "Q"),
    ))
    make_assembly(c, cfr,(
        (cgi, 1, "COLOR", "R"),
    ))

def build_bom(top, data):
    res = []

    def runner(t):
        res.append(data[t]["code"])
        for k, v in data[t]["deps"].items():
            runner(v["code_id"])

    runner(top)
    return res

def make_big_assemblies(c):

    assemblies_params = [
        # leaf size, bom height
        (7, 3),  # tot     57
        (5, 4),  # tot    156
        (6, 4),  # tot    259
        (7, 4),  # tot    400
        (5, 5),  # tot    781
        (6, 5),  # tot   1555
        (7, 5),  # tot   2801
        (6, 6),  # tot   9331
        (7, 6),  # tot  19608
        (6, 7),  # tot  55987
        (7, 7),  # tot 137257
        (6, 8),  # tot 325923
        (7, 8),  # tot 960800
    ]

    codes_map = dict()
    def build_assy(leaf_size, bom_deep, cnt):
        new_code = "BIG-ASSY-%dx%d-%03d"%(leaf_size, bom_deep, cnt)
        #print("\t", new_code, codes_map)
        if new_code in codes_map:
            arid, acid, count = codes_map[new_code]
            return arid, acid, count

        count = 1

        if bom_deep == 1:
            arid, acid = insert_code(c,
                "BIG ASSY COUNT=%7d, %d X %d"%(count, leaf_size, bom_deep),
                new_code, 0, 0, "NR")
            codes_map[new_code] = arid, acid, count
            return arid, acid, count

        codes_id = []
        cnt2 = 0
        for i in range(leaf_size):
            _, code_id, cnt3 = build_assy(leaf_size, bom_deep - 1, cnt2)
            cnt2 += 1
            count += cnt3
            codes_id.append(code_id)

        arid, acid = insert_code(c,
            "BIG ASSY COUNT=%7d, %d X %d"%(count, leaf_size, bom_deep),
            new_code, 0, 0, "NR")

        make_assembly(c, arid, codes_id)
        codes_map[new_code] = arid, acid, count

        return arid, acid, count

    for leaf_size, bom_deep in  assemblies_params:
        build_assy(leaf_size, bom_deep, 0)

def create_db(show_stat):
    dbtype = cfg.config()["BOMBROWSER"]["db"]
    c = cfg.config()[dbtype.upper()]
    db.init(dbtype, dict(c))
    d = db.DB()
    d.create_db()

    xrmdir("documents")
    os.mkdir("documents")
    os.mkdir("documents/drawings")
    os.mkdir("documents/boards")
    os.mkdir("documents/assembling-procedures")
    os.mkdir("documents/packaging-procedure")

    with db.Transaction(d) as c:
        # 510xxx
        print("Insert resistor 510xxx")
        insert_resistor(c)
        # 520xxx
        print("Insert capacitor 520xxx")
        insert_capacitor(c)
        # 530xxx
        print("Insert uprocessor 530xxx")
        insert_microprocessor(c)

        # 710xxx
        print("Insert screws 710xxx")
        insert_screws(c)
        # 720xxx
        print("Insert washer 720xxx")
        insert_washer(c)
        # 730xxx
        print("Insert elastic washer 730xxx")
        insert_elastic_washer(c)

        # 610xxx
        print("Insert boards 610xxx")
        insert_board(c)

        # 810###
        print("Insert mechanical componets 810xxx")
        insert_mechanical_components(c)

        # 820 #
        print("Insert mechanical assemblies 820xxx")
        insert_mechanical_assemblies(c)

        # 100xxx
        print("Insert top codes 100xxx")
        insert_top_codes(c)

        # 101xxx
        print("Insert spare parts 101xxx")
        insert_spare_parts(c)

        make_changes(c)
        make_prototype(c)

        if False:
            c.executemany("""
                INSERT INTO database_props(name, value)
                VALUES (?, ?)
            """,(
                    ("cfg.template_simple.name", "Simple table"),
                    ("cfg.template_simple.columns", "seq,level,code,indented_code,descr,qty,each,unit,ref,rev,iter,date_from,date_to"),
                    ("cfg.template_simple.captions", "Seq,Level,Code,Code,Description,Q.ty,Each,Unit,Reference,Rev,Iter,From date,To date"),

                    ("cfg.template_all.name", "Full table"),
                    ("cfg.template_all.columns", "seq,level,parent,parent_descr,\"-\",code,indented_code,descr,qty,each,unit,ref,rev,iter,date_from,date_to,gval1,gval2,gval3,gval4,gval5,gval6"),
                    ("cfg.template_all.captions", "Seq,Level,Parent code,Parent descritpion,-,Code,Code,Description,Q.ty,Each,Unit,Reference,Rev,Iter,From date,To date,Supplier#1 PN,Supplier#1 name,Manufacturer#1 PN,Manufacturer#1 name,Manufacturer#2 PN,Manufacturer#2 name"),

                    ("cfg.template_dummy.name", "Dummy table"),
                    ("cfg.template_dummy.columns", "seq,parent,code,descr,qty,each,unit,ref,rev,iter,date_from,date_to"),
                    ("cfg.template_dummy.captions", "Seq,Parent code,Code,Description,Q.ty,Each,Unit,Reference,Rev,Iter,From date,To date")

            ))

        print("Insert unicode codes")
        insert_unicode_code(c)
        print("Insert code with date")
        insert_codes_with_date(c)
        print("Insert assembly with a loop")
        insert_codes_with_loop(c)
        print("Insert assembly with color")
        insert_codes_with_color(c)

        print("Build big assemblies")
        make_big_assemblies(c)

        if show_stat:
            print()
            c.execute("SELECT COUNT(*) FROM items")
            cnt = c.fetchone()[0]
            print("Total number of code=", cnt)

            c.execute("SELECT COUNT(*) FROM item_revisions")
            cnt = c.fetchone()[0]
            print("Total number of revisions=", cnt)

            # we expect that the biggest assembly are inside the last
            # top assemblies
            c.execute("SELECT id FROM items WHERE code LIKE '100%' ORDER BY id ASC")
            codes = [x[0] for x in c.fetchall()][-10:]

    if show_stat:
        cnt = 0
        max_id = -1
        for code_id in codes:
            (top, data) = d.get_bom_by_code_id3(code_id, db.end_of_the_world)
            c = len(build_bom(top, data))
            if c > cnt:
                max_id = code_id
                cnt = c
        print("Maximum bom size=", cnt, "; code_id=", max_id)

def help_():
    print("usage: mkdb.py [--stat] --test-db|--big-db|--help")

def main(args):
    args = sys.argv
    show_stat = False
    i = 1
    if len(args) == 1:
        print("ERROR: missing parameter")
        help_()
        return

    while i < len(args):
        if args[i] == "--test-db":
            set_standard_config()
        elif args[i] == "--big-db":
            set_advance_config()
        elif args[i] == "--stat":
            show_stat = True
        else:
            print("ERROR: unknown parameter")
            help_()
            return
        i += 1

    create_db(show_stat)

main(sys.argv)


import tkinter as tk
from mylib.application import Application

if __name__ == "__main__":
    root = tk.Tk()
    w, h = root.winfo_screenwidth()-10, root.winfo_screenheight()-100
    root.geometry("%dx%d+0+0" % (w, h))
    root.title("Multi Map & Waypoints Merger")
    app = Application(root)
    app.mainloop()


import collections

class Solution:
    def minSetSize(self, arr) -> int:
        ## get the occurrence of each character
        mp = collections.Counter(arr)
        ## sort the occurrence and sort by the most frequent element
        ## (alt) mp = [count for _, count in mp.most_common()]
        mp = sorted(list(mp.items()), key=lambda item: item[1], reverse=True)
        target = len(arr)/2
        ## curr stores the current added number, curr_idx stores the number of different elements added on number
        curr, curr_idx = 0, 0
        for num in mp:
            curr += num[1]
            curr_idx += 1
            ## if current number greater than or equal to the half of the length, return the number of different elements
            if curr >= target:
                return curr_idx


import cv2
import os
import csv
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

import keras
import cv2
import re
import pandas as pd
import ntpath
import random

def img_preprocess(img):
    #img = img[60:135,:,:] #Das Bild kann zugeschnitten werden auf einen bestimmten Bereich
    img = cv2.cvtColor(img, cv2.COLOR_RGB2YUV) #colormap für NVIDIA-Model
    #img = cv2.GaussianBlur(img,  (3, 3), 0) #Blur um Bild zu smoothen
    img = cv2.resize(img, (100, 100)) #Auf richtige Größe für Model bringen
    img = img/255 #Bild normalisieren
    return img

print("start")

# print(len(im))
# print(len(im[0]))
# print(len(im[0][0]))
# print(len(im[0][0][0]))

model = keras.models.load_model("model.h5")

print(model.layers[0].input_shape)
print(model.summary())

dirname = os.path.dirname(__file__)
imagepath = dirname + "/daten/img"
images = os.listdir(imagepath)
print(images)
images.sort(key=lambda f: int(re.sub('\D', '', f)))
print(images)
true_data = np.genfromtxt(("daten/data.csv"), delimiter=',')    # alte csv in Array lesen
true_data = np.delete(true_data, 0, axis=0)     # labels entfernen

steering_plot = []
throttle_plot = []

counter = 0
for image in images:
    im = mpimg.imread("daten/img/" + image)

    im = img_preprocess(im)

    # plt.imshow(im)
    # plt.show()

    im = np.expand_dims(im, axis=0)

    model_output = model.predict(im)

    steering_value = model_output[0][0]
    steering_plot.append(steering_value)
    try:
        print("Image:", image, "steering value:", steering_value, ", true value: ", true_data[counter][3])
        throttle_plot.append(true_data[counter][3])
    except IndexError:
        pass
    counter += 1

plt.plot(steering_plot)
plt.plot(throttle_plot)
plt.legend(["model", "true"])
#plt.plot(throttle_plot)
plt.show()

print("end")


from urllib.request import urlopen
from bs4 import BeautifulSoup
import datetime
import random
import re
import json

random.seed(datetime.datetime.now())

def getLinks(articleUrl):
    try:
        html = urlopen('http://en.wikipedia.org'+articleUrl)
    except:
        return []
    bsObj = BeautifulSoup(html)
    return bsObj.find('div', {'id':'bodyContent'}).find_all('a',
                                                            href=re.compile('^(/wiki/)((?!:).)*$'))

def getHistoryIPs(pageUrl):
    pageUrl = pageUrl.replace('/wiki/', '')
    historyUrl = 'https://en.wikipedia.org/w/index.php?title=' + pageUrl + '&action=history'
    print('history url is: ' + historyUrl)
    try:
        html = urlopen(historyUrl)
    except:
        return set()
    bsObj = BeautifulSoup(html)
    ipAddresses = bsObj.find_all('a', {'class':'mw-anonuserlink'})
    addressList = set()
    for ipAddress in ipAddresses:
        addressList.add(ipAddress.get_text())
    return addressList

def getCountry(ipAddress):
    try:
        response = urlopen('http://freegeoip.net/json/' + ipAddress).read().decode('utf-8')
    except:
        return ''
    responseJson = json.loads(response)
    return responseJson.get('country_code')

if __name__ == '__main__':
    links = getLinks('/wiki/Python_(programming_language)')

    while(len(links) > 0):
        for link in links:
            print('-----------------------------')
            historyIPs = getHistoryIPs(link.attrs['href'])
            for historyIP in historyIPs:
                print(historyIP)
                country = getCountry(historyIP)
                if country != '':
                    print(historyIP + ' is from ' + country)


"""Package allows to extend a chatbot with FIFA World Cup results prediction game"""
from chatbot import Interface, Message

import membank

from chatbot_fifa_extension import fifa, memories
from .exceptions import *

def is_valid_result(result, callback):
    """Validates that result is input correctly"""
    if len(result) != 2:
        callback("Result must be two numbers with ':'(colon) in between")
        callback("If you want to cancel previous result, ask cancel")
        return False
    for i in result[0]:
        if not i.isdigit():
            callback(f"First result must be a number, was {result[0]}")
            return False
    for i in result[1]:
        if not i.isdigit():
            callback(f"Second result must be a number, was {result[1]}")
            return False
    return True

class FIFAGame(Interface):
    """Allows to make predictions for World Cup results"""
    aliases = {"play fifa", "play world cup"}

    def __init__(self, conf):
        Interface.__init__(self, conf)
        if "database_path" not in conf or "administrator" not in conf:
            msg = "FIFAGame requires a database path and administrator in config"
            raise RuntimeError(msg)
        url = f'sqlite://{conf["database_path"]}/db'
        self.mem = membank.LoadMemory(url)
        self._is_complete = False

    def consume(self, package):
        if package.message.text == "admin mode":
            package.conversation.data["admin mode"] = {}
            package.callback("Please identify yourself")
        elif "admin mode" in package.conversation.data:
            self.do_admin(package)
        elif "contest" not in package.conversation.data:
            package.callback("What is your contest code?")
            package.conversation.data["contest"] = None
        elif not package.conversation.data["contest"]:
            if "create contest" in package.message.text:
                package.conversation.data["create contest"] = True
                package.callback("Please state the name of the contest")
            else:
                self.check_contest(package)
        elif "player" not in package.conversation.data:
            self.add_player(package)
        else:
            self.get_bets(package)
            self.mem.put(package.conversation.data["player"])

    def check_contest(self, package):
        """try to find contest otherwise suggest to create one"""
        if "create contest" in package.conversation.data:
            self.create_contest(package)
        else:
            contest = self.mem.get.contest(code=package.message.text)
            if contest:
                package.conversation.data["contest"] = contest
                package.callback("OK. Now please state your name!")
            else:
                package.callback("Such contest does not exist. Try again")
                package.callback("If you want to create new contest call create contest")

    def create_contest(self, package):
        """creates new contest"""
        code = package.message.text
        contest = self.mem.get.contest(code=code)
        if not contest:
            contest = memories.Contest(package.message.text)
            self.mem.put(contest)
            package.callback("OK. New contest created")
        else:
            package.callback("Such contest already exists!")
        package.conversation.data["contest"] = contest
        package.callback("Now please state your name!")

    def add_player(self, package):
        """creates or restores to existing player"""
        player = self.mem.get.player(name=package.message.text)
        if not player:
            player = memories.Player(name=package.message.text)
            self.mem.put(player)
            package.conversation.data["contest"].players.append(player.name)
            self.mem.put(package.conversation.data["contest"])
            package.callback(f"Nice to meet you {player.name}")
        else:
            if player.name not in package.conversation.data["contest"].players:
                package.conversation.data["contest"].players.append(player.name)
                self.mem.put(package.conversation.data["contest"])
            package.callback(f"Welcome back {player.name}")
        package.conversation.data["player"] = player
        player.next_bet = ""
        self.get_bets(package)
        self.mem.put(player)

    # pylint: disable=too-many-branches
    def get_bets(self, package):
        """get all bet scores from the player"""
        admin = self.mem.get.player(name=self.conf["administrator"])
        bet = fifa.WorldCup(package.conversation.data["player"], admin.bets)
        if not bet.player.next_bet:
            if self.conf["administrator"] == package.conversation.data["player"].name:
                bet.load_next_admin_bet()
            else:
                bet.load_next_bet()
            if bet.player.next_bet:
                bet_call = "What will be result between " + bet.player.next_bet + "?"
                package.callback(bet_call)
            else:
                complete = fifa.bets_complete(admin.bets)
                if complete < 48:
                    package.callback("Please wait while group stage ends")
                elif complete < 56:
                    package.callback("Please wait while round 16 ends")
                elif complete < 60:
                    package.callback("Please wait while quarter finals end")
                elif complete < 62:
                    package.callback("Please wait while semi finals end")
                else:
                    champ = fifa.get_knock_win(bet.player.bets[-1])
                    package.callback(f"Congrats {champ} is your World Cup 2022 Champion!")
                    package.callback("Your bets are finalised! Good luck!!!")
                    self._is_complete = True
        else:
            if "cancel" in package.message.text:
                self.cancel_bet(bet, package)
            else:
                result = package.message.text.split(":", maxsplit=2)
                if is_valid_result(result, package.callback):
                    try:
                        bet.add_bet(result)
                        self.get_bets(package)
                    except DrawNotAllowed:
                        package.callback("Draw in knockout stage is not allowed")

    def is_complete(self):
        return self._is_complete

    def cancel_bet(self, bet, package):
        """cancel previous bet"""
        previous = bet.cancel_previous_bet()
        if previous:
            package.callback("OK. Canceled previous bet")
            self.get_bets(package)
        else:
            package.callback("Nothing to cancel. Enter first bet")

    def do_admin(self, package):
        """admin mode handling"""
        admin_conf = package.conversation.data["admin mode"]
        if not admin_conf:
            if package.message.text == self.conf["administrator"]:
                package.callback("You are identified. Commands available")
                admin_conf["command"] = ""
                admin_conf["data"] = {}
            else:
                package.callback("You are not identified. Please identify")
        else:
            if package.message.text == "add players to contest":
                admin_conf["command"] = "add_player_to_contest"
            if package.message.text == "predictions":
                admin_conf["command"] = "predictions"
            if package.message.text == "results":
                admin_conf["command"] = "results"
            if "command" in admin_conf and admin_conf["command"]:
                executor = getattr(self, admin_conf["command"])
                executor(package)
            else:
                package.callback("State your admin command")

    def add_player_to_contest(self, package):
        """add player name to contest"""
        data = package.conversation.data["admin mode"]["data"]
        if "done" == package.message.text:
            package.callback("OK.")
            package.conversation.data["admin mode"] = {}
        elif "contest" not in data:
            package.callback("State name of contest")
            data["contest"] = ""
        elif not data["contest"]:
            data["contest"] = package.message.text
            contest = self.mem.get.contest(code=package.message.text)
            if not contest:
                package.callback(f"Contest {package.message.text} not found")
            else:
                data["contest"] = contest
                package.callback("Contest is {data['contest'].code}")
                package.callback("State name of the player to add")
        else:
            player = self.mem.get.player(name=package.message.text)
            if not player:
                package.callback("Player {package.message.text} does not exist")
            else:
                name = player.name
                if name not in data["contest"].players:
                    data["contest"].players.append(name)
                    self.mem.put(data["contest"])
                package.callback("OK. Added")

    def predictions(self, package):
        """return predictions of all players within contest"""
        data = package.conversation.data["admin mode"]["data"]
        if "contest" not in data:
            package.callback("For which contest?")
            data["contest"] = ""
        elif not data["contest"]:
            contest = package.message.text
            contest = self.mem.get.contest(code=contest)
            if not contest:
                package.callback(f"Contest {package.message.text} not found")
            else:
                admin = self.mem.get.player(name=self.conf["administrator"])
                index = fifa.bets_complete(admin.bets)
                text = self._get_predictions(contest.players, admin.bets, index)
                if 31 < index < 48:
                    text += self._get_predictions(contest.players, admin.bets, index+1)
                package.callback(text.strip())

    def _get_predictions(self, players, bet_results, index):
        """get predictions for players for one game"""
        text = bet_results[index][0] + " "
        for i in players:
            player = self.mem.get.player(name=i)
            if player:
                text += player.name + " "
                if not player.bets[index][1]:
                    text += "missing bets"
                else:
                    text += str(player.bets[index][1][0])
                    text += ":"
                    text += str(player.bets[index][1][1])
                text += " "
        return text

    def results(self, package):
        """return results of all players within contest"""
        data = package.conversation.data["admin mode"]["data"]
        if "contest" not in data:
            package.callback("For which contest?")
            data["contest"] = ""
        elif not data["contest"]:
            contest = package.message.text
            contest = self.mem.get.contest(code=contest)
            if not contest:
                package.callback(f"Contest {package.message.text} not found")
            else:
                admin = self.mem.get.player(name=self.conf["administrator"])
                index = fifa.bets_complete(admin.bets)
                players = []
                for name in contest.players:
                    player = self.mem.get.player(name=name)
                    if player.name != self.conf["administrator"]:
                        players.append(self.mem.get.player(name=name))
                results = fifa.get_player_results(players, admin.bets, index)
                results = sorted(
                    [key + " " + str(value) for key, value in results.items()],
                    key = lambda x: int(x.split(" ", maxsplit=2)[1]),
                    reverse = True,
                )
                package.callback(" ".join(results))


"""added horse to results

Revision ID: 8555471fb164
Revises: e34e91e9d00d
Create Date: 2019-12-13 09:38:08.700589

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '8555471fb164'
down_revision = 'e34e91e9d00d'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_foreign_key(None, 'competition_ranking_association', 'rankinglists', ['rankinglist_id'], ['id'])
    op.create_foreign_key(None, 'competition_ranking_association', 'competitions', ['competition_id'], ['id'])
    op.add_column('results', sa.Column('horse_id', sa.Integer(), nullable=False))
    op.create_foreign_key(None, 'results', 'horses', ['horse_id'], ['id'])
    op.create_foreign_key(None, 'results', 'riders', ['rider_id'], ['id'], ondelete='CASCADE')
    op.create_foreign_key(None, 'results', 'tests', ['test_id'], ['id'])
    op.create_foreign_key(None, 'tests', 'competitions', ['competition_id'], ['id'])
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(None, 'tests', type_='foreignkey')
    op.drop_constraint(None, 'results', type_='foreignkey')
    op.drop_constraint(None, 'results', type_='foreignkey')
    op.drop_constraint(None, 'results', type_='foreignkey')
    op.drop_column('results', 'horse_id')
    op.drop_constraint(None, 'competition_ranking_association', type_='foreignkey')
    op.drop_constraint(None, 'competition_ranking_association', type_='foreignkey')
    # ### end Alembic commands ###


from flask import request
from flask_restx import Resource
from backend.main.service.auth_helper import Auth
from backend.main.service.log_service import WriteException
from backend.main.util.dto import AuthDto

api = AuthDto.api
user_auth = AuthDto.user_auth

@api.route('/login')
class UserLogin(Resource):
    """
        User Login Resource
    """

    @api.doc('user login')
    @api.expect(user_auth, validate=True)
    def post(self):
        # get the post data
        try:
            post_data = request.json
            return Auth.login_user(data=post_data)
        except Exception:
            WriteException()
            response_object = {
                'status': 'error',
                'message': 'Algo salio mal, intentalo mas tarde'
            }
            return response_object, 401


"""
This module contains LabelSmoothingLossFunction, which a target-smoother + KLDivergence
loss function used in the original Transformer model.
"""

import torch
import torch.nn as nn
from torch.autograd import Variable

class LabelSmoothingLossFunction(nn.Module):
    """
    Implementation of label smoothing,
    a technique useful to improve robustness in multi-class classification
    that uses soft max.

    Rather than making the target a specific token_index,
    this loss function creates a probability distribution
    for each observation centered at the target and employs KL Divergence
    to minimize the cross entropy between
    the 2 distributions (predicted probabilites and "smoothed" y distribution).

    Attention (2017) uses smoothing hyper-param == 0.1.

    Design inspired by "Annotated Transformer":
    https://nlp.seas.harvard.edu/2018/04/03/attention.html.
    """

    def __init__(self, vocab_size: int, padding_idx: int, smoothing: float = 0.1):
        """
        Args:
            vocab_size (int): Size of target dictionary.
            padding_idx (int): Index of padding token in the target dictionary.
            smoothing (float): hyper-parameter used in smoothing.
        """
        super(LabelSmoothingLossFunction, self).__init__()
        self._criterion = nn.KLDivLoss(size_average=False)
        self._padding_idx = padding_idx
        self._smoothing = smoothing
        self._vocab_size = vocab_size

    def forward(self, y_hat: torch.Tensor, target: torch.Tensor) -> Variable:
        """
        Main function call of label smoother.
        Args:
            y_hat (torch.Tensor):
                A sequence of (batch_size*max_seq_length,target_vocab_size) probability values.
            target (torch.Tensor):
                A 1D sequence of (batch_size) indicating the token values of the target (one-hot-encoding).

        Returns:
            Variable object containing the loss function.
        """

        # produces a target_smooth distribution that is (batch_size*max_seq_length,target_vocab_size)
        true_dist = self._compute_label_smoothing(target, y_hat)

        # return KL divergence
        return self._criterion(y_hat, Variable(true_dist, requires_grad=False))

    def _compute_label_smoothing(self, target: torch.Tensor, y_hat: torch.Tensor) -> torch.Tensor:
        """

        Args:
            y_hat (torch.Tensor):
                A sequence of (batch_size*max_seq_length,target_vocab_size) probability values.
            target (torch.Tensor):
                A 1D sequence of (batch_size) indicating the token values of the target (one-hot-encoding).

        Returns:
            a target_smooth distribution that is (batch_size*max_seq_length,target_vocab_size)

        """
        assert y_hat.size(1) == self._vocab_size

        # generate matrix of the same distribution as y_hat (copy)
        true_dist = y_hat.data.clone()
        # fill with the smoothing values / vocab_size so that probabilities add to 1.0
        true_dist.fill_(self._smoothing / (self._vocab_size - 2))
        # along dimension 1, for the target indices (one-hot encoding indices), set to peak
        # of distribution which is 1.0 - smoothing_value
        true_dist.scatter_(1, target.data.unsqueeze(1), 1.0 - self._smoothing)
        # make sure the padding index probability is == 0.0
        true_dist[:, self._padding_idx] = 0
        mask = torch.nonzero(target.data == self._padding_idx)
        true_dist.index_fill_(0, mask.squeeze(), 0.0)
        return true_dist


#board = [['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9']]

#def drawGrid(board):
#    print("-------------------")
#    for i in range(3):
#        print("   |", end="")
#        for j in range(3):
#            print(f" {board[i][j]} |", end="")
#        if (i < 2):
#            print("\n   -------------")
#        else:
#            print("\n-------------------")

board = ['1', '2', '3', '4', '5', '6', '7', '8', '9']

def drawGrid(board):
    print("---------------------")
    print(f"  | {board[0]} | {board[1]} | {board[2]} |")
    print("---------------------")
    print(f"  | {board[3]} | {board[4]} | {board[5]} |")
    print("---------------------")
    print(f"  | {board[6]} | {board[7]} | {board[8]} |")
    print("---------------------")

# Ask player for square number to play.
# The number is checked to ascertain that it is within the range of 1 - 9.
def askInput(player):
    value = 0
    while value not in range(1, 10):
        value = int(input(f"{player}, where to place your move? "))

    return value

# Determine if somebody has won.
# Moves played have exceeded 9, then it is a stalemate.
def determineWin(board, movesPlayed):
    #pass
    if movesPlayed >= 9:
        return True
    # Check individual squares here.
    elif board[0] == board[1] and board[1] == board[2]:
        return True
    elif board[3] == board[4] and board[4] == board[5]:
        return True
    elif board[6] == board[7] and board[7] == board[8]:
        return True
    elif board[0] == board[3] and board[3] == board[6]:
        return True
    elif board[1] == board[4] and board[4] == board[7]:
        return True
    elif board[2] == board[5] and board[5] == board[8]:
        return True
    elif board[0] == board[4] and board[4] == board[8]:
        return True
    elif board[2] == board[4] and board[4] == board[6]:
        return True
    else:
        return False

# Ensure turn taking in the game
def swapTurn(turn):
    if turn == 'X':
        return 'O'
    else:
        return 'X'

# int main()
if __name__ == '__main__':

    # Store number of moves played to quit
    movesPlayed = 0
    # check whether a player has won
    wonState = False
    # Start the game by asking for player 1 to take a turn
    while True:
        turn = input("Player 1, choose between X and O: ")
        if (turn == 'X' or turn == 'O'):
            break

    while not wonState:
        # Draw the board they playing on
        drawGrid(board)
        # Get where the current player wants to put his piece
        value = askInput(turn)
        # Mark where the player wants to get marked
        board[value - 1] = turn
        # Count the number of times the game has been played
        movesPlayed += 1
        # Check if any player has won
        wonState = determineWin(board, movesPlayed)
        # Swap turn
        turn = swapTurn(turn)

    turn = swapTurn(turn)

    print(f"\n\nGAME WON by {turn}".format(swapTurn(turn)))
    drawGrid(board)


import json

class JsonToCsvScript:
    output_fp = 'converted_nfts_records.csv'

    data = json.loads(open('metadata.json', encoding='utf-8').read())

    csv_headers = [
        "name", "description", "image", "trait_background", "trait_base_character",
        "trait_clothing", "trait_expression", "trait_accessories"
    ]

    def __init__(self):
        self.csv_writer = self.get_csv_writer()
        self.parse_json_files()

    def parse_json_files(self):
        for rec in self.data["collection"]:
            item = {}
            item["name"] = rec['name'].replace('#', 'GGoose ')
            item["description"] = rec['description']
            item["image"] = rec['image']

            for attr in rec['attributes'] or []:
                if attr['trait_type'] == 'Background':
                    item["trait_background"] = attr['value']
                elif attr['trait_type'] == 'Base Character':
                    item["trait_base_character"] = attr['value']
                elif attr['trait_type'] == 'Clothing':
                    item["trait_clothing"] = attr['value']
                elif attr['trait_type'] == 'Expression':
                    item["trait_expression"] = attr['value']
                elif attr['trait_type'] == 'Accessories':
                    item["trait_accessories"] = attr['value']

            self.write_to_csv(item)

    def write_to_csv(self, item):
        row = ','.join('"{}"'.format(item.get(h) or '') for h in self.csv_headers) + '\n'
        self.csv_writer.write(row)
        print("Record inserted into CSV file.")
        print(item)

    def get_csv_writer(self):
        file = open(self.output_fp, mode='w', encoding='utf-8')
        file.write(','.join(h for h in self.csv_headers) + '\n')
        return file

if __name__ == "__main__":
    JsonToCsvScript()


import os
import sys
import json
from .base_data import BaseData

class ExampleModel(BaseData):
    def __init__(self):
        super(ExampleModel, self).__init__()
        self.__weights = []
        self.factory[self.__class__.__name__] = self

    def load(self, path):
        super(ExampleModel, self).load(path)
        self.__weight = list(map(float, self._data[0].rstrip('\n').split(',')))

    def query(self, key, value):
        qf, df = key['q_vec'], key['d_vec']
        score = []
        for i in range(len(df)):
            s = sum([qq * dd for qq, dd in zip(qf, df[i])])
            score.append(s)
        return score


import pandas as pd
import sqlite3

conn = sqlite3.connect('parser.sqlite')
cur = conn.cursor()

df = pd.read_excel('test_task.xlsx')

df = df.drop(labels=[0, 1])
df = df.rename(columns={
    'fact': 'data1(Qliq(fact))',
    'Unnamed: 3': 'data2(Qliq(fact))',
    'Unnamed: 4': 'data1(Qoil(fact))',
    'Unnamed: 5': 'data2(Qoil(fact))',
    'forecast': 'data1(Qliq(forecast))',
    'Unnamed: 7': 'data2(Qliq(forecast))',
    'Unnamed: 8': 'data1(Qoil(forecast))',
    'Unnamed: 9': 'data2(Qoil(forecast))'
})

df.to_sql(name='Table1', con=conn, index=False)


from typing import Dict, Any

from terraformpy import Provider, Terraform

from qhub_tf.utils import ResourceCollection, require_environment_variables
from qhub_tf.schema import QHubConfig
from qhub_tf.modules.gcp.network import Network
from qhub_tf.modules.gcp.registry import ContainerRegistry
from qhub_tf.modules.gcp.kubernetes import KubernetesCluster

class Infrastructure(ResourceCollection):
    qhub_config: QHubConfig

    def create_resources(self):
        require_environment_variables([
            'PROJECT_ID',
            'GOOGLE_CREDENTIALS',
        ])

        Terraform(
            required_providers={
                "google": {
                    "source": "hashicorp/google",
                    "version": "~> 4.0",
                }
            }
        )

        with Provider('google', alias='infrastructure'):
            Network(
                name="atest-deleteme",
                region="us-east1",
            )

            ContainerRegistry()

            KubernetesCluster(
                name="atest-deleteme",
                location="us-east1",
                project="asdf",
                node_locations=["us-east1-a", "us-east1-b"],
            )


import random

board = [' ' for _ in range (0, 9)]
availablenumbers = [0,1,2,3,4,5,6,7,8]

def print_board(board):
    top = board[0:3]
    middle = board[3:6]
    bottom = board[6:9]
    print(top)
    print(middle)
    print(bottom)

print_board(board)

playerturn = 0 # player O
stopgame = False
def decide_winner(y):
    if board[0] == y and board[1] == y and board[2] == y:
        return True
    elif board[3] == y and board[4] == y and board[5] == y:
        return True
    elif board[6] == y and board[7] == y and board[8] == y:
        return True
    elif board[0] == y and board[3] == y and board[6] == y:
        return True
    elif board[1] == y and board[4] == y and board[7] == y:
        return True
    elif board[2] == y and board[5] == y and board[8] == y:
        return True
    elif board[0] == y and board[4] == y and board[8] == y:
        return True
    elif board[2] == y and board[4] == y and board[6] == y:
        return True

while stopgame == False:
    move = input('Which cell do you wanna occupy? (0-9)')
    if board[int(move)] == ' ':
        if playerturn == 0:
            board[int(move)] = 'O'
            playerturn = 1
        elif playerturn == 1:
            # board[int(move)] = 'X'
            randomspot= int(random.random() * availablenumbers)
            print(randomspot)
            if board[randomspot] == '':
                board[randomspot] = 'X'
                foundspot = True
            playerturn = 0
            del availablenumbers[randomspot]
    else:
        print('this field is already taken')

    print_board(board)

    if decide_winner('O'):
        print('player O is the winner')
        stopgame = True
    elif decide_winner('X'):
        print('player X is the winner')
        stopgame = True
    elif not board[0] == ' ' and not board[1] == ' ' and not board[2] == ' ' and not board[3] == ' ' and not board[4] == ' ' and not board[5] == ' ' and not board[6] == ' ' and not board[7] == ' ' and not board[8] == ' ':
        print('it is a tie')
        stopgame = True

    print(round)


# -*- coding: utf-8 -*-
"""
Created on Sun Feb 21 14:31:06 2021

@author: mohit
"""

import pandas as pd
cust_rating = pd.read_csv('https://query.data.world/s/ILc-P4llUraMaYN6N6Bdw7p6kUvHnj')

cust_rating['avg_rating'] = round(cust_rating.iloc[:,2:].mean(axis = 1))

print(cust_rating.head(10))

odi_batting = pd.read_csv(r'E:\UPgrade\odi-batting.csv')

odi_batting['century'] = odi_batting['Runs'].apply(lambda x : 1 if x  >99 else 0)
name = odi_batting.groupby('Player')['century'].sum()
odi_batting['strike_rate'] = (odi_batting['Runs']*100)/odi_batting['Balls']
test = odi_batting[odi_batting['century'] == 1]
odi_batting['Year'] = odi_batting['MatchDate'].apply(lambda x: x[6:])

odi_batting[odi_batting['Country'] == 'India'].groupby('Year')['century'].sum()

order = pd.read_csv('https://query.data.world/s/3hIAtsCE7vYkPEL-O5DyWJAeS5Af-7')
order['Order_Date'] = pd.to_datetime(order['Order_Date'])

order['day'] = order['Order_Date'].dt.day_name()

order['day'] = order['Order_Date'].apply(lambda x: int(x[7:9]))

print(order.head(10))

grades = pd.read_csv(r'E:\UPgrade\grades.csv')

grades['format'] = grades['submission'].apply(lambda x : x[-3:])
grades['format'] = grades['format'].apply(lambda x : x[1:] if x == '.7z' else (x[-1] if x[-1] == 'R' else x))
grades['format'].value_counts()
grades['percent'] = grades['format'].value_counts()/grades['format'].count()
grades['submit_time'] = pd.to_datetime(grades['submit_time'])
grades['submission'].value_counts()
grades['day'] = grades['submit_time'].dt.day
grades['month'] = grades['submit_time'].dt.month
grades['year'] = grades['submit_time'].dt.year
grades['date'] = grades['submit_time'].dt.date
grades['hour'] = grades['submit_time'].dt.hour

grades[(grades['day'] > 3) & (grades['month'] >= 1) & (grades['year'] > 2016)].describe()
grades['date'].value_counts()
grades['hour'].value_counts()


"""
Derived module from filehandler.py to handle LS-DYNA keyword (.k) files.
"""
import numpy as np
import pygem.filehandler as fh

class KHandler(fh.FileHandler):
    """
    LS-Dyna keyword file handler class
    :cvar string infile: name of the input file to be processed.
    :cvar string outfile: name of the output file where to write in.
    :cvar list extensions: extensions of the input/output files. It is equal
            to '.k'.
    """

    def __init__(self):
        super(KHandler, self).__init__()
        self.extensions = ['.k']

    def parse(self, filename):
        """
        Method to parse the file `filename`. It returns a matrix with all the
        coordinates. It reads only the section *NODE of the k files.
        :param string filename: name of the input file.
        :return: mesh_points: it is a `n_points`-by-3 matrix containing the
                coordinates of the points of the mesh.
        :rtype: numpy.ndarray
        """
        self._check_filename_type(filename)
        self._check_extension(filename)
        self.infile = filename
        index = -9
        mesh_points = []
        with open(self.infile, 'r') as input_file:
            for num, line in enumerate(input_file):
                if line.startswith('*NODE'):
                    index = num
                if num == index + 1:
                    if line.startswith('$'):
                        index = num
                    elif line.startswith('*'):
                        index = -9
                    else:
                        l = []
                        l.append(float(line[8:24]))
                        l.append(float(line[24:40]))
                        l.append(float(line[40:56]))
                        mesh_points.append(l)
                        index = num
            mesh_points = np.array(mesh_points)
        return mesh_points

    def write(self, mesh_points, filename):
        """
        Writes a .k file, called filename, copying all the lines from
        self.filename but the coordinates. mesh_points is a matrix that
        contains the new coordinates to write in the .k file.
        :param numpy.ndarray mesh_points: it is a `n_points`-by-3 matrix
                          containing the coordinates of the points of the mesh
        :param string filename: name of the output file.
        """
        self._check_filename_type(filename)
        self._check_extension(filename)
        self._check_infile_instantiation()
        self.outfile = filename
        index = -9
        i = 0
        with open(self.outfile, 'w') as output_file:
            with open(self.infile, 'r') as input_file:
                for num, line in enumerate(input_file):
                    if line.startswith('*NODE'):
                        index = num
                    if num == index + 1:
                        if line.startswith('$'):
                            index = num
                        elif line.startswith('*'):
                            index = -9
                        else:
                            for j in range(0, 3):
                                line = line[:8 + 16 * (j)] + '{:16.10f}'.format(
                                    mesh_points[i][j]) + line[8 + 16 * (j + 1):]
                            i += 1
                            index = num
                    output_file.write(line)


from .DAG_base import DAG_base, Node
from .DAG_base import idx_list

class DAG_FCP(DAG_base):
    # ＜コンストラクタ＞
    def __init__(self):
        '''
        file_name : .tgffファイルの名前
        num_of_node : DAG内のノード数
        nodes[]: ノードの集合
        '''
        self.critical_path=[]
        super(DAG_FCP, self).__init__()

    # クリティカルパスの特定
    def find_critical_path(self):
        # ソースとシンクの特定（DAGなら戦闘と末尾でも可）
        for n in self.nodes:
            if n is not None:
                if n.src == 1:
                    src = n.idx
                if n.snk == 1:
                    snk = n.idx

        # 最悪ケースの開始時間の設定
        self.culc_wcft(src, 0)

        # 動作テスト
        #for i,node in enumerate(self.nodes):
        #    print(i,":",node.c)

        # 末尾から、snkに最悪ケースの開始時間を与えるノードを特定
        i = snk
        while(self.nodes[i].src == 0):
            self.critical_path.append(i)
            tmp = src
            for p in self.nodes[i].pre:
                if self.nodes[tmp].wcft <= self.nodes[p].wcft:
                    tmp = p
            i = tmp
        self.critical_path.append(src)

        self.critical_path.reverse()

    # 最悪ケースの開始時間の設定
    def culc_wcft(self, i, start_time):
        # 最悪ケースの開始時間が更新されたら
        if self.nodes[i].wcft <= start_time + self.nodes[i].sc():
            self.nodes[i].wcft = start_time + self.nodes[i].sc()
            # 自分の後ろも更新する
            for s in self.nodes[i].suc:
                if s is not None:
                    self.culc_wcft(s, start_time + self.nodes[s].sc())

    def print_critical_path(self):
        print(self.critical_path)


import tornado.ioloop
import os.path

import tornado.web

from blackwidow import config

PARENT_DIR = os.path.abspath(os.path.dirname(__file__))
STATIC_DIR = os.path.join(PARENT_DIR, 'static')
HTML_FILE = os.path.join(STATIC_DIR, 'graph.html')

class MainHandler(tornado.web.RequestHandler):

    def get(self):
        self.render(HTML_FILE)

class DataHandler(tornado.web.RequestHandler):

    def get(self):
        self.write(self.json_data)

application = tornado.web.Application([
    (r"/", MainHandler),
    (r"/json", DataHandler)
], static_path=STATIC_DIR)

def serve(data, port=None):
    """
    Render graph visualization
    """
    DataHandler.json_data = data
    application.listen(port or config.PORT)
    print("Navigate to http://localhost:%d/ to view the package graph" % config.PORT)
    tornado.ioloop.IOLoop.current().start()


import os
import unittest
import pytest
from uuid import uuid4
from tempfile import TemporaryDirectory
from argparse import Namespace
import tarfile
import ppcg_qc_from_sanger.extract_qc as extract_qc
from ppcg_qc_from_sanger.sanger_qc_extractor import SangerQcMetricsExtractor

class PpcgQcFromSangerExtractQcTestCase(unittest.TestCase):

    def setUp(self):
        self.test_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data')

    def write_to_file(self, dir, content, file_name=None):
        if file_name is None:
            test_file = os.path.join(dir, str(uuid4()))
        else:
            test_file = os.path.join(dir, file_name)
        with open(test_file, 'w') as f:
            f.write(content)
        return test_file

    def test_validate_tar_name(self):
        testing_function = SangerQcMetricsExtractor.validate_tar_name
        with pytest.raises(RuntimeError) as exc:
            result = testing_function(os.path.join('x', 'y', 'z', '.tar.gz'))
        self.assertIn('should have ".tar.gz" suffix', str(exc.value))
        with pytest.raises(RuntimeError) as exc:
            result = testing_function(os.path.join('x', 'y', 'z.gz'))
        self.assertIn('should have ".tar.gz" suffix', str(exc.value))

    def test_validate_bas(self):
        testing_function = SangerQcMetricsExtractor.validate_bas

        # test invalid file name
        with pytest.raises(RuntimeError) as exc:
            test = testing_function('test.bam.basss')
        self.assertIn('file test.bam.basss does not exist.', str(exc.value))

        # test valid content
        test_file = os.path.join(self.test_dir, 'test_sample.bam.bas')
        self.assertEqual(testing_function(test_file), None)

        test_content = open(test_file).read()
        with TemporaryDirectory() as temp_dir:

            # test bad header
            test_file_content = 'bas_file_name\n' + test_content
            test_file = self.write_to_file(temp_dir, test_file_content, 'test_1.bam.bas')
            with pytest.raises(RuntimeError) as exc:
                test = testing_function(test_file)
            self.assertIn('invalid BAS header', str(exc.value))

            # test fewer columns in a row
            test_file_content = test_content + '-\ttest_sample\n'
            test_file = self.write_to_file(temp_dir, test_file_content, 'test_2.bam.bas')
            with pytest.raises(RuntimeError) as exc:
                test = testing_function(test_file)
            self.assertIn('invalid row in BAS file', str(exc.value))

    def test_extract_from_sanger(self):
        '''
        testing the whole process
        '''
        testing_function = extract_qc.extract_from_sanger
        with TemporaryDirectory() as temp_dir:
            good_args = Namespace(
                tumour_bas=[os.path.join(self.test_dir, 'tumour_sample.bam.bas')],
                normal_bas=[os.path.join(self.test_dir, 'normal_sample.bam.bas')],
                variant_call_tar=[os.path.join(self.test_dir, 'test.tar.gz')],
                output_tar=os.path.join(temp_dir, 'result.tar.gz'),
                genome_size=3137454505,
                debug=False,
                count_variants=False,
                metadata=[]
            )
            # run the function to have outputs
            self.assertEqual(testing_function(good_args), None)

            # check if the metrics are as expected.
            metrics_file = 'ppcg_sanger_metrics.txt'
            with tarfile.open(good_args.output_tar, 'r:gz') as tar:
                result_files = [a_file.name for a_file in tar.getmembers()]
                tar.extract(metrics_file, path=temp_dir)
            self.assertEqual(
                open(os.path.join(temp_dir, metrics_file), 'r').read(),
                open(os.path.join(self.test_dir, 'expected_metrics.txt')).read()
            )

            # check if expected files are in the tar
            e_genotype_files = \
                SangerQcMetricsExtractor.get_metrics_file_names('tumour_sample', 'normal_sample')[3]
            for a_file in e_genotype_files:
                self.assertTrue(os.path.basename(a_file) in result_files)

            # test wrong genome size type
            # a shallow copy of good_args
            args = Namespace(**vars(good_args))
            args.genome_size = '1'
            with pytest.raises(SystemExit) as exc:
                testing_function(args)
            self.assertEqual(exc.type, SystemExit)

            # test non exist file
            args = Namespace(**vars(good_args))
            args.tumour_bas = 'some should not exist file.file'
            with pytest.raises(SystemExit) as exc:
                testing_function(args)
            self.assertEqual(exc.type, SystemExit)

            # test if variants can be counted correctly
            args = Namespace(**vars(good_args))
            args.count_variants = True
            args.output_tar = os.path.join(temp_dir, 'result_2.tar.gz')
            # run the function to have outputs
            self.assertEqual(testing_function(args), None)
            metrics_file = 'ppcg_sanger_metrics.txt'
            with tarfile.open(args.output_tar, 'r:gz') as tar:
                result_files = [a_file.name for a_file in tar.getmembers()]
                tar.extract(metrics_file, path=temp_dir)
            self.assertEqual(
                open(os.path.join(temp_dir, metrics_file), 'r').read(),
                open(os.path.join(self.test_dir, 'expected_metrics_with_variant_counts.txt')).read()
            )


#!/usr/bin/python
from ming import *

m =  SWFMovie();

# SWF_SETBACKGROUNDCOLOR
m.setBackground(0xff, 0xff, 0xff);

# SWF_DEFINESPRITE

	#  MovieClip 1
character1 =  SWFMovieClip(); # 1 frames

# SWF_SHOWFRAME
character1.nextFrame(); # end of clip frame 1

# SWF_END

# SWF_PLACEOBJECT2
# PlaceFlagHasCharacter
m.add(character1);
# PlaceFlagHasMatrix
# outputSWF_MATRIX is broken, so it is being skipped..

# SWF_SHOWFRAME
m.nextFrame(); # end of frame 1

# SWF_END

m.save("test02.swf");


# Run this app with `python app.py` and
# visit http://127.0.0.1:8050/ in your web browser.
import dash_bootstrap_components as dbc
import os
import psycopg2
from dash import Dash, callback, html
from whitenoise import WhiteNoise
from components.header_footer import generate_header, generate_footer
from components.figures import (
    generate_overall_handover_status_pie,
    generate_per_subcomplex_handover_status_pie,
    generate_sample_bar_chart,
    generate_table
)

app = Dash(__name__, external_stylesheets=[dbc.themes.FLATLY])

server = app.server
server.wsgi_app = WhiteNoise(server.wsgi_app, root='static/')

DATABASE_URL = os.environ['DATABASE_URL']
if 'postgresql' not in DATABASE_URL:
        DATABASE_URL = DATABASE_URL.replace('postgres', 'postgresql')

conn = psycopg2.connect(DATABASE_URL, sslmode='require')
# conn = psycopg2.connect(DATABASE_URL)

def create_dash_layout(app):
    colors = {
        'background': '#111111',
        'text': '#7FDBFF'
    }

    table_style = {
        'header': {
            'backgroundColor': 'rgb(30, 30, 30)',
            'color': 'white'
        },
        'data': {
            'backgroundColor': 'rgb(50, 50, 50)',
            'color': 'white',
            'whiteSpace': 'normal',
            'height': 'auto',
            'lineHeight': '15px'
        },
        'label': {
            'textAlign': 'center',
            'color': colors['text']
        }
    }

    # Set browser tab title
    app.title = "Cendana Peak Statistics"

    # Header
    header = html.Div(style={'textAlign': 'center', 'color': colors['text']}, children=generate_header())

    # Body
    body = html.Div([
        generate_overall_handover_status_pie(conn, colors),
        generate_per_subcomplex_handover_status_pie(conn, colors),
        generate_table(table_style, conn)
    ])

    # Footer
    footer = html.Div(style={'textAlign': 'center', 'color': colors['text']}, children=generate_footer())

    # Assemble dash layout
    app.layout = html.Div(style={'textAlign': 'center', 'backgroundColor': colors['background']}, children=[header, body, footer])

    return app

create_dash_layout(app)

if __name__ == '__main__':
    app.run_server(debug=True)


import unittest
from typing import List

class Solution:
    def rotate(self, matrix: List[List[int]]) -> None:
        """
        Do not return anything, modify matrix in-place instead.
        """
        i, n = 0, len(matrix)
        while i < n // 2:
            matrix[i], matrix[-i - 1] = matrix[-i - 1], matrix[i]
            i += 1

        for i in range(n):
            for j in range(i):
                matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]

        return matrix

class Tests(unittest.TestCase):
    def tests(self):
        sol = Solution()
        self.assertEqual(
            [[7, 4, 1], [8, 5, 2], [9, 6, 3]],
            sol.rotate(matrix=[[1, 2, 3], [4, 5, 6], [7, 8, 9]]),
        )
        self.assertEqual(
            [[15, 13, 2, 5], [14, 3, 4, 1], [12, 6, 8, 9], [16, 7, 10, 11]],
            sol.rotate(
                matrix=[[5, 1, 9, 11], [2, 4, 8, 10], [13, 3, 6, 7], [15, 14, 12, 16]]
            ),
        )

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


import os
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras import layers
from tensorflow.keras.layers import Dense, Input, InputLayer, Flatten, Conv2D, BatchNormalization, MaxPooling2D, Activation , GlobalAveragePooling2D
from tensorflow.keras.models import Sequential,  Model
import tensorflow.keras.backend as K
import wandb
import tensorflow as tf
from tensorflow import keras
from sklearn import metrics
import pandas as pd
import cv2
from wandb.keras import WandbCallback
import pathlib
import seaborn as sns
from CNN_model import*
from get_data import*
from paths import*

# input image dimensions that we are using for the following task
img_size = (128,128)

'''
Sweep config for the hyperparameter tuning
'''
sweep_config = {
  "name": "Bayesian Sweep",
  "method": "bayes",
  "metric":{
  "name": "val_accuracy", # optimizing wrt to maximizing the validation accuracy
  "goal": "maximize"
  },
  "parameters": {

        "activation":{
            "values": ["relu"]
        },
        "filter_size": {
            "values": [(2,2), (3,3), (4,4)]
        },
        "batch_size": {
            "values": [32, 64]
        },
        "padding": {
            "values": ["same","valid"]
        },
        "data_augmentation": {
            "values": [True, False]
        },
        "optimizer": {
            "values": ["adam", "sgd", "rmsprop"]
        },
        "batch_normalization": {
            "values": [True, False]
        },
        "batchnorm_location": {
            "values": ["After", "Before"]
        },
        "num_filters": {
            "values": [32,64, 128]
        },
        "dense_neurons": {
            "values": [64, 128, 256]
        },
        "dropout_loc": {
            "values": ["conv","dense","all"]
        },
        "dropout": {
            "values": [None, 0.2,0.3]
        },
        "gap": {
            "values": [False,True]
        },
        "filter_multiplier": {
            "values": ["standard","half","double"]
        },
    }
}

'''
Initializing the wandb sweeps for tuning
'''
sweep_id = wandb.sweep(sweep_config,project=project_name, entity=entity_name)

'''
Training function for sweeping and hyperparameter tuning
'''
def train():
    #project name for wandb initialization
    project_name = 'CS6910-Assignment2-PartA_final'
    # model save path location
    model_save_path = "./TrainedModel/"
    img_size=(128,128)
    #config default values for sweeping and values with only one value.
    config_defaults = dict(
            num_hidden_cnn_layer = 5 ,
            activation = 'relu',
            batch_normalization = True,
            batchnorm_location = "After",
            filter_multiplier = "double" ,
            filter_size = (3,3),
            num_filters  = 32,
            dropout = None,
            dropout_loc = "dense",
            pool_size = (2,2),
            padding = 'same',
            dense_neurons = 128,
            num_classes = 10,
            optimizer = 'adam',
            epochs = 7,
            batch_size = 32,
            data_augmentation = False,
            gap = True,
            img_size = img_size
        )

    # Initializing the wandb with project name and entity name
    wandb.init(project = project_name, config = config_defaults,entity=entity_name)
    CONFIG = wandb.config

    # Naming convention fo the wandb runs
    wandb.run.name = "Image_recog" + str(CONFIG.num_hidden_cnn_layer) + "_dn_" + str(CONFIG.dense_neurons) + "_opt_" + CONFIG.optimizer + "_dro_" + str(CONFIG.dropout) + "_bs_"+str(CONFIG.batch_size) + "_fm_" + CONFIG.filter_multiplier + "_bnl_" + CONFIG.batchnorm_location + "_dpl_" + CONFIG.dropout_loc

    with tf.device('/device:GPU:0'):
        # Create object of the CNN model class
        imageRecog = cnnModel(CONFIG.img_size, CONFIG, 10)
        # Build the model
        model = imageRecog.build_cnnmodel()

        # Printing the model summary
        model.summary()

        # Reading the data
        train_generator,validation_generator = get_data(CONFIG.batch_size,CONFIG.data_augmentation,train_data_path)

        # Model compilation
        model.compile(
        optimizer=CONFIG.optimizer,  # Optimizer
        # Loss function to minimize (categorial classification for multiclass classification)
        loss=tf.keras.losses.CategoricalCrossentropy(from_logits=True),
        # Monitoring accuracy
        metrics=['accuracy'],
        )
        # Model fit for training
        history = model.fit(
                        train_generator,
                        steps_per_epoch = train_generator.samples // CONFIG.batch_size,
                        validation_data = validation_generator,
                        validation_steps = validation_generator.samples // CONFIG.batch_size,
                        epochs = CONFIG.epochs,
                        callbacks=[WandbCallback()]
                        )

        # Save the models in the follwoing path
        model.save(model_save_path+wandb.run.name)
        wandb.finish()
        # Returning the model and the history
        return model, history

wandb.agent(sweep_id, train, count = 30)