from flask import flash, redirect, render_template, session, url_for, request, jsonify
from flaskapp.forms import LoginForm
from manage import app
from flaskapp.forms import LoginForm
import datetime
import jwt
from functools import wraps

# Token required Decorator function
def token_required(func):
    @wraps(func)
    def wrapped(*args, **kwargs):
        try:
            token = request.args['token']
        except:
            flash('Please login', 'danger')
            return redirect(url_for('login'))
        if not token:
            return jsonify({'Alert':'Invalid Token'})
        try:
            payload = jwt.decode(token, app.config['SECRET_KEY'], 'HS256')
        except:
            return jsonify({'message':'Token Expired'}), 403
        return func(*args, **kwargs)
    return wrapped

# user route after login
@app.route('/user', methods = ['POST','GET'])
@token_required
def user():
    try:
        username = request.args['username']
        return render_template('user.html', title = 'user', data = username)
    except:
        return redirect(url_for('login'))

# login route for user
@app.route('/login', methods = ['POST', "GET"])
def login():
    form = LoginForm()
    if form.validate_on_submit():
        if form.username.data and form.password.data :
            session['logged_in'] = True
            token = jwt.encode({
                'user':form.username.data,
                'exp': datetime.datetime.utcnow() + datetime.timedelta(seconds = 60)
            }, app.config['SECRET_KEY'])
            flash('you have been logged in!', 'success')
            return redirect(url_for('user', token = token, username = form.username.data))
        else:
            flash('Login unsuccesful!')
    return render_template('login.html', title = 'login', form = form)

# home route redirect to login
@app.route('/')
def home():
    return redirect(url_for('login'))


"""Tests for the random variable implementation."""

import itertools
import unittest

import numpy as np
import pytest
import scipy.stats

import probnum
from probnum import linops, randvars
from tests.testing import NumpyAssertions

def test_rng_not_generator_raises_error():

    # dummy random variable that implements a sample method.
    rv = randvars.Normal(0.0, 1.0)

    # incorrect random number generator (np.random.Generator is expected)
    rng = np.random.RandomState(seed=1)

    with pytest.raises(TypeError):
        rv.sample(rng=rng)

class RandomVariableTestCase(unittest.TestCase, NumpyAssertions):
    """General test case for random variables."""

    def setUp(self) -> None:
        """Scalars, arrays, linear operators and random variables for tests."""
        # Seed
        self.rng = np.random.default_rng(42)

        # Random variable instantiation
        self.scalars = [0, int(1), 0.1, np.nan, np.inf]
        self.arrays = [np.empty(2), np.zeros(4), np.array([]), np.array([1, 2])]

        # Random variable arithmetic
        self.arrays2d = [
            np.empty(2),
            np.zeros(2),
            np.array([np.inf, 1]),
            np.array([1, -2.5]),
        ]
        self.matrices2d = [np.array([[1, 2], [3, 2]]), np.array([[0, 0], [1.0, -4.3]])]
        self.linops2d = [linops.Matrix(A=np.array([[1, 2], [4, 5]]))]
        self.randvars2d = [
            randvars.Normal(mean=np.array([1, 2]), cov=np.array([[2, 0], [0, 5]]))
        ]
        self.randvars2x2 = [
            randvars.Normal(
                mean=np.array([[-2, 0.3], [0, 1]]),
                cov=linops.SymmetricKronecker(A=np.eye(2), B=np.ones((2, 2))),
            ),
        ]

        self.scipyrvs = [
            scipy.stats.bernoulli(0.75),
            scipy.stats.norm(4, 2.4),
            scipy.stats.multivariate_normal(np.random.randn(10), np.eye(10)),
            scipy.stats.gamma(0.74),
            scipy.stats.dirichlet(alpha=np.array([0.1, 0.1, 0.2, 0.3])),
        ]

class InstantiationTestCase(RandomVariableTestCase):
    """Test random variable instantiation."""

    def test_rv_dtype(self):
        """Check the random variable types."""
        pass

    def test_rv_from_number(self):
        """Create a random variable from a number."""
        for x in self.scalars:
            with self.subTest():
                rv = probnum.asrandvar(x)
                self.assertIsInstance(rv, randvars.RandomVariable)

    def test_rv_from_ndarray(self):
        """Create a random variable from an array."""
        for arr in self.arrays2d:
            with self.subTest():
                rv = probnum.asrandvar(arr)
                self.assertIsInstance(rv, randvars.RandomVariable)

    # def test_rv_from_linearoperator(self):
    #     """Create a random variable from a linear operator."""
    #     for linop in linops:
    #       with self.subTest():
    #           probnum.asrandvar(A)

    def test_rv_from_scipy(self):
        """Create a random variable from a scipy random variable."""
        for scipyrv in self.scipyrvs:
            with self.subTest():
                rv = probnum.asrandvar(scipyrv)
                self.assertIsInstance(rv, randvars.RandomVariable)

class ArithmeticTestCase(RandomVariableTestCase):
    """Test random variable arithmetic and broadcasting."""

    def test_rv_addition(self):
        """Addition with random variables."""
        for (x, rv) in list(itertools.product(self.arrays2d, self.randvars2d)):
            with self.subTest():
                z1 = x + rv
                z2 = rv + x
                self.assertEqual(z1.shape, rv.shape)
                self.assertEqual(z2.shape, rv.shape)
                self.assertIsInstance(z1, randvars.RandomVariable)
                self.assertIsInstance(z2, randvars.RandomVariable)

    def test_rv_scalarmult(self):
        """Multiplication of random variables with scalar constants."""
        for (alpha, rv) in list(itertools.product(self.scalars, self.randvars2d)):
            with self.subTest():
                if np.isinf(alpha):
                    with self.assertWarns(RuntimeWarning):
                        z = alpha * rv
                else:
                    z = alpha * rv
                self.assertEqual(z.shape, rv.shape)
                self.assertIsInstance(z, randvars.RandomVariable)

    def test_rv_broadcasting(self):
        """Broadcasting for arrays and random variables."""
        for alpha, rv in list(itertools.product(self.scalars, self.randvars2d)):
            with self.subTest():
                z = alpha + rv
                self.assertEqual(z.shape, rv.shape)

                z = rv - alpha
                self.assertEqual(z.shape, rv.shape)

    def test_rv_dotproduct(self):
        """Dot product of random variables with constant vectors."""
        for x, rv in list(
            itertools.product([np.array([1, 2]), np.array([0, -1.4])], self.randvars2d)
        ):
            with self.subTest():
                z1 = rv @ x
                z2 = x @ rv
                self.assertIsInstance(z1, randvars.RandomVariable)
                self.assertIsInstance(z2, randvars.RandomVariable)
                self.assertEqual(z1.shape, ())
                self.assertEqual(z2.shape, ())

    def test_rv_matmul(self):
        """Multiplication of random variables with constant matrices."""
        for A, rv in list(itertools.product(self.matrices2d, self.randvars2d)):
            with self.subTest():
                y2 = A @ rv
                self.assertEqual(y2.shape[0], A.shape[0])
                self.assertIsInstance(y2, randvars.RandomVariable)

    def test_rv_linop_matmul(self):
        """Linear operator applied to a random variable."""
        for A, rv in list(itertools.product(self.linops2d, self.randvars2d)):
            with self.subTest():
                y = A @ rv + np.array([-1, 1.1])
                self.assertEqual(y.shape[0], A.shape[0])

    def test_rv_vector_product(self):
        """Matrix-variate random variable applied to vector."""
        for rv in self.randvars2x2:
            with self.subTest():
                x = np.array([[1], [-4]])
                y = rv @ x
                X = np.kron(np.eye(rv.shape[0]), x)
                truemean = rv.mean @ x
                truecov = X.T @ rv.cov.todense() @ X
                self.assertIsInstance(
                    y,
                    randvars.RandomVariable,
                    "The variable y does not have the correct type.",
                )
                self.assertEqual(
                    y.shape, (2, 1), "Shape of resulting random variable incorrect."
                )
                self.assertAllClose(
                    y.mean, truemean, msg="Means of random variables do not match."
                )
                self.assertAllClose(
                    y.cov.todense(),
                    truecov,
                    msg="Covariances of random variables do not match.",
                )

class ShapeTestCase(RandomVariableTestCase):
    """Test methods related to the shape of a random variable or its realizations."""

    def test_reshape(self):
        """Reshape a random variable and test for correct output shape."""
        for rv in self.randvars2x2:
            for shape in [(4, 1), (2, 2), (4,), (1, 4)]:
                with self.subTest():
                    try:
                        reshaped_rv = rv.reshape(newshape=shape)

                        self.assertEqual(reshaped_rv.shape, shape)
                        self.assertEqual(
                            reshaped_rv.sample(rng=self.rng, size=1).shape, shape
                        )
                    except NotImplementedError:
                        pass
        for rv in self.randvars2d:
            for shape in [(2, 1), (2,), (1, 2)]:
                with self.subTest():
                    try:
                        reshaped_rv = rv.reshape(newshape=shape)

                        self.assertEqual(reshaped_rv.shape, shape)
                        self.assertEqual(
                            reshaped_rv.sample(rng=self.rng, size=1).shape, shape
                        )
                    except NotImplementedError:
                        pass

    def test_sample_shape(self):
        """Sample from a random variable with different sizes and check sample
        shapes."""
        pass


#!/usr/bin/python3.7
# 剑指 Offer 31. 栈的压入、弹出序列
from typing import List
class Solution:
    def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool:
        stack,index = [],0
        for i in range(len(pushed)):
            stack.append(pushed[i])
            while stack and popped[index] == stack[-1]:
                stack.pop()
                index += 1
        return not stack

if __name__ == '__main__':

    try:
        assert exec
        print('解答正确')
    except AssertionError:
        print('解答错误')

剑指 Offer 31. 栈的压入、弹出序列.py


import sys

print('start!')

fname = 'server.dat'

try:
    f = open(fname, 'r')
except IOError:
    print('Could not read file.', fname)
    sys.exit()

with f:
    lines = f.readlines()
    for line in lines:
        print(line)
    f.close()


from bisect import bisect_left
from functools import total_ordering
from heapq import merge
import time
from random import randint

@total_ordering
class Stack(list):
    def __lt__(self, other):
        return self[-1] < other[-1]
    def __eq__(self, other):
        return self[-1] == other[-1]
def patience_sort(collection: list) -> list:
    stacks = []
    # sort into stacks
    for element in collection:
        new_stacks = Stack([element])
        i = bisect_left(stacks, new_stacks)
        if i != len(stacks):
            stacks[i].append(element)
        else:
            stacks.append(new_stacks)

    # use a heap-based merge to merge stack efficiently
    collection[:] = merge(*[reversed(stack) for stack in stacks])
    return collection

def quick_sort(array):
    if len(array) < 2:
        return array
    else:
        pivot = array[0]
        less = [i for i in array[1:] if i <= pivot]
        greater = [i for i in array[1:] if i > pivot]
        return quick_sort(less) + [pivot] + quick_sort(greater)

testList = []
for i in range(1000000):
    testList.append(randint(0,1000000))

testList2 = testList.copy()

start = time.time()
sortedList = quick_sort(testList)
end = time.time()
print("\nTime for quick_sort :")
print(end - start)

start2 = time.time()
sortedList = patience_sort(testList)
end2 = time.time()
print("\nTime for patience_sort :")
print(end2 - start2)


import pandas as pd
import requests
import numpy as np

if __name__ == '__main__':
    features = ["age", "sex", "cp", "trestbps", "chol", "fbs", "restecg", "thalach", "exang", "oldpeak", "slope", "ca",
                "thal"]
    num_requests = 5
    address = 'http://127.0.0.1:8000/predict'
    for i in range(num_requests):
        num_rows = np.random.randint(low=1, high=10, size=1).item()
        data = np.random.randint(low=1, high=190, size=(num_rows, len(features)))
        response = requests.get(address, json={'data': data.tolist(), 'features': features})
        print(response.json())

    csv_path = 'data/heart.csv'
    df = pd.read_csv(csv_path)
    for i in range(df.shape[0]):
        row = df.iloc[i].tolist()[:-1]
        response = requests.get(address, json={'data': [row], 'features': features})
        print(response.json())


from django.contrib import messages
from django.contrib.sites.shortcuts import get_current_site
from django.core.mail import EmailMessage
from django.template.loader import render_to_string
from django.utils.encoding import force_bytes
from django.utils.http import urlsafe_base64_encode
from django.utils.safestring import mark_safe
from accounts.tokens import account_activation_token

def send_activation_email(request, user):
    current_site = get_current_site(request)
    subject = 'Activate Your Account'
    message = render_to_string('accounts/account_activation_email.html', {
        'user': user,
        'domain': current_site.domain,
        'uid': urlsafe_base64_encode(force_bytes(user.pk)),
        'token': account_activation_token.make_token(user),
        'protocol': 'https' if request.is_secure() else 'http'
    })
    email = EmailMessage(
        subject, message, to=[user.email]
    )
    if email.send():
        messages.success(request, mark_safe(f'Dear {user}, please go to you email {user.email} inbox '
                                            f'and click on received activation link to confirm and complete the '
                                            f'registration. Note: Check your spam folder.'))
    else:
        messages.error(request, mark_safe(f'Problem sending confirmation email to {user.email}, check if you typed it '
                                          f'correctly.'))


#import io
#from gensim.models.fasttext import FastText as FT_gensim
#from gensim.test.utils import datapath
from gensim.models.keyedvectors import KeyedVectors
import pickle

class Sim_finder:
    def __init__(self, model_file="saved_objects/fasttext/wiki-news-300d-1M.vec", pick_file="saved_objects/fasttext/model.p"):
        try:
            self.model = pickle.load(open(pick_file, "rb"))
            print("loaded fasttext from pickle")
        except:
            print("loading fasttext for the first time")
            self.model = KeyedVectors.load_word2vec_format(model_file)
            print("saving with pickle")
            pickle.dump(self.model, open(pick_file, "wb"))

    def gensim_test():
        model = KeyedVectors.load_word2vec_format("wiki-news-300d-1M.vec")
        print(model.most_similar("nights"))

    def get_close_words(self, positive, negative=None, n=50):
        if type(positive) == str: positive = positive.split()
        return [item[0] for item in self.model.most_similar(positive, negative, topn=n)]


# !usr/bin/env python

# test
import time
#import pyinterface
import rospy
from necst.msg import Status_limit_msg
from necst.msg import Status_encoder_msg
from necst.msg import Status_drive_msg

class limit(object):
    #self.dio = pyinterface.create_gpg2000(3)

    stop_flag = 0
    ret = [0]*4
    error_box = [1]*32
    msg = ""

    def sub_enc(self, req):
        self.error_box[0:1] = [req.value[0]]
        self.error_box[2:3] = [req.value[1]]

    def read(self):
        pub = rospy.Publisher("limit_check", Status_limit_msg, queue_size=10, latch=True)
        st = Status_limit_msg()
        while self.stop_flag == 0:

            try:
                st.error_box = self.error_box
                st.error_msg = self.msg
                pub.publish(st)
            except:
                rospy.logerr("no publish")
                self.stop_flag = 1

            time.slep(0.1)

if __name__ == "__main__":
    li = limit()
    rospy.init_node("limit_check")
    sub = rospy.Subscriber("status_drive", Status_drive_msg, li.sub_enc)
    time.sleep(3)
    li.read()


def main():
    n = int(input())
    if n == 2:
        a, b = map(int, input().split())
        k = gcd(a, b)
        num = [i for i in range(1, k + 1) if k % i == 0]
        print(*num, sep='\n')
    elif n == 3:
        t = list(map(int, input().split()))
        while True:
            if t[0] == t[1] == t[2]:
                break
            i, j, k = gcd(t[0], t[1]), gcd(t[1], t[2]), gcd(t[2], t[0])
            t[0], t[1], t[2] = i, j, k
        num = [i for i in range(1, t[0] + 1) if t[0] % i == 0]
        print(*num, sep='\n')

    # input()
    # a = list(map(int, input().split()))
    # g = a[0]
    # for i in range(1, len(a)):
    #     g = math.gcd(g, a[i])
    # s = set()
    # for i in range(1, int(g ** 0.5) + 1):
    #     if g % i == 0:
    #         s.update([i, g // i])
    # for i in sorted(s):
    #     print(i)

def gcd(m, n):
    if m < n:
        m, n = n, m
    if n == 0:
        return m
    if m % n == 0:
        return n
    else:
        return gcd(n, m % n)

if __name__ == "__main__":
    main()


def decode_crontab(expression):
    fields = expression.split()
    if len(fields) != 5:
        return "Invalid crontab expression. Ensure you have 5 time scheduling fields."

    field_names = ['minute', 'hour', 'day of the month', 'month', 'day of the week']
    field_ranges = [(0, 59), (0, 23), (1, 31), (1, 12), (0, 6)]
    field_values = ['0-59', '0-23', '1-31', '1-12 or JAN-DEC', '0-6 or SUN-SAT']

    decoded_expression = []

    for idx, field in enumerate(fields):
        # Range validation
        if not field == "*":
            if "," in field:
                values = field.split(',')
                for val in values:
                    if not val.isdigit() or not field_ranges[idx][0] <= int(val) <= field_ranges[idx][1]:
                        return f"Value {val} for {field_names[idx]} is out of range ({field_values[idx]})."
            elif "-" in field:
                start, end = field.split('-')
                if not (start.isdigit() and end.isdigit()) or not (field_ranges[idx][0] <= int(start) <= field_ranges[idx][1] and field_ranges[idx][0] <= int(end) <= field_ranges[idx][1]):
                    return f"Range {start}-{end} for {field_names[idx]} is out of range ({field_values[idx]})."
            elif "/" in field:
                value, step = field.split('/')
                if not (value.isdigit() and step.isdigit()) or not (field_ranges[idx][0] <= int(value) <= field_ranges[idx][1]):
                    return f"Step value {value}/{step} for {field_names[idx]} is out of range ({field_values[idx]})."
            elif not field.isdigit() or not field_ranges[idx][0] <= int(field) <= field_ranges[idx][1]:
                return f"Value {field} for {field_names[idx]} is out of range ({field_values[idx]})."

        # Decode special characters
        decoded = field
        if '*' in field:
            decoded = decoded.replace('*', 'every')
        if ',' in field:
            decoded = decoded.replace(',', ' and ')
        if '-' in field:
            start, end = field.split('-')
            decoded = f"from {start} to {end}"
        if '/' in field:
            value, step = field.split('/')
            decoded = f"every {step} starting from {value}"
        if 'L' in field:
            decoded = decoded.replace('L', 'last')
        if 'W' in field:
            decoded = decoded.replace('W', 'nearest weekday')

        decoded_expression.append(f"For {field_names[idx]}, it's set to: {decoded} ({field_values[idx]})")

    return '. '.join(decoded_expression)


from django.urls import path
from order.views import basket, add_to_basket, remove_from_basket, delivery, take_away, order_status, \
    change_order_status, admin_orders_panel, received_order, expand_quantity, reduce_quantity, \
    admin_order_detail

urlpatterns = [
    path('basket/', basket, name='basket'),
    path('add_to_basket/<int:element_pk>', add_to_basket, name='add_to_basket'),
    path('remove_from_basket/', remove_from_basket, name='remove_from_basket'),
    path('delivery/', delivery, name='delivery'),
    path('take_away/', take_away, name='take_away'),
    path('order_status/', order_status, name='order_status'),
    path('change_order_status/<int:order_pk>', change_order_status, name='change_order_status'),
    path('admin_orders_panel/', admin_orders_panel, name='admin_orders_panel'),
    path('received_order/', received_order, name='received_order'),
    path('expand_quantity/<int:element_pk>', expand_quantity, name='expand_quantity'),
    path('reduce_quantity/<int:element_pk>', reduce_quantity, name='reduce_quantity'),
    path('admin_order_detail/<int:user_pk>', admin_order_detail, name='admin_order_detail'),
]


import dash
import dash_bootstrap_components as dbc
import pandas as pd
import plotly.express as px
import plotly.graph_objs as go
from dash import html
from dash import dcc
from dash.dependencies import Input, Output

data = pd.DataFrame({
    'Task': ['Task 1', 'Task 2', 'Task 3'],
    'Start': ['2022-01-01', '2022-01-05', '2022-01-10'],
    'Finish': ['2022-01-10', '2022-01-15', '2022-01-20'],
    'Capacity': [50, 70, 80]
})

fig = px.timeline(data, x_start="Start", x_end="Finish", y="Task")
fig.update_yaxes(autorange="reversed")

timeline = go.Figure()
timeline.add_trace(go.Scatter(x=data['Start'], y=data['Capacity'], name='Capacity'))
timeline.update_layout(title='Team Capacity Timeline', xaxis_title='Date', yaxis_title='Capacity')

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

app.layout = html.Div([
    html.H1('Team Capacity Dashboard'),
    dcc.Graph(id='gantt', figure=fig),
    dcc.Graph(id='timeline', figure=timeline)
])

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


from __future__ import print_function
import numpy as np
import networkx as nx
import scipy.sparse as sp
import tensorflow as tf
from utilities import run_random_walks_n2v
import dill

flags = tf.app.flags
FLAGS = flags.FLAGS
np.random.seed(123)

def load_graphs(dataset_str):
    """Load graph snapshots given the name of dataset"""
#np.load()专门用来加载.npy文件，.npy文件是一种用来保存数据的文件。
#graphs.npz文件里面存储的是一个graph文件（可以看作一个对象）。.npz文件是一个压缩文件，文件下面会有很多子文件
#graphs是一个列表，列表存储了图的静态快照，列表中的每个元素是一个图对象。
    graphs = np.load("data/{}/{}".format(dataset_str, "graphs.npz"), allow_pickle=True, encoding="latin1")['graph']
    print("Loaded {} graphs ".format(len(graphs)))
#map()会根据提供的函数对指定序列做映射。
#第一个参数 function 以参数序列中的每一个元素调用 function 函数，返回包含每次 function 函数返回值的新列表。
#lambda后面接的是函数的形参
#python3的map函数返回的是一个迭代器，python2的map函数返回的是一个新列表
#adj_matrices可以看作是一个新的列表，列表中的每个元素为图对象转换后的邻接矩阵。
    adj_matrices = list(map(lambda x: nx.adjacency_matrix(x), graphs))
    return graphs, adj_matrices

def load_feats(dataset_str):
    """ Load node attribute snapshots given the name of dataset (not used in experiments)"""
    features = np.load("data/{}/{}".format(dataset_str, "features.npz"), allow_pickle=True)['feats']
    print("Loaded {} X matrices ".format(len(features)))
    return features

def sparse_to_tuple(sparse_mx):
    """Convert scipy sparse matrix to tuple representation (for tf feed dict)."""
    #将邻接矩阵转化成元组表示[[],[],[],...]=[[(),a],[(),a],[(),a]...[(),a]]
    def to_tuple(mx):
#coo矩阵用来压缩系数矩阵，coo矩阵由三个数组组成，一个存放非零元素的值。
#剩下的两个数组，一个用来存放非零元素位置的行，一个用来存放非零元素位置的列。
        if not sp.isspmatrix_coo(mx):
            mx = mx.tocoo()
#[row],[col](2*10)=[[第一非零元素行位置，第一非零元素列位置],[],...,[],[]](10*2)
        coords = np.vstack((mx.row, mx.col)).transpose()
        #values是一个列表[_,_,_,...,_]
        values = mx.data
        shape = mx.shape
        return coords, values, shape

    def to_tuple_list(matrices):
        # Input is a list of matrices.
        coords = []
        values = []
        shape = [len(matrices)]
        for i in range(0, len(matrices)):
            mx = matrices[i]
            if not sp.isspmatrix_coo(mx):
                mx = mx.tocoo()
            # Create proper indices - coords is a numpy array of pairs of indices.
            #np.vstack((a,b))返回的是一个a和b堆叠的数组.
            coords_mx = np.vstack((mx.row, mx.col)).transpose()
            z = np.array([np.ones(coords_mx.shape[0]) * i]).T
#z就变为了[[_,_,_],[_,_,_],...,[_,_,_]]
            z = np.concatenate((z, coords_mx), axis=1)
            #astype(int)将数组z中的所有数据元素类型更换为int类型。
            z = z.astype(int)
#extend()函数用于在列表末尾一次性追加另一个序列中的多个值（用新列表扩展原来的列表）。
#coords = [[_,_,0],[_,_,1],[_,_,2],...,[_,_,n]]最后一个数字是几，就表示是第几个矩阵。
#values = [_,_,_,...,_]
            coords.extend(z)
            values.extend(mx.data)

#shape = [_,_,_] 三维
        shape.extend(matrices[0].shape)
        shape = np.array(shape).astype("int64")
        values = np.array(values).astype("float32")
        coords = np.array(coords)
        return coords, values, shape

#isinstance() 函数来判断一个对象是否是一个已知的类型，类似 type()。
    if isinstance(sparse_mx, list) and isinstance(sparse_mx[0], list):
        # Given a list of lists, convert it into a list of tuples.
        for i in range(0, len(sparse_mx)):
            sparse_mx[i] = to_tuple_list(sparse_mx[i])

    elif isinstance(sparse_mx, list):
        for i in range(len(sparse_mx)):
            sparse_mx[i] = to_tuple(sparse_mx[i])
    else:
        sparse_mx = to_tuple(sparse_mx)

    return sparse_mx

def preprocess_features(features):
    """Row-normalize feature matrix and convert to tuple representation"""
    #sum(1)将一个向量[_,_,...,_]全部元素加和。
    #rowsum = [_]
    rowsum = np.array(features.sum(1))
    r_inv = np.power(rowsum, -1).flatten()
    r_inv[np.isinf(r_inv)] = 0.
    r_mat_inv = sp.diags(r_inv)
    features = r_mat_inv.dot(features)
    return features.todense(), sparse_to_tuple(features)

def normalize_graph_gcn(adj):
    """GCN-based normalization of adjacency matrix (scipy sparse format). Output is in tuple format"""
#adj就变为了coo类型的存储矩阵
#sp.coo_matrix()用来创建稀疏矩阵，该函数返回的是一个矩阵。
    adj = sp.coo_matrix(adj)
#sp.eye函数返回一个adj.shape[0]*adj.shape[0]形状的对角矩阵，对角元素为1。
    adj_ = adj + sp.eye(adj.shape[0])
#先将adj_的元素进行加和，加和后的结果再加上1。
    rowsum = np.array(adj_.sum(1))
#a.flatten()：a是个数组，a.flatten()就是把a降到一维，默认是按行的方向降。
#           ：a若是个矩阵，a.flatten()之后还是个矩阵[[_,_,...,_]]。
#power(x, y) 函数，计算 x 的 y 次方。
#    当 np.diag(array) 中
#    array是一个1维数组时，结果形成一个以一维数组为对角线元素的矩阵
#    array是一个二维矩阵时，结果输出矩阵的对角线元素
    degree_mat_inv_sqrt = sp.diags(np.power(rowsum, -0.5).flatten())
    #dot()返回的是矩阵之间的乘积。（向量之间就是点积）（两个数之间就是乘法）
    #tocoo([copy])：返回稀疏矩阵的coo_matrix形式
    adj_normalized = adj_.dot(degree_mat_inv_sqrt).transpose().dot(degree_mat_inv_sqrt).tocoo()
    return sparse_to_tuple(adj_normalized)

def get_context_pairs_incremental(graph):
    return run_random_walks_n2v(graph, graph.nodes())

def get_context_pairs(graphs, num_time_steps):
    """ Load/generate context pairs for each snapshot through random walk sampling."""
    load_path = "data/{}/train_pairs_n2v_{}.pkl".format(FLAGS.dataset, str(num_time_steps - 2))
    try:
#        dill 可以用于保存对象等大多数Python的数据格式
        context_pairs_train = dill.load(open(load_path, 'rb'))
        print("Loaded context pairs from pkl file directly")
    except (IOError, EOFError):
        print("Computing training pairs ...")
        context_pairs_train = []
        #num_time_steps表示训练的静态快照图，
        #所以这里的context_pairs_train列表包含num_time_steps个数的字典。
        for i in range(0, num_time_steps):
            #run_random_walks_n2v()返回的是一个字典。
            context_pairs_train.append(run_random_walks_n2v(graphs[i], graphs[i].nodes()))
        #保存不同时刻图的随机游走的固定窗口的节点对。
        dill.dump(context_pairs_train, open(load_path, 'wb'))
        print ("Saved pairs")

    return context_pairs_train

def get_evaluation_data(adjs, num_time_steps, dataset):
    """ Load train/val/test examples to evaluate link prediction performance"""
    eval_idx = num_time_steps - 2
    #.npz文件相当于一个文件夹，文件夹目录下面有许多文件。
    eval_path = "data/{}/eval_{}.npz".format(dataset, str(eval_idx))
    try:
        #空格加反斜杠是续行的意思，一行代码太长后面加上 \表示接下来的一行是跟着这一行的。
        #左斜杠/也叫正斜杠，右斜杠\又叫做反斜杠。
        train_edges, train_edges_false, val_edges, val_edges_false, test_edges, test_edges_false = \
            np.load(eval_path, encoding='bytes', allow_pickle=True)['data']
        print("Loaded eval data")
    except IOError:
#adj是图的邻接矩阵，[[第一个图的邻接矩阵],[第二个图的邻接矩阵],[第三个图的邻接矩阵]]
        #next_adjs表示的是一个邻接矩阵
        next_adjs = adjs[eval_idx + 1]
        print("Generating and saving eval data ....")
        train_edges, train_edges_false, val_edges, val_edges_false, test_edges, test_edges_false = create_data_splits(adjs[eval_idx], next_adjs, val_mask_fraction=0.2, test_mask_fraction=0.6)
#数据是以字典的形式存储在.npz文件中，这里键是data。
        np.savez(eval_path, data=np.array([train_edges, train_edges_false, val_edges, val_edges_false,
                                           test_edges, test_edges_false]))

    return train_edges, train_edges_false, val_edges, val_edges_false, test_edges, test_edges_false

def create_data_splits(adj, next_adj, val_mask_fraction=0.2, test_mask_fraction=0.6):
    """In: (adj, next_adj) along with test and val fractions. For link prediction (on all links), all links in
    next_adj are considered positive examples.
    Out: list of positive and negative pairs for link prediction (train/val/test)"""
#edges_all就是[[第一个非零元素行位置，第一个非零元素列位置],[],...,[],[]]
    edges_all = sparse_to_tuple(next_adj)[0]  # All edges in original adj.
#diagonal()查看矩阵对角线上的元素，返回矩阵对角线元素的列表。
#np.newaxis的作用就是在这一位置增加一个一维。就是shape的形状多一个位置用1填充。
    adj = adj - sp.dia_matrix((adj.diagonal()[np.newaxis, :], [0]), shape=adj.shape)  # Remove diagonal elements
    #将adj矩阵的非零元素给消除掉，adj依旧是一个矩阵。
    adj.eliminate_zeros()
#adj.todense()返回一个矩阵。
#np.diag(a)将a数组元素变化为一个对角矩阵。
#当参数a是一个1维数组时，结果形成一个以一维数组为对角线元素的矩阵。[[],[],...,[]]
#当参数a是一个二维矩阵时，结果输出矩阵的对角线元素，[_,_,_,...,_]。
    #sum()该函数返回一个数。
    assert np.diag(adj.todense()).sum() == 0
    if next_adj is None:
        raise ValueError('Next adjacency matrix is None')

#list函数是替换的意思，将原先的数据类型去除掉，然后替换成list这种数据类型。
    edges_next = np.array(list(set(nx.from_scipy_sparse_matrix(next_adj).edges())))
    edges = []   # Constraint to restrict new links to existing nodes.
    #edges_next = [(_,_),(_,_),...,(_,_)]
    for e in edges_next:
        if e[0] < adj.shape[0] and e[1] < adj.shape[0]:
            edges.append(e)
    #edges = [(_,_),(_,_),...,(_,_)]
    edges = np.array(edges)

    def ismember(a, b, tol=5):
    #np.all是与操作，所有元素为true，则输出为true。
    #np.any是或操作，任意一个元素为true，则输出为true。
#round() 方法返回浮点数x的四舍五入值
        rows_close = np.all(np.round(a - b[:, None], tol) == 0, axis=-1)
        return np.any(rows_close)

#edges.shape[0]表示的是边的个数。
    all_edge_idx = list(range(edges.shape[0]))
    np.random.shuffle(all_edge_idx)
    #用来测试的边数和用来验证的边数。
    num_test = int(np.floor(edges.shape[0] * test_mask_fraction))
    num_val = int(np.floor(edges.shape[0] * val_mask_fraction))
    #将图中的前num_val数量的边用来验证，后num_test数量的边用来测试。
    val_edge_idx = all_edge_idx[:num_val]
    test_edge_idx = all_edge_idx[num_val:(num_val + num_test)]
#test_edges = [(),(),...,()]    val_edges = [(),(),...,()]
    test_edges = edges[test_edge_idx]
    val_edges = edges[val_edge_idx]
#np.delete(_,_,_)表明删除目标向量中的子向量
#第一个参数为目标向量，第二个参数为删除子向量的位置，第三个参数表明沿着哪个维度删除。
#将一个图分为训练集，验证集，测试集。
#先将t3时刻的图的节点向量表示出来，然后用t3时刻更新的节点向量表示t4时刻节点的向量。
#也就是以t3时刻更新的节点向量来预测t4时刻哪些节点之间会有连边。
#将t4时刻的图分为训练集，验证集，测试集。以t3时刻更新的节点向量作为输入特征，用训练集训练一个分类器。
    train_edges = np.delete(edges, np.hstack([test_edge_idx, val_edge_idx]), axis=0)

    # Create train edges.
    train_edges_false = []
    while len(train_edges_false) < len(train_edges):
        #adj.shape[0]表示的是adj这个图有多少个节点。
        idx_i = np.random.randint(0, adj.shape[0])
        idx_j = np.random.randint(0, adj.shape[0])
        if idx_i == idx_j:
            #continue退出整个循环。
            continue
        if ismember([idx_i, idx_j], edges_all):
            continue
        if ismember([idx_j, idx_i], edges_all):
            continue
        if train_edges_false:
            if ismember([idx_j, idx_i], np.array(train_edges_false)):
                continue
            if ismember([idx_i, idx_j], np.array(train_edges_false)):
                continue
        #train_edges_false是[[_,_],[_,_],[_,_],...,[_,_]]
        train_edges_false.append([idx_i, idx_j])

    # Create test edges.
    test_edges_false = []
    while len(test_edges_false) < len(test_edges):
        idx_i = np.random.randint(0, adj.shape[0])
        idx_j = np.random.randint(0, adj.shape[0])
        if idx_i == idx_j:
            continue
        if ismember([idx_i, idx_j], edges_all):
            continue
        if ismember([idx_j, idx_i], edges_all):
            continue
        if test_edges_false:
            if ismember([idx_j, idx_i], np.array(test_edges_false)):
                continue
            if ismember([idx_i, idx_j], np.array(test_edges_false)):
                continue
        test_edges_false.append([idx_i, idx_j])

    # Create val edges.
    val_edges_false = []
    while len(val_edges_false) < len(val_edges):
        idx_i = np.random.randint(0, adj.shape[0])
        idx_j = np.random.randint(0, adj.shape[0])
        if idx_i == idx_j:
            continue
        if ismember([idx_i, idx_j], edges_all):
            continue
        if ismember([idx_j, idx_i], edges_all):
            continue

        if val_edges_false:
            if ismember([idx_j, idx_i], np.array(val_edges_false)):
                continue
            if ismember([idx_i, idx_j], np.array(val_edges_false)):
                continue
        val_edges_false.append([idx_i, idx_j])

    assert ~ismember(test_edges_false, edges_all)
    assert ~ismember(val_edges_false, edges_all)
    assert ~ismember(val_edges, train_edges)
    assert ~ismember(test_edges, train_edges)
    assert ~ismember(val_edges, test_edges)
    #无false的是正样本（正类），有false的是负样本（负类）。
    print("# train examples: ", len(train_edges), len(train_edges_false))
    print("# val examples:", len(val_edges), len(val_edges_false))
    print("# test examples:", len(test_edges), len(test_edges_false))

#为什么会有两个train_edges和train_edges_false,因为一个是正样本，一个是负样本。
    return list(train_edges), train_edges_false, list(val_edges), val_edges_false, list(test_edges), test_edges_false


from setuptools import find_packages, setup

project = "rodeo"
version = "0.1.0"

setup(
    name=project,
    version=version,
    description="Art Management Service",
    author="Dino Moraites",
    author_email='dino.moraites@globality.com',
    packages=find_packages(),
    include_package_data=True,
    zip_safe=False,
    keywords='rodeo',
    install_requires=[
        'Flask>=0.10.1',
        'Flask-Admin>=1.3.0',
        'Flask-Cache>=0.13.1',
        'Flask-Migrate>=1.3.0',
        'Flask-SQLAlchemy>=2.0',
        'Flask-Script>=2.0.5',
        'Flask-Testing>=0.4.2',
        'Flask-UUID>=0.2',
        'pyparsing>=2.0.5',
        'PyYAML',
        'psycopg2>=2.6.1',
        'requests>=2.5.3',
        'sqlalchemy-utils>=0.31.0',
        'webargs>=0.11.0',
        'uwsgi>=2.0.10',
    ],
    extras_require={
        'aws': [
            'boto>=2.38.0',
        ],
    },
    setup_requires=[
        'nose>=1.3.6',
    ],
    dependency_links=[
    ],
    entry_points={
        'console_scripts': [
            'createall = rodeo.cmd:createall',
        ]
    },
    test_suite='tests',
    tests_require=[
        'coverage>=3.7.1',
        'mock>=1.0.1',
    ],
)


import math

PI = math.pi  # Para criar uma constante se utiliza o nome com letra maiúcula

def dobra_lista(lista):
    return [x*2 for x in lista]

def multiplica(lista):
    r = 1
    for i in lista:
        r *= i
    return r

###  INFORMAÇÃO IMPORTANTE ###
if __name__ == '__main__':  # Se esse módulo estiver sendo importado, não será executada essa parte. Será se tiver sendo executado localmente
    lista = [1,2,3,4,5]

    print(dobra_lista(lista))
    print(multiplica(lista))
    print(PI)

Módulo 2 - Python Intermediário (Programação Procedural)/Aula 62 - Criando Módulos/calculos.py


# -*- coding: utf-8 -*-
import numpy as np
import cupy as cp
#from numba import jit
import time
import pandas as pd
import random
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
import copy
import os
os.chdir('D:/R.data/DSL_paper') #设定路径

#----时间纪录(开始)----
startTime = time.time()

#----资料初始化(本地端)----
sourceData = pd.read_csv('assets_cupy/StoreData.csv') #读取原始资料
preferenceTable = pd.read_csv('assets_cupy/preferenceTable.csv') #读取商品偏好表
sourceData.drop(['产品代号', '品名','输入格式(序,产品代码+品名,单价,体积,种类)'], axis = 1, inplace=True) #移除不必要的资料字段
sourceData.rename(index=str, columns={"重量(g)": "重量"}, inplace=True) #重新命名字段名称
goodData = copy.copy(sourceData) #将原始资料复制一份
sourceData = sourceData.values

goodData['Selected'], goodData['Preference'] = 0, 1 #新增被选择字段
#goodData = goodData.values #转换成ndarry型态
#preferenceTable = preferenceTable.values

#----环境参数设定----
maxVolume = 47*32*39 #最大箱子体积
maxWeight = 16000 #最大重量(g)
popAmount = 30 #人口数量
crossRate = 1 #交配率
mutationRate = 0.001#突变率
eliteValues = round(popAmount*0.1) #菁英数量
maxGen = 100 #世代次数

#----使用者需输入的参数(假设)----
dietHabit = '荤食' #荤食与素食的选择
userItemValues = 22 #使用者需要的数量
maxPrice = 1500 #使用者金额
exceptBrandList = np.array(['大同']) #将要剔除的品牌

#----Function----
#饮食习惯(荤或素食):
#在开始算法前先将该饮食习惯给加入
#若为荤食则包含素食和荤食, 反之只有素食
def diet_select(good_data, diet_habit_list):
    #good_data: 原始商品资料集
    #diet_habit_list: 荤食或素食的选择
    if diet_habit_list=='素食':
        good_data = good_data[good_data.iloc[:, 8] == diet_habit_list]
    return(good_data)

#剔除品牌的方法:
#在开始算法前先将该品牌给移除
def except_brand(good_data, except_brand_list):
    #good_data: 原始商品资料集
    #except_brand_list: 剔除品牌的名称
    for i in range(len(except_brand_list)):
        good_data = good_data[good_data.iloc[:,3] != exceptBrandList[i]] #将要剔除的厂牌移除
    return(good_data)

#偏好值与类别合并:
#将使用者对商品种类的偏好与原始商品资料进行合并成一个Data Frame, 使原始资料有使用者对每个商品的品项偏好
def preference_match(good_data, preference_table):
    #gene_list: 被选择出的基因清单
    #require_goods: 必要性的商品清单
    #non_require_goods: 不必要性的商品清单
    #user_preference: 使用者对商品种类的偏好
    for i in range(len(preference_table)):
        temp = good_data[good_data.iloc[:, 7] == preference_table.iloc[:,0][i]]
        temp.loc[:, 'Preference'] = preference_table.iloc[:,1][i]
        good_data[good_data.iloc[:, 7] == preference_table.iloc[:,0][i]] = temp
    return(good_data)

#初始族群
def initial_pop(good_data, require_goods, non_require_goods, non_require_values, limit_weight):
    #good_data: 原始商品资料集
    #require_goods: 必要性的商品清单
    #non_require_goods: 不必要性的商品清单
    #non_require_values: 不必要性的商品数量
    #limit_weight: 最大重量限制
    while True:
        temp_good = copy.copy(good_data) #先将原始资料暂时给另外一个变数使用
        for i in range(len(require_goods)):
            get_index = temp_good[(temp_good[:,6] == require_goods[i]) & (temp_good[:, 8] !=1)] #取得符合条件的资料
            temp_index = random.randint(0, (len(get_index)-1)) #随机取得品项
            get_index = get_index[temp_index] #取得商品名称
            get_index[8] = 1
            temp_good[temp_good[:,0] == get_index[0]] = get_index

        selected_require = np.random.choice(non_require_goods, non_require_values, replace=False)

        for i in range(len(selected_require)):
            get_index = temp_good[(temp_good[:,6] == selected_require[i]) & (temp_good[:, 8] !=1)] #取得符合条件的资料
            temp_index = random.randint(0, (len(get_index)-1)) #随机取得品项
            get_index = get_index[temp_index] #取得商品名称
            get_index[8] = 1
            temp_good[temp_good[:,0] == get_index[0]] = get_index

        selected_good = temp_good[temp_good[:, 8] == 1]
        sum_weight = np.sum(selected_good[:,7])
        if sum_weight <= limit_weight:
            break
    return(selected_good) #回传结果

#编码染色体:
#必要性商品必定方在最前段, 选择性商品必定放在后段
def create_chromosome(gene_list):
    #gene_list: 被选择出的基因清单
    for i in range(len(gene_list)):
        chromosome = (gene_list[i]['data.frame'][:,0])
        gene_list[i]['chromosome'] = chromosome
    return(gene_list)

#计算总重量
def total_weight(gene_list):
    #gene_list: 被选择出的基因清单
    for i in range(len(gene_list)):
        sum_weight = cp.sum(gene_list[i]['data.frame'][:,8])
        gene_list[i]['totalWeight'] = cp.array([sum_weight])
    return(gene_list)

#偏好的适应度方法(算式分母为偏好值1~偏好的最大值)
def fitness_preference(gene_list, require_goods, non_require_values, preference_table):
    #gene_list: 被选择出的基因清单
    #require_goods: 必要性的商品清单
    #non_require_goods: 不必要性的商品清单
    #user_preference: 使用者对商品种类的偏好

    max_preference = np.max(preference_table[:,1])
    total_preference = 0
    for i in range(0, max_preference+1, 1):
        total_preference = total_preference + i**2

    for i in range(len(gene_list)):
        reuslt = 1
        for j in range((len(require_goods) + non_require_values)):
            temp_preferenced = 1+(((gene_list[i]['data.frame'][:,9][j]**2)-1) / total_preference)
            reuslt *=temp_preferenced
            #temp.append(reuslt)

        gene_list[i]['fitPreference'] = reuslt
        sum_preferenced = (gene_list[i]['data.frame'][:,9]).sum()
        gene_list[i]['totalPreference'] = sum_preferenced
    return(gene_list)

#----执行----
goodData = diet_select(good_data = goodData, diet_habit_list = dietHabit) #荤素的方法
goodData = except_brand(good_data = goodData, except_brand_list = exceptBrandList) #剔除掉不想要的品牌

level = preferenceTable.iloc[:,0] #取得所有种类
requiredList = level[0:6] #必要性商品种类
nonRequiredList = (level[len(requiredList):len(level)]) #选择性商品种类
nonRequiredValues = userItemValues-len(requiredList) #选择性商品的数量

goodData = preference_match(good_data = goodData, preference_table = preferenceTable) #必要性商品种类
goodData.drop(['厂牌', '荤素'], axis = 1, inplace=True) #移除不必要的资料字段
goodData = goodData.values #转换成ndarry型态
preferenceTable = preferenceTable .values

##基因算法开始
#产生初始口(遵照popAmount数量)
geneList = []
for i in range(popAmount):
    geneDict = {'data.frame': initial_pop(good_data = goodData, require_goods = requiredList, non_require_goods = nonRequiredList, non_require_values = nonRequiredValues, limit_weight = maxWeight)}
    geneList.append(geneDict)

#编码染色体
geneList = create_chromosome(gene_list = geneList)

#计算每个基因总重量
geneList = total_weight(gene_list = geneList)

#计算偏好适应度(目前仅计算总偏好值)
fitnessPreference = fitness_preference(gene_list = geneList, require_goods = requiredList, non_require_values =  nonRequiredValues, preference_table = preferenceTable)


import os
from datetime import datetime

from config import configs

dirnames = [
    'img_cardshare_raw_dir',
    'img_cardshare_work_dir',
    'img_cardshare_ready_dir',
    'tmp_dir'
]

now = datetime.timestamp(datetime.now())

for dname in dirnames:
    dpath = configs[dname].split('/')
    dpath = os.path.join(os.getcwd(), *dpath)

    for dirpath, dirnames, filenames in os.walk(dpath):
        for fname in filenames:
            fpath = os.path.join(dpath, fname)
            ctime = os.path.getctime(fpath)
            if (now - ctime) >= (90 * 60):
                print(f"removing {fpath}")
                try:
                    os.remove(fpath)
                except Exception as e:
                    print(e)


# -*- coding: utf-8 -*-
# evidence
import numpy as np
from constants import *

## create evidence dict
service_time = np.arange(START_AGE+TIME_INTERVAL,END_AGE+TIME_INTERVAL,TIME_INTERVAL)

evidence_list = np.empty(service_time.shape)
evidence_list[:] = np.nan
evidence_dict = {'iniStat': np.copy(evidence_list),
                 'halfCell': np.copy(evidence_list),
                 'icorr': np.copy(evidence_list),
                 'crkStat': np.copy(evidence_list),
                 'conditionState': np.copy(evidence_list)}

# node iniStat
#evidence_dict['iniStat'][service_time==NO_INI_YR] = False
#evidence_dict['iniStat'][service_time==IS_INI_YR] = True

# half cell potential evidence
#evidence_dict['halfCell'][service_time==HC_DATA1['year']] = HC_DATA1['potential']
#evidence_dict['halfCell'][service_time==HC_DATA2['year']] = HC_DATA2['potential']

# corrosion rate evidence
#evidence_dict['icorr'][service_time==CORROSION_RATE_DATA1['year']] = CORROSION_RATE_DATA1['current']
#evidence_dict['icorr'][service_time==CORROSION_RATE_DATA2['year']] = CORROSION_RATE_DATA2['current']

# node crkStat
#evidence_dict['crkStat'][service_time==NO_CRK_YR] = False
#evidence_dict['crkStat'][service_time==IS_CRK_YR] = True

# condition state evidence
evidence_dict['conditionState'][service_time<=CS1_TO_CS2] = 1
evidence_dict['conditionState'][np.logical_and(service_time>CS1_TO_CS2, service_time<=CS2_TO_CS3)] = 2
evidence_dict['conditionState'][np.logical_and(service_time>CS2_TO_CS3, service_time<=CS3_TO_CS4)] = 3
evidence_dict['conditionState'][np.logical_and(service_time>CS3_TO_CS4, service_time<=EVIDENCE_END_YR)] = 4

#evidence_dict['conditionState'][service_time==CS1_TO_CS2] = 2
#evidence_dict['conditionState'][service_time==CS2_TO_CS3] = 3
#evidence_dict['conditionState'][service_time==CS3_TO_CS4] = 4


import streamlit as st
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
import torch.nn.functional as F
from sklearn import preprocessing
import tezhenggx as tz

class Act_fun(nn.Module):
    def __init__(self):
        super(Act_fun, self).__init__()

    def forward(self, x):
        x = torch.sigmoid(x)
        x = x * 100
        return x

st.title('demo')
add_selectbox = st.sidebar.selectbox(
    "How would you like to be contacted?",
    ("Email", "Home phone", "Mobile phone")
)

# 载入数据
@st.cache
def load():
    X_train = pd.read_csv(r'C:\Users\zhao\Desktop\streamlit_webapp\X_train_sc_st_pca.csv')
    X_test = pd.read_csv(r'C:\Users\zhao\Desktop\streamlit_webapp\X_test_sc_st_pca.csv')
    y_train = pd.read_csv(r'C:\Users\zhao\Desktop\streamlit_webapp\train_y.csv')
    y_test = pd.read_csv(r'C:\Users\zhao\Desktop\streamlit_webapp\test_y.csv')
    test_data = pd.read_csv(r'D:\jupyter_project\大创\test_data.csv')
    train_data = pd.read_csv(r'D:\jupyter_project\大创\train_data.csv')
    X_train = np.array(X_train)
    X_test=np.array(X_test)
    y_train=np.array(y_train)
    y_test=np.array(y_test)
    return X_train,X_test,y_train,y_test,train_data,test_data

data_load_state = st.text('Loading data...')
X_train,X_test,y_train,y_test,train_data,test_data = load()
data_load_state.text('Loading data...done!')
if st.checkbox('Show raw data'):
    st.subheader('Raw data')
    st.write(train_data)
lr_to_filter = st.slider('学习率X10^5', 1,50,10)
num_to_filter = st.slider('学习次数', 1,5000,1000)
#模型
input_size = X_train.shape[1]
hidden_size1 = 100
output_size = 1
batch_size = 10
# 框架

my_nn = torch.nn.Sequential(
    torch.nn.Linear(input_size, hidden_size1),
    torch.nn.ReLU(),
    torch.nn.Linear(hidden_size1, output_size),
    Act_fun(),
)
cost = torch.nn.MSELoss(reduction='mean')
optimizer = torch.optim.Adam(my_nn.parameters(), lr=lr_to_filter*10**-4)
for i in range(num_to_filter):
    batch_loss = []
    # MINI-Batch方法来进行训练
    for start in range(0, len(X_train), batch_size):
        end = start + batch_size if start + batch_size < len(X_train) else len(X_train)
        xx = torch.tensor(X_train[start:end], dtype=torch.float, requires_grad=True)
        yy = torch.tensor(y_train[start:end], dtype=torch.float, requires_grad=True)
        prediction = my_nn(xx)
        # 降为一维，否则loss会告警
        prediction = prediction.squeeze(-1)
        loss = cost(prediction, yy)
        optimizer.zero_grad()
        loss.backward(retain_graph=True)
        optimizer.step()
        batch_loss.append(loss.data.numpy())

    # 打印损失
    if i % 100 == 0:
        #losses.append(np.mean(batch_loss))
        st.write('第',i,'次训练', '均方误差为:',np.mean(batch_loss))


from fastapi import FastAPI, Response
from pymongo import MongoClient
from pprint import pprint
from fastapi.middleware.cors import CORSMiddleware
import os

# Import the phonebook model from the models directory
from models.phonebook_entry import PhoneBookEnrty

# Create a mongo client and connect it to the DB
client = MongoClient("172.17.0.2:27017")

db = client.admin
# Issue the serverStatus command and print the results
serverStatusResult=db.command("serverStatus")
pprint(serverStatusResult)

# Create a FastAPI app
app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins = ["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

@app.get("/")
async def root():
    return {'message':'Hello World!'}

# Add a phonebook entry to the database
@app.post("/create_phonebook_entry")
async def createPhonebookEntry(response:Response, entry : PhoneBookEnrty):
    phonebookDB = client.phonebook
    phonebookDB.entries.insert_one(entry.dict())
    response.headers["Access-Control-Allow-Origin"] = "*"
    return entry.dict()

# Get all phonebook entries contained within the database
@app.get("/get_phonebook_entries")
async def getPhonebookEntries(response:Response):
    returnList = []
    for document in client.phonebook.entries.find():
        returnList.append({
            "name":document["name"],
            "address":document["address"],
            "phoneNumber":document["phoneNumber"],
        })
    response.headers["Access-Control-Allow-Origin"] = "*"
    return {"data":returnList}


from sklearn import datasets
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import RidgeClassifier
from sklearn.model_selection import train_test_split
import re
from bs4 import BeautifulSoup
news20 = datasets.fetch_20newsgroups()

print(len(news20['data']))
print(news20['target'][0])
print(news20['target_names'])
print(news20['data'][0])

def text_cleaning(text):
    '''
    Cleans text into a basic form for NLP. Operations include the following:-
    1. Remove special charecters like &, #, etc
    2. Removes extra spaces
    3. Removes embedded URL links
    4. Removes HTML tags
    5. Removes emojis

    text - Text piece to be cleaned.
    '''
    template = re.compile(r'https?://\S+|www\.\S+') #Removes website links
    text = template.sub(r'', text)

    soup = BeautifulSoup(text, 'lxml') #Removes HTML tags
    only_text = soup.get_text()
    text = only_text

    emoji_pattern = re.compile("["
                               u"\U0001F600-\U0001F64F"  # emoticons
                               u"\U0001F300-\U0001F5FF"  # symbols & pictographs
                               u"\U0001F680-\U0001F6FF"  # transport & map symbols
                               u"\U0001F1E0-\U0001F1FF"  # flags (iOS)
                               u"\U00002702-\U000027B0"
                               u"\U000024C2-\U0001F251"
                               "]+", flags=re.UNICODE)
    text = emoji_pattern.sub(r'', text)

    text = re.sub(r"[^a-zA-Z\d]", " ", text) #Remove special Charecters
    text = re.sub(' +', ' ', text) #Remove Extra Spaces
    text = text.strip()
    text=text.lower()# remove spaces at the beginning and at the end of string
    #text=TextBlob(text).correct()

    return text

texts = news20['data']
texts = list(map(text_cleaning, texts))
y = news20['target']

vectorizer = TfidfVectorizer(ngram_range=(3 ,5), max_df=0.5, min_df=3, analyzer = 'char_wb')
X = vectorizer.fit_transform(texts)
print('vectorize done')
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

RC = RidgeClassifier()
RC.fit(X_train, y_train)
print('fit done')
y_pred = RC.predict(X_test)
print('acc:',sum(y_pred==y_test)/len(y_test))


from turtle import *
import math
# Name your Turtle.
Mily = Turtle()
Mily.penup()
setup(400,200)
x_pos =50
y_pos =50

Mily.pendown()
Mily.speed(10)
answer=input("How many sides would you like?")
intanswer= int(answer)
color=input("what color would you like?")
Mily.pencolor(color)
Mily.fillcolor(color)
Mily.begin_fill()
for answer in range(intanswer):
    Mily.forward(50)
    Mily.right(360/intanswer)
Mily.end_fill()
def Shape(steps, angle):
    Mily.forward(steps)
    Mily.right(angle)

Shape(50,360/intanswer)
exitonclick()


FILE = "test.txt"
STEPS = 10

def step(polymer):
	temp = polymer

	for i, current in enumerate(polymer):
		try: next = polymer[i+1]
		except IndexError: break

		key = current + next
		value = table[key]
		temp = temp[:2*i+1] + value + temp[2*i+1:]

	return temp

def calculate_value(polymer):
	count = {}
	for letter in polymer:
		if letter in count:
			count[letter] += 1
		else:
			count[letter] = 1

	values = count.values()
	return max(values) - min(values)

table = {}
with open(FILE, 'r') as input:
	polymer = input.readline().strip()
	input.readline()

	for line in input:
		left, right = line.split('->')
		table[left.strip()] = right.strip()

for _ in range(STEPS):
	polymer = step(polymer)

value = calculate_value(polymer)
print(f"Value: {value}")


#!/usr/bin/python3
# -*- coding: utf-8 -*-

import os
import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtNetwork import *
# Download OpenGL dll from here
# http://download.qt.io/development_releases/prebuilt/llvmpipe/windows/
#
from PyQt5.QtWebEngineWidgets import *
from PyQt5.QtXmlPatterns import *
if __package__ is None or __package__ == '':
    from executor import PyExecutor
    import meshandler, dbpool
    import util as cu
else:
    from . import (meshandler, dbpool)
    from .executor import PyExecutor
    from . import util as cu
from datetime import timedelta, datetime

class XmlRestExecutor(PyExecutor):
    def __init__(self, iniFile, parent=None):
        super().__init__(iniFile,parent)

        self.lastReply = None
        self.man = QNetworkAccessManager(self)
        #self.man.finished.connect(self.netFinished)
        self.man.sslErrors.connect(self.netSslErrors)
        self.man.authenticationRequired.connect(self.authenticationRequired)
        self.man.proxyAuthenticationRequired.connect(self.authenticationRequired)
        self.page = QWebEngineView(self)
        self.resultLay.addWidget(self.page)
        #self.loadIni(iniFile)
        self.startTime = datetime.now()
        self.timer = QTimer(self)
        self.timer.setInterval(1000)
        self.timer.timeout.connect(self.showTime)

    def loadIni(self, iniFile):
        super().loadIni(iniFile)
        ini = QSettings(iniFile, QSettings.IniFormat)
        ini.setIniCodec("utf-8")
        print(iniFile,ini.childGroups())
        ini.beginGroup("WebPage")
        self.url = ini.value('Url')
        self.bodyFile = ini.value('Body')
        self.method = ini.value('Method')
        if self.method is None:
            self.method = 'get' if self.bodyFile == None else 'post'
        if self.bodyFile:
            self.bodyFile = cu.nearFile(iniFile, self.bodyFile)

##        print(self.method)
        self.transformTemplate = ini.value('Transform')
        if self.transformTemplate:
            self.transformTemplate = cu.nearFile(iniFile, self.transformTemplate)
        ini.endGroup()

##        self.readInputs(ini)
##
##        self.runBtn = QPushButton("Run")
##        self.runBtn.setDefault(True)
##        self.runBtn.clicked.connect(self.run)
##        self.btnLay = QHBoxLayout()
##        self.btnLay.addStretch()
##        self.btnLay.addWidget(self.runBtn)
##        self.lay.addRow(self.btnLay)

    def readInputs(self, ini):
        self.inputs = {}
        self.params = []
        ini.beginGroup("Input")
        for key in sorted(ini.childKeys()):
            v = ini.value(key)
            print(v)
            if type(v) != type([]):
                v = [v]
            if len(v)>1:
                paramTitle = v[0]
                paramValue = v[1]
            else:
                paramTitle = key
                paramValue = v[0]
            self.params.append([key, paramTitle, paramValue])
            if paramTitle != '':
                le = QLineEdit()
                self.inputs[key] = le
                le.setText(paramValue)
                le.paramTitle = paramTitle
                self.lay.addRow(paramTitle, le)
        for kp in self.params:
            key = kp[0]
            paramTitle = kp[1]
            paramValue = kp[2]
            if paramTitle == '':
                le = self.inputs[paramValue]
                self.inputs[key] = le
        ini.endGroup()

    def startedRun(self):
        self.runBtn.setEnabled(False)
        self.startTime = datetime.now()
        self.timer.start()
        self.showTime()

    def finishedRun(self):
        self.timer.stop()
        self.showTime()
        self.runBtn.setEnabled(True)
        self.lastReply = None

    def run(self):
        try:
            self.startedRun()
            values = { kp[0]:self.inputs[kp[0]].text() for kp in self.params}
##            print(values)
            url = self.url.format(**values)
##            print('url:',url)
            req = QNetworkRequest(QUrl(url))
##            print('method:',self.method)
            if self.method == 'get':
##                print("get", url)
                req.setHeader(QNetworkRequest.ContentTypeHeader,"text/xml;charset=UTF-8")
                req.setHeader(QNetworkRequest.ContentLengthHeader,"0")
##                req.setRawHeader("Accept","application/xhtml+xml")
##                print(req.rawHeaderList())
                reply = self.man.get(req)
            else:
                f = QFile(self.bodyFile)
                if f.open(QIODevice.ReadOnly):
                    body = str(f.readAll(),'utf-8-sig')
                else:
                    body = ''
                body = body.format(**values)
##                print('body:')
##                print(body)
##                print('----')
                body = bytes(body,'utf-8')
                req.setHeader(QNetworkRequest.ContentTypeHeader,"text/xml;charset=UTF-8")
##                print("post", url)
                reply = self.man.post(req,body)
            reply.finished.connect(self.replyFinished)
            reply.error.connect(self.replyError)
            #reply.sslErrors.connect(self.replySslErrors)
            reply.downloadProgress.connect(self.downloadProgress)
            err = reply.error()
            self.lastReply = reply
##            print("Error", err, reply.errorString())
##            print("Reply is running:", reply.isRunning())
##            print("Reply is finished:", reply.isFinished())
##            print("Reply man:", reply.manager())
##            print("Self man:", self.man)
            if err != QNetworkReply.NoError:
                self.finishedRun()
##            r = requests.get(self.url)
##            print(r.content)
        except:
            print(str(sys.exc_info()[1]))
            self.finishedRun()

    def replyFinished(self):
        self.netFinished(self.sender())

    def netFinished(self, reply):
        #reply = self.sender()
        #buf = reply.readAll()
##        print("finished")
        self.finishedRun()
        if reply is None:
##            print("reply is None")
            return
        if reply.isOpen():
            hasTempl = True
            if self.transformTemplate != None:
                filename, ext = os.path.splitext(self.transformTemplate)
    ##            print(filename, ext)
                if ext.lower() == '.xq' or ext.lower() == '.xquery':
                    lang = QXmlQuery.XQuery10
                elif  ext.lower() == '.xsl' or ext.lower() == '.xslt':
                    lang = QXmlQuery.XSLT20
                else:
                    return
                templ = QFile(self.transformTemplate)
                if not templ.open(QIODevice.ReadOnly):
                    print("Can't open template",self.transformTemplate)
                    hasTempl = False
            if hasTempl:
                query = QXmlQuery(lang)
                query.setMessageHandler(XmlQueryMessageHandler())
                query.setFocus(reply)
                query.setQuery(templ)
                if query.isValid():
                    out = query.evaluateToString()
    ##                print("out",out)
                    if out != None:
                        self.page.setHtml(out)
                        #self.finishedRun()
                        return
            b = reply.readAll()
        if reply.error() != QNetworkReply.NoError:
            print("Error:", reply.error())
        else:
            self.page.setContent(b,"text/plain;charset=windows-1251",reply.url())
        #self.finishedRun()

    def showTime(self):
        time = datetime.now()
        delta = time - self.startTime
        s = "In progress" if self.timer.isActive() else "Finished"
        self.bar.showMessage("%s. %s s" % (s, delta.seconds))
        if delta.seconds >= 30 and not (self.lastReply is None):
            self.lastReply.abort()

    def netSslErrors(self, reply, errors):
        print(errors)
        reply.ignoreSslErrors()

    def replySslErrors(self, errors):
        print(errors)
        self.netSslErrors(self.sender(), errors)

    def replyError(self, errorCode):
        reply = self.sender()
        print("Error", errorCode, reply.errorString())

    def authenticationRequired(self, reply, auth):
        print("authenticationRequired")

    def downloadProgress(self, received, total):
        print("progress %s/%s bytes" % (received, total))

class XmlQueryMessageHandler(QAbstractMessageHandler):
    def handleMessage(self, msgType, desc, idUrl, source):
        print("Msg(%s) on (%s,%s): %s" % (msgType, source.line(), source.column(), desc))

if __name__ == '__main__':
    os.environ.putenv('QT_ANGLE_PLATFORM','warp') # d3d11, d3d9 and warp
## warp:
##    ---------------------------
##QtWebEngineProcess.exe - Системная ошибка
##---------------------------
##Запуск программы невозможен, так как на компьютере отсутствует VCRUNTIME140.dll. Попробуйте переустановить программу.
##---------------------------
##ОК
##---------------------------

    os.environ.putenv('QT_OPENGL','software') # desktop, software, angle
#    os.environ.putenv('QT_OPENGL','angle') # desktop, software, angle
## software - нужно положить vcruntime140 в Qt/bin

    #os.environ.putenv('QT_D3DCOMPILER_DLL','C:/Windows/System32/D3DCompiler_47.dll')
    app = QApplication(sys.argv)
    ex = XmlRestExecutor("../data/ЦБ/valcurs.ini")
    ex.show()
    sys.exit(app.exec_())


def main():
    print(fib_sum())
def fib_sum():
    num = [0, 1]

    while len(num) < 50:
        nxt = num[-2] + num[-1]
        num.append(nxt)
    return sum(num)

if __name__ == "__main__":
    main()


import json
import os.path

import speech_recognition
from vosk import Model

from src.settings import SETTINGS

class Recognizer:

    def __init__(self):
        self.engine = speech_recognition.Recognizer()
        self.engine.pause_threshold = 0.5
        self.engine.non_speaking_duration = 0.3
        self.engine.vosk_model = Model(
            model_path=SETTINGS.stt_model_path,
            model_name=os.path.basename(SETTINGS.stt_model_path),
            lang=SETTINGS.language
        )

    def recognize(self) -> str:
        with speech_recognition.Microphone(device_index=0) as source:
            audio_data = self.engine.listen(source)
        recognized_result = self.engine.recognize_vosk(audio_data, language=SETTINGS.language)
        try:
            parsed_result = json.loads(recognized_result)
            return parsed_result["text"]
        except (KeyError, ValueError):
            return recognized_result


'''
5
R R R U D D
'''
N = int(input())
arr = list(input().split())
X = Y = 1

def check(T):
    if T < 1 or T==N :return True
    return False

for i in range(len(arr)):
    if arr[i] == "R":
        if check(Y+1): continue
        Y += 1
    elif arr[i] == "D":
        if check(X+1): continue
        X += 1
    elif arr[i] == "L":
        if check(Y-1): continue
        Y -= 1
    else: #U
        if check(X-1): continue
        X -= 1
print(X,Y)

예제4-1상하좌우.py


from __future__ import annotations
from . import mp_out_utils
from ..cgt_naming import COLLECTIONS, cgt_defaults
from ..cgt_bpy import cgt_bpy_utils, cgt_collection, cgt_object_prop

class MPFaceOutputNode(mp_out_utils.BpyOutputNode):
    face = []
    col_name = COLLECTIONS.face
    parent_col = COLLECTIONS.drivers

    def __init__(self):
        data = cgt_defaults

        references = {}
        for i in range(468):
            references[f'{i}'] = f"cgt_face_vertex_{i}"
        for k, name in data.face.items():
            references[f'{468+int(k)}'] = name

        self.face = cgt_bpy_utils.add_empties(references, 0.005)
        for ob in self.face[468:]:
            cgt_object_prop.set_custom_property(ob, "cgt_id", data.identifier)

        cgt_collection.add_list_to_collection(self.col_name, self.face[468:], self.parent_col)
        cgt_collection.add_list_to_collection(self.col_name+"_DATA", self.face[:468], self.col_name)

    def update(self, data, frame):
        loc, rot, sca = data
        for data, method in zip([loc, rot, sca], [self.translate, self.euler_rotate, self.scale]):
            try:
                method(self.face, data, frame)
            except IndexError:
                pass
        return data, frame


import pandas as pd
import matplotlib.pyplot as plt

# Load data from your three tables: Original, Developers, and Management
original_data = pd.read_excel('Data/output/readability_metrics3Original.xlsx')  # Replace with the actual file path
developers_data = pd.read_excel('Data/output/readability_metrics3Devs.xlsx')  # Replace with the actual file path
management_data = pd.read_excel('Data/output/readability_metrics3Manager.xlsx')  # Replace with the actual file path

# Ensure that the three tables have the same structure (columns)
# You can rename columns if needed to match
original_data = original_data.rename(columns={'Project Name': 'Project', 'Type of test': 'Test', 'Average': 'Original'})
developers_data = developers_data.rename(columns={'Project Name': 'Project', 'Type of test': 'Test', 'Average': 'Developers'})
management_data = management_data.rename(columns={'Project Name': 'Project', 'Type of test': 'Test', 'Average': 'Management'})

# Merge the data from the three tables using 'Project' and 'Test' columns as keys
merged_data = pd.merge(original_data, developers_data, on=['Project', 'Test'])
merged_data = pd.merge(merged_data, management_data, on=['Project', 'Test'])

# Create a comparison plot
plt.figure(figsize=(10, 6))

# Customize plot style for a research paper
plt.style.use('seaborn-whitegrid')

# Sort the data for better visualization
merged_data = merged_data.sort_values(by=['Original'])

# Plot the data
plt.plot(merged_data['Project'], merged_data['Original'], marker='o', label='Original', linestyle='-', color='b', linewidth=2)
plt.plot(merged_data['Project'], merged_data['Developers'], marker='s', label='Developers', linestyle='--', color='g', linewidth=2)
plt.plot(merged_data['Project'], merged_data['Management'], marker='^', label='Management', linestyle='-.', color='r', linewidth=2)

# Add labels and title
plt.xlabel('Project')
plt.ylabel('Average Value')
plt.title('Comparison of Developers and Management to Original Data')
plt.xticks(rotation=45)
plt.legend()

# Save the plot as a high-resolution image suitable for a research paper
plt.savefig('comparison_plot.png', dpi=300, bbox_inches='tight')

# Show the plot
plt.show()


from services.server import Server
from ui.ui_main import UI_main

if __name__ == "__main__":
    root_ui = UI_main()
    server = Server()

    root_ui.add_socket_queue(server.socket_queue)
    server.add_ui_queue(root_ui.ui_queue)

    server.start()
    root_ui.mainloop()


import sys
from collections import deque

M,N,H = map(int, sys.stdin.readline().split())

li = []
que = deque()
zcnt = 0

for i in range(H):
    tmp = []
    for j in range(N):
        tmp.append(list(map(int, sys.stdin.readline().split())))
    li.append(tmp)

for k in range(H):
    for i in range(N):
        for j in range(M):
            if li[k][i][j] == 1:
                que.append([k,i,j])
            if li[k][i][j] == 0:
                zcnt += 1

def BFS():
    cnt = 0
    global zcnt
    if zcnt == 0:
        return 0
    while que:
        cnt += 1
        for i in range(len(que)):
            xyz = que.popleft()
            k = xyz[0]
            i = xyz[1]
            j = xyz[2]

            if k+1 < H and li[k+1][i][j] == 0:
                li[k+1][i][j] = 1
                zcnt -= 1
                que.append([k+1,i,j])

            if k-1 >= 0 and li[k-1][i][j] == 0:
                li[k-1][i][j] = 1
                zcnt -= 1
                que.append([k-1,i,j])

            if i+1 < N and li[k][i+1][j] == 0:
                li[k][i+1][j] = 1
                zcnt -= 1
                que.append([k,i+1,j])

            if i-1 >= 0 and li[k][i-1][j] == 0:
                li[k][i-1][j] = 1
                zcnt -= 1
                que.append([k,i-1,j])

            if j+1 < M and li[k][i][j+1] == 0:
                li[k][i][j+1] = 1
                zcnt -= 1
                que.append([k,i,j+1])

            if j-1 >= 0 and li[k][i][j-1] == 0:
                li[k][i][j-1] = 1
                zcnt -= 1
                que.append([k,i,j-1])

            if zcnt == 0:
                return cnt
    return -1
print(BFS())


'''
# Module name:
    wxbot.py

# Prerequisite:
    Based on Python 3.4
    Need python module pillow and wxpy.

# Purpose:
    An independent wechat service based on Wxpy for hass component wechat3.py.

# Author:
    Retroposter retroposter@outlook.com

# Created:
    Nov.22th 2017

# Last Modified:
    Nov.22th 2017
'''

REQUIREMENTS = ['wxpy==0.3.9.8','pillow']

import configparser
import json
import logging
import os
import socket

from wxpy import *

# No attribute config???
#logging.config.fileConfig('logging.conf')
_LOGGER = logging.getLogger('wxbot')

ATTR_TARGET = 'target'
ATTR_MESSAGE = 'message'
ATTR_DATA = 'data'

ATTR_IMAGE = 'image'
ATTR_VIDEO = 'video'
ATTR_FILE = 'file'

GROUP_POSTFIX = '#group#'

SERVICE_PORT = 18001
CUSTOMER_PORT = 18002

is_running = False

def get_config_value(conf, section, option, default=''):
    try:
        value = conf.get(section, option)
    except:
        value = default
    return value

def run_service():
    is_running = True
    conf = configparser.ConfigParser()
    conf.read('config.conf')
    tuling_api_key = get_config_value(conf, 'bot', 'tuling_api_key')
    cmder = get_config_value(conf, 'cmd', 'cmder')
    cmd_prefix = get_config_value(conf, 'cmd', 'cmd_prefix')
    tts_prefix = get_config_value(conf, 'cmd', 'tts_prefix')

    cache_path = os.path.join(os.getcwd(), 'wxpy.pkl')
    bot = Bot(cache_path=cache_path, console_qr=True)

    if cmder != '':
        try:
            cmder = ensure_one(bot.friends().search(cmder))
        except:
            _LOGGER.error('Failed to set commander: no friend named %s.', cmder)
            cmder = None
    def process_special_msg(msg, msg_type, sender, sex, chat):
        msg_data = {'msg_type': msg_type, 'message': msg, 'sender': sender, 'sex': sex}
        msg_to_send = json.dumps(msg_data)
        try:
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            sock.connect(('localhost', CUSTOMER_PORT))
            sock.send(msg_to_send.encode())
            sock.close()
            replay = '{0} msg: {1} was sent successfully.'.format(msg_type, msg)
        except Exception as ex:
            replay = '{0} msg: {1} failed to be sent.'.format(msg_type, msg)
            _LOGGER.exception(ex)
        chat.send_msg(replay)

    def is_specified_fmt(fmt_prefix, msg_text):
        if fmt_prefix is None or msg_text is None:
            return False
        text = msg_text.lower()
        fmt_pf = fmt_prefix.lower()
        return text.startswith(fmt_pf)

    def is_cmd_fmt(msg_text):
        return is_specified_fmt(cmd_prefix, msg_text)

    def is_tts_fmt(msg_text):
        return is_specified_fmt(tts_prefix, msg_text)

    @bot.register(Friend)
    def on_msg_received(msg):
        if msg.text is not None:
            # If it is sent by specified user, check if it metches cmd fmt. If it does,
            # invoke cmd handler to process
            if cmder is not None and msg.sender == cmder and is_cmd_fmt(msg.text):
                # If it matches cmd fmt
                process_special_msg(msg.text, 'cmd', msg.sender.remark_name, msg.sender.sex, msg.chat)
                return
            # If it matches tts fmt
            if is_tts_fmt(msg.text):
                process_special_msg(msg.text, 'tts', msg.sender.remark_name, msg.sender.sex, msg.chat)
                return
        if tuling_api_key != '':
            tuling = Tuling(api_key=tuling_api_key)
            tuling.do_reply(msg)

    wechat_service = WeChatService(bot)

    def process_buffer(msg_bytes):
        try:
            msg_str = str(msg_bytes, encoding='utf8')
            json_obj = json.loads(msg_str)
            target = json_obj.get(ATTR_TARGET)
            message = json_obj.get(ATTR_MESSAGE)
            data = json_obj.get(ATTR_DATA)
            wechat_service.send_message(message, target=target, data=data)
        except Exception as ex:
            _LOGGER.error('Something wrong when processing msg.')
            _LOGGER.exception(ex)

    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.bind(('localhost', SERVICE_PORT))
    sock.listen(5)
    while True:
        conn, addr = sock.accept()
        try:
            conn.settimeout(50)
            buf = conn.recv(1024)
            process_buffer(buf)
        except socket.timeout:
            _LOGGER.error('Conn timeout.')
        conn.close()

class WeChatService():
    def __init__(self, bot):
        """Initialize the service."""
        self.bot = bot

    def send_message(self, message="", **kwargs):
        targets = kwargs.get(ATTR_TARGET)
        data = kwargs.get(ATTR_DATA)
        # Send to file helper if no target specified.
        if targets is None:
            if data is not None:
                if ATTR_IMAGE in data:
                    image = data.get(ATTR_IMAGE, None)
                    self.bot.file_helper.send_image(image)
                elif ATTR_VIDEO in data:
                    video = data.get(ATTR_VIDEO, None)
                    self.bot.file_helper.send_video(video)
                elif ATTR_FILE in data:
                    file = data.get(ATTR_FILE, None)
                    self.bot.file_helper.send_file(file)
                else:
                    _LOGGER.error('No image, video or file in data.')
            self.bot.file_helper.send_msg(message)
        else:
            for tar in targets:
                if tar == '':
                    _LOGGER.error('Invalid target.')
                    continue
                # Determine the target is a friend or group.
                target = tar.split(GROUP_POSTFIX)
                if len(target) == 1:
                    chats = self.bot.friends().search(target[0])
                else:
                    chats = self.bot.groups().search(target[0])
                chats_count = len(chats)
                if chats is not None and chats_count > 0:
                    # Only send to the first chat matched specified target.
                    chat = chats[0]
                    if data is not None:
                        if ATTR_IMAGE in data:
                            image = data.get(ATTR_IMAGE, None)
                            chat.send_image(image)
                        elif ATTR_VIDEO in data:
                            video = data.get(ATTR_VIDEO, None)
                            chat.send_video(video)
                        elif ATTR_FILE in data:
                            file = data.get(ATTR_FILE, None)
                            chat.send_file(file)
                        else:
                            _LOGGER.error('No image, video or file in data.')
                    chat.send_msg(message)
                else:
                    _LOGGER.error('Incorrect name of friend or group: %s', target[0])

if __name__ == '__main__':
    if not is_running:
        run_service()
    else:
        _LOGGER.warning('It is running.')


# -*- coding:utf-8
import os
import stat
import magic
import codecs
import re
import logging

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

def is_binary_file_1(file_path):
    '''
    根据text文件数据类型判断是否是二进制文件
    :param file_path: 文件名（含路径）
    :return: True或False，返回是否是二进制文件
    '''
    TEXT_BOMS = (
        codecs.BOM_UTF16_BE,
        codecs.BOM_UTF16_LE,
        codecs.BOM_UTF32_BE,
        codecs.BOM_UTF32_LE,
        codecs.BOM_UTF8,
    )
    with open(file_path, 'rb') as file:
        CHUNKSIZE = 8192
        initial_bytes = file.read(CHUNKSIZE)
        file.close()
    #: BOMs to indicate that a file is a text file even if it contains zero bytes.
    return not any(initial_bytes.startswith(bom) for bom in TEXT_BOMS) and b'\0' in initial_bytes

def is_binary_file_2(ff):
    '''
    根据magic文件的魔术判断是否是二进制文件
    :param ff: 文件名（含路径）
    :return: True或False，返回是否是二进制文件
    '''
    mime_kw = 'x-executable|x-sharedlib|octet-stream|x-object'  ###可执行文件、链接库、动态流、对象
    try:
        magic_mime = magic.from_file(ff, mime=True)
        magic_hit = re.search(mime_kw, magic_mime, re.I)
        if magic_hit:
            return True
        else:
            return False
    except Exception as e:
        return False

# 判断文件是否是elf文件
def is_ELFfile(filepath):
    if not os.path.exists(filepath):
        # logger.info('file path {} doesnot exits'.format(filepath))
        return False
    # 文件可能被损坏，捕捉异常
    try:
        FileStates = os.stat(filepath)
        FileMode = FileStates[stat.ST_MODE]
        if not stat.S_ISREG(FileMode) or stat.S_ISLNK(FileMode):  # 如果文件既不是普通文件也不是链接文件
            return False
        with open(filepath, 'rb') as f:
            header = (bytearray(f.read(4))[1:4]).decode(encoding="utf-8")
            # logger.info("header is {}".format(header))
            if header in ["ELF"]:
                # print header
                return True
    except UnicodeDecodeError as e:
        # logger.info("is_ELFfile UnicodeDecodeError {}".format(filepath))
        # logger.info(str(e))
        pass

    return False

def is_binary_file(path):
    return any((is_ELFfile(path), is_binary_file_1(path), is_binary_file_2(path)))

def count_line(path):
    sum = 0
    for root, dirs, files in os.walk(path):
        for name in files:
            tmp_file = os.path.join(root, name)
            if tmp_file.find('venv') != -1:
                continue
            if tmp_file.find('idea') != -1:
                continue
            if tmp_file.find('__pycache__') != -1:
                continue
            if tmp_file.find('.git') != -1:
                continue
            tmp = 0
            if is_binary_file(tmp_file):
                continue
            for index, line in enumerate(open(tmp_file, 'r', encoding='utf8')):
                tmp += 1
            # print(tmp_file, ":", tmp)
            sum += tmp
    return sum

if __name__ == '__main__':
    print(count_line("."))


#
# @lc app=leetcode id=752 lang=python3
#
# [752] Open the Lock
#

# @lc code=start
class Solution:
    def openLock(self, deadends: List[str], target: str) -> int:
        deadends = set(deadends)
        queue = collections.deque([("0000", 0)])
        visited = set([("0000")])

        def neighbors(node):
          for i in range(4):
            x = int(node[i])
            for y in [1, -1]:
              temp = (x+y) % 10
              yield node[:i] + str(temp) + node[i+1:]

        while queue:
          node, depth = queue.popleft()
          if node == target:
            return depth
          if node in deadends:
            continue
          for nei in neighbors(node):
            if nei not in visited:
              visited.add(nei)
              queue.append((nei, depth+1))
        return -1

# @lc code=end


from __future__ import annotations

import pickle
from pathlib import Path

import numpy as np
import pandas as pd
from hydra.utils import get_original_cwd
from omegaconf import DictConfig
from sklearn.preprocessing import LabelEncoder
from tqdm import tqdm

def add_features(df: pd.DataFrame) -> pd.DataFrame:
    """
    Add new features to the dataset.
    Args:
        df (pd.DataFrame): Dataset.
    Returns:
        pd.DataFrame: Dataset with new features.
    """
    df["fnlwgt"] = np.log1p(df["fnlwgt"])
    df["workclass_occupation"] = df["workclass"] + "#" + df["occupation"]
    df["workclass_education"] = df["workclass"] + "#" + df["education"]
    df["occupation_education"] = df["occupation"] + "#" + df["education"]
    df["marital_status_relationship"] = df["marital_status"] + "#" + df["relationship"]
    df["race_sex"] = df["race"] + "#" + df["sex"]
    df["capital_margin"] = df["capital_gain"] - df["capital_loss"]
    df["capital_total"] = df["capital_gain"] + df["capital_loss"]
    df["capital_margin_flag"] = np.nan
    df.loc[df["capital_margin"] == 0, "capital_margin_flag"] = "zero"
    df.loc[df["capital_margin"] > 0, "capital_margin_flag"] = "positive"
    df.loc[df["capital_margin"] < 0, "capital_margin_flag"] = "negative"

    return df

def encode_frequency(df: pd.DataFrame, columns: list[str]) -> pd.DataFrame:
    """
    Frequency encoding for categorical features.
    Args:
        df (pd.DataFrame): Dataset.
        columns (list): List of categorical features.
    Returns:
        pd.DataFrame: Dataset with frequency encoded features.
    """
    for column in columns:
        vc = df[column].value_counts(dropna=False, normalize=True).to_dict()
        df[f"{column}"] = df[column].map(vc)
        df[f"{column}"] = df[f"{column}"].astype("float32")

    return df

def categorize_train_features(config: DictConfig, train: pd.DataFrame) -> pd.DataFrame:
    """
    Categorical encoding
    Args:
        config: config
        train: dataframe
    Returns:
        dataframe
    """
    path = Path(get_original_cwd()) / config.data.encoder
    label_encoder = LabelEncoder()

    for cat_feature in tqdm(config.data.cat_features):
        train[cat_feature] = label_encoder.fit_transform(train[cat_feature])
        with open(path / f"{cat_feature}.pkl", "wb") as f:
            pickle.dump(label_encoder, f)

    return train

def categorize_test_features(config: DictConfig, test: pd.DataFrame) -> pd.DataFrame:
    """
    Categorical encoding
    Args:
        config: config
        test: dataframe
    Returns:
        dataframe
    """
    path = Path(get_original_cwd()) / config.data.encoder

    for cat_feature in tqdm(config.data.cat_features):
        le_encoder = pickle.load(open(path / f"{cat_feature}.pkl", "rb"))
        test[cat_feature] = le_encoder.transform(test[cat_feature])

    return test