#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 28 16:22:45 2018

@author: thiru
"""

from nltk.corpus import stopwords
from apple.models import *
from nltk.stem.wordnet import WordNetLemmatizer
import string
import gensim
from gensim import corpora
import pandas as pd
import datetime
import re
import pyLDAvis.gensim as gensimvis
import pyLDAvis

words_to_remove = set(['rt','apple','apple_support','applesupport','support'])
stop = set(stopwords.words('english'))
stop = stop.union(words_to_remove)

exclude = set(string.punctuation)
lemma = WordNetLemmatizer()

date_timelines = [1,7,30] #days
explained = ['daily','weekly','monthly']
dates = [datetime.date.today() - datetime.timedelta(days=x) for x in date_timelines]

def run():
    tweets = Tweet.objects.filter(created_at__gte = dates[-1]) # last
    for i in range(len(dates)-1,-1,-1):
        tweets = tweets.filter(created_at__gte = dates[i])
        num_topics = 6

        text = [x.text for x in tweets]
        preprocessed_text = preprocess_text(text)
        texts_cleaned = [(clean(x)).split() for x in preprocessed_text]
        if len(texts_cleaned) == 0:
            texts_cleaned = ['no text'.split()]
        texts_cleaned = pd.Series(texts_cleaned)

        ldamodel,dictionary,doc_term_matrix  = run_lda(texts_cleaned,num_topics)

#        results = pretty_print_results(ldamodel,num_topics,num_words = 7)
#        for entry in results:
#            print(entry)
#        print('\n')

        vis_data = gensimvis.prepare(ldamodel,doc_term_matrix, dictionary )
        pyLDAvis.save_html(vis_data,'apple/static/ldatopics/{}.html'.format(explained[i]))

    print("LDA job complete!")

def clean(doc):
    stop_free = ' '.join([i for i in doc.lower().split() if i not in stop])
    punc_free = ''.join(ch for ch in stop_free if ch not in exclude)
    normalized = " ".join(lemma.lemmatize(word) for word in punc_free.split())
    return normalized

def run_lda(doc_clean,num_topics):
    dictionary = corpora.Dictionary(doc_clean)
    doc_term_matrix = [dictionary.doc2bow(doc) for doc in doc_clean]
    lda = gensim.models.ldamodel.LdaModel
    ldamodel = lda(doc_term_matrix,num_topics=num_topics,id2word = dictionary,passes = 60)
    return ldamodel,dictionary,doc_term_matrix

def pretty_print_results(ldamodel,num_topics,num_words):
    z = ldamodel.print_topics(num_topics=num_topics,num_words=num_words)
    group = 1
    for i in range(len(z)):
        entry = z[i][1]
        entry = ''.join([i for i in entry if not (i.isdigit() or i == '.' or i == '*')])
        entry = entry.replace('"','')
        entry = '{}. {}'.format(group,entry)
        entry = entry.replace(' + ',', ')
        z[i] = entry
        group += 1
    return z

def preprocess_text(text_list):
    words_to_remove = ['rt']
    text_list = [x.lower() for x in text_list]
    # strip urls
    text_list = [re.sub(r'(https|http)?:\/\/(\w|\.|\/|\?|\=|\&|\%)*\b', '', x, flags=re.MULTILINE) for x in text_list]
    text_list = [' '.join([i for i in x.lower().split() if i not in stop]) for x in text_list]

    for word in words_to_remove:
        c = re.compile('(\s*){}(\s*)'.format(word))
        text_list = [c.sub('',x) for x in text_list]

    return text_list


# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: percent
#       format_version: '1.3'
#       jupytext_version: 1.14.5
#   kernelspec:
#     display_name: Python 3 (ipykernel)
#     language: python
#     name: python3
# ---

# %% [markdown]
# # Count peptides over all files

# %%
import os
import sys
import logging
from pathlib import Path
import random
import yaml
import json

import pandas as pd
import ipywidgets as widgets

### Logging setup ######
from vaep.logging import setup_nb_logger
setup_nb_logger()

### vaep imports ######
from vaep.io.mq import MaxQuantOutputDynamic
from vaep.io.data_objects import MqAllSummaries
from vaep.io.data_objects import PeptideCounter
import vaep.pandas

##################
##### CONFIG #####
##################
from config import FOLDER_MQ_TXT_DATA, FOLDER_PROCESSED

from config import FOLDER_DATA # project folder for storing the data
logging.info(f"Search Raw-Files on path: {FOLDER_MQ_TXT_DATA}")

# %% [markdown]
# Use samples previously loaded.

# %%
ELIGABLE_FILES_YAML = Path('config/eligable_files.yaml')
MAP_FOLDER_PATH = Path('config/file_paths')

with open(ELIGABLE_FILES_YAML) as f:
    files = set(yaml.safe_load(f)['files'])
    logging.info(f"Found a total of {len(files):,d} eligable files.")
with open(MAP_FOLDER_PATH) as f:
    folders_dict = yaml.safe_load(f)
    folders_dict = {folder: folders_dict[folder] for folder in files}  # only select folders selected

folders = [Path(folders_dict[folder]) for folder in files]
assert len(files) == len(folders_dict) == len(folders)

# %%
fn_id_old_new: str = 'data/rename/selected_old_new_id_mapping.csv' # selected samples with pride and original id
df_ids = pd.read_csv(fn_id_old_new)
df_ids

# %%
folders_dict = { sample_id: FOLDER_MQ_TXT_DATA / sample_id for sample_id in df_ids['Sample ID']}
# folders_dict = {p.stem : p.parent / p.stem for p in folders_dict}
# folders_dict

# %%
OVERWRITE = False

from config import FNAME_C_PEPTIDES, FNAME_C_EVIDENCE, FNAME_C_PG, FNAME_C_GENES

FNAME_C_PEPTIDES, FNAME_C_EVIDENCE, FNAME_C_PG, FNAME_C_GENES

# %% [markdown]
# ## Random example

# %%
import random
pd.set_option('display.max_columns', 60)
random_folder, random_path = random.sample(folders_dict.items(), 1)[0]
mq_output = MaxQuantOutputDynamic(random_path)
print(f"peptides.txt from {random_folder!s}")
mq_output.peptides

# %%
use_columns = mq_output.peptides.columns[33:45]
df = mq_output.peptides[use_columns].convert_dtypes() #.to_json('test.json')
df

# %%
df_json_string = df.to_json(orient='index', indent=4)
df_json_string[:1000]

# %%
df_csv = df.to_csv()
df_csv[:1000]

# %%
pd.read_json(df_json_string, orient='index')

# %%
mq_output.peptides.Intensity # as is in peptides.txt, comma seperated thousands

# %% [markdown]
# ## Count aggregated peptides

# %%
peptide_counter = PeptideCounter(FNAME_C_PEPTIDES, overwrite=OVERWRITE)
peptide_counter

# %%
if peptide_counter.loaded:
    print(peptide_counter.counter.most_common(10),
          len(peptide_counter.loaded),
          sep='\n')
else:
    print('New file created.')

# %% [markdown]
# - creates peptide intensity dumps for each MQ outputfolder per default `count_peptides` function (default processing function for `PeptideCounter`)

# %%
# %%time
# folders = [Path(folder_path) for folder_path in folders_dict.values()]
c = peptide_counter.sum_over_files(folders=folders)

# %%
c.most_common(10) # peptide_counter.counter.most_common(10)

# %%
# To share as python file
N = 1000
with open(FOLDER_PROCESSED / f'most_common_{10}_peptides.py', 'w') as f:
    f.write('import pandas as pd\n\n')

    #pprint.pformat list -> do this using standardlibrary
    # https://docs.python.org/3/library/pprint.html
    f.write(f"most_common = [\n  ")
    f.write(',\n  '.join(f"{str(t)}" for t in c.most_common(N)))
    f.write("\n]\n\n")

    #peptide_counter.loaded()

    f.write("pd.DataFrame.from_records(most_common, index='Sequence', columns=['Sequence', 'counts'])\n")

# %% [markdown] Collapsed="false"
# ## Peptides by charge
#
# - count peptides by charge state (which are aggregated in `peptides.txt`)

# %%
evidence_cols = vaep.pandas.get_columns_accessor(mq_output.evidence.reset_index())
evidence_cols # vaep.mq get this list

# %%
evidence = mq_output.evidence.set_index(evidence_cols.Charge, append=True)
evidence

# %% [markdown]
# Modifikationen könnten noch zum index hinzugefügt werden

# %%
evidence.Modifications.value_counts()

# %%
vaep.pandas.prop_unique_index(evidence)

# %% [markdown]
# Using the protein AA sequence and it's charge as identifiers, does not yield a unique index.
#
# First potential contaminants and peptides with zero intensity (or missing intensity) can be removed from the table.
#
# These are apparently peptides identified by an MS2 spectrum but which could not be quantified by a MS1 scans

# %%
mask =  evidence[evidence_cols.Intensity].isna()
evidence.loc[mask, evidence_cols.Type].value_counts()

# %%
evidence_cols = vaep.io.data_objects.evidence_cols
use_cols = [evidence_cols.mz, evidence_cols.Protein_group_IDs, evidence_cols.Intensity, evidence_cols.Score, evidence_cols.Potential_contaminant]

evidence_selected = vaep.io.data_objects.select_evidence(evidence[use_cols])
evidence_selected

# %%
evidence_selected = evidence_selected.sort_values(by=['Sequence', 'Charge', 'Score'], ascending=False)
evidence_selected

# %%
evidence_selected = vaep.pandas.select_max_by(evidence_selected.reset_index(), [evidence_cols.Sequence, evidence_cols.Charge], evidence_cols.Score)
evidence_selected

# %%
from collections import Counter
c = Counter()
c.update(evidence.index)
c.most_common(10)

# %%
example = evidence.loc[c.most_common(10)[0][0]]

vaep.pandas.show_columns_with_variation(example)

# %% [markdown]
# - `Type`: only `MULTI-MSMS` and `MULIT-SECPEP` are quantified (does this mean a matching MS1 spectrum?)

# %%
evidence[evidence_cols.Type].value_counts()

# %% [markdown]
# Some peptides can be assigned to different protein group IDs (razor peptides)
#  - option: discared non-unique peptides (and Protein group IDs can be already a combination of several isotopes)
#  - option: select on `Score` or `Intensity` (is there a relationship?)
#  - option: select based on `Number of isotopic peaks`

# %%
evidence[evidence_cols.Protein_group_IDs].value_counts()

# %% [markdown]
# ## Count peptides based on evidence files

# %%
evidence_counter = vaep.io.data_objects.EvidenceCounter(FNAME_C_EVIDENCE, overwrite=OVERWRITE)
c = evidence_counter.sum_over_files(folders=folders)

# %% [markdown]
# ## Protein Groups
#
# - protein groups between files
#     - aggregate by GENE ?
#     -

# %%
mq_output.proteinGroups.describe(include='all')

# %%
pg_cols = vaep.pandas.get_columns_accessor(mq_output.proteinGroups.reset_index())
pg_cols

# %%
use_cols = [
# pg_cols.Protein_IDs,
 pg_cols.Majority_protein_IDs,
 pg_cols.Gene_names,
 pg_cols.Evidence_IDs,
 pg_cols.Q_value,
 pg_cols.Score,
 pg_cols.Only_identified_by_site,
 pg_cols.Reverse,
 pg_cols.Potential_contaminant,
 pg_cols.Intensity,
]

pd.options.display.max_rows = 100
pd.options.display.min_rows = 40
mask = mq_output.proteinGroups[[pg_cols.Only_identified_by_site, pg_cols.Reverse, pg_cols.Potential_contaminant]].notna().sum(axis=1) > 0
mq_output.proteinGroups.loc[mask, use_cols]

# %%
msg = "Omitting the data drops {0:.3f} % of the data."
print(msg.format(
mask.sum() / len(mask) * 100
))

# %%
selection = mq_output.proteinGroups.loc[~mask, use_cols]
gene_counts = selection[pg_cols.Gene_names].value_counts() # Gene Names not unique
msg = 'proportion of entries with non-unique genes: {:.3f}'
print(msg.format(gene_counts.loc[gene_counts > 1].sum() / gene_counts.sum()))
gene_counts.head(20)

# %%
mask = selection.Intensity > 0
msg = "Proportion of non-zero Intensities: {:.3f} (zero_ count = {})"
print(msg.format(mask.sum() / len(mask), (~mask).sum()))
selection.loc[~mask]

# %%
selection = selection.loc[mask]

# %% [markdown]
# Some Proteins have no gene annotation
#   - P56181 -> mitochondrial
#
# In the online version of Uniprot these seems to be annotated (brief check).
# So latest version probably has a gene annotation, so therefore these files are kept

# %%
gene_set = selection[pg_cols.Gene_names].str.split(';')

col_loc_gene_names = selection.columns.get_loc(pg_cols.Gene_names)
_ = selection.insert(col_loc_gene_names+1, 'Number of Genes', gene_set.apply(vaep.pandas.length))

mask = gene_set.isna()
selection.loc[mask]

# %%
cols = vaep.pandas.get_columns_accessor(selection)
gene_counts = vaep.pandas.counts_with_proportion(selection[cols.Number_of_Genes])
gene_counts

# %% [markdown]
# Most `proteinGroups` have single genes assigned to them. If one only looks at gene sets,
# one can increase uniquely identified `proteinGroups` further.
#
# > Can `geneGroups` (sets of `Gene Names`) be used instead of `proteinGroups`?

# %%
gene_sets_counts = selection[cols.Gene_names].value_counts()
gene_sets_counts.value_counts()

# %% [markdown]
# Potential solutions:
# - summarize intensity per gene. One of the isoforms seems to have the major proportion of intensity assigned.
# - select maximum by score (higher scores seem to be related to higher intensity)

# %%
non_unique_genes = gene_sets_counts.loc[gene_sets_counts > 1].index

mask = selection[cols.Gene_names].isin(non_unique_genes)
selection.loc[mask].reset_index().set_index(cols.Gene_names).sort_index()

# %% [markdown]
# Protein Groups with Gene set with three and more genes:

# %%
selection.loc[selection[cols.Number_of_Genes] > 2]

# %%
logging.info(f"Selection shape before dropping duplicates by gene: {selection.shape}")
mask_no_gene = selection[pg_cols.Gene_names].isna()
selection_no_gene = selection.loc[mask_no_gene]
logging.info(f"Entries without any gene annotation: {len(selection_no_gene)}")
selection_no_gene

# %%
selection = vaep.pandas.select_max_by(df=selection.loc[~mask_no_gene].reset_index(), grouping_columns=[pg_cols.Gene_names], selection_column=pg_cols.Score)
logging.info(f"Selection shape after  dropping duplicates by gene: {selection.shape}")
selection = selection.set_index(pg_cols.Protein_IDs)
mask = selection[cols.Gene_names].isin(non_unique_genes)
selection.loc[mask]

# %%
selection = selection.append(selection_no_gene)

# %%
protein_groups_counter = vaep.io.data_objects.ProteinGroupsCounter(FNAME_C_PG, overwrite=OVERWRITE)
c = protein_groups_counter.sum_over_files(folders=folders)

# %%
vaep.pandas.counts_with_proportion(pd.Series(c)) # Most proteinGroups are unique

# %% [markdown]
# ### Count genes
# Genes sets could be used to identify common features.
#
# > The assignment of isoforms to one proteinGroup or another might be volatile.
# > A single (unique) peptide could lead to different assignments.
# > Imputation on the evidence level could be a way to alleviate this problem
#
# - If genes set are not unique for a single run, one would have to decide which to take

# %%
gene_counter = vaep.io.data_objects.GeneCounter(FNAME_C_GENES, overwrite=OVERWRITE)

if not gene_counter.dumps:
    #empty dict, replace
    gene_counter.dumps = dict(protein_groups_counter.dumps) # prot proteinGroups files to GeneCounter
pg_dumps = list(gene_counter.dumps.values())

c_genes = gene_counter.sum_over_files(folders=pg_dumps)

c_genes = pd.Series(c_genes)
vaep.pandas.counts_with_proportion(c_genes) # Most proteinGroups are unique

# %% [markdown] Collapsed="false"
# ## Theoretial Peptides from used fasta-file
#
# > `01_explore_FASTA.ipynb` (formely `misc_FASTA_tryptic_digest.ipynb`)

# %% [markdown]
#


import numpy as np
import scipy.io as scio

def ReadData1(data_All1):
    data_cup1_tmp = []
    data_cup1_tmp0 = np.concatenate((data_All1[0],
                                     np.zeros((2000 - data_All1[0].shape[0], 39))))
    for i in range(1, 100):
        data_cup1_tmp = np.concatenate((data_All1[i],
                                        np.zeros((2000 - data_All1[i].shape[0], 39))))
        if i == 1:
            data_cup1_tmp_a = np.array(data_cup1_tmp0)
            data_cup1_tmp_b = np.array(data_cup1_tmp)
            data_cup11 = np.array([data_cup1_tmp_a, data_cup1_tmp_b])
        else:
            data_cup1_com_1 = np.append(data_cup11, data_cup1_tmp)
            data_cup1_dim = data_cup11.shape
            data_cup11 = data_cup1_com_1.reshape(data_cup1_dim[0] + 1,
                                                 data_cup1_dim[1],
                                                 data_cup1_dim[2])

    return data_cup11

def LoadData():
    path_base_cat1 = '/Users/mengxue/Documents/Paper/ChestAuthentication/Material/BigData/Final/GQY/Matlab/HeySiri'
    path_base_cat2 = '/Users/mengxue/Documents/Paper/ChestAuthentication/Material/BigData/Final/DengYangTao/Matlab/HeySiri'
    path_base_cat3 = '/Users/mengxue/Documents/Paper/ChestAuthentication/Material/BigData/Final/WuYuan/Matlab/HeySiri'
    path_base_cat4 = '/Users/mengxue/Documents/Paper/ChestAuthentication/Material/BigData/Final/HuHaiYan/Matlab/HeySiri'
    path_base_cat5 = '/Users/mengxue/Documents/Paper/ChestAuthentication/Material/BigData/Final/OuRunMin/Matlab/HeySiri'

    cupLabels = []

    # Get the data from person one.
    for i in range(1, 101):
        path_tmp = path_base_cat1 + str(i) + '.mat'
        data_tmp = scio.loadmat(path_tmp)
        together_data = np.concatenate((data_tmp['cor1_ccc2'], data_tmp['cor2_ccc2']), axis=0)
        if i == 1:
            data_All1 = [together_data]
            cupLabels.append('A')  # label person 1
        else:
            data_All1.append(together_data)
            cupLabels.append('A')  # label person 1
    data_cup1 = ReadData1(data_All1)
    # print("Person1 shape", data_cup1.shape, '\t', "Person1 data num", len(data_All1))

    # Get the data from person two.
    for i in range(1, 101):
        path_tmp = path_base_cat2 + str(i) + '.mat'
        data_tmp = scio.loadmat(path_tmp)
        together_data = np.concatenate((data_tmp['cor1_ccc2'], data_tmp['cor2_ccc2']), axis=0)
        if i == 1:
            data_All2 = [together_data]
            cupLabels.append('B')  # label person 2
        else:
            data_All2.append(together_data)
            cupLabels.append('B')  # label person 2
    data_cup2 = ReadData1(data_All2)
    # print("Person2 shape", data_cup2.shape, '\t', "Person2 data num", len(data_All2))

    # # Get the data from person Three.
    for i in range(1, 101):
        path_tmp = path_base_cat3 + str(i) + '.mat'
        data_tmp = scio.loadmat(path_tmp)
        together_data = np.concatenate((data_tmp['cor1_ccc2'], data_tmp['cor2_ccc2']), axis=0)

        if i == 1:
            data_All3 = [together_data]
            cupLabels.append('C')  # label person 3
        else:
            data_All3.append(together_data)
            cupLabels.append('C')  # label person 3
    data_cup3 = ReadData1(data_All3)
    # print("Person3 shape", data_cup3.shape, '\t', "Person3 data num", len(data_All3))

    # Get the data from person Four.
    for i in range(1, 101):
        path_tmp = path_base_cat4 + str(i) + '.mat'
        data_tmp = scio.loadmat(path_tmp)
        together_data = np.concatenate((data_tmp['cor1_ccc2'], data_tmp['cor2_ccc2']), axis=0)

        if i == 1:
            data_All4 = [together_data]
            cupLabels.append('D')  # label person 3
        else:
            data_All4.append(together_data)
            cupLabels.append('D')  # label person 3
    data_cup4 = ReadData1(data_All4)
    # print("Person4 shape", data_cup4.shape, '\t', "Person4 data num", len(data_All4))

    # Get the data from person Five.
    for i in range(1, 101):
        path_tmp = path_base_cat5 + str(i) + '.mat'
        data_tmp = scio.loadmat(path_tmp)
        together_data = np.concatenate((data_tmp['cor1_ccc2'], data_tmp['cor2_ccc2']), axis=0)

        if i == 1:
            data_All5 = [together_data]
            cupLabels.append('E')  # label person 3
        else:
            data_All5.append(together_data)
            cupLabels.append('E')  # label person 3
    data_cup5 = ReadData1(data_All5)
    # print("Person5 shape", data_cup5.shape, '\t', "Person5 data num", len(data_All5))

    # Concatenate the data off three people.
    data_cup = np.concatenate((data_cup1, data_cup2, data_cup3, data_cup4, data_cup5), axis=0)  #

    return data_cup, cupLabels


import requests
import bs4
from bs4 import BeautifulSoup as BS
import re
URL = "https://www.google.com/search?q=chees"
def start_ref(q):
    global data
    data = {}
    headers = {
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
    "User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:99.0) Gecko/20100101 Firefox/99.0"
    }
    req = requests.get(q, headers=headers)
    src = req.text
    with open('indextest.html', 'w') as f:
        f.write(src)
    soup = BS(src, "lxml")
#    menu = soup.find("h3").find_parents("div", id="search").
    menu = soup.find_all("h3")

    for i in menu:

        inmenu = i.find_parent().find_parent().find_parent().find_parent()
        for q in 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12:
            lincs = inmenu.get("href")
            h3 = inmenu.find("h3").text
            span = inmenu.find("span")

            data = {q: [lincs, h3, span]}
            print(data)

    return 0

if __name__ == '__main__':
     start_ref(URL)


import pandabear as pd
import matplotlib.pyplot as plt

from sklearn.model_selection import  train_test_split
from sklearn.neighbors import KNeighborsClassifier
# KNN 알고리즘 사용을 위한 모듈

 #1. 전처리 하기 전 데이터 확인

data = pd.read_csv ("sample_iris.csv")
print(data.head())

# 지도학습 데이터

X = data.values[:, :data.shape[1] - 1]
Y = data.values[:, data.shape[1] - 1]

# X 트레인과 Y 트레인 train test split : 데이터를 마구 섞는다.

X_train, X_test, Y_train, Y_test = train_test_split(X,Y, test_size= 0.3,random_state=1)

# 2개의 개체를 다음과 같이 나눈다. 값을 유지시키기 위해서 고정값 1

knn = KNeighborsClassifier(n_neighbors=5)
# 거리값  K  거리값 정의하고 학습하는 것

knn.fit(X_train,Y_train)
#이 트레이닝으로 학습을 하겠다. 라는 의미
print('KNN iris of acc > ' , format(knn.score(X_test, Y_test)*100)) # X Y 트레인을 비교해서 값을 나타낸 것,

train_acc = [] # 트레이닝 정확도
test_acc = []  # 테스트 정확도를 리스트에 담아둠
k_list = range(1, 100) # 1부터 31까지를 / 거리값을 계산시 위치를 바꿔주면됌
for k in k_list:
    clf = KNeighborsClassifier(n_neighbors=k) #1부터 30까지의 모든 거리 값을 다 계산
    clf.fit(X_train, Y_train)
    train_acc.append(clf.score(X_train, Y_train)) # 리스트 증가 clf = knn
    test_acc.append(clf.score(X_test, Y_test)) # 트레이닝한거 테스트한거

# 시각화
plt.plot(k_list, test_acc, label= 'test_acc of ')
plt.plot(k_list, train_acc, label = 'train _ acc of')

plt.xlabel('k')
plt.ylabel('acc')
plt.legend()
plt.show()

 # > 여기까지가 KNN


#coding=utf-8
import requests
import time
import pandas as pd
import numpy as np

def get_data():
    data = pd.DataFrame()
    for i in range(1,98):
        url = f"http://www.lottery.gov.cn/historykj/history_{i}.jspx?_ltype=dlt"
        res = requests.get(url,headers=header).text  #获取网页内容
        df = pd.read_html(res)[0]  #使用pandas快速获取表格数据
        data = data.append([df])
        time.sleep(0.1)
    return data

def remake_data():
    data_list = []
    for list in get_data().values:
        # 将8到15之间的数据两两相加，再插入数组中
        list = np.insert(list, 8,[list[8] + list[10], list[9] + list[11], list[12] + list[14], list[13] + list[15]])
        list = np.delete(list, -4)  # 删除“nan“
        list = np.delete(list, np.s_[12:20])  # 删除“8到15之间的数据“
        # 将1到7之间的数据两两相减，再添加到数组末尾
        list = np.append(list, [list[2] - list[1], list[3] - list[2], list[4] - list[3], list[5] - list[4],list[7] - list[6]])

        data_list.append(list)  #将处理好的数据装入空列表data_list中
    #用new_title 作为key ，data_list作为values 创建一个二维数组
    dataframe = pd.DataFrame(data_list, columns=new_title)
    return dataframe

if __name__ == '__main__':
    #url = f"http://www.lottery.gov.cn/historykj/history_1.jspx?_ltype=dlt"
    header = {"User-Agent": "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1 Trident/5.0;"}
    new_title = ['期号', 'first1', 'first2', 'first3', 'first4', 'first5', 'last1', 'last2', '一等奖注数', '一等奖奖金(元)',
                 '二等奖注数', '二等奖奖金(元)', '销售额(元)', '奖池奖金（元）', '开奖日期', '1~2', '2~3', '3~4', '4~5', 'last1~last2']

    #remake_data().to_excel("lottery1.xls",index=False)
    remake_data().to_csv("lottery1.csv",index=False)


import pandas as pd
import numpy as np

from sklearn.linear_model import LogisticRegression
from sklearn.cross_validation import KFold, StratifiedKFold, cross_val_score
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import LabelEncoder, LabelBinarizer

import matplotlib.pyplot as plt
from sklearn_pandas import DataFrameMapper

DATA = '../data/'

mapper = DataFrameMapper([
    ('MSSubClass', None),
    ('LotArea', None),
    ('LotShape', LabelBinarizer()),
])

features = [
    'MSSubClass',
    'LotArea',
    'LotShape',
]

pipeline = Pipeline([
    ('featurize', mapper),
    ('regr', LogisticRegression(fit_intercept=True)),
])

fts = [
    'MSSubClass',
    'LotArea',
    'LotShape',
]

fts1 = fts + ['SalePrice']

def load():
    df_train = pd.read_csv(DATA + 'train.csv', header = 0, index_col = 'Id')
    df_test = pd.read_csv(DATA + 'test.csv', header = 0, index_col = 'Id')

    return df_train, df_test

def main():
    df_train, df_test = load()

    import pdb; pdb.set_trace()

if __name__ == '__main__':
    main()


import pandas
MARKER = b'PAR1'
import struct
import msgpack
from io import BytesIO as cStringIO
from array import array
import zlib
import numpy
def differ(first, second):
    return [item for item in first if item not in second]

def globalorder(f,gdict, chunk, ordering):
    output = cStringIO()
    output.truncate(0)
    headindex = [0]*4
    minmax = [None]*2
    minmax[0] = min(chunk)
    minmax[1] = max(chunk)
    type = 'i'*len(headindex)
    output.write(struct.pack(type, *headindex))
    l1 = output.tell()
    output.write(msgpack.dumps(minmax))
    l2 = output.tell()
    headindex[0] = l2-l1
    v = len(gdict)
    newvals = differ(set(list(chunk)), gdict)
    for val in newvals:
          gdict[val] = v
          v+=1

    l3 = output.tell()
    if minmax[0] != minmax[1]:
        offsets = [gdict[y] for y in chunk]
        ll = max(offsets)
        if ll<256:
            type1 = 'B'
            headindex[2] = 2
        elif ll<65536:
            type1 = 'H'
        #type1 = np.int16
            headindex[2] = 1
        else:
            type1 = 'i'
        #type1 = np.int32
        output.write(struct.pack(type1*len(offsets), *offsets))
    else:
    	output.write(struct.pack('i',gdict[minmax[0]]))
    #output.write(array(type1,[coldict[y] for y in chunk]).tostring())
    l4 = output.tell()
    headindex[1] = l4-l3
    headindex[3] = len(chunk)
    output.seek(0)
    output.write(struct.pack(type, *headindex))
    return output.getvalue()

def write_global(data,filen, ordering, row_group_offsets):
    gdict = {}
    blocksdump = []
    f = open(filen,"w+b")
    f.write(MARKER)
    headindex = [0]*4
    l = len(data)
    headindex[0] = l
    headindex[1] = len(data.columns)
    type = 'i'*len(headindex)
    f.write(struct.pack(type, *headindex))
    nparts = max((l - 1) // row_group_offsets + 1, 1)
    chunksize = max(min((l - 1) // nparts + 1, l), 1)
    blocks = [0]*(nparts+1)
    blocks[nparts] = chunksize
    headindex[3] = nparts
    blocksind = f.tell()
    f.write(struct.pack('L'*len(blocks),*blocks))
    for col in data:
        i=0
        j=1
        for part in range(nparts):
            chunk = data[col][chunksize*i:chunksize*j]
            blocksdump.append(globalorder(f,gdict, chunk, ordering))
            i+=1
            j+=1
    l1 = f.tell()
    f.write(msgpack.dumps(sorted(gdict.keys(), key=gdict.get)))
    l2 = f.tell()
    headindex[2] = l2-l1
    f.seek(4)
    f.write(struct.pack(type, *headindex))
    f.seek(l2)
    i = 0
    for bl in blocksdump:
        blocks[i] = f.tell()
        f.write(bl)
        i+=1
    f.seek(blocksind)
    f.write(struct.pack('L'*len(blocks),*blocks))

import numpy as np

def globalindirect(f,global_dict,ordering,chunk):
    output = cStringIO()
    indirect = 0
    output.truncate(0)
    headindex = [0]*7
    minmax = [None]*2
    minmax[0] = min(chunk)
    minmax[1] = max(chunk)
    local_dict = np.sort(chunk.unique())

    type = 'i'*len(headindex)
    output.write(struct.pack(type, *headindex))
    l1 = output.tell()
    output.write(msgpack.dumps(minmax))
    l2 = output.tell()
    headindex[0] = l2-l1
    coldict = {}

    size_1 = len(local_dict)
    size_2 = len(global_dict)

    res2 = [None]*size_1
    i, j = 0, 0
    if not ordering:
      res2 = list(np.intersect1d(local_dict,global_dict,assume_unique=True,return_indices=True)[2])
    else:
      while i < size_1:
        if local_dict[i] > global_dict[j]:
            j+=1
        elif local_dict[i] == global_dict[j]:
          res2[i] = j
          i += 1
          j += 1

    ### chose if indirect
    locallen = len(local_dict)
    maxres = max(res2)
    globallen = len(global_dict)
    datalen = len(chunk)
    if locallen < 256:
            s1 = 1
    elif locallen < 65536:
            s1 = 2
    else:
            s1 = 4
    if globallen < 256:
            s2 = 1
    elif globallen < 65536:
            s2 = 2
    else:
            s2 = 4
    if maxres < 256:
            s3 = 1
    elif maxres < 65536:
            s3 = 2
    else:
            s3 = 4

    if datalen*s1 + locallen*s3 < datalen*s2:
        indirect = 1
    #######################

    if indirect:
        coldict = dict(((x, y) for y, x in enumerate(local_dict)))
    else:
        coldict = dict(((x, y) for y, x in enumerate(global_dict)))

    l3 = output.tell()
    if minmax[0] != minmax[1]:
        offsets = [coldict[y] for y in chunk]
        ll = max(offsets)
        if ll<256:
            type1 = 'B'
            headindex[2] = 2
        elif ll<65536:
            type1 = 'H'
        #type1 = np.int16
            headindex[2] = 1
        else:
            type1 = 'i'
        #type1 = np.int32
        globall = max(res2)
        if globall<256:
            type2 = 'B'
            headindex[5] = 2
        elif globall<65536:
            type2 = 'H'
        #type1 = np.int16
            headindex[5] = 1
        else:
            type2 = 'i'
        l4 = output.tell()
        if indirect:
            output.write(struct.pack(type2*len(res2), *res2))
            l4 = output.tell()
            headindex[4] = l4-l3 # here is the size of the indirect mapping stored after minmax
            headindex[6] = len(res2)
        output.write(struct.pack(type1*len(offsets), *offsets))
    else:
        output.write(struct.pack('i',int(np.where(global_dict == minmax[0])[0])))
        headindex[6] = 1
        l4 = output.tell()
        headindex[4] = l4-l3
        output.write(struct.pack('i',coldict[minmax[0]]))
    #output.write(array(type1,[coldict[y] for y in chunk]).tostring())
    l5 = output.tell()
    headindex[1] = l5-l4
    headindex[3] = len(chunk)
    output.seek(0)
    output.write(struct.pack(type, *headindex))
    f.write(output.getvalue())

def write_indirect(data,filen, ordering, row_group_offsets):
    f = open(filen,"w+b")
    f.write(MARKER)
    headindex = [0]*4
    l = len(data)
    headindex[0] = l
    headindex[1] = len(data.columns)
    type = 'i'*len(headindex)
    f.write(struct.pack(type, *headindex))
    nparts = max((l - 1) // row_group_offsets + 1, 1)
    chunksize = max(min((l - 1) // nparts + 1, l), 1)
    blocks = [0]*(nparts+1)
    blocks[nparts] = chunksize
    headindex[3] = nparts
    blocksind = f.tell()
    f.write(struct.pack('L'*len(blocks),*blocks))

    headindex[3] = nparts
    for col in data:
        l1 = f.tell()
        if ordering == 1:
            global_dict = sorted(data[col].unique())
            f.write(msgpack.dumps(global_dict))
        else:
            global_dict = data[col].unique()
            f.write(msgpack.dumps(list(global_dict)))
        l2 = f.tell()
        headindex[2] = l2-l1
        f.seek(4)
        f.write(struct.pack(type, *headindex))
        f.seek(l2)
        i=0
        j=1
        for part in range(nparts):
            blocks[i] = f.tell()
            chunk = data[col][chunksize*i:chunksize*j]
            globalindirect(f,global_dict,ordering,chunk)
            i+=1
            j+=1
    f.seek(blocksind)
    f.write(struct.pack('L'*len(blocks),*blocks))

import pyarrow as pa
import pyarrow.parquet as pq

def write_parquet(data,filen, row_group_offsets):
    if row_group_offsets == -1:
        table = pa.Table.from_pandas(data)
        pq.write_table(table, filen)
        return

    l = len(data)
    nparts = max((l - 1) // row_group_offsets + 1, 1)
    chunksize = max(min((l - 1) // nparts + 1, l), 1)

    ## write default no row groups
    #pq.write_table(table, 'example.parquet',compression={'10.1145/3025453.3025878': 'snappy', '10.1145/2317956.2318088': 'gzip'},use_dictionary=['10.1145/3025453.3025878','10.1145/2317956.2318088'])

    c = 0
    table = pa.Table.from_pandas(data)
    writer = pq.ParquetWriter(filen, table.schema, use_dictionary = True)
    for i in range(nparts):
        if i!=nparts:
            writer.write_table(table[c*chunksize:chunksize+c*chunksize])
            c+=1
        else:
            writer.write_table(table[:len(data)%chunksize])
    writer.close()


import pygame  # Importa a biblioteca pygame
from pygame.locals import *  # Importa de pygame tudo do submódulo locals
from sys import exit  # Importa a função exit do módulo sys que fecha a janela

# Inicializa todas as funções e variáveis da biblioteca pygame
pygame.init()

# Cria o objeto tela
largura = 640  # Medida da tela em pixel
altura = 480  # Medida da tela em pixel
tela = pygame.display.set_mode((largura, altura))  # Objeto que configura display

pygame.display.set_caption('Jogo')  # Configura nome da tela

# loop principal do jogo
while True:
    for event in pygame.event.get():  # Detecta se algum evento ocorreu
        if event.type == QUIT:  # Para a janela fechar ao clicar em fechar
            pygame.quit()
            exit()
    pygame.display.update()  #  Atualiza tela do jogo a cada interação do loop principal


"""
Define the C-variables and functions from the C-files that are needed in Python
"""
from ctypes import *
import sys
import math
import numpy

DEBUG = 0

def restrict(x, minx, maxx):
    return max(min(maxx, x), minx)

class _lpoint(Structure):
    #_pack_=2
    _fields_ = [("x", c_uint16),
                ("y", c_uint16),
                ("r", c_uint16),
                ("g", c_uint16),
                ("b", c_uint16),
                ("Ch6", c_uint16),
                ("Ch7", c_uint16),
                ("Ch8", c_uint16),
                ("TTL", c_uint16)]
c_point_p = POINTER(_lpoint)

class _lpoints(Structure):
    #_pack_=2
    _fields_ = [('length', c_int),
                # an array of structs
                ('struct_arr', c_point_p)]

    def __init__(self, num_of_structs):
        elems = (_lpoint * num_of_structs)()
        self.struct_arr = cast(elems, c_point_p)
        self.length = num_of_structs

        for num in range(0, num_of_structs):
            self.struct_arr[num].x = 0
            self.struct_arr[num].y = 0
            self.struct_arr[num].r = 0
            self.struct_arr[num].g = 0
            self.struct_arr[num].b = 0

class shape:
    def __init__(self):
        """ empty constructor """
        self.points = numpy.empty((0, 5), dtype='uint16')
        self.npoints = 0

    def __init__(self, points, color = None):
        """ constructor with array of colored points. Dimension = (npoints, 5) """
        if type(color) == type(None):
            self.points = points
            self.npoints = len(points)
            return

        """ Alternative:
            define a shape by numpy arrays for coordinates and color.
            points: for example: numpy.array([[0, 1], [2, 3], [4, 5]])
            color: for example: numpy.array([[128, 255, 128], [255, 255, 128], [128, 255, 255]])
            note: points and color must be equal in size, or color must be of size 1 (one color for all points) """
        plength = len(points)
        clength = len(color)
        if plength == 0 or clength == 0:
            raise Exception("[ERROR] Arrays must not be empty.")

        if plength != clength and clength != 1:
            raise Exception("[ERROR] Points and color array must be equal in size, or color array must be of size 1.")

        self.points = numpy.empty((plength, 5), dtype='uint16')
        for i in range(0, plength):
            self.points[i, 0] = points[i, 0]
            self.points[i, 1] = points[i, 1]
            self.points[i, 2] = color[i % clength, 0]
            self.points[i, 3] = color[i % clength, 1]
            self.points[i, 4] = color[i % clength, 2]

        self.npoints = plength

    def get_points(self):
        return self.points

    def get_number_of_points(self):
        return self.npoints

class geometry:
    def __circle_point(x, y, r, i, npoints):
        th = 2 * math.pi / npoints * i
        xunit = int(r * math.cos(th) + x)
        yunit = int(r * math.sin(th) + y)
        return xunit, yunit

    def circle(x0, y0, r, npoints, rd, gr, bl):
        points = numpy.empty((npoints, 2), dtype='uint16')
        colors = numpy.array([[rd, gr, bl]])
        for i in range(0, npoints):
            x, y = geometry.__circle_point(x0, y0, r, i, npoints)
            points[i, 0] = x
            points[i, 1] = y
        return shape(points, colors)

    def line(x0, y0, x1, y1, npoints, rd, gr, bl):
        if npoints < 2:
            npoints = 2
        lx = numpy.linspace(x0, x1, npoints)
        ly = numpy.linspace(y0, y1, npoints)
        points = numpy.empty((npoints, 5), dtype='uint16')
        for i in range(0, npoints):
            points[i, 0] = lx[i]
            points[i, 1] = ly[i]
            points[i, 2] = rd
            points[i, 3] = gr
            points[i, 4] = bl
        return shape(points)

    def triangle(x0, y0, x1, y1, x2, y2, npoints, rd, gr, bl):
        if npoints < 2:
            npoints = 2
        lx1 = numpy.linspace(x0, x1, npoints)
        ly1 = numpy.linspace(y0, y1, npoints)
        lx2 = numpy.linspace(x1, x2, npoints)
        ly2 = numpy.linspace(y1, y2, npoints)
        lx3 = numpy.linspace(x2, x0, npoints)
        ly3 = numpy.linspace(y2, y0, npoints)
        points = numpy.empty((3 * npoints - 2, 5), dtype='uint16')
        index = 0
        for i in range(0, npoints - 1):
            points[index, 0] = lx1[i]
            points[index, 1] = ly1[i]
            points[index, 2] = rd
            points[index, 3] = gr
            points[index, 4] = bl
            index += 1
        for i in range(0, npoints - 1):
            points[index, 0] = lx2[i]
            points[index, 1] = ly2[i]
            points[index, 2] = rd
            points[index, 3] = gr
            points[index, 4] = bl
            index += 1
        for i in range(0, npoints):
            points[index, 0] = lx3[i]
            points[index, 1] = ly3[i]
            points[index, 2] = rd
            points[index, 3] = gr
            points[index, 4] = bl
            index += 1
        return shape(points)

    def tetragon(x0, y0, x1, y1, x2, y2, x3, y3, npoints, rd, gr, bl):
        if npoints < 2:
            npoints = 2
        lx1 = numpy.linspace(x0, x1, npoints)
        ly1 = numpy.linspace(y0, y1, npoints)
        lx2 = numpy.linspace(x1, x2, npoints)
        ly2 = numpy.linspace(y1, y2, npoints)
        lx3 = numpy.linspace(x2, x3, npoints)
        ly3 = numpy.linspace(y2, y3, npoints)
        lx4 = numpy.linspace(x3, x0, npoints)
        ly4 = numpy.linspace(y3, y0, npoints)
        points = numpy.empty((4 * npoints - 3, 5), dtype='uint16')
        index = 0
        for i in range(0, npoints - 1):
            points[index, 0] = lx1[i]
            points[index, 1] = ly1[i]
            points[index, 2] = rd
            points[index, 3] = gr
            points[index, 4] = bl
            index += 1
        for i in range(0, npoints - 1):
            points[index, 0] = lx2[i]
            points[index, 1] = ly2[i]
            points[index, 2] = rd
            points[index, 3] = gr
            points[index, 4] = bl
            index += 1
        for i in range(0, npoints - 1):
            points[index, 0] = lx3[i]
            points[index, 1] = ly3[i]
            points[index, 2] = rd
            points[index, 3] = gr
            points[index, 4] = bl
            index += 1
        for i in range(0, npoints):
            points[index, 0] = lx4[i]
            points[index, 1] = ly4[i]
            points[index, 2] = rd
            points[index, 3] = gr
            points[index, 4] = bl
            index += 1
        return shape(points)

    def rotate_shape(shape1, pivot, angle_deg):
        points = shape1.get_points()
        if DEBUG:
            print("[DEBUG] points = {}".format(points[0]))
        # This code was generated by an AI (https://chat.openai.com/chat)
        angle_radians = math.radians(angle_deg)
        cos_angle = math.cos(angle_radians)
        sin_angle = math.sin(angle_radians)
        # Translate points so that the pivot point is at the origin
        translated_points = [(point[0] - pivot[0], point[1] - pivot[1], point[2], point[3], point[4]) for point in points]
        # Rotate each point around the origin
        rotated_points = [(cos_angle * point[0] - sin_angle * point[1], sin_angle * point[0] + cos_angle * point[1], point[2], point[3], point[4]) for point in translated_points]
        # Translate points back to their original position
        rotated_points = [(point[0] + pivot[0], point[1] + pivot[1], point[2], point[3], point[4]) for point in rotated_points]
        rotated_points = numpy.array(rotated_points, dtype='uint16')
        if DEBUG:
            print("[DEBUG] len(rotated_points) = {}".format(len(rotated_points)))
            print("[DEBUG] rotated_points = {}".format(rotated_points[0]))
        return shape(rotated_points)

class lumax:
    global lumax_lib
    lib_path = './lumax/python/libs/liblumax_%s.so' % (sys.platform)
    try:
        lumax_lib = CDLL(lib_path)
    except:
        try:
            lumax_lib = CDLL('./lumax/python/libs/lumax.dll')
        except:
            lib_path = './libs/liblumax_%s.so' % (sys.platform)
            try:
                lumax_lib = CDLL(lib_path)
            except:
                try:
                    lumax_lib = CDLL('./libs/lumax.dll')
                except:
                    print('[ERROR] OS %s not recognized or library not found.' % (sys.platform))

    c_int_p = POINTER(c_int)

    lumax_lib.Lumax_GetApiVersion.argtypes = None
    lumax_lib.Lumax_GetApiVersion.restype = c_int

    lumax_lib.Lumax_GetPhysicalDevices.argtypes = None
    lumax_lib.Lumax_GetPhysicalDevices.restype = c_int

    lumax_lib.Lumax_OpenDevice.argtypes = (c_int, c_int)
    lumax_lib.Lumax_OpenDevice.restype = c_void_p

    lumax_lib.Lumax_SetTTL.argtypes = (c_void_p, c_int)
    lumax_lib.Lumax_SetTTL.restype = c_int

    lumax_lib.Lumax_WaitForBuffer.argtypes = (c_void_p, c_int, c_int_p, c_int_p)
    lumax_lib.Lumax_WaitForBuffer.restype = c_int

    lumax_lib.Lumax_SendFrame.argtypes = (c_void_p, c_point_p, c_int, c_int, c_int, c_int_p)
    lumax_lib.Lumax_SendFrame.restype = c_int

    lumax_lib.Lumax_StopFrame.argtypes = (c_void_p,)
    lumax_lib.Lumax_StopFrame.restype = c_int

    lumax_lib.Lumax_CloseDevice.argtypes = (c_void_p,)
    lumax_lib.Lumax_CloseDevice.restype = c_int

    def get_api_version():
        global lumax_lib
        return int(lumax_lib.Lumax_GetApiVersion())

    def get_physical_devices():
        global lumax_lib
        return int(lumax_lib.Lumax_GetPhysicalDevices())

    def open_device(numDev, channel):
        global lumax_lib
        try:
            return int(lumax_lib.Lumax_OpenDevice(c_int(numDev), c_int(channel)))
        except:
            return 0

    def setTTL(handle, ttl):
        global lumax_lib
        return int(lumax_lib.Lumax_SetTTL(c_void_p(handle), c_int(ttl)))

    def wait_for_buffer(handle, timeOut):
        global lumax_lib
        timeToWait = c_int(0)
        bufferChanged = c_int(0)
        ret = lumax_lib.Lumax_WaitForBuffer(c_void_p(handle), c_int(timeOut), byref(timeToWait), byref(bufferChanged))
        return int(ret), int.from_bytes(timeToWait, byteorder='big', signed=True), int.from_bytes(bufferChanged, byteorder='big', signed=True)

    def send_frame(handle, points, scanSpeed, updateMode):
        global lumax_lib
        numOfPoints = points.length
        timeToWait = c_int(0)
        ret = lumax_lib.Lumax_SendFrame(c_void_p(handle), points.struct_arr, c_int(numOfPoints), c_int(scanSpeed), c_int(updateMode), byref(timeToWait))
        return int(ret), int.from_bytes(timeToWait, byteorder='big', signed=True)

    def stop_frame(handle):
        global lumax_lib
        return int(lumax_lib.Lumax_StopFrame(c_void_p(handle)))

    def close_device(handle):
        global lumax_lib
        return int(lumax_lib.Lumax_CloseDevice(c_void_p(handle)))

class lumax_renderer:
    def __init__(self):
        ret = lumax.get_api_version()
        ret1 = lumax.get_physical_devices()
        self.lhandle = lumax.open_device(1, 0)
        ret2, timeToWait, bufferChanged = lumax.wait_for_buffer(self.lhandle, 17)
        if DEBUG:
            print("[DEBUG] API version: {}".format(ret))
            print("[DEBUG] Number of physical devices: {}".format(ret1))
            print("[DEBUG] Lumax handle: {}".format(self.lhandle))
            #print("[DEBUG] SetTTL return: {}".format(lumax.setTTL(self.lhandle, 0)))
            print("[DEBUG] WaitForBuffer return: {}, {}, {}".format(ret2, timeToWait, bufferChanged))
        self.shapes = numpy.array([])
        self.totnpoints = 0

    def new_frame(self):
        self.shapes = numpy.array([])
        self.totnpoints = 0

    def add_shape_to_frame(self, shape):
        self.shapes = numpy.append(self.shapes, shape)
        self.totnpoints += shape.get_number_of_points()

    def add_point_to_frame(self, point):
        if len(point) != 5:
            raise Exception("[ERROR] Point must be of dimension 5 (x, y, r, g, b)")
        xypoint = numpy.array([[point[0], point[1]]])
        color = numpy.array([[point[2], point[3], point[4]]])
        shp = shape(xypoint, color)
        lumax_renderer.add_shape_to_frame(self, shp)

    def add_points_to_frame(self, points):
        if len(points) == 0:
            raise Exception("[ERROR] Points must not be empty.")
        shp = shape(points)
        lumax_renderer.add_shape_to_frame(self, shp)

    def __generate_buffer(self):
        # TODO: von außerhalb setzen!
        mirrorx = 1
        mirrory = 1

        # two extra points for every shape + one extra point in the center of the frame
        nextrapoints = 2 * len(self.shapes) + 1
        buffer = _lpoints(self.totnpoints + nextrapoints)
        # start in the center of the screen
        index = 0
        buffer.struct_arr[index].x = 128 * 255
        buffer.struct_arr[index].y = 128 * 255
        buffer.struct_arr[index].r = 0
        buffer.struct_arr[index].g = 0
        buffer.struct_arr[index].b = 0

        for i in range(0, len(self.shapes)):
            p = self.shapes[i].get_points()
            # insert blank point at the beginning
            index += 1
            buffer.struct_arr[index].x = (255 * 255 * mirrorx) + (-1)**(mirrorx) * p[0, 0]
            buffer.struct_arr[index].y = (255 * 255 * mirrory) + (-1)**(mirrory) * p[0, 1]
            buffer.struct_arr[index].r = 0
            buffer.struct_arr[index].g = 0
            buffer.struct_arr[index].b = 0

            # copy the points
            for j in range(0, len(p)):
                index += 1
                buffer.struct_arr[index].x = (255 * 255 * mirrorx) + (-1)**(mirrorx) * p[j, 0]
                buffer.struct_arr[index].y = (255 * 255 * mirrory) + (-1)**(mirrory) * p[j, 1]
                buffer.struct_arr[index].r = p[j, 2]
                buffer.struct_arr[index].g = p[j, 3]
                buffer.struct_arr[index].b = p[j, 4]

            # insert blank point at the end
            index += 1
            buffer.struct_arr[index].x = (255 * 255 * mirrorx) + (-1)**(mirrorx) * p[len(p) - 1, 0]
            buffer.struct_arr[index].y = (255 * 255 * mirrory) + (-1)**(mirrory) * p[len(p) - 1, 1]
            buffer.struct_arr[index].r = 0
            buffer.struct_arr[index].g = 0
            buffer.struct_arr[index].b = 0

        return buffer

    def send_frame(self, pointrate : int):
        if len(self.shapes) == 0:
            raise Exception("Frame is empty.")

        buffer = lumax_renderer.__generate_buffer(self)

        # print the points
        #for i in range(0, buffer.length):
        #    print("p{} = {}, {}, {}, {}, {}".format(i, buffer.struct_arr[i].x, buffer.struct_arr[i].y, buffer.struct_arr[i].r, buffer.struct_arr[i].g, buffer.struct_arr[i].b))

        ret, timeToWait = lumax.send_frame(self.lhandle, buffer, pointrate, 0)
        if DEBUG:
            print("[DEBUG] SendFrame return: {}, {}".format(ret, timeToWait))
        ret, timeToWait, bufferChanged = lumax.wait_for_buffer(self.lhandle, 17)
        return

    def stop_frame(self):
        ret = lumax.stop_frame(self.lhandle)
        if DEBUG:
            print("[DEBUG] StopFrame return: {}".format(ret))
        return

    def close_device(self):
        ret = lumax.stop_frame(self.lhandle)
        ret1 = lumax.close_device(self.lhandle)
        if DEBUG:
            print("[DEBUG] StopFrame return: {}".format(ret))
            print("[DEBUG] CloseDevice return: {}".format(ret1))
        return


from intcode import *
import sys

if __name__ == '__main__':

    out_sum = 0
    machine = IntCode(sys.argv[1])
    for i in range(50):
        for j in range(50):

            if machine.run([i, j]):
                out_sum += 1
            machine.reset()
    print(out_sum)


# (C) 2021 GoodData Corporation
""" Module containing a class that provides access to metadata and afm services.
"""

from __future__ import annotations

from typing import Optional

import gooddata_afm_client as afm_client
import gooddata_metadata_client as metadata_client
import gooddata_scan_client as scan_client
from gooddata_sdk import __version__

USER_AGENT = f"gooddata-python-sdk/{__version__}"

class GoodDataApiClient:
    """Provide access to metadata and afm services."""

    def __init__(
        self,
        host: str,
        token: str,
        custom_headers: Optional[dict[str, str]] = None,
        extra_user_agent: Optional[str] = None,
    ) -> None:
        """Take url, token for connecting to GoodData.CN.

        HTTP requests made by this class may be enriched by `custom_headers` dict
        containing header names as keys and header values as dict values.

        `extra_user_agent` is optional string to be added to default http User-Agent
        header. This takes precedence over custom_headers setting.
        """
        self._hostname = host
        self._token = token
        self._custom_headers = custom_headers or {}

        user_agent = f"{USER_AGENT} {extra_user_agent}" if extra_user_agent is not None else USER_AGENT

        self._metadata_config = metadata_client.Configuration(host=host)
        self._metadata_client = metadata_client.ApiClient(
            configuration=self._metadata_config,
            header_name="Authorization",
            header_value=f"Bearer {token}",
        )
        self._set_default_headers(self._metadata_client.default_headers)
        for header_name, header_value in self._custom_headers.items():
            self._metadata_client.default_headers[header_name] = header_value
        self._metadata_client.user_agent = user_agent

        self._scan_config = scan_client.Configuration(host=host)
        self._scan_client = scan_client.ApiClient(
            configuration=self._scan_config,
            header_name="Authorization",
            header_value=f"Bearer {token}",
        )
        self._set_default_headers(self._scan_client.default_headers)
        for header_name, header_value in self._custom_headers.items():
            self._scan_client.default_headers[header_name] = header_value
        self._scan_client.user_agent = user_agent

        self._afm_config = afm_client.Configuration(host=host)
        self._afm_client = afm_client.ApiClient(
            configuration=self._afm_config,
            header_name="Authorization",
            header_value=f"Bearer {token}",
        )
        self._set_default_headers(self._afm_client.default_headers)
        for header_name, header_value in self._custom_headers.items():
            self._afm_client.default_headers[header_name] = header_value
        self._afm_client.user_agent = user_agent

    @staticmethod
    def _set_default_headers(headers: dict) -> None:
        headers["X-Requested-With"] = "XMLHttpRequest"
        headers["X-GDC-VALIDATE-RELATIONS"] = "true"

    @property
    def afm_client(self) -> afm_client.ApiClient:
        return self._afm_client

    @property
    def metadata_client(self) -> metadata_client.ApiClient:
        return self._metadata_client

    @property
    def scan_client(self) -> scan_client.ApiClient:
        return self._scan_client


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#
#  Author: Mauro Soria

import sys

if sys.version_info < (3, 0):
    sys.stdout.write("Sorry, dirsearch requires Python 3.x\n")
    sys.exit(1)

from lib.core import ArgumentParser
from lib.controller import *
from lib.output import *

class Program(object):
    def __init__(self):
        self.script_path = (os.path.dirname(os.path.realpath(__file__)))
        self.arguments = ArgumentParser(self.script_path)
        self.output = CLIOutput()
        self.controller = Controller(self.script_path, self.arguments, self.output)

if __name__ == '__main__':
    main = Program()


from aws_cdk import (
    # Duration,
    Stack, Fn,
    aws_rds,
    aws_iam,
    aws_ec2,
    aws_eks,
    RemovalPolicy, SecretValue,
    aws_lambda,
    aws_apigateway as apigw
)

from constructs import Construct
cidr="10.10.0.0/16"
key_name = "my-key-pair"

class SaasSolutionCdkStack(Stack):

    def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        azs = Fn.get_azs()
        self.vpc = aws_ec2.Vpc(self, "VPC",
                           max_azs=2,
                           ip_addresses=aws_ec2.IpAddresses.cidr(cidr),
                           nat_gateways=2,
                           enable_dns_hostnames=True,
                           enable_dns_support=True,
                           subnet_configuration=[
                               aws_ec2.SubnetConfiguration(
                                   name="public",
                                   subnet_type=aws_ec2.SubnetType.PUBLIC,
                                   cidr_mask=24),
                               aws_ec2.SubnetConfiguration(
                                   subnet_type=aws_ec2.SubnetType.PRIVATE_WITH_EGRESS,
                                   name="private",
                                   cidr_mask=24) # could be /16 to have more instances, but this is a demo scope.
                           ]
                           )

        postgres = aws_rds.DatabaseInstance(self, "PostgresqlInstance",
                                database_name="tenantdb",
                                engine=aws_rds.DatabaseInstanceEngine.postgres(version=aws_rds.PostgresEngineVersion.VER_14_5),
                                vpc_subnets=aws_ec2.SubnetSelection(subnet_type=aws_ec2.SubnetType.PRIVATE_WITH_EGRESS),
                                vpc=self.vpc,
                                port=5432,
                                removal_policy=RemovalPolicy.DESTROY,
                                deletion_protection=False,
                                max_allocated_storage=200,
                                publicly_accessible=True
                        )

        # Create an IAM role for worker groups and kubernetes RBAC configuration
        self.eks_admin_role = aws_iam.Role(self, 'eksAdmin',
                                    assumed_by=aws_iam.ServicePrincipal(service='ec2.amazonaws.com'),
                                    role_name='eks-cluster-role',
                                    managed_policies=
                                        [aws_iam.ManagedPolicy.from_aws_managed_policy_name(managed_policy_name='AdministratorAccess')])
        self.eks_instance_profile = aws_iam.CfnInstanceProfile(self, 'instanceprofile',
                                                      roles=[self.eks_admin_role.role_name],
                                                      instance_profile_name='eks-cluster-role')

        cluster = aws_eks.Cluster(self, 'demo-cluster',
                                  masters_role=self.eks_admin_role,
                                  vpc=self.vpc,
                                  default_capacity=2,
                                  vpc_subnets=[aws_ec2.SubnetSelection(subnet_type=aws_ec2.SubnetType.PRIVATE_WITH_EGRESS)],
                                  version=aws_eks.KubernetesVersion.V1_24,
                                  output_cluster_name=True
                                  )


import pytest

import immoscrapy

@pytest.mark.parametrize(
    "real_estate_type, return_type",
    [
        ('HOUSE_BUY', immoscrapy.HOUSE_BUY),
        ('HOUSE_RENT', immoscrapy.HOUSE_RENT),
        ('APARTMENT_BUY', immoscrapy.APARTMENT_BUY),
        ('APARTMENT_RENT', immoscrapy.APARTMENT_RENT),
     ]
)
def test_query_berlin(real_estate_type, return_type):
    result = immoscrapy.query('de', 'berlin', 'berlin', real_estate_type)
    assert isinstance(result[0], return_type)

def test_query_raises_value_error():
    with pytest.raises(ValueError):
        immoscrapy.query('de', 'berlin', 'berlin', 'foo')

def test_regression_gh_8():
    """Apparently this query raises an 401."""
    # I cannot reproduce it on my machine though
    immoscrapy.query('de', 'chemnitz', 'chemnitz', 'HOUSE_BUY', price=900000)


"""
输入一个链表，按链表值从尾到头的顺序返回一个ArrayList。
"""

# 递归

import list
# -*- coding:utf-8 -*-
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    # 返回从尾部到头部的列表值序列，例如[1,2,3]
    def printListFromTailToHead(self, listNode):

        if listNode is None:
            return []
        return self.printListFromTailToHead(listNode.next) + [listNode.val]

node_list1 = [3, 2, 1]
l1 = list.ListNode_handle()
for i in node_list1:
    l1.add(i)

A = Solution()
B = A.printListFromTailToHead(l1.cur_node)

print(B)


import unittest

from domain.validators import Validator
from exceptions.exceptions import ValidationException, PbLabNotFoundException
from repository.pbLab_repo import PbLabFileRepo
from service.pbLab_service import PbLabService

class TestCasePbLabService(unittest.TestCase):
    def setUp(self) -> None:
        self.__repo = PbLabFileRepo('test_e_pbLab_service.txt')
        validator = Validator()
        self.__srv = PbLabService(self.__repo, validator)

    def test_add_pbLab(self):
        added_student = self.__srv.add_pbLab('2_77', 'ceva nou', '5 februarie')
        self.assertTrue(added_student.getDescriere() == 'ceva nou')
        self.assertTrue(added_student.getDeadline() == '5 februarie')

        self.assertEqual(len(self.__srv.get_all_problems()), 1)
        self.assertRaises(ValidationException, self.__srv.add_pbLab, '2_789', 'ceva', '5 februarie')

    def test_delete_pbLab(self):
        self.__srv.add_pbLab('2_77', 'ceva nou', '5 februarie')
        deleted_student = self.__srv.delete_pbLab('2_77')

        self.assertEqual(len(self.__srv.get_all_problems()), 0)
        self.assertEqual(deleted_student.getDescriere(), 'ceva nou')
        self.assertEqual(deleted_student.getDeadline(), '5 februarie')
        self.assertRaises(PbLabNotFoundException, self.__srv.delete_pbLab, '3_2')

    def test_get_all_pbLab(self):
        self.__srv.add_pbLab('2_77', 'ceva nou', '5 februarie')
        self.__srv.add_pbLab('5_7', 'ceva', '7 februarie')
        self.assertIsInstance(self.__srv.get_all_problems(), list)
        self.assertEqual(len(self.__srv.get_all_problems()), 2)

    def test_edit_pbLab_blackbox(self):
        self.__srv.add_pbLab('2_77', 'ceva nou', '5 februarie')
        updated_pbLab = self.__srv.edit_pbLab('2_77', 'altceva', '7 mai')

        self.assertTrue(updated_pbLab.getDescriere() == 'altceva')
        self.assertTrue(updated_pbLab.getDeadline() == '7 mai')
        self.assertRaises(PbLabNotFoundException, self.__srv.edit_pbLab, '7_7', 'ceva nou', '5 februarie')

    def tearDown(self) -> None:
        self.__repo.delete_all()

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


#!/usr/bin/env python
from __future__ import absolute_import
import json
import logging

from . import IndicatorTypes

API_V1_ROOT = "{}/api/v1/"
PULSES_ROOT = "{}/pulses".format(API_V1_ROOT)
SUBSCRIBED = "{}/subscribed".format(PULSES_ROOT)
EVENTS = "{}/events".format(PULSES_ROOT)

try:
    # For Python2
    from urllib2 import URLError, HTTPError, build_opener, ProxyHandler
except ImportError:
    # For Python3
    from urllib.error import URLError, HTTPError
    from urllib.request import build_opener, ProxyHandler

logger = logging.getLogger("OTXv2")

class InvalidAPIKey(Exception):
    def __init__(self, value):
        self.value = value

    def __str__(self):
        return repr(self.value)

class BadRequest(Exception):
    def __init__(self, value):
        self.value = value

    def __str__(self):
        return repr(self.value)

class OTXv2(object):
    """
    Main class to interact with the AlienVault OTX API.
    """

    def __init__(self, api_key, proxy=None, server="https://otx.alienvault.com", project="SDK"):
        self.key = api_key
        self.server = server
        self.proxy = proxy
        self.sdk = 'OTX Python {}/1.0'.format(project)

    def get(self, url):
        """
        Internal API for GET request on a OTX URL
        :param url: URL to retrieve
        :return: response in JSON object form
        """
        if self.proxy:
            proxy = ProxyHandler({'http': self.proxy})
            request = build_opener(proxy)
        else:
            request = build_opener()
        request.addheaders = [
            ('X-OTX-API-KEY', self.key),
            ('User-Agent', self.sdk)
        ]
        response = None
        try:
            response = request.open(url)
        except URLError as e:
            if isinstance(e, HTTPError):
                if e.code == 403:
                    raise InvalidAPIKey("Invalid API Key")
                elif e.code == 400:
                    raise BadRequest("Bad Request")
            else:
                raise e
        data = response.read().decode('utf-8')
        json_data = json.loads(data)
        return json_data

    def create_url(self, url_path, **kwargs):
        uri = url_path.format(self.server)
        uri += "?"
        for parameter, value in kwargs.items():
            uri += parameter
            uri += "="
            uri += str(value)
            uri += "&"
        return uri

    def getall(self, limit=20):
        """
        Get all pulses user is subscribed to.
        :param limit: The page size to retrieve in a single request
        :return: the consolidated set of pulses for the user
        """
        pulses = []
        next = self.create_url(SUBSCRIBED, limit=limit)
        while next:
            json_data = self.get(next)
            for r in json_data["results"]:
                pulses.append(r)
            next = json_data["next"]
        return pulses

    def getall_iter(self, limit=20):
        """
        :param limit:
        :return:
        """
        pulses = []
        next = self.create_url(SUBSCRIBED, limit=limit)
        while next:
            json_data = self.get(next)
            for r in json_data["results"]:
                yield r
            next = json_data["next"]

    def getsince(self, mytimestamp, limit=20):
        """
        Get all pulses created or updated since a timestamp
        :param mytimestamp: timestamp to filter returned pulses
        :param limit: The page size to retrieve in a single request
        :return: the consolidated set of pulses for the user
        """
        pulses = []
        next = self.create_url(SUBSCRIBED, limit=limit, modified_since=mytimestamp)
        while next:
            json_data = self.get(next)
            for r in json_data["results"]:
                pulses.append(r)
            next = json_data["next"]
        return pulses

    def getsince_iter(self, mytimestamp, limit=20):
        pulses = []
        next = self.create_url(SUBSCRIBED, limit=limit, modified_since=mytimestamp)
        while next:
            json_data = self.get(next)
            for r in json_data["results"]:
                yield r
            next = json_data["next"]

    def get_all_indicators(self, indicator_types=IndicatorTypes.all_types):
        """
        Get all the indicators contained within your pulses of the IndicatorTypes passed.
        By default returns all IndicatorTypes.
        :param indicator_types: IndicatorTypes to return
        :return: yields the indicator object for use
        """
        name_list = IndicatorTypes.to_name_list(indicator_types)
        for pulse in self.getall_iter():
            for indicator in pulse["indicators"]:
                if indicator["type"] in name_list:
                    yield indicator

    def getevents_since(self, mytimestamp, limit=20):
        """
        Get all events (activity) created or updated since a timestamp
        :param mytimestamp: timestamp to filter returned activity
        :param limit: The page size to retrieve in a single request
        :return: the consolidated set of pulses for the user
        """
        events = []
        next = self.create_url(EVENTS, limit=limit, since=mytimestamp)
        while next:
            json_data = self.get(next)
            for r in json_data["results"]:
                events.append(r)
            next = json_data["next"]
        return events


import copy
import csv
import random
from typing import List, Tuple

def create_base(
    trainSamples: int,
) -> Tuple[List[List[float]], List[List[float]], List[int], List[int]]:
    X_test = []
    X_train = []
    with open("src/datasets/jm1.csv", newline="") as csvfile:
        spamreader = csv.reader(csvfile, delimiter=",", quotechar="|")
        jm1 = [row for nr, row in enumerate(spamreader)]
        jm1_true = [j for j in jm1 if j[21] == "true"]
        jm1_false = [j for j in jm1 if j[21] == "false"]
        random.shuffle(jm1_true)
        random.shuffle(jm1_false)
        trainPart = int(
            trainSamples / 2
        )  # Test sample divided between true answers and false answers
        jm1_train = jm1_true[:trainPart]
        jm1_train = jm1_train + jm1_false[:trainPart]
        # jm1_test = jm1_true[trainPart:trainSamples]
        # jm1_test = jm1_test + jm1_false[trainPart:trainSamples]
        jm1_test = jm1_true[trainPart:]
        jm1_test = jm1_test + jm1_false[trainPart:]
        random.shuffle(jm1_train)
        random.shuffle(jm1_test)
        jm1_test = jm1_test[:trainSamples]

    Y_train_str = [j.pop(-1) for j in jm1_train]
    Y_train = [1 if x == "true" else 0 for x in Y_train_str]

    for jt in jm1_train:
        X_train.append([float(j) for j in jt])

    Y_test_str = [j.pop(-1) for j in jm1_test]
    Y_test = [1 if x == "true" else 0 for x in Y_test_str]
    for jt in jm1_test:
        X_test.append([float(j) for j in jt])

    return X_test, X_train, Y_train, Y_test

def save_classifier_if_diverse(
    classif_score: float,
    classif_predict: List[int],
    predicts: List[List[int]],
    params: List[str],
    nr_items: int,
    string_parameters: str,
) -> None:
    if classif_score >= 0.6:
        if len(predicts) > 0:
            flag = False
            for pred in predicts:
                s = sum(
                    x != y
                    for x, y in zip(
                        classif_predict,
                        pred,
                    )
                )
                if s < (0.05 * nr_items):
                    flag = True
                    break

            if not flag:
                predicts.append(copy.deepcopy(classif_predict))
                params.append(string_parameters)
        else:
            predicts.append(copy.deepcopy(classif_predict))
            params.append(string_parameters)

def save_quality_classifier(
    f1_score: float,
    params: List[str],
    string_parameters: str,
):
    if f1_score > 0.3:
        params.append(string_parameters)


from funcionalidades import *
import menuTurmas
from Listas import *

opcao = 0
while(opcao != '8'):

    print('Selecione a opção desejada')
    print('Aperte 1 para cadastrar uma nova materia')
    print('Aperte 2 para cadastrar um novo professor')
    print('Aperte 3 para cadastrar um nova aluno')
    print('Aperte 4 para mostrar todas as materias cadastradas')
    print('Aperte 5 para mostrar todos os professores cadastradas')
    print('Aperte 6 para mostrar todos os alunos cadastrados')
    print('Aperte 7 para abrir o Menu de Turmas')
    print('Aperte 8 para Sair do programa\n')
    opcao = input()
    if (opcao == '1'):
        nome_Materia = input('Qual o nome da matéria? ')
        codigo_Materia = input('Qual o código da matéria? ')
        if VerificaVazio(codigo_Materia) or VerificaVazio(nome_Materia):
                print('\nUm dos nomes não foi informado, verifique a opção e selecione novamente\n')
        else:
            cadastroMateria(ListaMaterias, nome_Materia, codigo_Materia)

    elif (opcao == '2'):
        nome_Professor = input('Qual o nome do professor? ')
        departamento_professor = input('Qual seu departamento? ')
        if VerificaVazio(nome_Professor) or VerificaVazio(departamento_professor):
                print('\nUm dos nomes não foi informado, verifique a opção e selecione novamente\n')
        else:
            cadastroProfessor(ListaProfessores, nome_Professor, departamento_professor)

    elif (opcao == '3'):
        nome_Aluno = input('Qual o nome do aluno? ')
        dre_Aluno = input('Qual o DRE do aluno? ')
        periodo_Aluno = input('Qual o periodo do aluno? ')
        if VerificaVazio(nome_Aluno) or VerificaVazio(dre_Aluno) or VerificaVazio(periodo_Aluno):
                print('\nUm dos nomes não foi informado, verifique a opção e selecione novamente\n')
        else:
            cadastroAluno(ListaAlunos, nome_Aluno, dre_Aluno, periodo_Aluno)

    elif (opcao == '4'):
        printLista(ListaMaterias)

    elif (opcao == '5'):
        printLista(ListaProfessores)

    elif (opcao == '6'):
        printLista(ListaAlunos)

    elif (opcao == '7'):
        menuTurmas.menu_Turma()
    elif (not '8'):
        print('A opção não existe')


import json

from Products.ZenRRD.CommandParser import CommandParser

class table(CommandParser):
    def processResults(self, cmd, result):
        data = None
        try:
            data = json.loads(cmd.result.output)
        except ValueError:
            return

        if 'databases' not in data:
            return result

        for point in cmd.points:
            component = cmd.points[0].component

            table = None
            for dbName, dbStats in data['databases'].items():
                if table is not None:
                    break

                for tableName, tableStats in dbStats['tables'].items():
                    component_id = '{0}_{1}'.format(dbName, tableName)
                    if component_id == component:
                        table = tableStats
                        break

            if table is None:
                # No matching table found.
                continue

            if point.id in table:
                result.values.append((point, table[point.id]))

        return result


import collections
import csv
from dm_control import mujoco
from dm_control.rl import control
from dm_control.suite import base
from dm_control.suite import common
from dm_control.utils import containers
from dm_control.utils import rewards
import numpy as np
import os

_DEFAULT_TIME_LIMIT = 40
_CONTROL_TIMESTEP = .04
_JOINTS = ['root',
           'tail1',
           # 'tail_twist',
           'tail2']
           # 'finright_roll',
           # 'finright_pitch',
           # 'finleft_roll',
           # 'finleft_pitch']
SUITE = containers.TaggedTasks()

def get_model_and_assets():
  """Returns a tuple containing the model XML string and a dict of assets."""
  # return common.read_model(os.path.join(os.path.abspath('./'),'fishD.xml')), common.ASSETS
  return common.read_model(os.path.join(os.path.dirname(os.path.realpath(__file__)),'fishD.xml')), common.ASSETS
  # return common.read_model(sys.a'fishD.xml'), common.ASSETS

@SUITE.add('benchmarking')
def swim(time_limit=_DEFAULT_TIME_LIMIT, random=None, environment_kwargs=None):
  """Returns the Fish Swim task."""
  physics = Physics.from_xml_string(*get_model_and_assets())
  task = Swim(random=random)
  environment_kwargs = environment_kwargs or {}
  return control.Environment(physics, task, control_timestep=_CONTROL_TIMESTEP, time_limit=time_limit,
      **environment_kwargs)

class Physics(mujoco.Physics):
  """Physics simulation with additional features for the Fish domain."""

  def upright(self):
    """Returns projection from z-axes of torso to the z-axes of worldbody."""
    return self.named.data.xmat['torso', 'zz']

  def torso_velocity(self):
    """Returns velocities and angular velocities of the torso."""
    return self.data.sensordata

  def joint_velocities(self):
    """Returns the joint velocities."""
    return self.named.data.qvel[_JOINTS]

  def joint_angles(self):
    """Returns the joint positions."""
    return self.named.data.qpos[_JOINTS]

  def mouth_to_target(self):
    """Returns a vector, from mouth to target in local coordinate of mouth."""
    data = self.named.data
    mouth_to_target_global = data.geom_xpos['target'] - data.geom_xpos['mouth']
    return mouth_to_target_global.dot(data.geom_xmat['mouth'].reshape(3, 3))

class Swim(base.Task):
  """A Fish `Task` for swimming with smooth reward."""

  def __init__(self, random=None,reward_mode='speed'):
    """Initializes an instance of `Swim`.

    Args:
      random: Optional, either a `numpy.random.RandomState` instance, an
        integer seed for creating a new `RandomState`, or None to select a seed
        automatically (default).
    """
    self.reward_mode = reward_mode
    self.init=True
    f1 = open('./log-fish.csv', "w") #erase
    super().__init__(random=random)

  def initialize_episode(self, physics):
    """Sets the state of the environment at the start of each episode."""

    # quat = self.random.randn(4)
    # physics.named.data.qpos['root'][3:7] = quat / np.linalg.norm(quat)
    # for joint in _JOINTS:
    #   physics.named.data.qpos[joint] = self.random.uniform(-.2, .2)
    # # Randomize target position.
    # physics.named.model.geom_pos['target', 'x'] = self.random.uniform(-.4, .4)
    # physics.named.model.geom_pos['target', 'y'] = self.random.uniform(-.4, .4)
    # physics.named.model.geom_pos['target', 'z'] = self.random.uniform(.1, .3)
    super().initialize_episode(physics)

  def get_observation(self, physics):
    """Returns an observation of joints, target direction and velocities."""
    obs = collections.OrderedDict()
    obs['joint_angles'] = physics.joint_angles()
    # obs['upright'] = physics.upright()
    # obs['target'] = physics.mouth_to_target()
    obs['velocity'] = physics.joint_velocities()
    return obs

  def get_reward(self, physics):
    """Returns a smooth reward."""
    radii = physics.named.model.geom_size[['mouth', 'target'], 0].sum()
    in_target = rewards.tolerance(np.linalg.norm(physics.mouth_to_target()), bounds=(0, radii), margin=2*radii)
    # is_upright = 0.5 * (physics.upright() + 1)
    if self.reward_mode=='speed':
      # return np.array([physics.named.data.xpos['torso', 'y']])#good
      # return physics.named.data.qpos['root']
      # t=physics.named.data.qvel['root']
      # print(physics.named.data.qvel['root'])
      return physics.named.data.qvel['root'][0]
    return in_target

  # def after_step(self,physics):
  #     row = {'j_p': physics.named.data.qpos[_JOINTS[0]][0], 'j_v': physics.named.data.qvel[_JOINTS[0]][0], 'j_acc': physics.named.data.qacc[_JOINTS[0]][0],
  #            's_acc': physics.named.data.qacc["root"][0], 's_p': physics.named.data.qpos["root"][0], 's_v': physics.named.data.qvel["root"][0], 'F_x':physics.named.data.sensordata[6],'F_y':physics.named.data.sensordata[7],'F_z':physics.named.data.sensordata[8]}
  #
  #     with open('./log-fish-results.csv', 'a', newline='') as f:
  #     # with open('./log.csv', 'a', newline='') as f:
  #         # create the csv writer
  #         writer = csv.DictWriter(f, fieldnames=row.keys())
  #         if self.init:
  #             writer.writeheader()
  #             self.init = False
  #         # write a row to the csv file
  #         writer.writerow(row)
  #         f.close()


# https://alpha-school.programmers.co.kr/learn/challenge_codes/95

def strange_sort(strings, n):
    '''strings의 문자열들을 n번째 글자를 기준으로 정렬해서 return하세요'''
    dic = {}
    for text in strings:
        # 딕셔너리 안에 이미 해당 key 값이 있다면?
        if text[n] in dic:
            #원래 key에 해당하는 value 뒤에 ',값'을 붙여준다.
            dic[text[n]] = dic[text[n]]+","+text
        #없다면 그냥 key/value 패어로 추가
        else:
            dic[text[n]] = text
    #딕셔너리의 키값들만 리스트로 만들기
    keyList = list(dic.keys())
    #알파벳 오름차순으로 정렬
    keyList.sort()
    resultList = []

    #키값들만 있는 리스트를 돌리면서 딕셔너리에 대입하면서 원래 단어를 찾는다.
    for key in keyList:
        #만약에 value 값에 ,가 포함되어 있으면 같은 key값을 공유하던 단어들이므로
        #split을 써서 나눠준 후 각각을 resultList 리스트에 붙여준다.
        if ',' in dic[key]:
            for word in dic[key].split(","):
                resultList.append(word)
        #,가 없다면 한 단어 뿐이므로 그냥 resultList 리스트에 추가해준다.
        else:
            resultList.append(dic[key])
    return resultList

# 아래는 테스트로 출력해 보기 위한 코드입니다.
print( strange_sort(["sun", "bed", "cer", "gogogoo", "google"], 1) )

문자열_내맘대로_정렬하기.py


import os
import shutil
import subprocess

# Configuration file for the Sphinx documentation builder.
#
# For the full list of built-in configuration values, see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html

# -- Project information -----------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information

project = "OptiHPLCHandler"
copyright = (
    "2023, Søren Furbo, Samual Burnage, Erik Trygg, Jacob Kofoed, Søren Bertelsen"
)
author = "Søren Furbo, Samual Burnage, Erik Trygg, Jacob Kofoed, Søren Bertelsen"
release = "2.5.0"

# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration

extensions = ["sphinx.ext.autodoc"]

templates_path = ["_templates"]
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]

# -- Options for HTML output -------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output

html_theme = "alabaster"
html_static_path = ["_static"]

# Making the class diagrams
# Approach inspired by https://pythonhosted.org/theape/documentation/developer/explorations/explore_graphs/explore_pyreverse.html # noqa: E501
os.makedirs("./_readthedocs/html/_static/", exist_ok=True)
class_diagram_files = ["empower_instrument_method", "empower_handler"]
for file in class_diagram_files:
    # Might be more readable with shlex.split as in the link above.
    subprocess.call(
        [
            "pyreverse",
            "-A",
            "-S",
            "-o",
            "html",
            "-p",
            file,
            "./src/OptiHPLCHandler/" + file + ".py",
        ]
    )  # Crating the class diagrams
    shutil.move(
        "classes_" + file + ".html", "_readthedocs/html/_static/" + file + ".html"
    )  # Moving the class diagrams so that sphinx can find them


import argparse
import json
import os
import subprocess
import sys
from ddkcore.parser import DdkParser

class Kit(object):

    __app_description = 'Docker Development Kit'
    __app_name = 'ddk'
    __commands = {}
    __help_message = "Show this help message and exit."
    __parser = None
    __subparsers = None
    __working_directory = None

    def __init__(self, conf, app_version, terminal, exe_path):
        self.__app_version = app_version
        self.__config = None
        self.__default_config = conf
        self.__exe_path = exe_path
        self.__terminal = terminal

    def __configure_commands(self):
        for cmd_name in self.__commands.keys():
            command = self.get_command(cmd_name)
            command.configure()
            parser_config = dict(help=command.description, add_help=False, conflict_handler="resolve")
            parser = self.__subparsers.add_parser(cmd_name, **parser_config)
            self.add_default_arguments(parser)
            if command.has_sub_commands():
                command.configure_sub_commands(parser)
            else:
                command.configure_parser(parser)
                parser.set_defaults(cmd_callback=command)

            if len(command.usage) > 0:
                parser.usage = self.__commands[cmd_name].usage

    def __detect_working_dir(self):
        terminal = self.get_terminal()
        terminal.output("Detection of working directory...", terminal.VERBOSITY_DEBUG)
        current_dir = os.getcwd()
        file_name = self.__default_config["config-name"]
        result_dir = None
        while result_dir is None and current_dir != "/":
            config_file = os.path.abspath(current_dir + "/" + file_name)
            terminal.output("  - " + config_file, terminal.VERBOSITY_DEBUG)
            if os.path.isfile(config_file):
                result_dir = current_dir
            else:
                current_dir = os.path.abspath(current_dir + "/..")

        config_file = os.path.abspath(current_dir + "/" + file_name)
        if os.path.isfile(config_file):
            result_dir = current_dir

        if result_dir is None:
            terminal = self.get_terminal()
            terminal.output("Can't find ddk.json file.")
            terminal.output("Run next command if you don't have ddk environment:\n")
            terminal.output("    " + self.get_name() + " init", terminal.VERBOSITY_SILENT, terminal.COLOR_YELLOW)
            sys.exit(1)

        result_dir = os.path.abspath(result_dir)
        terminal.output("Working directory is " + result_dir, terminal.VERBOSITY_DEBUG)
        self.set_work_dir(result_dir)

    def add_command(self, cmd):
        cmd.set_kit(self)
        self.__commands[cmd.get_name()] = cmd

    def add_default_arguments(self, parser):
        quiet_help = "Operate quietly. This option disables all output."
        verbose_help = "Increases verbosity level. Does not affect if --quiet option is set."
        parser.add_argument('-h', '--help', action='help', default=argparse.SUPPRESS, help=self.__help_message)
        parser.add_argument('-q', '--quiet', action="store_true", help=quiet_help)
        parser.add_argument('-v', '--verbose', action="count", default=0, help=verbose_help)
        parser.add_argument('-d', '--dir', help="Set up working directory.", metavar="PATH")

    def call_package_post_install(self, package):
        config = self.get_config()
        package_dir = config["packages-dir"] + "/" + package
        package_config_path = package_dir + "/ddk.json"
        package_config = self.read_configuration_file(package_config_path)
        if "ddk-post-install" in package_config:
            self.get_terminal().output("Calling post install commands...")
            for command in package_config["ddk-post-install"]:
                variables = {"package_path": self.get_full_path(package_dir)}
                command = self.resolve_variables(command, **variables)
                self.execute_shell_command(command)

    def call_project_init(self, project):
        terminal = self.get_terminal()
        if not self.project_exists(project):
            terminal.output("Project " + project + " is not installed", text_format=terminal.COLOR_RED)
            sys.exit(1)

        config = self.get_config()
        project_dir = config["projects-base-dir"] + "/" + project
        project_config_path = project_dir + "/" + config["projects-ddk-path"]
        if os.path.exists(project_config_path):
            project_config = self.read_configuration_file(project_config_path)
            if "on-init" in project_config:
                variables = {
                    "project_dir": project,
                    "project_path": self.get_full_path(project_dir),
                }
                for command in project_config["on-init"]:
                    command = self.resolve_variables(command, **variables)
                    terminal.output("    > " + command, 0, terminal.COLOR_PURPLE)
                    self.execute_shell_command(command)

    def ensure_packages_dir(self):
        """Make directory for packages if it doesn't exists"""
        dir_name = self.get_full_path(self.get_config()["packages-dir"])
        if not os.path.exists(dir_name):
            terminal = self.get_terminal()
            terminal.output("Ensure packages directory: " + dir_name, terminal.VERBOSITY_DEBUG)
            os.mkdir(dir_name)

    def ensure_projects_dir(self):
        """Make directory for projects if it doesn't exists"""
        config = self.get_config()
        projects_dir = self.get_full_path(config["projects-base-dir"])
        if not os.path.isdir(projects_dir):
            terminal = self.get_terminal()
            terminal.output("Make directory for projects: " + projects_dir, terminal.VERBOSITY_DEBUG)
            os.makedirs(projects_dir)

    def execute_shell_command(self, command):
        terminal = self.get_terminal()
        terminal.output("    > " + command, terminal.VERBOSITY_VERBOSE, terminal.COLOR_PURPLE)
        subprocess.call(command, shell=True)

    def get_command(self, name):
        return self.__commands[name]

    def get_config(self, with_user=True):
        if not with_user:
            return self.__default_config

        if self.__config is None:
            user_config_path = self.get_work_dir() + "/" + self.__default_config["config-name"]
            if os.path.exists(user_config_path):
                user_config = self.read_configuration_file(user_config_path)
            else:
                user_config = {}

            kit_configuration = self.__default_config.copy()
            kit_configuration.update(user_config)
            self.__config = kit_configuration

        return self.__config

    def get_exe_path(self):
        return self.__exe_path

    def get_full_path(self, path):
        if path.startswith("/"):
            return path

        return self.get_work_dir() + "/" + path

    def get_list_of_packages(self):
        terminal = self.get_terminal()
        packages = []
        config = self.get_config()
        packages_dir = config["packages-dir"]
        full_packages_dir = self.get_full_path(packages_dir)
        terminal.output("Scanning " + full_packages_dir, terminal.VERBOSITY_DEBUG)
        if os.path.isdir(packages_dir):
            terminal.output("Scan the packages directory: " + full_packages_dir, terminal.VERBOSITY_DEBUG)
            for package in os.listdir(full_packages_dir):
                if os.path.isdir(full_packages_dir + "/" + package):
                    packages.append(package)

        return packages

    def get_list_of_projects(self):
        """Scan the project directory to get list of available projects"""
        config = self.get_config()
        projects = []
        projects_dir = self.get_full_path(config["projects-base-dir"])
        if os.path.isdir(projects_dir):
            terminal = self.get_terminal()
            terminal.output("Scan the projects directory: " + projects_dir, terminal.VERBOSITY_DEBUG)
            for project in os.listdir(projects_dir):
                if os.path.isdir(projects_dir + "/" + project):
                    terminal.output("  - " + project, terminal.VERBOSITY_DEBUG)
                    projects.append(project)

        return projects

    def get_name(self):
        return self.__app_name

    def get_terminal(self):
        return self.__terminal

    def get_version(self):
        return self.__app_version

    def get_work_dir(self):
        if self.__working_directory is None:
            self.__detect_working_dir()
        return self.__working_directory

    def install_package(self, package):
        config = self.get_config()
        package_dir = self.get_full_path(config["packages-dir"] + "/" + package)
        clone_cmd = "git clone " + config["package-repo-prefix"][0] + package + ".git " + package_dir
        try:
            self.execute_shell_command(clone_cmd)
            self.call_package_post_install(package)
            return True
        except subprocess.CalledProcessError:
            self.get_terminal().output("Can't install the package: " + package, self.get_terminal().COLOR_RED)
            return False

    def package_is_installed(self, package):
        """Check the package is installed"""
        dir_name = self.get_full_path(self.get_config()["packages-dir"] + "/" + package)
        self.get_terminal().output("Check the " + package + " exists: " + dir_name, self.get_terminal().VERBOSITY_DEBUG)
        return os.path.exists(dir_name)

    def process(self, args):
        ddk_params = dict(
            prog=self.__app_name,
            description=self.__app_description,
            add_help=False,
            conflict_handler="resolve",
            usage="%(prog)s <command> ... [options...]"
        )
        self.__parser = DdkParser(**ddk_params)
        self.add_default_arguments(self.__parser)

        self.__subparsers = self.__parser.add_subparsers(dest="command", metavar="", title="Available commands")
        self.__configure_commands()

        args = self.__parser.parse_args(args)

        terminal = self.get_terminal()
        verbosity = args.verbose
        if args.quiet:
            verbosity = terminal.VERBOSITY_QUIET
        terminal.set_verbosity(verbosity)

        command = args.cmd_callback
        del args.cmd_callback
        del args.command

        if args.dir:
            self.set_work_dir(args.dir)

        command.run(args, terminal)

    def project_exists(self, pid):
        """Check project ID is valid item of projects list"""
        return pid in self.get_list_of_projects()

    def read_configuration_file(self, file_path):
        terminal = self.get_terminal()
        terminal.output("Reading configuration file: " + self.get_full_path(file_path), terminal.VERBOSITY_DEBUG)
        json_file = open(file_path)
        config = json.load(json_file)
        json_file.close()
        return config

    def resolve_variables(self, text, **kwargs):
        """Replace variables to passed values"""
        config = self.get_config()
        variables = {
            "network_name": config["network-name"],
            "packages_path": self.get_full_path(config["packages-dir"]),
            "projects_path": self.get_full_path(config["projects-base-dir"]),
            "share_path": self.get_full_path(config["share-dir"]),
        }
        variables.update(kwargs)

        text = str(text)
        terminal = self.get_terminal()
        terminal.output("Resolving variables for " + text, terminal.VERBOSITY_DEBUG)
        for var_name, var_value in variables.iteritems():
            text = str.replace(text, "${" + str.upper(var_name) + "}", str(var_value))
        terminal.output(" > " + text, terminal.VERBOSITY_DEBUG)
        return text

    def set_work_dir(self, work_dir):
        self.__working_directory = work_dir
        os.chdir(work_dir)


# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
        p_stack = list()
        q_stack = list()

        while (p and q) or (p_stack and q_stack):
            while p and q:
                if p.val != q.val:
                    # 當前節點不同
                    return False
                p_stack.append(p)
                q_stack.append(q)
                p = p.left
                q = q.left
            if p != q:
                # 表示當其中一為None時, 而另一節點還可以繼續走訪
                return False
            p = p_stack.pop()
            q = q_stack.pop()
            p = p.right
            q = q.right
        return p == q


from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm import relationship
from .db import db
class Mall(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50))
    account_id=db.Column(db.Integer, db.ForeignKey('account.id'))

    unit = relationship("Unit", uselist=False, back_populates="mall") # one to one

    def __repr__(self):
        return '<Mall %s>' % self.name


# coding=utf-8
# created by msg on 2019/11/24 2:30 下午

import re
import unicodedata
import tensorflow as tf
from sklearn.model_selection import train_test_split

# 第一个参数指定字符串标准化的方式。
# NFC表示字符应该是整体组成(比如可能的话就使用单一编码)
# NFD表示字符应该分解为多个组合字符表示。
# unicodedata.category(chr) 把一个字符返回它在UNICODE里分类的类型
def unicode_to_ascii(s):
    return ''.join(c for c in unicodedata.normalize('NFD', s) if unicodedata.category(c) != 'Mn')

# 预处理
def preprocess_sentence(w):
    # 转小写换编码
    w = unicode_to_ascii(w.lower().strip())

    # 标点符号前后加上空格
    #  eg: "he is a boy." => "he is a boy ."
    w = re.sub(r"([?.!,¿])", r" \1 ", w)

    # 去掉多余空格
    w = re.sub(r'[" ]+', " ", w)

    # 去掉前后空格
    w = w.rstrip().strip()

    # 加开始和结束字符
    w = '<start> ' + w + ' <end>'
    return w

# 创建数据集
def parse_data(filename):
    lines = open(filename, encoding='utf-8').read().strip().split('\n')
    sentence_pairs = [line.split('\t') for line in lines]
    preprocessed_sentence_pairs = [(preprocess_sentence(en), preprocess_sentence(spa)) for en, spa in sentence_pairs]
    return zip(*preprocessed_sentence_pairs)

# 分词和词语到索引的转换
def tokenizer(lang):
    # 用空格分词
    lang_tokenizer = tf.keras.preprocessing.text.Tokenizer(num_words=None, filters='', split=' ')
    # 先分词
    lang_tokenizer.fit_on_texts(lang)
    # 再创建词到索引的映射
    tensor = lang_tokenizer.texts_to_sequences(lang)
    # 不够长度的在后边补0
    tensor = tf.keras.preprocessing.sequence.pad_sequences(tensor, padding='post')
    return tensor, lang_tokenizer

# 获取句子的最大长度
def max_length(tensor):
    return max(len(t) for t in tensor)

# 拆分训练测试数据集
def split(input_tensor, output_tensor, test_size=0.2):
    return train_test_split(input_tensor, output_tensor, test_size=test_size)

# 构建训练集
def make_dataset(input_tensor, output_tensor, batch_size=64, epochs=20, shuffle=True):
    dataset = tf.data.Dataset.from_tensor_slices((input_tensor, output_tensor))
    if shuffle:
        dataset = dataset.shuffle(30000)
    dataset = dataset.repeat(epochs).batch(batch_size, drop_remainder=True)
    return dataset

if __name__ == '__main__':
    spa_eng_path = 'spa-eng/spa.txt'
    en_dataset, spa_dataset = parse_data(spa_eng_path)
    print(en_dataset[-1])
    print(spa_dataset[-1])

    # 西班牙语到英语的训练
    input_tensor, input_tokenizer = tokenizer(spa_dataset[:30000])
    output_tensor, output_tokenizer = tokenizer(en_dataset[:30000])

    print(input_tensor)
    print(output_tensor)

    # 查看输入和输出的最大长度
    print(max_length(input_tensor))
    print(max_length(output_tensor))

    # 拆分数据集为训练集和验证集
    input_train, input_eval, output_train, output_eval = split(input_tensor, output_tensor)
    print(len(input_train), len(input_eval), len(output_train), len(output_eval))

    # 构建tf.data格式
    train_dataset = make_dataset(input_train, output_train)
    eval_dataset = make_dataset(input_eval, output_eval)

    # 打印一下
    for x, y in train_dataset.take(1):
        print(x.shape)
        print(y.shape)
        print(x)
        print(y)


"""
Milan Murray 08/11/2019
Exercise 4: Currency conversion from Euro to Yen. Input rate and output equivalent Yen.
"""
print("\n---\tExercise 4: Currency conversion from Euro to Yen. Input rate and output equivalent Yen.\t---\n")
# Other

# Constants
CURRENCY_SIGNIFICANT_FIGURE = 2

# Inputs
currency_rate = float(input("Please enter the rate of yen to euro, enter \"0\" to stop: "))  # 120.77
if currency_rate != 0:
    euro = float(input("Please enter an amount of euro: "))

# Processes
    while euro != 0:
        yen = euro * currency_rate
        yen = round(yen, CURRENCY_SIGNIFICANT_FIGURE)
        print(euro, "euro equals", yen, "yen.")
        euro = float(input("Please enter an amount of euro, enter \"0\" to stop: "))
else:
    euro = 0

semester-1/week-7/Lab 7 X00162027/Exercise 4.py