# -*- coding: utf-8 -*-
import os
import sys
__project_path = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
if __project_path not in sys.path:
    sys.path.append(__project_path)
import mongo_db
from bson.objectid import ObjectId
import datetime
import random
from log_module import get_logger
from werkzeug.contrib.cache import SimpleCache

simple_cache = SimpleCache()
logger = get_logger()

"""
此模块用于根据老师喊单的信号，生成对应的虚拟老师数据
"""

class Deposit(mongo_db.BaseDoc):
    """
    入金/存款(记录),当老师做单的时候,如果资金不足,就会激发入金行为
    """
    _table_name = "deposit"
    type_dict = dict()
    type_dict['_id'] = ObjectId
    type_dict['t_id'] = ObjectId  # 老师id
    type_dict['num'] = float  # 入金金额.
    type_dict['time'] = datetime.datetime

    @classmethod
    def generator_deposit(cls, min_money: float) -> float:
        """
        生成一个加金的数字
        :param min_money: 最低额度.加金金额必须大于此额度.
        :return: 实际的加金额度.
        """
        l = [10000,  15000, 20000, 30000, 50000, 100000]
        r = 0
        for x in l:
            if min_money * 2 <= x:
                r = x
                break
            else:
                pass
        return r

class Teacher(mongo_db.BaseDoc):
    """
    老师,微信项目（Webchat_Server）有一个同名的类，不同的是：
    Message_Server项目下的Teacher类主要负责item_module.Trade.sync_from_signal(生成虚拟老师交易信号)时取老师列表。
    Webchat_Server项目项目下的Teacher类，主要负责老师的管理。（属性，头像的修改）
    两个类属性的字段相同即可.
    Webchat_Server项目项目下的teacher_module函数更丰富一些。
    你会在2个项目下分别看到这两个模块。
    """
    _table_name = "teacher"
    type_dict = dict()
    """真实老师的id取自简道云"""
    type_dict['_id'] = ObjectId
    """真实老师的name可能取自简道云(也可再修改)"""
    type_dict['name'] = str   # 展示的名字，比如青云老师等
    type_dict['real_name'] = str  # 真实姓名，非必须
    type_dict['head_img'] = str  # 头像文件相当与项目根目录的路径
    type_dict['img'] = str  # 半身像文件相当与项目根目录的路径
    type_dict['level'] = str  # 老师等级
    type_dict['motto'] = str  # 座右铭
    type_dict['feature'] = str  # 特性，风格，特点
    type_dict['resume'] = str  # 简历
    type_dict['create_date'] = datetime.datetime
    type_dict['native'] = bool  # 是否是真实的teacher？
    type_dict['from_id'] = ObjectId  # 虚拟老师专有，发源老师id，保持不连，除非修改
    type_dict['direction'] = str  # 虚拟老师专有，跟的方向，有三种 follow/reverse/random
    type_dict['win_ratio'] = float  # 胜率率, 每次close时候计算
    type_dict['case_count'] = int  # 总单数, 每次close时候计算
    type_dict['win_count'] = int  # 盈利的单数, 每次close时候计算
    type_dict['profit_ratio'] = float  # 盈利率, 每次close时候计算
    type_dict['profit_amount'] = float  # 盈利总额.每次close时候计算
    type_dict['deposit'] = float  # 存款,当前本金.每次close时候计算
    type_dict['deposit_amount'] = float  # 历次存款总额,每次close时候计算
    type_dict['lots_range'] = list  # 手数范围.
    # type_dict['show'] = bool  # 是否显示?默认显示

    @classmethod
    def instance(cls, **kwargs):
        if "create_date" not in kwargs:
            kwargs['create_date'] = datetime.datetime.now()
        # if "show" not in kwargs:
        #     kwargs['show'] = True
        if "name" not in kwargs:
            ms = "name必须"
            raise ValueError(ms)
        if "native" not in kwargs:
            ms = "native必须"
            raise ValueError(ms)
        return cls(**kwargs)

    @classmethod
    def init_head_image_list(cls) -> list:
        """
        初始化头像列表
        :return:
        """
        d = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
        p = os.path.join(d, "static", 'images', 'head_image')
        ns = os.listdir(p)
        res = list()
        for name in ns:
            u = os.path.join("/static/images/head_image", name)
            res.append(u)
        key = "t_head_img"
        simple_cache.set(key=key, value=res, timeout=900)
        return res

    @classmethod
    def pop_head_image(cls) -> str:
        """
        尽量不重复的生成老师的头像,如果头像资源用尽,那就重头来.
        :return:
        """
        key = "t_head_img"
        m = simple_cache.get(key)
        if m is None or len(m) == 0:
            m = cls.init_head_image_list()
        else:
            pass
        url = m.pop(random.randint(0, len(m) - 1))
        simple_cache.set(key=key, value=m, timeout=900)
        return url

    @classmethod
    def generator_lots_range(cls) -> list:
        """
        虚拟一个手数范围
        :return:
        """
        begin = random.choice([1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3])
        step = random.choice([2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 7, 7])
        return [begin, begin + step]

    @classmethod
    def generator_init(cls, t_id: (str, ObjectId), t_name: str, create_date: datetime.datetime) -> list:
        """
        根据一个老师的id,名字和创建时间.生成3个虚拟的老师,加上原始的真实老师,一起返回(4个doc)
        :param t_id:
        :param t_name:
        :param create_date:
        :return:
        """
        t_id = ObjectId(t_id) if isinstance(t_id, str) and len(t_id) == 24 else t_id
        """原始的老师的init字典"""
        raw = {
            "_id": t_id,  # 老师id
            "name": t_name,
            "direction": "raw",
            "head_img": cls.pop_head_image(),
            "create_date": create_date,
            "native": True,  # 真实老师
            "lots_range": cls.generator_lots_range(),  # 手数范围,和性格有关
            "deposit": 0.0,     # 当前存款,初始资金,都是0
            "deposit_amount": 0.0,     # 历史存款累计，用于计算盈利率
            "profit_ratio": 0.0,   # 盈利率, 每次close时候计算,profit_amount/deposit得出.百分比数值
            "profit_amount": 0,        # 盈利总额.每次close时候计算,初始0,
            # "from_id": t_id,  # 发源老师id, 主要是名字来源,没有其他方面的意义

        }
        """正向"""
        follow = {
            "_id": ObjectId(),  # 老师id
            "name": "{}_follow".format(t_name),
            "direction": "follow",
            "head_img": cls.pop_head_image(),
            "create_date": create_date,
            "native": False,  # 真实老师
            "lots_range": cls.generator_lots_range(), # 手数范围,和性格有关
            "deposit": 0.0,     # 当前存款,初始资金,都是0
            "deposit_amount": 0.0,  # 历史存款累计，用于计算盈利率
            "profit_ratio": 0.0,   # 盈利率, 每次close时候计算,profit_amount/deposit得出.百分比数值
            "profit_amount": 0,        # 盈利总额.每次close时候计算,初始0,
            "from_id": t_id,  # 发源老师id, 主要是名字来源,没有其他方面的意义
            "direction": "follow"
        }
        """反向"""
        reverse = {
            "_id": ObjectId(),  # 老师id
            "name": "{}_reverse".format(t_name),
            "direction": "reverse",
            "head_img": cls.pop_head_image(),
            "create_date": create_date,
            "native": False,  # 真实老师
            "lots_range": cls.generator_lots_range(),  # 手数范围,和性格有关
            "deposit": 0.0,  # 当前存款,初始资金,都是0
            "deposit_amount": 0.0,  # 历史存款累计，用于计算盈利率
            "profit_ratio": 0.0,  # 盈利率, 每次close时候计算,profit_amount/deposit得出.百分比数值
            "profit_amount": 0,  # 盈利总额.每次close时候计算,初始0,
            "from_id": t_id,  # 发源老师id, 主要是名字来源,没有其他方面的意义
            "direction": "reverse"
        }
        """随机"""
        rand = {
            "_id": ObjectId(),  # 老师id
            "name": "{}_random".format(t_name),
            "direction": "random",
            "head_img": cls.pop_head_image(),
            "create_date": create_date,
            "native": False,  # 真实老师
            "lots_range": cls.generator_lots_range(),  # 手数范围,和性格有关
            "deposit": 0.0,  # 当前存款,初始资金,都是0
            "deposit_amount": 0.0,  # 历史存款累计，用于计算盈利率
            "profit_ratio": 0.0,  # 盈利率, 每次close时候计算,profit_amount/deposit得出.百分比数值
            "profit_amount": 0,  # 盈利总额.每次close时候计算,初始0,
            "from_id": t_id,  # 发源老师id, 主要是名字来源,没有其他方面的意义
            "direction": "random"
        }
        return [raw, follow, reverse, rand]

    @classmethod
    def rebuild(cls) -> None:
        """
        从交易信号中，初始化老师，这个动作会清除所有的老师记录.
        仅在初始化时候使用。
        :return:
        """
        t = datetime.datetime.now()
        t = t - datetime.timedelta(days=90)
        f = {"create_time": {"$gte": t}}
        s = [("create_time", -1)]
        p = ['creator_id', 'creator_name', 'create_time']
        ses = mongo_db.get_conn(table_name="signal_info")
        args = {"filter": f, "sort": s, "projection": p}
        r = ses.find(**args)
        native = dict()
        for x in r:
            _id = ""
            try:
                _id = x['creator_id']
                name = x['creator_name']
            except KeyError as e:
                # print(e)
                # print(x)
                try:
                    name = x['updater_name']
                except KeyError as e:
                    print(e)
                    print(x)
            if (isinstance(_id, str) and len(_id) == 24) or isinstance(_id, ObjectId):
                _id = _id if isinstance(_id, ObjectId) else ObjectId(_id)
                if _id not in native:
                    native[_id] = name
                else:
                    prev_name = native[_id]
                    if prev_name == name:
                        pass
                    else:
                        ms = "新旧老师名字不一致：prev={}, now={}".format(prev_name, name)
                        raise ValueError(ms)
            else:
                ms = "异常的_id：{}".format(_id)
                print(ms)
        print(native)

        res = list()
        now = datetime.datetime.now()
        for k, v in native.items():
            res.extend(cls.generator_init(t_id=k, t_name=v, create_date=now))
        client = mongo_db.get_client()
        t = client[mongo_db.db_name][cls.get_table_name()]
        with client.start_session(causal_consistency=True) as ses:
            with ses.start_transaction():
                t.delete_many(filter=dict(), session=ses)
                t.insert_many(documents=res, session=ses)

    @classmethod
    def direction_map(cls, include_raw: bool = True) -> dict:
        """
        获取以方向为key,老师的doc的list为val的字典.
        用于在生成虚拟信号的时候提供随机特性
        : param include_raw: 是否包含原生的方向.
        :return:
        """
        f = {"create_date": {"$gt": mongo_db.get_datetime_from_str("2018-9-3 0:0:0")}}
        ts = Teacher.find_plus(filter_dict=f, to_dict=True)
        t_dict = dict()  # 方向和老师的字典
        for t in ts:
            d = t.get("direction")
            if include_raw:
                if d is not None:
                    if d in t_dict:
                        t_dict[d].append(t)
                    else:
                        t_dict[d] = [t]
            else:
                if d is not None and d != "raw":
                    if d in t_dict:
                        t_dict[d].append(t)
                    else:
                        t_dict[d] = [t]
        return t_dict

if __name__ == "__main__":
    Teacher.rebuild()
    pass


import os
import logging
import time
import sys

from postgresDbloader import dbLoader

import propertyReader as pr

def write_2_qli_app_name_tab_qvd_name(str):
    pass

def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.

    config = pr.getPropInstance('config/', 'NLP.properties')
    __prop_dict = dict(config.items('log'))
    logFile = __prop_dict["logfilepath"] + sys.argv[0].split('\\')[-1] + "_" + os.getlogin() + "_" + time.strftime(
        '%Y%m%d%H%M%S') + '.log'

    log_format = '%(asctime)s - [%(levelname)s] - [%(filename)s->%(funcName)s():%(lineno)d] - %(message)s'
    log_date_format = '[%m/%d/%Y %I:%M:%S %p]'

    logging.basicConfig(
        level=logging.INFO
        # , filename = logFile
        , format=log_format
        , datefmt=log_date_format
    )

    ch = logging.StreamHandler(sys.stdout)
    logging.getLogger().addHandler(ch)

    tabs = {}
    qvd_dict = {}
    new_tabs = {}

    path = "D:\\Users\\georgj3\\Documents\\MRO_QVD"

    sql = open("D:\\Users\\georgj3\\Documents\\sap.sql", "w", encoding="utf8")

    qli_app_name_tab_qvd_name = open("D:\\Users\\georgj3\\Documents\\qli_app_name_tab_qvd_name11.sql", "w",
                                     encoding="utf8")

    lists_path = "D:\\Users\\georgj3\\Documents\\mro_qvdtest.txt"

    """ creating db loader

    """
    ldbLoader = dbLoader(__prop_dict)
    ldbLoader.init_db_conn()

    """ truncate table """
    # ldbLoader.truc_qlik_apps_info()
    # ldbLoader.truc_QVD_tab_columns()

    with open(lists_path, "r", encoding="utf8")  as f:
        filenames = f.readlines()
    for filename in filenames:
        filename = filename.strip() + ".txt"
        # for filename in os.listdir(path):
        # logging.info("  {}  ".format(filename))
        tabs = {}

        with open(os.path.join(path, filename), "r", encoding="utf8")  as f:
            lines = f.readlines()

        logging.info("--GEO--C-- file name {}\n".format(filename))
        sql.write("--GEO--C-- file name {}\n".format(filename))
        for l in lines:

            # print(0, l.split("///$tab"))
            temp = l.split("///$tab")
            for ll in temp:
                # print(ll)
                tabs[ll.split("\\r\\n")[0]] = '\n'.join(ll.split("\\r\\n")[1:])
        """loop thru each tabs
        """
        tmpe_tab = {}

        """
        limit to only one TAB
        """
        new_qlik_tabs = {}
        for key, value in tabs.items():
            # print("KEY = " + key  + " " )
            # tab_list = ['TIDPOLIN ALL REVISIONS', "TID WOOOS"]

            # if key.strip() in tab_list :

            lines_in_tabs = value.split("\n")

            new_lines_intab = []
            for lintab in lines_in_tabs:
                if not lintab.strip().startswith("//") and not lintab.strip() == "":
                    new_lines_intab.append(lintab)
            new_qlik_tabs[key.strip()] = '\r\n'.join([str(elem) for elem in new_lines_intab])

        for key, value in new_qlik_tabs.items():

            inc = 0
            # if ("MDM" in key or "MDM" in value) and "(qvd)" in value:
            # logging.info("Valeue before cleanup  {}".format(value[:30]))
            cleaned_value = pr.cleanup_fld(value)
            logging.info("Value after cleanup  {}".format(cleaned_value[:20]))

            qvds = cleaned_value.split("LIB ")  # Split by LIB
            logging.info("key: {}  has 4 Select FROM clauses   {}".format(key, len(qvds)))

            for qvd in qvds:  ## loop thru each LIB
                inc = inc + 1

                qvd_load = qvd
                # replace the 1st \r\r

                qvd_script_lines = qvd.strip().split("\r\n")
                if len(qvd_script_lines) > 1:

                    """
                    Our pattenr will be LIB CONNECT connectionname;
                    qvdtablename:
                    Load

                    Select .....
                    from
                    """
                    semicolondelimetedline = qvd_load.split(";")
                    for line in semicolondelimetedline:
                        # get QVD NAME
                        if "vQVDTableFileName" in line:
                            qlik_qvd_name = line.split(' vQVDTableFileName ')[-1].replace('=', '').strip().replace("'",
                                                                                '')
                            logging.info("qvd_name = {}".format(qlik_qvd_name))
                        # check for connect first and get connection name
                        if "CONNECT" in line.strip():
                            connection_name = line.strip().split(' ')[-1].strip().replace("'", '')
                            logging.info("Conectionname = {}".format(connection_name))
                        elif "SELECT " in line.upper().strip():  # split by from

                            from_stat = line.upper().split('FROM ')
                            # if "WHERE" in from_stat[1].upper():
                            db_tab_name = from_stat[1].upper().split('WHERE')[0].replace('\\\"', "").strip()

                            logging.info("Select  = {}\n  from tab = {} ".format(from_stat[0][:30], db_tab_name))
                            asd = 100

                            selectcolumns = pr.load_qvd_build_info(from_stat[0].replace("\\t", "\t"))
                            # for loop to fire insert
                            # create table qvd_tab_columns(qlik_app_name varchar(100), qlik_tab_name varchar(200), qlik_conn_name varchar(200), db_tab_name varchar(200), db_col_name varchar(200))
                            logging.info(
                                "qlik_conn_name= {}; qlik_qvd_name = {}; db_tab_name = {}; column count = [{}] {}".format(
                                    connection_name, qlik_qvd_name, db_tab_name, len(selectcolumns), key))
                            for column in selectcolumns:
                                column_cleaned = column
                                for rep in ['\\\"', ',']:
                                    column_cleaned = column_cleaned.replace(rep, "")
                                logging.info(
                                    "qlik_app_name= {}; qlik_tab_name = {} qlik_conn_name= {}; qlik_qvd_name = {}; db_tab_name = {}; db_col_name= {} ".format(
                                        filename.replace(".txt", "")
                                        , key, connection_name, qlik_qvd_name, db_tab_name, column_cleaned[:20])
                                )
                                ldbLoader.load_qvd_tab_columns(filename.replace(".txt", ""), key, connection_name,
                                                               qlik_qvd_name, db_tab_name, column_cleaned)

    sql.close()
    qli_app_name_tab_qvd_name.close()

# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    print_hi('PyCharm')


import boto3
from dotenv import load_dotenv
import os

# Load environment variables from .env file
load_dotenv()

access_key = os.getenv("AWS_ACCESS_KEY_ID")
secret_key = os.getenv("AWS_SECRET_ACCESS_KEY")
region = 'us-west-2'  # Replace with your desired region

# Create a Boto3 client for EC2 with explicit credentials
ec2_client = boto3.client('ec2', region_name=region, aws_access_key_id=access_key, aws_secret_access_key=secret_key)

response = ec2_client.describe_images(
    Filters=[
        {'Name': 'name', 'Values': ['ubuntu/images/hvm-ssd/ubuntu-focal-22.04-amd64-server-*']},
        {'Name': 'architecture', 'Values': ['x86_64']},
        {'Name': 'state', 'Values': ['available']},
    ],
    Owners=['099720109477']  # Canonical owner ID
)

images = response['Images']
images.sort(key=lambda x: x['CreationDate'], reverse=True)

if images:
    latest_ubuntu_2204_ami_id = images[0]['ImageId']
    print("Latest Ubuntu Server 22.04 LTS AMI ID:", latest_ubuntu_2204_ami_id)
else:
    print("No matching AMI found.")


import numpy as np
import os
from gap_statistic import OptimalK
from matplotlib import pyplot as plt
from sklearn.manifold import MDS
from sklearn.neighbors import DistanceMetric

from DataGenerator import generateOneClusterData
from Settings import (DEFAULT_FEATURE_MEAN_RANGE, DEFAULT_NUMBER_OF_FEATURES,
                      DEFAULT_NUMBER_OF_RECORDS_PER_CLASS,
                      DEFAULT_RANDOM_NUMBER_SEED)

metrics = ("euclidean", "manhattan", "chebyshev",
            "minkowski")
nMetrics = len(metrics)
meanStress = np.empty((nMetrics,))
stdStress = np.empty((nMetrics,))
meanClusterCount = np.empty((nMetrics,))
stdClusterCount = np.empty((nMetrics,))

nDifferentDataSet = 20
stress = np.empty((nDifferentDataSet,4))

for j, metric in enumerate(metrics):

    clusterCounts = np.empty((nDifferentDataSet,))

    dist = DistanceMetric.get_metric(metric)
    print("MDS Metric: {}".format(metric))

    for i in range(nDifferentDataSet):

        data = generateOneClusterData(DEFAULT_NUMBER_OF_FEATURES,
                                    DEFAULT_NUMBER_OF_RECORDS_PER_CLASS,
                                    DEFAULT_FEATURE_MEAN_RANGE,
                                    i,
                                    distribution="normal")
        precomputedMetricData = dist.pairwise(data)

        mds = MDS(n_components=8, n_jobs=-1, dissimilarity="precomputed")
        mdsData = mds.fit_transform(precomputedMetricData)

        optimalK = OptimalK(parallel_backend='joblib', n_jobs=-1)
        clusterCount = optimalK(mdsData, n_refs=3, cluster_array=np.arange(1, 10))
        clusterCounts[i] = clusterCount
        stress[i,j] = mds.stress_

    meanClusterCount[j] = np.mean(clusterCounts)
    stdClusterCount[j] = np.std(clusterCounts)

    meanStress[j] = np.mean(stress[:,j])
    stdStress[j] = np.std(stress[:,j])

saveDir = os.path.join("data", "MDS-stressPerMetric.npy")
np.save(saveDir, stress)

plt.subplot(1,2,1)
plt.tight_layout()
plt.title("Metrics VS Cluster Counts")
plt.xlabel("Metric")
plt.ylabel("Cluster Counts")
plt.errorbar(metrics, meanClusterCount, yerr=stdClusterCount,
            capthick=2, capsize=10, linewidth=3)
plt.subplot(1,2,2)
plt.title("Metrics VS Cluster Counts")
plt.xlabel("Metric")
plt.ylabel("Stress")
plt.errorbar(metrics, meanStress, yerr=stdStress,
            capthick=2, capsize=10, linewidth=3)
plt.show()


from typing import List
import torch
import torch.optim as optim
import torch.nn as nn
import torch.nn.functional as F

class RewriteDecider(nn.Module):
    def __init__(self, hps, device=torch.device("cpu")):
        super(RewriteDecider, self).__init__()
        self._input_size = hps["encoder"]["output_size"]
        self.layers = nn.Sequential(
            nn.Linear(self._input_size, hps["rewriter_hidden_size"]),
            nn.Sigmoid(),
            nn.Linear(hps["rewriter_hidden_size"], 1),
            nn.Sigmoid()
        )
        self.to(device)
        self.optim = optim.Adam(self.parameters(), lr=hps["learning_rate"])

    def forward(self, rnn_outputs: List[torch.Tensor]) -> torch.Tensor:
        """
        :param rnn_outputs: list of [1, complexity_feature_hidden_state]
        :return:
        """
        outputs = list()
        for encoding in rnn_outputs:
            outputs.append(self.layers(encoding))
        outputs = torch.cat(outputs, dim=1)
        return outputs

if __name__ == "__main__":
    import numpy as np

    hps = dict(complexity_split=10, encoder=dict(output_size=36), rewriter_hidden_size=32)
    rnn_output = [torch.tensor(np.random.uniform(-50, 50, [1, 1, 36]), dtype=torch.float)
                  for _ in range(10)]
    classifier = RewriteDecider(hps)
    print(classifier(rnn_output).shape)


from django.urls import path, include

from .views import (
	ArticuloListView,
	ArticuloCreateView,
	ArticuloUpdateView,
	ArticuloDeleteView,
	Ingreso_DetalleCreateView,
	Salida_DetalleCreateView,
	GeneratePDF
	)

urlpatterns = [
	path("listarArticulo", ArticuloListView.as_view(), name="Listar_Articulo"),
	path("crearArticulo", ArticuloCreateView.as_view(), name="Crear_Articulo"),
	path("<pk>/editar", ArticuloUpdateView.as_view(), name="Editar_Articulo"),
	path("<pk>/borrar/", ArticuloDeleteView.as_view(), name="Eliminar_Articulo"),

	path('ingresar_articulo/', Ingreso_DetalleCreateView.as_view(), name='ingreso_articulo_detalle'),
	path('salir_articulo/', Salida_DetalleCreateView.as_view(), name='salida_articulo_detalle'),
	path('ingresos/', include('ingresos.urls')),
	path('salidas/', include('salidas.urls')),
	path('pdf', GeneratePDF.as_view())
	]


from selenium import webdriver
import time
import os
from openpyxl import Workbook
import pandas as pd
from openpyxl.utils.dataframe import dataframe_to_rows

from links import links, country_name, zipcode, radius
from parsers.parsers import PageParser

'''
Files takes in list object from links.py
will take list object and then run chromedriver on each link in list
take data [list object] from each individual page and add to dictionary
take dictionary and write to excel file
'''

data=[]
chrome = webdriver.Chrome(executable_path = 'PASTE YOUR PATH HERE -- ex: /usr/local/bin/chromedriver.exec')

#loop iterates through each link.. goes to page.. and extracts info
for i in links:
    chrome.get(i)
    time.sleep(2)
    parser=PageParser(chrome)
    dealer = parser.extract_dealer_info
    data.append(dealer)
    time.sleep(1)

#converting data to dataframe
dataframe= pd.DataFrame(data=data, columns=['Dealer Name', 'Address', 'City', 'Website'])

#writing to excel file
i=0
file = f'cedia_dealers_{country_name}_{zipcode}_{radius.strip()}.xlsx'
while os.path.exists(file):
    i+=1
    file= f'cedia_dealers{i}_{country_name}_{zipcode}_{radius.strip()}.xlsx'

wb = Workbook()

ws = wb.active
ws.column_dimensions['A'].width = 40
ws.column_dimensions['B'].width = 40
ws.column_dimensions['C'].width = 40
ws.column_dimensions['D'].width = 40
ws.column_dimensions['E'].width = 40

for r in dataframe_to_rows(dataframe):
    ws.append(r)

wb.save(filename=file)
chrome.close()
print(f'File Saved.. {file}')


import requests
from datetime import datetime, timedelta
import time
import config
import sys
import logging

logging.basicConfig(level=logging.INFO, filename=config.LOGS, format='%(asctime)s-%(levelname)s-%(message)s')
logging.getLogger().addHandler(logging.StreamHandler(sys.stdout))

age = config.age
pincodes = config.pincodes
CHAT_ID=config.CHAT_ID
print_flag = 'Y'
print("Starting search for Covid vaccine slots!")
print(pincodes)
NOTIFICATION_FLAG=1

def telegram_bot_sendtext(bot_message,bot_chatID):
    try:
        bot_token = config.bot_token
        send_text = 'https://api.telegram.org/bot' + bot_token + '/sendMessage?chat_id=' + bot_chatID + '&parse_mode=Markdown&text=' + bot_message

        response = requests.get(send_text)

        return response.json()
    except Exception as e:
        #logging.error('Unable to notify due to {}'.format(e),exc_info=True )
        print("UNABLE TO NOTIFY")

while True:
    counter = 0

    for pincode in pincodes:
        actual = datetime.today()
        given_date = actual.strftime("%d-%m-%Y")
        URL = "https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByPin?pincode={}&date={}".format(pincode, given_date)
        header = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.76 Safari/537.36'}
        try:
            result = requests.get(URL, headers=header)
        except Exception as e:
            #logging.error('Unable to get availability due to {} '.format(e),exc_info=True)
            print("NO/POOR INTERNET CONNECTION ON  ",datetime.now())
            continue
        #print(result,given_date)
        if result.ok:
            response_json = result.json()
            if response_json["centers"]:
                if(print_flag.lower() =='y'):
                    for center in response_json["centers"]:
                        for session in center["sessions"]:
                            f=False
                            if (config.DOSE2 and session["available_capacity_dose2"] > 0):
                                f=True
                            if (config.DOSE1 and session["available_capacity_dose1"] > 0):
                                f=True
                            if (session["min_age_limit"] <= age and session["available_capacity"] > 0 and f ):
                                msg='FOR AGE {} :\n'.format(age)
                                print('Pincode: ' + pincode)
                                msg+='Pincode: ' + pincode+"\n"
                                print("Available on: {}".format(session['date']))
                                msg+="Available on: {} \n".format(session['date'])
                                print("\t", center["name"])
                                msg+=center["name"]+"\n"
                                print("\t", center["block_name"])
                                msg+=center["block_name"]+"\n"
                                print("\t Price: ", center["fee_type"])
                                msg+="Price: " + str(center["fee_type"])+"\n"
                                print("Availablity : ", session["available_capacity"])
                                msg+="Availablity : "+str(session["available_capacity"])+"\n"
                                print("\t DOSE_1: ", session["available_capacity_dose1"])
                                msg+="DOSE 1 : "+str(session["available_capacity_dose1"])+"\n"
                                print("\t DOSE_2 : ", session["available_capacity_dose2"])
                                msg+="DOSE 2 : "+str(session["available_capacity_dose2"])+"\n"
                                if(session["vaccine"] != ''):
                                    print("\t Vaccine type: ", session["vaccine"])
                                    msg+="Vaccine type: " + session["vaccine"]
                                print("\n")
                                msg+="\n\nAS CHECKED ON: "+str(datetime.now())
                                telegram_bot_sendtext(msg,CHAT_ID[0])
                                #telegram_bot_sendtext(msg,CHAT_ID[1])
                                counter = counter + 1
        else:
            print(result)
    if not counter:
        if not NOTIFICATION_FLAG:
            telegram_bot_sendtext(":( MISSED THIS TIME :( ",CHAT_ID[0])
            #telegram_bot_sendtext(":( MISSED THIS TIME :( ",CHAT_ID[1])
            NOTIFICATION_FLAG=1
        print("No Vaccination slot available! as checked on",datetime.now())
    else:
        telegram_bot_sendtext(":) VACCINE AVAILABLE, BOOK YOUR SLOT ASAP :)",CHAT_ID[0])
        #telegram_bot_sendtext(":) VACCINE AVAILABLE, BOOK YOUR SLOT ASAP :)",CHAT_ID[1])
        NOTIFICATION_FLAG=0
        print("Vaccination slot AVAILABLE AND NOTIFIED! on ",datetime.now())

    time.sleep(10)


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# coding: utf-8
import numpy as np
from scipy.optimize import fmin_bfgs

def sigmoid(z):
    g = 1.0/(1.0+np.exp(-z))
    return g

def lrGD(theta, *args):
    theta = np.matrix(theta).transpose()
    X, y, the_lambda = args
    m = y.shape[0]
    grad = np.zeros(theta.shape)

    htheta = sigmoid(X * theta)
    grad[0] = 1.0/m * np.sum(np.array((htheta-y))*np.array(X[:, 0:1]))
    for i in range(1, theta.shape[0]):
        grad[i] = 1.0/m * np.sum(np.array((htheta-y)) *
                                 np.array(X[:, i:i+1])) + the_lambda/m*theta[i]
    return grad.flatten()

def lrCostFunction(theta, *args):
    theta = np.matrix(theta).transpose()
    X, y, the_lambda = args
    m = y.shape[0]
    htheta = sigmoid(X*theta)
    J = -np.sum(np.array(y)*np.array(np.log(htheta)) + np.array((1-y))
                * np.array(np.log(1-htheta)))/m + the_lambda*np.sum(
        np.array(theta[1:]) * np.array(theta[1:]))/(2*m)
    return J

def oneVsAll(X, y, num_labels, the_lambda):
    m, n = np.shape(X)
    all_theta = np.matrix(np.zeros((num_labels, n+1)))
    X = np.hstack((np.ones((m, 1)), X))
    initial_theta = np.zeros((n+1, 1))
    for c in range(num_labels):
        print ('Training for %d/%d' % (c+1, num_labels))
        args = (X, (y == c), the_lambda)
        theta = fmin_bfgs(lrCostFunction, initial_theta,
                          fprime=lrGD, args=args, maxiter=50)
        all_theta[c, :] = theta.transpose()

    return all_theta

def train():
    '''开始训练'''
    num_labels = 36
    the_lambda = 0.1
    data = np.matrix(np.loadtxt('data.dat'))
    y = data[:, 336]
    X = data[:, :336]
    all_theta = oneVsAll(X, y, num_labels, the_lambda)
    ''' 生成训练后的特征文本 '''
    np.savetxt('training_model/scipy_model/theta.dat', all_theta)

train()


import csv
import errno
import sys
import os
import map_reader
import xlxcreator
from collections import OrderedDict

def get_prj_names(full_path):
    build_conf = os.path.basename(os.path.dirname(full_path))
    project_name = os.path.splitext(os.path.basename(full_path))[0]
    return project_name, build_conf

def adjust_maximum_name_size(current_maximum_name_size, name):
    return max(current_maximum_name_size, len(name))

def get_summary_sizes(map_file):
    section_data = map_reader.generate_report(map_file)
    otp_size = sum([int(section_data[sec]["size"], 16) for sec in section_data
                    # DA1468x layout
                    if 0x7fc0000 > int(section_data[sec]["address"], 16) >= 0x7f80000 or
                    # DA1469x layout
                    0x10090000 > int(section_data[sec]["address"], 16) >= 0x10080000])
    qspi_size = sum([int(section_data[sec]["size"], 16) for sec in section_data
                     # DA1468x layout
                     if 0xBF00000 > int(section_data[sec]["address"], 16) >= 0x8000000 or
                     # DA1469x layout, code ramapped to 0
                     0x2000000 > int(section_data[sec]["address"], 16) >= 0x00])
    ram_size = sum([int(section_data[sec]["size"], 16) for sec in section_data
                    # DA1468x layout
                    if 0x7fe0000 > int(section_data[sec]["address"], 16) >= 0x7fc0000 or
                    # DA1469x layout
                    0x20080000 > int(section_data[sec]["address"], 16) >= 0x20000000])
    cache_ram_size = sum([int(section_data[sec]["size"], 16) for sec in section_data
                          # DA1468x layout
                          if 0x8000000 > int(section_data[sec]["address"], 16) >= 0x7fe0000 or
                          # DA1469x layout
                          0x30070000 > int(section_data[sec]["address"], 16) >= 0x30060000])
    retained_size = sum([int(section_data[sec]["size"], 16) for sec in section_data
                         # DA1468x layout
                         if "RETENTION" in sec or
                         # DA1469x layout
                         ".retention" in sec])

    # Reassign done calculation if RAM target
    for key in section_data:
        # find stack segment
        if "stack" in key:
            # stack segment located within SYSRAM region for DA1469x, then it means the RAM target
            if 0 < int(section_data[key]["address"], 16) < 0x80000:
                # calculation made for QSPI size above acctually deals with RAM target where
                # all code + data lands in SYSRAM
                ram_size = qspi_size
                qspi_size = 0
                break

    if os.path.isfile(map_file[:-4] + '.bin'):
        bin_size = os.stat(map_file[:-4] + '.bin').st_size
    else:
        print("WARNING: " + map_file[:-4] + '.bin' + " does not exist")
        bin_size = 0
    return otp_size, qspi_size, ram_size, cache_ram_size, retained_size, bin_size

def main():
    search_path = os.path.abspath(sys.argv[1])
    report_name = sys.argv[2]

    # Generate CSV Folder
    csv_folder = os.path.abspath(os.path.splitext(os.path.basename(report_name))[0])
    csv_data = []

    if not os.path.exists(csv_folder):
        try:
            os.makedirs(csv_folder)
        except OSError as exc:  # Guard against race condition
            if exc.errno != errno.EEXIST:
                raise

    # Start Excel file creation
    E = xlxcreator.XLwithXlswriter(report_name)

    # Create Main worksheet
    E.create_sheet("Projects Summary")

    # Write main title
    title_data = [["Project", "Configuration", "OTP size", "QSPI size", "RAM size",
                   "Cache RAM size", "Retained RAM size", "Bin file size"]]

    csv_header = title_data[0][2:]

    E.write_worksheet("Projects Summary", 0, 0, title_data, "main row title")

    maximum_prj_name_size = len(title_data[0][0])
    maximum_cfg_name_size = len(title_data[0][1])

    # Write data
    active_row = 1
    for root, dirs, files in os.walk(search_path):
        for f in files:
            if f.endswith(".map"):
                project, configuration = get_prj_names(os.path.join(root, f))
                maximum_prj_name_size = adjust_maximum_name_size(maximum_prj_name_size, project)
                maximum_cfg_name_size = adjust_maximum_name_size(maximum_cfg_name_size,
                                                                 configuration)
                E.write_worksheet("Projects Summary", active_row, 0, [[project]],
                                  "main column title")
                E.write_worksheet("Projects Summary", active_row, 1, [[configuration]],
                                  "main column title")
                otp, qspi, ram, cram, ret, bins = get_summary_sizes(os.path.join(root, f))
                E.write_worksheet("Projects Summary", active_row, 2,
                                  [[otp, qspi, ram, cram, ret, bins]], "data")
                active_row += 1
                # Create the CSV data
                csv_data.append((os.path.join(csv_folder, "{}.csv".format('_'.join([project,
                                                                            configuration]))),
                    OrderedDict(zip(csv_header, [otp, qspi, ram, cram, ret, bins]))))

    # Set column widths
    E.set_column("Projects Summary", 0, maximum_prj_name_size + 2)
    E.set_column("Projects Summary", 1, maximum_cfg_name_size + 2)
    E.set_column("Projects Summary", 2, len(title_data[0][2]) + 2)
    E.set_column("Projects Summary", 3, len(title_data[0][3]) + 2)
    E.set_column("Projects Summary", 4, len(title_data[0][4]) + 2)
    E.set_column("Projects Summary", 5, len(title_data[0][5]) + 2)
    E.set_column("Projects Summary", 6, len(title_data[0][6]) + 2)
    E.set_column("Projects Summary", 7, len(title_data[0][7]) + 2)

    # Add an auto-filter in project name column to allow filtering out some
    E.add_drop_down_selector("Projects Summary", 0, 0, active_row - 1, 0)

    # Add comments
    E.add_comment("Projects Summary", 0, 2,
                  "The sum of sizes of sections within the OTP memory region.")
    E.add_comment("Projects Summary", 0, 3,
                  "The sum of sizes of sections within the QSPI memory region.")
    E.add_comment("Projects Summary", 0, 4,
                  "The sum of sizes of sections within the RAM memory region.")
    E.add_comment("Projects Summary", 0, 5,
                  "The sum of sizes of sections within the Cache RAM memory region.")
    E.add_comment("Projects Summary", 0, 6,
                  "The sum of sizes of sections  that end up in retained RAM.\n\n"
                  "For the SDK projects this is the sum of the sections that have"
                  " \"RETENTION\" in their name.")
    E.add_comment("Projects Summary", 0, 7,
                  "The size of the .bin file. If the .bin is not found then the size is 0.")

    # Close Excel file
    E.close_workbook()

    # Generate CSV reports
    for filename, values in csv_data:
        with open(filename, 'w') as csv_fp:
            csv_writer = csv.DictWriter(csv_fp, dialect='excel', fieldnames=values.keys())
            csv_writer.writeheader()
            csv_writer.writerow(values)

if __name__ == '__main__':
    main()


from typing import List
import numpy as np
class Solution_1:
    def maximalSquare(self, matrix: List[List[str]]) -> int:
        if not matrix:
            return 0
        res = 0
        m, n = len(matrix), len(matrix[0])
        for i in range(m):
            for j in range(n):
                matrix[i][j] = int(matrix[i][j])
        matrix = np.array(matrix, dtype=np.int)
        for i in range(m):
            for j in range(n):
                if matrix[i][j] == 1:
                    cur = 1
                    x, y = i, j
                    while x + 1 < m and y + 1 < n and matrix[x+1, y+1] == 1:
                        if np.sum(matrix[i:x+2, j: y+2]) == matrix[i:x+2, j:y+2].size:
                            cur = matrix[i:x+2, j: y+2].size
                            x += 1
                            y += 1
                        else:
                            break
                    if cur > res:
                        res = cur
        return res

class Solution_2:
    def maximalSquare(self, matrix: List[List[str]]) -> int:
        if not matrix:
            return 0
        max_len = 0
        m, n = len(matrix), len(matrix[0])
        dp = [[0]*(n+1) for _ in range(m+1)]
        for i in range(1, m+1):
            for j in range(1, n+1):
                if matrix[i-1][j-1] == '1':
                    dp[i][j] = min([dp[i-1][j], dp[i-1][j-1], dp[i][j-1]]) + 1
                    if dp[i][j] > max_len:
                        max_len = dp[i][j]
        return max_len ** 2

class Solution:
    def maximalSquare(self, matrix: List[List[str]]) -> int:
        if not matrix:
            return 0
        max_len = 0
        m, n = len(matrix), len(matrix[0])
        dp = [0]*(n+1)
        for i in range(1, m+1):
            tmp = 0
            for j in range(1, n+1):
                if matrix[i-1][j-1] == '1':
                    cur = min([dp[j-1], dp[j], tmp]) + 1
                    dp[j-1] = tmp
                    tmp = cur
                    #print(i, j, cur)
                    if cur > max_len:
                        max_len = cur
                else:
                    dp[j-1] = tmp
                    tmp = 0
            dp[n] = tmp
        return max_len ** 2

'''
matrix = [['1', '0', '1', '0', '0'],
          ['1', '0', '1', '1', '1'],
          ['1', '1', '1', '1', '1'],
          ['1', '0', '0', '1', '0']]
'''
matrix = [['1', '0', '1', '0'],
          ['1', '0', '1', '1'],
          ['1', '0', '1', '1'],
          ['1', '1', '1', '1']]

obj = Solution()
print(obj.maximalSquare(matrix))


import  qrcode #install pip qrcode
qr = qrcode.QRCode(    #make object qr and define the box size you want
        version=1,
        box_size=10,
        border=5)

data="https://www.youtube.com/channel/UC_BIOy6FLEzWGEvvkczeutg" #add link for which you want to generate qr
qr.add_data(data)
qr.make(fit=True)
img=qr.make_image(fill="black",back_color="white")
img.save("myqrcode.jpg")


"""empty message

Revision ID: 06c3e3242926
Revises: 460bb858ff31
Create Date: 2021-05-13 23:42:08.735959

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '06c3e3242926'
down_revision = '460bb858ff31'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('user', sa.Column('name', sa.String(length=150), nullable=False))
    op.add_column('user', sa.Column('sex', sa.String(length=120), nullable=False))
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('user', 'sex')
    op.drop_column('user', 'name')
    # ### end Alembic commands ###


from datetime import datetime, date

import graphene
from graphene import relay

from authentication.types import UserType
from class_payment.fields import MyClassPaymentMasterField
from class_payment.fields.is_approved_class_payment_master_field import IsApprovedClassPaymentMasterField
from class_payment.fields.my_gym_class_payment_master_field import MyGymClassPaymentMasterField
from class_payment.models import ClassPaymentMaster
from class_payment.mutations.class_cancel_payment import ClassCancelPayment
from class_payment.mutations.class_deposit_without_account import ClassDepositWithoutAccount
from class_payment.mutations.hello_world import HelloWorld
from class_payment.mutations.is_approved import IsApproved
from class_payment.mutations.paid_check import PaidCheck
from class_payment.mutations.send_class_payment_alarm import SendClassPaymentAlarm
from class_payment.mutations.update_class_payment_master import UpdateClassPaymentMaster
from class_payment.types.class_payment_master.class_payment_master_node import ClassPaymentMasterNode
from class_payment.types.gym_bank_account_type import GymBankAccountType
from class_payment.types.payment_per_month_type import PaymentPerMonthType
from gym_class.models import ClassMaster, AttendanceDetail
from gym_class.types.attendance.attendance_type import AttendanceType

class Query(graphene.ObjectType):
    my_class_payment_masters = MyClassPaymentMasterField(ClassPaymentMasterNode)
    class_payment_master = relay.Node.Field(ClassPaymentMasterNode)
    my_gym_class_payment_masters = MyGymClassPaymentMasterField(ClassPaymentMasterNode, filtering_name=graphene.String(default_value=''), filtering_days=graphene.Int(default_value=3000))
    absent_history = graphene.List(AttendanceType, class_payment_master_id=graphene.Int())
    gym_account_info = graphene.Field(GymBankAccountType)
    payment_per_month = graphene.List(PaymentPerMonthType, year=graphene.Int())
    is_approved_class_payment_masters = IsApprovedClassPaymentMasterField(ClassPaymentMasterNode)
    @staticmethod
    def resolve_payment_per_month(_, info, year):
        gym = info.context.user.gym
        class_masters = gym.class_masters.all()
        gym_created = gym.date_created
        gym_created_year = int(gym_created.strftime('%Y'))
        gym_created_month = int(gym_created.strftime('%m'))
        if year == datetime.today().year:
            month = datetime.today().month
        else:
            month = 12
        payment_list = []
        for i in range(month):
            if date(gym_created_year, gym_created_month, 1) <= date(year, i+1, 1):
                class_payment_masters = ClassPaymentMaster.objects.filter(class_master__in=class_masters,
                                                                          date_paid__year=year,
                                                                          date_paid__month=i+1,
                                                                          payment_status="납부완료")
                amount = 0
                for class_payment_master in class_payment_masters:
                    amount += class_payment_master.price_to_pay
                payment_dic = {
                    'month': i+1,
                    'amount': amount
                }
                payment_list.append(payment_dic)
        return payment_list

    @staticmethod
    def resolve_gym_account_info(_, info):
        parent = info.context.user.parent
        student = parent.students.first()
        gym = student.class_master.gym
        info_dic = {
            'bank_name': gym.refund_bank_name,
            'bank_owner_name': gym.refund_bank_owner_name,
            'bank_account_no': gym.refund_bank_account_no
        }
        return info_dic

    @staticmethod
    def resolve_class_payment_masters(_, info, class_master_id=None, did_paid=None, ):
        gym = info.context.user.gym
        if class_master_id:
            class_masters = ClassMaster.objects.filter(pk=class_master_id, gym=gym)
        else:
            class_masters = gym.class_masters.all()

        if did_paid:
            return ClassPaymentMaster.objects.filter(class_master__in=class_masters,
                                                     did_paid=did_paid)
        else:
            return ClassPaymentMaster.objects.filter(class_master__in=class_masters)

    @staticmethod
    def resolve_class_payment_master(_, __, class_payment_master_id):
        return ClassPaymentMaster.objects.get(pk=class_payment_master_id)

    @staticmethod
    def resolve_absent_history(_, __, class_payment_master_id):
        class_payment_master = ClassPaymentMaster.objects.get(pk=class_payment_master_id)
        student = class_payment_master.student
        return AttendanceDetail.objects.filter(student=student, type='결석',
                                               attendance_master__date__range
                                               =[class_payment_master.date_from, class_payment_master.date_to]
                                               ).exclude(absent_reason='무단결석')

    @staticmethod
    def resolve_payment_by_parent(_, info):
        parent = info.context.user.parent.get()
        return ClassPaymentMaster.objects.filter(student__in=parent.students.all(),
                                                 payment_method="미납")

    @staticmethod
    def resolve_payment_completion_by_parent(_, info, year, month):
        parent = info.context.user.parent.get()
        new_month = 0
        new_year = 0
        if month+1 == 13:
            new_month = 1
            new_year = year+1
        return ClassPaymentMaster.objects.filter(student__in=parent.students.all(),
                                                 payment_status="완납",
                                                 date_paid__lt=datetime(new_year, new_month, 1),
                                                 date_paid__gte=datetime(year, month, 1))
    """
    @staticmethod
    def resolve_class_payment_master_by_parent(_, info):
        parent = info.context.user.parent.get()
        return ClassPaymentMaster.objects.filter(student__parent=parent,
                                                 is_approved=True)
    """

class Mutation(graphene.ObjectType):
    paid_check = PaidCheck.Field()
    class_cancel_payment = ClassCancelPayment.Field()
    class_deposit_without_account = ClassDepositWithoutAccount.Field()
    is_approved = IsApproved.Field()
    update_class_payment_master = UpdateClassPaymentMaster.Field()
    send_class_payment_alarm = SendClassPaymentAlarm.Field()
    hello_world = HelloWorld.Field()
schema = graphene.Schema(query=Query, mutation=Mutation)