category
This commit is contained in:
		| @@ -0,0 +1,118 @@ | ||||
| """ | ||||
| This code is supported by the website: https://www.guanjihuan.com | ||||
| The newest version of this code is on the web page: https://www.guanjihuan.com/archives/417 | ||||
| """ | ||||
|  | ||||
|  | ||||
| # 第一部分:Python基本操作(循环,判断,函数,文件写入) | ||||
| print('\n第一部分:Python基本操作(循环,判断,函数,文件写入)\n')  # \n代表换行 | ||||
|  | ||||
| for i in range(5):  # 循环(这里只举例for循环,还有其他循环) | ||||
|     print('我是循环产生的数:', i)  # Python中没有end,因此每个语句的缩进很重要 | ||||
|     if i == 2:   # 判断 | ||||
|         print('判断:我是第三个数 2') | ||||
|     else: | ||||
|         pass  # pass代表不执行任何语句,用于占位,可以之后再补充,不然空着会报错 | ||||
| print()  # 输出空一行 | ||||
|  | ||||
| def fun0(arg):  # 定义函数 | ||||
|     print('我是函数中的内容,参数值为:', arg)  | ||||
|     return arg*2  # 返回值 | ||||
|  | ||||
| print('函数返回值:', fun0(5))  # 调用函数 | ||||
| print() | ||||
|  | ||||
| def main():  # “主函数”,其实也是一个普通的函数,也可以起其他名字 | ||||
|     print('我是主函数中的内容。') | ||||
|     print() | ||||
|  | ||||
| if __name__ == '__main__':  # 如果直接运行本文件,那么执行以下内容。如果是import本文件,那么不执行。 | ||||
|     main() | ||||
|  | ||||
| # 关于类class,这里不举例了。科学计算中主要还是面向过程,面向对象用的比较少。 | ||||
|  | ||||
| # 文件写入 | ||||
| # 第一种方式 | ||||
| with open('test1.txt', 'w') as f1:   # 其中'w'为重新写入,改为'a'是补充内容 | ||||
|     f1.write(str(100)+'\n这是第一种方式写入文件')  # str()为转换成字符串 | ||||
| # 第二种方式 | ||||
| f2 = open('test2.txt', 'w')  # 打开文件 | ||||
| f2.write(str(200)+'\n这是第二种方式写入文件')  # 写入文件 | ||||
| f2.close()  # 关闭文件 | ||||
| print('已写入文件!') | ||||
| print() | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
| #  第二部分:Numpy库中常用的语句 | ||||
| print('\n\n\n第二部分:Numpy库中常用的语句\n')  | ||||
| import numpy as np | ||||
|  | ||||
| print('零矩阵:\n', np.zeros((2, 3)))  # 注意np.zeros()里需要填元组,因此显示的是两个括号 | ||||
| print('单位矩阵:\n', np.identity(3))    # 3行3列的单位矩阵,或者可以用np.eye() | ||||
| print('把一维数组按对角矩阵排列:\n', np.diag([1, 3, 5])) | ||||
| print() | ||||
|  | ||||
| print('指定步长的等差数列:\n', np.arange(1, 5, .5))  # 区间是左闭右开[1, 5),步长为0.5 | ||||
| print('指定个数的等差数列:\n', np.linspace(-2, 2, 5))  # 区间是左闭右闭[-2, 2], 数量是5 | ||||
| print() | ||||
|  | ||||
| print('随机数:\n', np.random.uniform(-2, 2))  # 随机浮点数 | ||||
| print('随机整数:\n', np.random.randint(-10, 10))  # 区间是左闭右开[-10, 10) | ||||
| print() | ||||
|  | ||||
| # 随机数除了使用numpy库,也使用random生成 | ||||
| import random | ||||
| print('使用random库的随机数:\n', random.uniform(-2,2)) # 随机浮点数 | ||||
| print('使用random库的随机整数:\n', random.randint(-10, 10))  # 区间是左闭右闭[-10, 10] | ||||
| print() | ||||
|  | ||||
| print('数组从小到大排列:\n', np.sort([1, 7, 0, 3])) | ||||
| print('数组从小到大排列对应的索引:\n', np.argsort([1, 7, 0, 3]))  # 注意Python中下标是从0开始的 | ||||
| print() | ||||
|  | ||||
| matrix0 = np.array([[1, 2+9j, 3], [2, 5, 7]])  # numpy数组 | ||||
| print('矩阵0:\n', matrix0) | ||||
| print('矩阵的维度:\n', matrix0.shape)  # 查看矩阵的维度 | ||||
| print('矩阵的行数:\n', matrix0.shape[0])  # 查看矩阵的行数 | ||||
| print('矩阵的列数:\n', matrix0.shape[1])  # 查看矩阵的列数 | ||||
| print('矩阵转置:\n', matrix0.transpose())  # 矩阵转置 | ||||
| print('矩阵转置共轭:\n', matrix0.transpose().conj())  # 矩阵转置共轭 | ||||
| print() | ||||
|  | ||||
| matrix1 = np.array([[3, 5], [2, 7]]) | ||||
| eigenvalue, eigenvector = np.linalg.eig(matrix1)  # 求本征值,本征向量 | ||||
| print('矩阵1:\n', matrix1) | ||||
| print('本征值:\n', eigenvalue) | ||||
| print('本征向量:\n', eigenvector) # 列向量为本征向量 | ||||
| print('逆矩阵:\n', np.linalg.inv(matrix1))  # 求逆 | ||||
| print('计算行列式:\n', np.linalg.det(matrix1))  # 行列式 | ||||
| print() | ||||
|  | ||||
| matrix2 = np.array([[1, 2], [3, 4]]) | ||||
| print('矩阵2:\n', matrix2) | ||||
| print('矩阵1和矩阵2相乘:\n', np.matmul(matrix1, matrix2))  # 矩阵乘积,或者可以用np.dot() | ||||
| print() | ||||
|  | ||||
| a = np.array([1, 2]) | ||||
| print('numpy数组a=', a) | ||||
| b = np.array([3, 4]) | ||||
| print('numpy数组b=', b) | ||||
| c = np.append(a, b, axis=0)  # 增加元素 | ||||
| print('numpy数组增加元素:\n', c)   | ||||
| d = np.append([a], [b], axis=0) # 增加行(列数要相同),或者用np.row_stack(([a], [b])) | ||||
| print('numpy数组增加行:\n', d)  | ||||
| e = np.append([a], [b], axis=1) # 增加列(行数要相同),或者用np.column_stack(([a], [b])) | ||||
| print('numpy数组增加列:\n', e)   | ||||
| print('重新观察:a=', a) | ||||
| print('重新观察:b=', b) | ||||
| print() | ||||
|  | ||||
|  | ||||
| # 如果不是numpy数组,原python数组可以直接用以下方法增加元素 | ||||
| c = [100, 200] | ||||
| print('python数组c=', c) | ||||
| c.append(300) | ||||
| print('增加元素后,c=', c) | ||||
| print() | ||||
| @@ -0,0 +1,77 @@ | ||||
| import tensorflow as tf | ||||
| import numpy as np | ||||
| import matplotlib.pyplot as plt | ||||
|  | ||||
|  | ||||
| def add_layer(inputs, in_size, out_size, activation_function=None):  # 定义一层的所有神经元 | ||||
|     Weights = tf.Variable(tf.random_normal([in_size, out_size]))  # 定义Weights为tf变量,并给予初值 | ||||
|     biases = tf.Variable(tf.zeros([1, out_size]) + 0.1)  # 定义biases为tf变量,并给予初值 | ||||
|     Wx_plus_b = tf.matmul(inputs, Weights) + biases  # 得分 | ||||
|     if activation_function is None:  # 没有激活函数 | ||||
|         outputs = Wx_plus_b | ||||
|     else: | ||||
|         outputs = activation_function(Wx_plus_b)  # 使用激活函数 | ||||
|     return outputs  # 返回该层每个神经元的输出值(维度为out_size) | ||||
|  | ||||
|  | ||||
| # 产生训练的数据 | ||||
| x_data = np.linspace(-1, 1, 300, dtype=np.float32)[:, np.newaxis]  # 产生数据,作为神经网络的输入数据。注:[:, np.newaxis]是用来增加一个轴,变成一个矩阵。 | ||||
| noise = np.random.normal(0, 0.05, x_data.shape).astype(np.float32)  # 产生噪声 | ||||
| y_data = np.square(x_data) - 0.5 + noise  # x_data加上噪声,作为神经网络的输出数据。 | ||||
| print(x_data.shape)  # 查看数据维度 | ||||
| print(noise.shape)  # 查看数据维度 | ||||
| print(y_data.shape)  # 查看数据维度 | ||||
| print()  # 打印输出空一行 | ||||
|  | ||||
|  | ||||
| # 神经网络模型的建立 | ||||
| xs = tf.placeholder(tf.float32, [None, 1])  # 定义占位符,为神经网络训练的输入数据。这里的None代表无论输入有多少数据都可以 | ||||
| ys = tf.placeholder(tf.float32, [None, 1])  # 定义占位符,为神经网络训练的输出数据。 | ||||
| l1 = add_layer(xs, 1, 10, activation_function=tf.nn.relu)  # 增加一个隐藏层 | ||||
| prediction = add_layer(l1, 10, 1, activation_function=None)  # 输出层 | ||||
| loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction), reduction_indices=[1]))  # 损失函数 | ||||
| train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)  # 梯度下降 | ||||
| init = tf.global_variables_initializer()  # 变量初始化 | ||||
|  | ||||
| # 画出原始的输入输出数据点图 | ||||
| fig = plt.figure() | ||||
| ax = fig.add_subplot(1, 1, 1) | ||||
| ax.scatter(x_data, y_data) | ||||
| plt.ion()  # 开启交互模式 | ||||
| plt.show()  # 显示图像 | ||||
|  | ||||
| # 训练神经网络模型 | ||||
| sess = tf.Session()  # 启动一个会话 | ||||
| sess.run(init)  # 初始化变量 | ||||
| for i in range(1000):  # 训练1000次 | ||||
|     sess.run(train_step, feed_dict={xs: x_data, ys: y_data})  # 喂数据,梯度下降循环1000次。 | ||||
|     if i % 50 == 0:  # 每训练50次画一下图 | ||||
|         try:  # to visualize the result and improvement | ||||
|             ax.lines.remove(lines[0]) | ||||
|         except Exception: | ||||
|             pass | ||||
|         prediction_value = sess.run(prediction, feed_dict={xs: x_data})  # 神经网络预测的值 | ||||
|         print('loss=', sess.run(loss, feed_dict={xs: x_data, ys: y_data}))  # 打印输出,查看损失函数下降情况 | ||||
|         print('prediction=', sess.run(prediction, feed_dict={xs: [x_data[0, :]]}))  # # 打印输出神经网络预测的值 | ||||
|         print()  # 打印空一行 | ||||
|         lines = ax.plot(x_data, prediction_value, 'r-', lw=5) # 画出预测的值,用线连起来 | ||||
|         plt.pause(.1)  # 暂停0.1,防止画图过快看不清。 | ||||
| plt.ioff()  # 关闭交互模式,再画一次图。作用是不让图自动关掉。 | ||||
| lines = ax.plot(x_data, prediction_value, 'r-', lw=5) | ||||
| plt.show() | ||||
|  | ||||
|  | ||||
| # 保存训练好的神经网络模型tf.train.Saver() | ||||
| saver = tf.train.Saver() | ||||
| save_path = saver.save(sess, "my_net/save_net.ckpt")  # 保存模型 | ||||
| print("Save to path: ", save_path) | ||||
| print() | ||||
| sess.close()  # 关闭会话 | ||||
|  | ||||
|  | ||||
| # 调用神经网络模型,来预测新的值 | ||||
| with tf.Session() as sess2: | ||||
|     saver.restore(sess2, "my_net/save_net.ckpt")  # 提取模型中的所有变量 | ||||
|     print(y_data[0, :])  # 输出的原始值 | ||||
|     print(sess2.run(prediction, feed_dict={xs: [x_data[0, :]]}))  # 预测值 | ||||
|  | ||||
| @@ -0,0 +1,39 @@ | ||||
| import tensorflow as tf  # 导入tensorflow | ||||
|  | ||||
| greeting = tf.constant('Hello Google Tensorflow!')  # 定义一个常量 | ||||
|  | ||||
| # 第一种方式 | ||||
| sess = tf.Session()  # 启动一个会话 | ||||
| result = sess.run(greeting)  # 使用会话执行greeting计算模块 | ||||
| print(result)  # 打印显示 | ||||
| sess.close()  # 关闭会话 | ||||
|  | ||||
| # 第二种方式 | ||||
| with tf.Session() as sess:  # 启动一个会话 | ||||
|     print(sess.run(greeting))  # 打印显示 | ||||
|  | ||||
|  | ||||
| # 例子1: | ||||
| matrix1 = tf.constant([[1., 3.]])  # 定义常数矩阵1  tf.constant() | ||||
| matrix2 = tf.constant([[2.], [2.]])  # 定义常数矩阵2  tf.constant() | ||||
| product = tf.matmul(matrix1, matrix2)  # 矩阵乘积  tf.matmul() | ||||
| linear = tf.add(product, tf.constant(2.))  # 矩阵乘积后再加上一个常数  tf.add() | ||||
| with tf.Session() as sess:  # 启动一个会话  tf.Session() | ||||
|     print(sess.run(matrix1))  # 执行语句并打印显示  tf.Session().run | ||||
|     print(sess.run(linear))  # 执行语句并打印显示  tf.Session().run | ||||
| print(linear)  # 直接打印是不能看到计算结果的,因为还未执行,只是一个张量。这里打印显示的结果是:Tensor("Add:0", shape=(1, 1), dtype=float32) | ||||
|  | ||||
|  | ||||
| # 例子2:变量tf.Variable() | ||||
| state = tf.Variable(3, name='counter')  # 变量tf.Variable | ||||
| init = tf.global_variables_initializer()  # 如果定义了变量,后面一定要有这个语句,用来初始化变量。 | ||||
| with tf.Session() as sess: | ||||
|     sess.run(init)  # 变量一定要初始化变量 | ||||
|     print(sess.run(state))  # 执行语句并打印显示 | ||||
|  | ||||
| # 例子3:占位符tf.placeholder(),用来临时占坑,需要用feed_dict来传入数值。 | ||||
| x1 = tf.placeholder(tf.float32) | ||||
| x2 = tf.placeholder(tf.float32) | ||||
| y = x1 + x2 | ||||
| with tf.Session() as sess: | ||||
|     print(sess.run(y, feed_dict={x1: 7, x2: 2})) | ||||
| @@ -0,0 +1,359 @@ | ||||
| """ | ||||
| This code is supported by the website: https://www.guanjihuan.com | ||||
| The newest version of this code is on the web page: https://www.guanjihuan.com/archives/703 | ||||
| """ | ||||
|  | ||||
| import pygame | ||||
| import random | ||||
| import math | ||||
| import numpy as np | ||||
|  | ||||
| # 参数 | ||||
| screen_width = 1500  # 屏幕宽度 | ||||
| screen_height = 900  # 屏幕高度 | ||||
| map_width = screen_width*4  # 地图的大小 | ||||
| map_height = screen_height*4  # 地图的大小 | ||||
| number_enemy = map_width*map_height/500000  # 敌人的数量 | ||||
| number_dots = map_width * map_height / 50  # 点点的数量 | ||||
| max_show_size = 100  # 球显示的最大半径(屏幕有限,球再增大时,改变的地图比例尺寸) | ||||
|  | ||||
| my_value = 1000  # 我的初始值 | ||||
| enemy_value_low = 500  # 敌人的初始值(最低) | ||||
| enemy_value_high = 1500  # 敌人的初始值(最高) | ||||
| dot_value = 30  # 点点的值(地上的豆豆/食物值) | ||||
| my_speed = 10  # 我的球运动的速度 | ||||
| speed_up = 20  # 按下鼠标时加速 | ||||
| speed_enemy = 10  # 敌人球正常运动速度 | ||||
| speed_enemy_anomaly = 20  # 敌人突然加速时的速度(速度异常时的速度) | ||||
| anomaly_pro = 0.5  # 敌人加速的概率 | ||||
| change_pro = 0.05  # 敌人移动路径变化的概率,也就是1/change_pro左右会变化一次 | ||||
| eat_percent = 0.9  # 吃掉敌人的球,按多少比例并入自己的体积,1对应的是100% | ||||
| loss = 0.001  # 按比例减小体重(此外越重的减少越多,10万体积损失值为loss的一倍) | ||||
| enemy_bigger_pro = 0.0005  # 敌人的值增加了我的球的值的enemy_bigger_rate倍的几率 | ||||
| enemy_bigger_rate = 0.1  # 增加我的球的体积的enemy_bigger_rate倍 | ||||
|  | ||||
|  | ||||
| class Color(object):  # 定义颜色的类 | ||||
|     @classmethod  # 加了这个可以不需要把实例化,能直接调用类的方法 | ||||
|     def random_color(cls):  # cls, 即class,表示可以通过类名直接调用 | ||||
|         red = random.randint(0, 255) | ||||
|         green = random.randint(0, 255) | ||||
|         blue = random.randint(0, 255) | ||||
|         return red, green, blue | ||||
|  | ||||
|  | ||||
| class Ball(object):  # 定义球 | ||||
|     def __init__(self, x, y, sx, sy, color, value):  # 初始化 | ||||
|         self.x = x  # 球的地图位置参数 | ||||
|         self.y = y | ||||
|         self.sx = sx  # 速度参数 | ||||
|         self.sy = sy | ||||
|         self.color = color  # 颜色 | ||||
|         self.value = value  # 球的值,也就是球的大小(不是显示的大小) | ||||
|         self.is_alive = True  # 球默认是存活状态 | ||||
|  | ||||
|  | ||||
| class My_Ball(Ball):  # 定义我的球,继承了Ball类的方法 | ||||
|     def __init__(self, x, y, sx, sy, color, value): | ||||
|         # 注意:如果重写了__init__() 时,实例化子类,就不会调用父类已经定义的__init__() | ||||
|         # 如果子类不重写__init__()方法,实例化子类后,会自动调用父类的__init__()的方法 | ||||
|         # 如果子类重写__init__()方法又需要调用父类的方法,则要使用super关键词。 | ||||
|         super().__init__(x, y, sx, sy, color, value)  # 调用父类Ball的初始化方法__init__() | ||||
|         self.radius = int(self.value**0.5)  # 我的球的半径(不考虑系数pi) | ||||
|         if self.radius >= max_show_size:  # 如果半径比规定的最大半径还大,则显示最大半径 | ||||
|             self.show_radius = max_show_size  # 我的球显示的半径 | ||||
|         else: | ||||
|             self.show_radius = self.radius  # 如果半径没有超过规定最大的半径,则显示原来实际大小的半径 | ||||
|         self.position_x = int(screen_width/2)   # 把我的球固定在屏幕中间position_x,是屏幕显示的位置 | ||||
|         self.position_y = int(screen_height/2)  # 把我的球固定在屏幕中间position_y,是屏幕显示的位置 | ||||
|  | ||||
|     def draw(self, window):  # 把我的球画出来 | ||||
|         self.radius = int(self.value ** 0.5)   # 这里重复上面的,因为除了初始化之后,还要更新 | ||||
|         if self.radius >= max_show_size: | ||||
|             self.show_radius = max_show_size | ||||
|         else: | ||||
|             self.show_radius = self.radius | ||||
|         self.position_x = int(screen_width / 2) | ||||
|         self.position_y = int(screen_height / 2) | ||||
|         pygame.draw.circle(window, self.color, (self.position_x , self.position_y), self.show_radius) | ||||
|  | ||||
|     def eat_ball(self, other):  # 吃别的球(包括小点点和敌人) | ||||
|         if self != other and self.is_alive and other.is_alive:  # 如果other不是自身,自身和对方也都是存活状态,则执行下面动作 | ||||
|             distance = ((self.position_x - other.position_x) ** 2 + (self.position_y - other.position_y) ** 2) ** 0.5   # 两个球之间的距离 | ||||
|             if distance < self.show_radius and (self.show_radius > other.show_radius or (self.show_radius == other.show_radius and self.value > other.value)):  # 如果自身半径比别人大,而且两者距离小于自身半径,那么可以吃掉。 | ||||
|                 other.is_alive = False  # 吃球(敌方已死) | ||||
|                 self.value += other.value*eat_percent   # 自己的值增大(体量增大) | ||||
|                 self.radius = int(self.value ** 0.5)  # 计算出半径 | ||||
|                 if self.radius >= max_show_size:  # 我的球的显示半径 | ||||
|                     self.show_radius = max_show_size | ||||
|                 else: | ||||
|                     self.show_radius = self.radius | ||||
|  | ||||
|     def move(self):  # 移动规则 | ||||
|         self.x += self.sx  # 地图位置加上速度 | ||||
|         self.y += self.sy | ||||
|         # 横向出界 | ||||
|         if self.x < 0:  # 离开了地图左边 | ||||
|             self.x = 0 | ||||
|         if self.x > map_width:  # 离开了地图右边 | ||||
|             self.x = map_width | ||||
|         # 纵向出界 | ||||
|         if self.y <= 0:  # 离开了地图下边 | ||||
|             self.y = 0 | ||||
|         if self.y >= map_height:  # 离开了地图上边 | ||||
|             self.y = map_height | ||||
|  | ||||
|  | ||||
| class Enemy_Ball(Ball):  # 定义敌人的球,继承了Ball类的方法 | ||||
|     def __init__(self, x, y, sx, sy, color, value, host_ball):  # 初始化带上host_ball,也就是我的球 | ||||
|         super().__init__(x, y, sx, sy, color, value) | ||||
|         self.host_ball = host_ball | ||||
|         self.radius = int(self.value**0.5) | ||||
|         if self.host_ball.radius >= max_show_size:  # 如果我的球比规定的最大尺寸还大,则敌人的球显示的比例要减小 | ||||
|             self.show_radius = max(10, int(self.radius/(self.host_ball.radius/max_show_size)))  # 敌人的球也不能太小,最小半径为10 | ||||
|             self.position_x = int((self.x - self.host_ball.x) / (self.host_ball.radius / max_show_size)) + int( | ||||
|                 screen_width / 2)  # 计算出敌人的球和我的球的相对位置,并且按比例减小 | ||||
|             self.position_y = int((self.y - self.host_ball.y) / (self.host_ball.radius / max_show_size)) + int( | ||||
|                 screen_height / 2)  # 计算出敌人的球和我的球的相对位置,并且按比例减小 | ||||
|         else: | ||||
|             self.show_radius = self.radius  # 正常显示 | ||||
|             self.position_x = (self.x - self.host_ball.x) + int(screen_width / 2)  # 敌人和我的球的相对位置 | ||||
|             self.position_y = (self.y - self.host_ball.y) + int(screen_height / 2)  # 敌人和我的球的相对位置 | ||||
|  | ||||
|     # 画出球 | ||||
|     def draw(self, window): | ||||
|         self.radius = int(self.value ** 0.5) | ||||
|         if self.host_ball.radius >= max_show_size:  # 这边把初始化的内容再写一遍,因为敌人的球初始化之后还要根据我的球而动态改变 | ||||
|             self.show_radius = max(10, int(self.radius/(self.host_ball.radius/max_show_size))) | ||||
|             self.position_x = int((self.x - self.host_ball.x) / (self.host_ball.radius / max_show_size)) + int( | ||||
|                 screen_width / 2) | ||||
|             self.position_y = int((self.y - self.host_ball.y) / (self.host_ball.radius / max_show_size)) + int( | ||||
|                 screen_height / 2) | ||||
|         else: | ||||
|             self.show_radius = self.radius | ||||
|             self.position_x = (self.x - self.host_ball.x) + int(screen_width / 2) | ||||
|             self.position_y = (self.y - self.host_ball.y) + int(screen_height / 2) | ||||
|         pygame.draw.circle(window, self.color, (self.position_x, self.position_y), self.show_radius) | ||||
|  | ||||
|     def eat_ball(self, other): | ||||
|         if self != other and self.is_alive and other.is_alive: | ||||
|             distance = ((self.position_x - other.position_x) ** 2 + (self.position_y - other.position_y) ** 2) ** 0.5 | ||||
|             if distance < self.show_radius and (self.show_radius > other.show_radius or (self.show_radius == other.show_radius and self.value > other.value)): | ||||
|                 other.is_alive = False  # 吃球 | ||||
|                 self.value += other.value*eat_percent | ||||
|                 self.radius = int(self.value ** 0.5) | ||||
|  | ||||
|     def move(self):  # 移动规则 | ||||
|         self.x += self.sx  # 地图位置加上速度 | ||||
|         self.y += self.sy | ||||
|         # 横向出界 | ||||
|         if self.x < 0:  # 离开了地图左边 | ||||
|             self.sx = -self.sx | ||||
|             self.x = 0 | ||||
|         if self.x > map_width:  # 离开了地图右边 | ||||
|             self.sx = -self.sx | ||||
|             self.x = map_width | ||||
|         # 纵向出界 | ||||
|         if self.y <= 0:  # 离开了地图下边 | ||||
|             self.sy = -self.sy | ||||
|             self.y = 0 | ||||
|         if self.y >= map_height:  # 离开了地图上边 | ||||
|             self.sy = -self.sy | ||||
|             self.y = map_height | ||||
|  | ||||
|  | ||||
| class Dot_Ball(Ball):  # 定义地上的小点点,供自己的球和敌人的球吃,继承了Ball类的方法 | ||||
|     def __init__(self, x, y,  sx, sy, color, value, host_ball): | ||||
|         super().__init__(x, y, sx, sy, color, value) | ||||
|         self.host_ball = host_ball | ||||
|         self.radius = 8  # 初始小点点大小 | ||||
|         if self.host_ball.radius >= max_show_size: | ||||
|             self.show_radius = max(3, int(self.radius/(self.host_ball.radius/max_show_size)))  # 小点点显示也不能太小,最小显示半径为3 | ||||
|             self.position_x = int((self.x - self.host_ball.x) / (self.host_ball.radius / max_show_size)) + int( | ||||
|                 screen_width / 2) | ||||
|             self.position_y = int((self.y - self.host_ball.y) / (self.host_ball.radius / max_show_size)) + int( | ||||
|                 screen_height / 2) | ||||
|         else: | ||||
|             self.show_radius = self.radius | ||||
|             self.position_x = (self.x - self.host_ball.x) + int(screen_width / 2) | ||||
|             self.position_y = (self.y - self.host_ball.y) + int(screen_height / 2) | ||||
|  | ||||
|     # 画出球 | ||||
|     def draw(self, window): | ||||
|         if self.host_ball.radius >= max_show_size:  # 这边把初始化的内容再写一遍,因为小点点初始化之后还要根据我的球而动态改变 | ||||
|             self.show_radius = max(3, int(self.radius/(self.host_ball.radius/max_show_size))) | ||||
|             self.position_x = int((self.x - self.host_ball.x) / (self.host_ball.radius / max_show_size)) + int( | ||||
|                 screen_width / 2) | ||||
|             self.position_y = int((self.y - self.host_ball.y) / (self.host_ball.radius / max_show_size)) + int( | ||||
|                 screen_height / 2) | ||||
|         else: | ||||
|             self.show_radius = self.radius | ||||
|             self.position_x = (self.x - self.host_ball.x) + int(screen_width / 2) | ||||
|             self.position_y = (self.y - self.host_ball.y) + int(screen_height / 2) | ||||
|         pygame.draw.circle(window, self.color, (self.position_x, self.position_y) , self.show_radius) | ||||
|  | ||||
|  | ||||
| def creat_my_ball():  # 产生我的球 | ||||
|     x = random.randint(0, map_width)  # 我的球在地图中的位置,随机生成 | ||||
|     y = random.randint(0, map_height) | ||||
|     value = my_value  # 我的球的初始值 | ||||
|     color = 255, 255, 255  # 我的球的颜色 | ||||
|     sx = 0  # 速度默认为0 | ||||
|     sy = 0 | ||||
|     host_ball = My_Ball(x, y, sx, sy, color, value)  # 调用My_Ball类 | ||||
|     return host_ball  # 返回我的球 | ||||
|  | ||||
|  | ||||
| def auto_creat_ball(balls, host_ball):  # 自动产生敌人的球 | ||||
|     if len(balls) <= number_enemy:  # 控制敌人的数量,如果个数够了,就不再生成 | ||||
|         x = random.randint(0, map_width)  # 敌人球在地图中的位置,随机生成 | ||||
|         y = random.randint(0, map_height) | ||||
|         value = random.randint(enemy_value_low, enemy_value_high)  # 敌人的球初始值 | ||||
|         sx = random.randint(-speed_enemy, speed_enemy)  # 敌人的球移动速度 | ||||
|         i2 = random.randint(0, 1)  # y的移动方向 | ||||
|         if i2 == 0: | ||||
|             sy = int((speed_enemy**2 - sx**2) ** 0.5) | ||||
|         else: | ||||
|             sy = -int((speed_enemy ** 2 - sx ** 2) ** 0.5) | ||||
|         color = Color.random_color()  # 敌人的颜色随机生成 | ||||
|         enemy = Enemy_Ball(x, y, sx, sy, color, value, host_ball) | ||||
|         balls.append(enemy) | ||||
|  | ||||
|  | ||||
| def auto_creat_dots(dots, host_ball):  # 自动生成点点 | ||||
|     if len(dots) <= number_dots:  # 控制点点的数量 | ||||
|         x = random.randint(0, map_width)  # 随机生成点点的位置 | ||||
|         y = random.randint(0, map_height) | ||||
|         value = dot_value  # 点点的值 | ||||
|         sx = 0  # 点点速度为0 | ||||
|         sy = 0 | ||||
|         color = Color.random_color()  # 颜色 | ||||
|         dot = Dot_Ball(x, y, sx, sy, color, value, host_ball) | ||||
|         dots.append(dot) | ||||
|  | ||||
|  | ||||
| def control_my_ball(host_ball):  # 控制我的球 | ||||
|     host_ball.move() | ||||
|     host_ball.value = host_ball.value*(1-loss*host_ball.value/100000) | ||||
|     for event in pygame.event.get():  # 监控事件(鼠标移动) | ||||
|         # print(event) | ||||
|         if event.type == pygame.MOUSEBUTTONDOWN: | ||||
|             pos = event.pos | ||||
|             speed = speed_up | ||||
|         elif event.type == pygame.MOUSEMOTION: | ||||
|             pos = event.pos | ||||
|             if event.buttons[0] == 1: | ||||
|                 speed = speed_up | ||||
|             if event.buttons[0] == 0: | ||||
|                 speed = my_speed | ||||
|         elif event.type == pygame.MOUSEBUTTONUP: | ||||
|             pos = event.pos | ||||
|             speed = my_speed | ||||
|         else: | ||||
|             pos = [screen_width/2, screen_height/2] | ||||
|             speed = my_speed | ||||
|         if abs(pos[0] - screen_width/2) < 30 and abs(pos[1] - screen_height/2) < 30: | ||||
|             host_ball.sx = 0 | ||||
|             host_ball.sy = 0 | ||||
|         elif pos[0] > screen_width/2 and pos[1] >= screen_height/2: | ||||
|             angle = abs(math.atan((pos[1] - screen_height/2) / (pos[0] - screen_width/2))) | ||||
|             host_ball.sx = int(speed * math.cos(angle)) | ||||
|             host_ball.sy = int(speed * math.sin(angle)) | ||||
|         elif pos[0] > screen_width/2 and pos[1] < screen_height/2: | ||||
|             angle = abs(math.atan((pos[1] - screen_height/2) / (pos[0] - screen_width/2))) | ||||
|             host_ball.sx = int(speed * math.cos(angle)) | ||||
|             host_ball.sy = -int(speed * math.sin(angle)) | ||||
|         elif pos[0] < screen_width/2 and pos[1] >= screen_height/2: | ||||
|             angle = abs(math.atan((pos[1] - screen_height/2) / (pos[0] - screen_width/2))) | ||||
|             host_ball.sx = -int(speed * math.cos(angle)) | ||||
|             host_ball.sy = int(speed * math.sin(angle)) | ||||
|         elif pos[0] < screen_width/2 and pos[1] < screen_height/2: | ||||
|             angle = abs(math.atan((pos[1] - screen_height/2) / (pos[0] - screen_width/2))) | ||||
|             host_ball.sx = -int(speed * math.cos(angle)) | ||||
|             host_ball.sy = -int(speed * math.sin(angle)) | ||||
|         elif pos[0] == screen_width/2: | ||||
|             host_ball.sx = 0 | ||||
|             if pos[1] >= 0: | ||||
|                 host_ball.sy = speed | ||||
|             else: | ||||
|                 host.ball.sy = -speed | ||||
|  | ||||
|  | ||||
| def enemy_move(balls, host_ball):  # 敌人移动 | ||||
|     for enemy in balls: | ||||
|         enemy.move()  # 移动 | ||||
|         enemy.value = enemy.value*(1-loss*enemy.value/100000) | ||||
|         if random.randint(1, int(1/enemy_bigger_pro)) == 1: | ||||
|             enemy.value += host_ball.value*enemy_bigger_rate | ||||
|         if random.randint(1, int(1/anomaly_pro)) == 1: | ||||
|             speed_enemy0 = speed_enemy_anomaly  # 敌人异常速度 | ||||
|         else: | ||||
|             speed_enemy0 = speed_enemy  # 敌人正常速度 | ||||
|         i = random.randint(1, int(1/change_pro))  # 一定的概率改变轨迹 | ||||
|         if i == 1: | ||||
|             enemy.sx = random.randint(-speed_enemy0, speed_enemy0) | ||||
|             i2 = random.randint(0, 1) | ||||
|             if i2 == 0: | ||||
|                 enemy.sy = int((speed_enemy0 ** 2 - enemy.sx ** 2) ** 0.5) | ||||
|             else: | ||||
|                 enemy.sy = -int((speed_enemy0 ** 2 - enemy.sx ** 2) ** 0.5) | ||||
|  | ||||
|  | ||||
| def eat_each_other(host_ball, balls, dots):  # 吃球 | ||||
|     for enemy in balls: | ||||
|         for enemy2 in balls: | ||||
|             enemy.eat_ball(enemy2)  # 敌人互吃 | ||||
|         for food in dots: | ||||
|             enemy.eat_ball(food)  # 敌人吃点点 | ||||
|     for enemy in balls: | ||||
|         host_ball.eat_ball(enemy)  # 我吃敌人 | ||||
|         enemy.eat_ball(host_ball)  # 敌人吃我 | ||||
|     for food in dots: | ||||
|         host_ball.eat_ball(food)  # 我吃点点 | ||||
|  | ||||
|  | ||||
| def paint(host_ball, balls, dots, screen): | ||||
|     screen.fill((0, 0, 0))  # 刷漆 | ||||
|     if host_ball.is_alive: | ||||
|         host_ball.draw(screen) | ||||
|     for enemy in balls:  # 遍历容器 | ||||
|         if enemy.is_alive: | ||||
|             enemy.draw(screen) | ||||
|         else: | ||||
|             balls.remove(enemy) | ||||
|     for food in dots:  # 遍历容器 | ||||
|         if food.is_alive: | ||||
|             food.draw(screen) | ||||
|         else: | ||||
|             dots.remove(food) | ||||
|  | ||||
|  | ||||
| def main(): | ||||
|     pygame.init()  # 初始化 | ||||
|     screen = pygame.display.set_mode((screen_width, screen_height))  # 设置屏幕 | ||||
|     pygame.display.set_caption("球球大作战")  # 设置屏幕标题 | ||||
|     balls = []  # 定义一容器  存放所有的敌方球 | ||||
|     dots = []  # 定义一容器 存放所有的点点 | ||||
|     is_running = True  # 默认运行状态 | ||||
|     host_ball = creat_my_ball()  # 产生我的球 | ||||
|     i00 = 0  # 一个参数 | ||||
|     while is_running: | ||||
|         for event in pygame.event.get(): | ||||
|             if event.type == pygame.QUIT: | ||||
|                 is_running = False | ||||
|         auto_creat_dots(dots, host_ball)  # 自动生成点点 | ||||
|         auto_creat_ball(balls, host_ball)  # 自动生成敌人 | ||||
|         paint(host_ball, balls, dots, screen)  # 把所有的都画出来 调用draw方法 | ||||
|         pygame.display.flip()  # 渲染 | ||||
|         pygame.time.delay(30)  # 设置动画的时间延迟 | ||||
|  | ||||
|         control_my_ball(host_ball)  # 移动我的球 | ||||
|         enemy_move(balls, host_ball)  # 敌人的球随机运动 | ||||
|         eat_each_other(host_ball, balls, dots)  # 吃球 调用eat_ball方法 | ||||
|         i00 += 1 | ||||
|         if np.mod(i00, 50) == 0: | ||||
|             print(host_ball.value) | ||||
|  | ||||
|  | ||||
| if __name__ == '__main__': | ||||
|     main() | ||||
| @@ -0,0 +1,106 @@ | ||||
| """ | ||||
| This code is supported by the website: https://www.guanjihuan.com | ||||
| The newest version of this code is on the web page: https://www.guanjihuan.com/archives/706 | ||||
| """ | ||||
|  | ||||
| import numpy as np | ||||
| import time | ||||
| import matplotlib.pyplot as plt | ||||
| import tushare as ts | ||||
|  | ||||
|  | ||||
| def main(): | ||||
|     start_clock = time.perf_counter() | ||||
|     pro = ts.pro_api('到官网上注册,寻找Token填在这里!') | ||||
|     print('\n我的策略:见好就收,遇低抄底。\n' | ||||
|           '   【卖出】买入后涨了5%就卖出\n' | ||||
|           '   【买入】卖出后跌了5%就买入\n' | ||||
|           '注:第一天必须买进,最后一天前必须卖出(为了与不操作的做对比)\n') | ||||
|     number = 1 | ||||
|     for i in range(number): | ||||
|         data = pro.stock_basic(exchange='', list_status='L', fields='ts_code,symbol,name,area,industry,list_date')   # 所有股票列表 | ||||
|         # print(data.columns)  # 查看该数据的表头 | ||||
|         # print(data)  # 3688多行的股票数据 | ||||
|         i = 1  # 查看第二行数据“万科A”股 | ||||
|         ts_code = data.values[i, 0]  # 股票代码 | ||||
|         stock = data.values[i, 2]  # 股票名称 | ||||
|         industry = data.values[i, 4]  # 属于哪个行业 | ||||
|         start_date = '20110101'  # 开始时间 | ||||
|         end_date = '20191027'  # 结束时间 | ||||
|         df = pro.daily(ts_code=ts_code, start_date=start_date, end_date=end_date)  # 查看该股票的日线数据 | ||||
|         # print(df.columns)  # 查看该数据的表头 | ||||
|         # print(df)  # 查看该股票的日线数据 | ||||
|         close = np.array(list(reversed(df.values[:, 5])))  # 提取出收盘价,并按时间顺序排列,从过去到现在 | ||||
|         pct_chg = np.array(list(reversed(df.values[:, 8])))  # 提取出涨跌幅,并按时间顺序排列,从过去到现在 | ||||
|         # print(df.columns[5], '=', close, '\n')  # 查看收盘价 | ||||
|         # print(df.columns[8], '=', pct_chg, '\n')  # 查看涨跌幅 | ||||
|         profit, profit_no_operation, times, invest_money, buy_time_all, sell_time_all = back_test(close.shape[0], close, pct_chg) | ||||
|         # 调用回测函数,返回了“利润,未操作的利润, 按该策略操作了几次, 总投资金额, 按该策略买的时间, 按该策略卖的时间”的值 | ||||
|         print('\n------股票:', stock, ts_code, industry, '[买入市值=%7.2f' % invest_money, ']------') | ||||
|         print('回测时间段:', start_date, '-', end_date) | ||||
|         print('操作后利润= %6.2f' % profit, '  买入(卖出)次数=', times, ' ') | ||||
|         print('不操作利润= %6.2f' % profit_no_operation, '(第一天买入,最后一天卖出,中间未操作)') | ||||
|     end_clock = time.perf_counter() | ||||
|     print('CPU执行时间=', end_clock - start_clock, 's') | ||||
|     plt.figure(1) | ||||
|     plt.title('Stock Code: '+ts_code+'  (red point: buy,  green point: sell)') | ||||
|     plt.grid() | ||||
|     plt.plot(range(close.shape[0]), close, '-') | ||||
|     for i in buy_time_all: | ||||
|         plt.plot(i, close[int(i)], 'or', markersize=13)  # 红色是买进的点 | ||||
|     for i in sell_time_all: | ||||
|         plt.plot(i, close[int(i)], 'dg', markersize=13)  # 绿色是卖出的点 | ||||
|     plt.show() | ||||
|  | ||||
|  | ||||
| def back_test(days, close, pct_chg, money_in=10000):  # 定义该策略的回测效果(按旧数据检查该策略是否有效) | ||||
|     money_in_amount = int(money_in/close[0])  # 投资金额换算成股票股数 | ||||
|     invest_money = close[0]*money_in_amount  # 实际买了股票的金额 | ||||
|     profit_no_operation = (close[close.shape[0]-1]-close[0])*money_in_amount  # 不操作的利润 | ||||
|     position = -1  # 买入还是卖出的状态,默认卖出 | ||||
|     total_profit = 0 | ||||
|     times = 0 | ||||
|     current_buy_pct = -999 | ||||
|     current_sell_pct = 999 | ||||
|     buy_time_all = np.array([]) | ||||
|     sell_time_all = np.array([]) | ||||
|     for i in range(days):  # 总天数 | ||||
|         if i == 0:  # 第一天,满仓买买买!为了和不操作的对比,第一天就要买入。 | ||||
|             buy_time = i  # 买入时间 | ||||
|             buy_time_all = np.append(buy_time_all, [buy_time], axis=0)  # 买入时间存档 | ||||
|             position = 1  # 标记为买入状态 | ||||
|             print('------------------第', buy_time, '天买进-------------') | ||||
|         else: | ||||
|             profit = 0 | ||||
|             if position == 1:  # 买入状态 | ||||
|                 current_buy_pct = (close[i]-close[buy_time])/close[buy_time]*100  # 买入后的涨跌情况 | ||||
|                 # print('当前买进后的涨跌情况:第', i, '天=', current_buy_pct) | ||||
|             if position == 0:  # 卖出状态 | ||||
|                 current_sell_pct = (close[i]-close[sell_time])/close[sell_time]*100  # 卖出后的涨跌情况 | ||||
|  | ||||
|             if current_sell_pct < -5 and position == 0:  # 卖出状态,且卖出后跌了有3%,这时候买入 | ||||
|                 buy_time = i  # 买入时间 | ||||
|                 buy_time_all = np.append(buy_time_all, [buy_time], axis=0)  # 买入时间存档 | ||||
|                 print('------------------第', buy_time, '天买进-------------') | ||||
|                 position = 1  # 标记为买入状态 | ||||
|                 continue | ||||
|  | ||||
|             if current_buy_pct > 5 and position == 1:  # 买入状态,且买入后涨了有3%,这时候卖出 | ||||
|                 sell_time = i  # 卖出时间 | ||||
|                 sell_time_all = np.append(sell_time_all, [sell_time], axis=0)  # 卖出时间存档 | ||||
|                 print('----------第', sell_time, '天卖出,持有天数:', sell_time-buy_time, '--------------\n') | ||||
|                 position = 0  # 标记为卖出状态 | ||||
|                 profit = close[sell_time]-close[buy_time]  # 赚取利润 | ||||
|                 times = times + 1  # 买入(卖出)次数加1 | ||||
|             total_profit = total_profit + profit*money_in_amount  # 计算总利润 | ||||
|     if position == 1:  # 最后一天如果是买入状态,则卖出 | ||||
|         profit = close[i]-close[buy_time]  # 赚取利润 | ||||
|         total_profit = total_profit + profit  # 计算总利润 | ||||
|         times = times + 1  # 买入(卖出)次数加1 | ||||
|         print('--------------第', i, '天(最后一天)卖出,持有天数:', sell_time-buy_time, '--------------\n') | ||||
|         sell_time_all = np.append(sell_time_all, [i], axis=0)  # 卖出时间存档 | ||||
|     return total_profit, profit_no_operation, times, invest_money, buy_time_all, sell_time_all | ||||
|  | ||||
|  | ||||
| if __name__ == '__main__': | ||||
|     main() | ||||
| @@ -0,0 +1,11 @@ | ||||
| import imageio | ||||
| import numpy as np | ||||
| import os | ||||
| # os.chdir('E:/data')  # 设置文件读取和保存位置 | ||||
|  | ||||
| images = [] | ||||
| for i in range(1000): | ||||
|     image = str(i)+'.jpg' | ||||
|     im = imageio.imread(image) | ||||
|     images.append(im) | ||||
| imageio.mimsave("a.gif", images, 'GIF', duration=0.1)  # durantion是延迟时间 | ||||
| @@ -0,0 +1,35 @@ | ||||
| from multiprocessing import Process | ||||
| import os | ||||
| import time | ||||
| import numpy as np | ||||
| import guan | ||||
|  | ||||
| def main(parameter_array, task_index): | ||||
|     print ('Process id = %s' % (os.getpid())) | ||||
|     result_array = [] | ||||
|     for parameter in parameter_array: | ||||
|         result = parameter*2 | ||||
|         result_array.append(result) | ||||
|     time.sleep(np.random.uniform(1,10)) | ||||
|     guan.write_one_dimensional_data(parameter_array, result_array, filename='task_index='+str(task_index)) | ||||
|  | ||||
| if __name__ == '__main__': | ||||
|     cpus = 4 | ||||
|     parameter_array_all = np.arange(0, 17, 1)  | ||||
|     start_time = time.perf_counter() | ||||
|     process_array = [] | ||||
|     for task_index in range(cpus): | ||||
|         parameter_array = guan.preprocess_for_parallel_calculations(parameter_array_all, cpus, task_index) | ||||
|         process_array.append(Process(target=main, args=(parameter_array, task_index))) | ||||
|     for process in process_array: # 运行子进程 | ||||
|         process.start() | ||||
|     for process in process_array: # 等待子进程完成 | ||||
|         process.join()  | ||||
|     end_time = time.perf_counter() | ||||
|     print('运行时间=', (end_time-start_time), '\n') | ||||
|     f = open('result.txt', 'w') | ||||
|     for task_index in range(cpus): | ||||
|         with open('task_index='+str(task_index)+'.txt', 'r') as f0: | ||||
|             text = f0.read() | ||||
|         f.write(text) | ||||
|     f.close() | ||||
| @@ -0,0 +1,42 @@ | ||||
| from multiprocessing import Process | ||||
| import os | ||||
| import time | ||||
|  | ||||
| def run_proc(name): # 要执行的代码 | ||||
|     start_time = time.perf_counter() | ||||
|     time.sleep(2) | ||||
|     end_time = time.perf_counter() | ||||
|     print ('Process id running on %s = %s' % (name, os.getpid()), '; running time = %s' % (end_time-start_time)) | ||||
|  | ||||
|  | ||||
| if __name__ == '__main__': | ||||
|  | ||||
|     # 串行 | ||||
|     print('串行程序') | ||||
|     print('Process id = %s.' % os.getpid()) | ||||
|     start_time = time.perf_counter() | ||||
|     run_proc('job1') | ||||
|     run_proc('job2') | ||||
|     run_proc('job3') | ||||
|     run_proc('job4') | ||||
|     end_time = time.perf_counter() | ||||
|     print('CPU执行时间(s)=', (end_time-start_time), '\n') | ||||
|  | ||||
|     # 并行 | ||||
|     print('并行程序') | ||||
|     print('Process id = %s.' % os.getpid()) | ||||
|     start_time = time.perf_counter() | ||||
|     p1 = Process(target=run_proc, args=('job1',)) | ||||
|     p2 = Process(target=run_proc, args=('job2',)) | ||||
|     p3 = Process(target=run_proc, args=('job3',)) | ||||
|     p4 = Process(target=run_proc, args=('job4',)) | ||||
|     p1.start() | ||||
|     p2.start() | ||||
|     p3.start() | ||||
|     p4.start() | ||||
|     p1.join()  # join()方法可以等待子进程结束后再继续往下运行 | ||||
|     p2.join()   | ||||
|     p3.join()   | ||||
|     p4.join() | ||||
|     end_time = time.perf_counter() | ||||
|     print('运行时间(s)=', (end_time-start_time)) | ||||
| @@ -0,0 +1,16 @@ | ||||
| from multiprocessing import Process, Value | ||||
|  | ||||
| def run_proc(name, a, num): # 要执行的代码 | ||||
|     num.value = a | ||||
|  | ||||
| if __name__ == '__main__': | ||||
|     num1 = Value('d', 0.0)  # 共享内存 | ||||
|     num2 = Value('d', 0.0)  # 共享内存 | ||||
|     p1 = Process(target=run_proc, args=('job1', 100, num1)) | ||||
|     p2 = Process(target=run_proc, args=('job2', 200, num2)) | ||||
|     p1.start() | ||||
|     p2.start() | ||||
|     p1.join() | ||||
|     p2.join()  | ||||
|     print(num1.value) | ||||
|     print(num2.value) | ||||
| @@ -0,0 +1,30 @@ | ||||
| import numpy as np | ||||
| from math import *   | ||||
| # import os | ||||
| # os.chdir('D:/data')  # 设置路径 | ||||
|  | ||||
|  | ||||
| def main(): | ||||
|     k1 = np.arange(-pi, pi, 0.05) | ||||
|     k2 = np.arange(-pi, pi, 0.05) | ||||
|     value = np.ones((k2.shape[0], k1.shape[0])) | ||||
|     plot_matrix(k1, k2, value) | ||||
|  | ||||
|  | ||||
| def plot_matrix(k1, k2, matrix): | ||||
|     import matplotlib.pyplot as plt | ||||
|     from mpl_toolkits.mplot3d import Axes3D | ||||
|     from matplotlib import cm | ||||
|     from matplotlib.ticker import LinearLocator, FormatStrFormatter | ||||
|     fig = plt.figure() | ||||
|     ax = fig.gca(projection='3d') | ||||
|     k1, k2 = np.meshgrid(k1, k2) | ||||
|     ax.plot_surface(k1, k2, matrix, cmap=cm.coolwarm, linewidth=0, antialiased=False)  | ||||
|     plt.xlabel('k1') | ||||
|     plt.ylabel('k2')  | ||||
|     ax.set_zlabel('Z')   | ||||
|     plt.show() | ||||
|  | ||||
|  | ||||
| if __name__ == '__main__': | ||||
|     main() | ||||
| @@ -0,0 +1,31 @@ | ||||
| import numpy as np | ||||
| from math import *   | ||||
| # import os | ||||
| # os.chdir('D:/data')  # 设置路径 | ||||
|  | ||||
|  | ||||
| def main(): | ||||
|     k1 = np.arange(-pi, pi, 0.05) | ||||
|     k2 = np.arange(-pi, pi, 0.05) | ||||
|     value = np.ones((k2.shape[0], k1.shape[0])) | ||||
|     write_matrix(k1, k2, value) | ||||
|  | ||||
|  | ||||
| def write_matrix(k1, k2, matrix): | ||||
|     with open('a.txt', 'w') as f: | ||||
|         # np.set_printoptions(suppress=True)  # 取消输出科学记数法 | ||||
|         f.write('0           ') | ||||
|         for k10 in k1: | ||||
|             f.write(str(k10)+'   ') | ||||
|         f.write('\n') | ||||
|         i0 = 0 | ||||
|         for k20 in k2: | ||||
|             f.write(str(k20)) | ||||
|             for j0 in range(k1.shape[0]): | ||||
|                 f.write('  '+str(matrix[i0, j0])+'   ') | ||||
|             f.write('\n') | ||||
|             i0 += 1    | ||||
|  | ||||
|  | ||||
| if __name__ == '__main__': | ||||
|     main() | ||||
| @@ -0,0 +1,35 @@ | ||||
| import numpy as np | ||||
| from math import * | ||||
| # import os | ||||
| # os.chdir('D:/data')  # 设置路径 | ||||
|  | ||||
|  | ||||
| def hamiltonian(k): | ||||
|     pass | ||||
|  | ||||
|  | ||||
| def main(): | ||||
|     k = np.arange(-pi, pi, 0.05) | ||||
|     plot_bands_one_dimension(k, hamiltonian) | ||||
|  | ||||
|  | ||||
| def plot_bands_one_dimension(k, hamiltonian): | ||||
|     import matplotlib.pyplot as plt | ||||
|     dim = hamiltonian(0).shape[0] | ||||
|     dim_k = k.shape[0] | ||||
|     eigenvalue_k = np.zeros((dim_k, dim)) | ||||
|     i0 = 0 | ||||
|     for k0 in k: | ||||
|         matrix0 = hamiltonian(k0) | ||||
|         eigenvalue, eigenvector = np.linalg.eig(matrix0) | ||||
|         eigenvalue_k[i0, :] = np.sort(np.real(eigenvalue[:])) | ||||
|         i0 += 1 | ||||
|     for dim0 in range(dim): | ||||
|         plt.plot(k, eigenvalue_k[:, dim0], '-k') | ||||
|     plt.xlabel('k') | ||||
|     plt.ylabel('E')  | ||||
|     plt.show() | ||||
|  | ||||
|  | ||||
| if __name__ == '__main__': | ||||
|     main() | ||||
| @@ -0,0 +1,47 @@ | ||||
| import numpy as np | ||||
| from math import * | ||||
| # import os | ||||
| # os.chdir('D:/data')  # 设置路径 | ||||
|  | ||||
|  | ||||
| def hamiltonian(k1, k2): | ||||
|     pass | ||||
|  | ||||
|  | ||||
| def main(): | ||||
|     k1 = np.arange(-pi, pi, 0.05) | ||||
|     k2 = np.arange(-pi, pi, 0.05) | ||||
|     plot_bands_two_dimension(k1, k2, hamiltonian) | ||||
|  | ||||
|  | ||||
| def plot_bands_two_dimension(k1, k2, hamiltonian): | ||||
|     import matplotlib.pyplot as plt | ||||
|     from mpl_toolkits.mplot3d import Axes3D | ||||
|     from matplotlib import cm | ||||
|     from matplotlib.ticker import LinearLocator, FormatStrFormatter | ||||
|     dim = hamiltonian(0, 0).shape[0] | ||||
|     dim1 = k1.shape[0] | ||||
|     dim2 = k2.shape[0] | ||||
|     eigenvalue_k = np.zeros((dim2, dim1, dim)) | ||||
|     i0 = 0 | ||||
|     for k20 in k2: | ||||
|         j0 = 0 | ||||
|         for k10 in k1: | ||||
|             matrix0 = hamiltonian(k10, k20) | ||||
|             eigenvalue, eigenvector = np.linalg.eig(matrix0) | ||||
|             eigenvalue_k[i0, j0, :] = np.sort(np.real(eigenvalue[:])) | ||||
|             j0 += 1 | ||||
|         i0 += 1 | ||||
|     fig = plt.figure() | ||||
|     ax = fig.gca(projection='3d') | ||||
|     k1, k2 = np.meshgrid(k1, k2) | ||||
|     for dim0 in range(dim): | ||||
|         ax.plot_surface(k1, k2, eigenvalue_k[:, :, dim0], cmap=cm.coolwarm, linewidth=0, antialiased=False)  | ||||
|     plt.xlabel('k1') | ||||
|     plt.ylabel('k2')  | ||||
|     ax.set_zlabel('E')   | ||||
|     plt.show() | ||||
|  | ||||
|  | ||||
| if __name__ == '__main__': | ||||
|     main() | ||||
| @@ -0,0 +1,31 @@ | ||||
| import numpy as np | ||||
| from math import * | ||||
| # import os | ||||
| # os.chdir('D:/data')  # 设置路径 | ||||
|  | ||||
|  | ||||
| def hamiltonian(k): | ||||
|     pass | ||||
|  | ||||
|  | ||||
| def main(): | ||||
|     k = np.arange(-pi, pi, 0.05) | ||||
|     write_bands_one_dimension(k, hamiltonian) | ||||
|  | ||||
|  | ||||
| def write_bands_one_dimension(k, hamiltonian): | ||||
|     dim = hamiltonian(0).shape[0] | ||||
|     f = open('a.txt','w') | ||||
|     for k0 in k: | ||||
|         f.write(str(k0)+'   ') | ||||
|         matrix0 = hamiltonian(k0) | ||||
|         eigenvalue, eigenvector = np.linalg.eig(matrix0) | ||||
|         eigenvalue = np.sort(np.real(eigenvalue)) | ||||
|         for dim0 in range(dim): | ||||
|             f.write(str(eigenvalue[dim0])+'   ') | ||||
|         f.write('\n') | ||||
|     f.close() | ||||
|  | ||||
|  | ||||
| if __name__ == '__main__': | ||||
|     main() | ||||
| @@ -0,0 +1,42 @@ | ||||
| import numpy as np | ||||
| from math import * | ||||
| # import os | ||||
| # os.chdir('D:/data')  # 设置路径 | ||||
|  | ||||
|  | ||||
| def hamiltonian(k1, k2): | ||||
|     pass | ||||
|  | ||||
|  | ||||
| def main(): | ||||
|     k1 = np.arange(-pi, pi, 0.05) | ||||
|     k2 = np.arange(-pi, pi, 0.05) | ||||
|     write_bands_two_dimension(k1, k2, hamiltonian) | ||||
|  | ||||
|  | ||||
| def write_bands_two_dimension(k1, k2, hamiltonian): | ||||
|     f1 = open('a1.txt', 'w') | ||||
|     f2 = open('a2.txt', 'w') | ||||
|     f1.write('0     ') | ||||
|     f2.write('0     ') | ||||
|     for k10 in k1: | ||||
|         f1.write(str(k10)+'   ') | ||||
|         f2.write(str(k10)+'   ') | ||||
|     f1.write('\n') | ||||
|     f2.write('\n') | ||||
|     for k20 in k2: | ||||
|         f1.write(str(k20)+'   ') | ||||
|         f2.write(str(k20)+'   ') | ||||
|         for k10 in k1: | ||||
|             matrix0 = hamiltonian(k10, k20) | ||||
|             eigenvalue, eigenvector = np.linalg.eig(matrix0) | ||||
|             eigenvalue = np.sort(np.real(eigenvalue)) | ||||
|             f1.write(str(eigenvalue[0])+'   ') | ||||
|             f2.write(str(eigenvalue[1])+'   ') | ||||
|         f1.write('\n') | ||||
|         f2.write('\n') | ||||
|     f1.close() | ||||
|     f2.close() | ||||
|  | ||||
| if __name__ == '__main__': | ||||
|     main() | ||||
| @@ -0,0 +1,7 @@ | ||||
| import numpy as np  # 导入numpy库,用来存储和处理大型矩阵,比python自带的嵌套列表更高效。numpy库还包含了许多数学函数库。python+numpy等同于matlab。 | ||||
|  | ||||
| def main():  # 主函数的内容放在这里。 | ||||
|     pass | ||||
|  | ||||
| if __name__ == '__main__':  # 如果是当前文件直接运行,执行main()函数中的内容;如果是import当前文件,则不执行。同时将main()语句放在最后运行,可以避免书写的函数出现未定义的情况。 | ||||
|     main() | ||||
| @@ -0,0 +1,4 @@ | ||||
| 1   1.2   2.4 | ||||
| 2   5.5   3.2 | ||||
| 3   6.7   7.1 | ||||
| 4   3.6   4.9 | ||||
| @@ -0,0 +1,4 @@ | ||||
| 0      1      2      3      4 | ||||
| 1     1.3    2.7    6.7    8.3 | ||||
| 2     4.3    2.9    5.4    7.4 | ||||
| 3     9.1    8.2    2.6    3.1 | ||||
| @@ -0,0 +1,47 @@ | ||||
| import numpy as np   | ||||
| import matplotlib.pyplot as plt | ||||
| # import os | ||||
| # os.chdir('D:/data')  # 设置路径 | ||||
|  | ||||
|  | ||||
| def main():   | ||||
|     x, y = read_one_dimension('1D_data.txt') | ||||
|     for dim0 in range(y.shape[1]): | ||||
|         plt.plot(x, y[:, dim0], '-k') | ||||
|     plt.show() | ||||
|  | ||||
|  | ||||
| def read_one_dimension(file_name): | ||||
|     f = open(file_name, 'r') | ||||
|     text = f.read() | ||||
|     f.close() | ||||
|     row_list = np.array(text.split('\n'))  # 根据“回车”分割成每一行 | ||||
|     # print('文本格式:') | ||||
|     # print(text) | ||||
|     # print('row_list:') | ||||
|     # print(row_list) | ||||
|     # print('column:') | ||||
|     dim_column = np.array(row_list[0].split()).shape[0] # 列数 | ||||
|     x = np.array([]) | ||||
|     y = np.array([]) | ||||
|     for row in row_list: | ||||
|         column = np.array(row.split())  # 每一行根据“空格”继续分割 | ||||
|         # print(column) | ||||
|         if column.shape[0] != 0:  # 解决最后一行空白的问题 | ||||
|             x = np.append(x, [float(column[0])], axis=0)  # 第一列为x数据 | ||||
|             y_row = np.zeros(dim_column-1) | ||||
|             for dim0 in range(dim_column-1): | ||||
|                 y_row[dim0] = float(column[dim0+1]) | ||||
|             if np.array(y).shape[0] == 0: | ||||
|                 y = [y_row] | ||||
|             else: | ||||
|                 y = np.append(y, [y_row], axis=0) | ||||
|     # print('x:') | ||||
|     # print(x) | ||||
|     # print('y:') | ||||
|     # print(y) | ||||
|     return x, y | ||||
|  | ||||
|  | ||||
| if __name__ == '__main__':  | ||||
|     main() | ||||
| @@ -0,0 +1,69 @@ | ||||
| import numpy as np | ||||
| # import os | ||||
| # os.chdir('D:/data')  # 设置路径 | ||||
|  | ||||
|  | ||||
| def main():   | ||||
|     x1, x2, matrix = read_two_dimension('2D_data.txt') | ||||
|     plot_matrix(x1, x2, matrix) | ||||
|  | ||||
|  | ||||
| def read_two_dimension(file_name): | ||||
|     f = open(file_name, 'r') | ||||
|     text = f.read() | ||||
|     f.close() | ||||
|     row_list = np.array(text.split('\n'))  # 根据“回车”分割成每一行 | ||||
|     # print('文本格式:') | ||||
|     # print(text) | ||||
|     # print('row_list:') | ||||
|     # print(row_list) | ||||
|     # print('column:') | ||||
|     dim_column = np.array(row_list[0].split()).shape[0] # 列数 | ||||
|     x1 = np.array([]) | ||||
|     x2 = np.array([]) | ||||
|     matrix = np.array([]) | ||||
|     for i0 in range(row_list.shape[0]): | ||||
|         column = np.array(row_list[i0].split())  # 每一行根据“空格”继续分割 | ||||
|         # print(column) | ||||
|         if i0 == 0: | ||||
|             x1_str = column[1::]  # x1坐标(去除第一个在角落的值) | ||||
|             x1 = np.zeros(x1_str.shape[0]) | ||||
|             for i00 in range(x1_str.shape[0]): | ||||
|                 x1[i00] = float(x1_str[i00])  # 字符串转浮点 | ||||
|         elif column.shape[0] != 0:  # 解决最后一行空白的问题 | ||||
|             x2 = np.append(x2, [float(column[0])], axis=0)  # 第一列为x数据 | ||||
|             matrix_row = np.zeros(dim_column-1) | ||||
|             for dim0 in range(dim_column-1): | ||||
|                 matrix_row[dim0] = float(column[dim0+1]) | ||||
|             if np.array(matrix).shape[0] == 0: | ||||
|                 matrix = [matrix_row] | ||||
|             else: | ||||
|                 matrix = np.append(matrix, [matrix_row], axis=0) | ||||
|     # print('x1:') | ||||
|     # print(x1) | ||||
|     # print('x2:') | ||||
|     # print(x2) | ||||
|     # print('matrix:') | ||||
|     # print(matrix) | ||||
|     return x1, x2, matrix | ||||
|  | ||||
|  | ||||
|  | ||||
| def plot_matrix(x1, x2, matrix): | ||||
|     import matplotlib.pyplot as plt | ||||
|     from mpl_toolkits.mplot3d import Axes3D | ||||
|     from matplotlib import cm | ||||
|     from matplotlib.ticker import LinearLocator, FormatStrFormatter | ||||
|     fig = plt.figure() | ||||
|     ax = fig.gca(projection='3d') | ||||
|     x1, x2 = np.meshgrid(x1, x2) | ||||
|     ax.plot_surface(x1, x2, matrix, cmap=cm.coolwarm, linewidth=0, antialiased=False)  | ||||
|     plt.xlabel('x1') | ||||
|     plt.ylabel('x2')  | ||||
|     ax.set_zlabel('z')   | ||||
|     plt.show() | ||||
|  | ||||
|  | ||||
|  | ||||
| if __name__ == '__main__':  | ||||
|     main() | ||||
| @@ -0,0 +1,18 @@ | ||||
| import numpy as np | ||||
| from math import *  | ||||
| # import os | ||||
| # os.chdir('D:/data')  # 设置路径  | ||||
|  | ||||
|  | ||||
| f = open('a.txt', 'w') | ||||
| f.write('0       ') | ||||
| for k1 in np.arange(-pi, pi, 0.05): | ||||
|     f.write(str(k1)+'   ') | ||||
| f.write('\n')   | ||||
| for k2 in np.arange(-pi, pi, 0.05): | ||||
|     f.write(str(k2)+'   ')  | ||||
|     for k1 in np.arange(-pi, pi, 0.05): | ||||
|         data = 1000  # 运算数据 | ||||
|         f.write(str(data)+'   ')  | ||||
|     f.write('\n')  | ||||
| f.close() | ||||
| @@ -0,0 +1,47 @@ | ||||
| from bs4 import BeautifulSoup | ||||
| from urllib.request import urlopen | ||||
|  | ||||
| # 最简单的情况 | ||||
| html = urlopen("https://mofanpy.com/static/scraping/basic-structure.html").read().decode('utf-8') | ||||
| print('\n显示网页的代码信息1:\n\n ----------------开始----------------\n', html, '\n\n----------------结束----------------')  # 显示网页的代码信息 | ||||
|  | ||||
| soup = BeautifulSoup(html, features='lxml')   # 把网页放进BeautifulSoup | ||||
| print('\n获取标签_标题h1_中的内容soup.h1:\n', soup.h1) | ||||
| print('\n获取标签_段落p_中的内容soup.p:\n', soup.p) | ||||
| print('\n获取标签_链接a_中的内容soup.a:\n', soup.a) | ||||
|  | ||||
| all_href = soup.find_all('a') | ||||
| print('\n获取所有"a标签"的内容soup.find_all(‘a’):\n', all_href) | ||||
|  | ||||
| print('\n获取某个字典的值_1:') | ||||
| for a in all_href: | ||||
|     print(a) | ||||
|     print(a['href']) | ||||
|  | ||||
| all_href = [a['href'] for a in all_href] | ||||
| print('\n获取某个字典的值_2:\n', all_href, '\n') | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
| # 加入CSS内容 | ||||
| html = urlopen("https://mofanpy.com/static/scraping/list.html").read().decode('utf-8') | ||||
| print('\n显示网页的代码信息2:\n\n ----------------开始----------------\n', html, '\n\n----------------结束----------------')  # 显示网页的代码信息 | ||||
|  | ||||
| soup = BeautifulSoup(html, features='lxml')  # 把网页放进BeautifulSoup | ||||
|  | ||||
| print('\n利用class筛选出所需要的信息:') | ||||
| month = soup.find_all('li', {"class": "month"}) | ||||
| print(month, '\n') | ||||
|  | ||||
| print('只显示文本:') | ||||
| for m in month: | ||||
|     print(m.get_text()) | ||||
|  | ||||
| print('\n 多次筛选:') | ||||
| january = soup.find('ul', {"class": 'jan'}) | ||||
| print(january, '\n') | ||||
| d_january = january.find_all('li')  # use january as a parent | ||||
| print(d_january, '\n') | ||||
| for d in d_january: | ||||
|     print(d.get_text()) | ||||
| @@ -0,0 +1,78 @@ | ||||
| """ | ||||
| This code is supported by the website: https://www.guanjihuan.com | ||||
| The newest version of this code is on the web page: https://www.guanjihuan.com/archives/6869 | ||||
| """ | ||||
|  | ||||
| import PyPDF2 | ||||
| import os | ||||
| import re  | ||||
| from bs4 import BeautifulSoup | ||||
| from urllib.request import urlopen | ||||
| import requests | ||||
|  | ||||
|  | ||||
|  | ||||
| def main(): | ||||
|     os.chdir('D:/')  # PDF文件存放的位置 | ||||
|     filename = input('输入PDF文件名:') | ||||
|     pdfFile = open(filename+'.pdf','rb')  # 打开PDF文件 | ||||
|     links = all_links_in_pdf(pdfFile)  # 获取PDF文件中的链接 | ||||
|     pdfFile.close()  # 关闭PDF文件 | ||||
|     os.chdir('D:/Reference')  # 设置参考文献保存的位置 | ||||
|     download(links)  # 下载文献 | ||||
|  | ||||
|  | ||||
|  | ||||
| def all_links_in_pdf(pdfFile):  | ||||
|     pdfReader = PyPDF2.PdfFileReader(pdfFile) | ||||
|     pages = pdfReader.getNumPages() | ||||
|     i0 = 0 | ||||
|     links = [] | ||||
|     print() | ||||
|     for page in range(pages): | ||||
|         pageSliced = pdfReader.getPage(page) | ||||
|         pageObject = pageSliced.getObject() | ||||
|         if '/Annots' in pageObject.keys(): | ||||
|             ann = pageObject['/Annots'] | ||||
|             old = '' | ||||
|             for a in ann: | ||||
|                 u = a.getObject() | ||||
|                 if '/A' in u.keys(): | ||||
|                     if re.search(re.compile('^https://doi.org'), u['/A']['/URI']):   # 排除其他形式的链接 | ||||
|                         if u['/A']['/URI'] != old: # 排除重复链接 | ||||
|                             print(i0 , u['/A']['/URI']) | ||||
|                             links.append(u['/A']['/URI']) # 链接存在link数组中  | ||||
|                             i0 += 1 | ||||
|                             old = u['/A']['/URI']         | ||||
|     return links | ||||
|  | ||||
|  | ||||
|  | ||||
| def download(links): | ||||
|     for i0 in [0, 1, 3]:  # 指定参考文献下载,如需全部下载用for i0 in range(links.shape[0]): | ||||
|         address = links[i0] | ||||
|         r = requests.post('https://sci-hub.st/', data={'request': address}) | ||||
|         print('\n响应结果是:', r) | ||||
|         print('访问的地址是:', r.url) | ||||
|         soup = BeautifulSoup(r.text, features='lxml') | ||||
|         pdf_URL = soup.embed['src'] | ||||
|         # pdf_URL = soup.iframe['src'] # This is a code line of history version which fails to get pdf URL. | ||||
|         if re.search(re.compile('^https:'), pdf_URL): | ||||
|             pass | ||||
|         else: | ||||
|             pdf_URL = 'https:'+pdf_URL | ||||
|         print('PDF的地址是:', pdf_URL) | ||||
|         name = re.search(re.compile('fdp.*?/'),pdf_URL[::-1]).group()[::-1][1::] | ||||
|         print('PDF文件名是:', name) | ||||
|         print('保存的位置在:', os.getcwd()) | ||||
|         print('\n正在下载第',i0,'篇') | ||||
|         r = requests.get(pdf_URL, stream=True) | ||||
|         with open(name, 'wb') as f: | ||||
|             for chunk in r.iter_content(chunk_size=32): | ||||
|                 f.write(chunk) | ||||
|         print('第',i0,'篇下载完成!') | ||||
|     print('\n全部下载完成!') | ||||
|  | ||||
|  | ||||
| if __name__ == '__main__': | ||||
|     main() | ||||
| @@ -0,0 +1,30 @@ | ||||
| """ | ||||
| This code is supported by the website: https://www.guanjihuan.com | ||||
| The newest version of this code is on the web page: https://www.guanjihuan.com/archives/6869 | ||||
| """ | ||||
|  | ||||
| import PyPDF2 | ||||
| import os | ||||
| import re  | ||||
|  | ||||
| os.chdir('D:/')  # PDF文件存放的位置 | ||||
| filename = input('输入PDF文件名:') | ||||
| pdfFile = open(filename+'.pdf','rb') | ||||
| pdfReader = PyPDF2.PdfFileReader(pdfFile) | ||||
| pages = pdfReader.getNumPages() | ||||
| i0 = 0 | ||||
| for page in range(pages): | ||||
|     pageSliced = pdfReader.getPage(page) | ||||
|     pageObject = pageSliced.getObject() | ||||
|     if '/Annots' in pageObject.keys(): | ||||
|         ann = pageObject['/Annots'] | ||||
|         old = '' | ||||
|         for a in ann: | ||||
|             u = a.getObject() | ||||
|             if '/A' in u.keys(): | ||||
|                 if re.search(re.compile('^https://doi.org'), u['/A']['/URI']):   # 排除其他形式的链接 | ||||
|                     if u['/A']['/URI'] != old: # 排除重复链接 | ||||
|                         print(i0 , u['/A']['/URI']) | ||||
|                         i0 += 1 | ||||
|                         old = u['/A']['/URI']         | ||||
| pdfFile.close() | ||||
| @@ -0,0 +1,47 @@ | ||||
| """ | ||||
| This code is supported by the website: https://www.guanjihuan.com | ||||
| """ | ||||
|  | ||||
| from bs4 import BeautifulSoup | ||||
| import re | ||||
| import requests | ||||
| import urllib.request | ||||
| import os | ||||
| import ssl | ||||
| ssl._create_default_https_context = ssl._create_unverified_context | ||||
| html = urllib.request.urlopen("https://www.guanjihuan.com/archives/4418").read().decode('utf-8') | ||||
| soup = BeautifulSoup(html, features='lxml') | ||||
| all_a_tag = soup.find_all('a', href=True) | ||||
| for a_tag in all_a_tag: | ||||
|     href = a_tag['href'] | ||||
|     if re.search('https://www.ldoceonline.com/dictionary/', href): | ||||
|         print(href[39:]) | ||||
|         exist_1 = os.path.exists('words_mp3_breProns/'+href[39:]+'.mp3') | ||||
|         exist_2 = os.path.exists('words_mp3_ameProns/'+href[39:]+'.mp3') | ||||
|         if exist_1 and exist_2: | ||||
|             continue | ||||
|         header = {'User-Agent':'Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'}   # 头部信息 | ||||
|         request = urllib.request.Request(href,headers=header) | ||||
|         reponse = urllib.request.urlopen(request).read() | ||||
|         soup2 = BeautifulSoup(reponse, features='lxml')  | ||||
|         span = soup2.find_all('span', {"class":"speaker brefile fas fa-volume-up hideOnAmp"}) | ||||
|         for span0 in span: | ||||
|             href2 = span0['data-src-mp3'] | ||||
|             if re.search('https://www.ldoceonline.com/media/english/breProns/', href2): | ||||
|                 print(href2) | ||||
|                 r = requests.get(href2, headers=header, stream=True) | ||||
|                 with open('words_mp3_breProns/'+href[39:]+'.mp3', 'wb') as f: | ||||
|                     for chunk in r.iter_content(chunk_size=32): | ||||
|                         f.write(chunk) | ||||
|             break | ||||
|         span = soup2.find_all('span', {"class":"speaker amefile fas fa-volume-up hideOnAmp"}) | ||||
|         for span0 in span: | ||||
|             href2 = span0['data-src-mp3'] | ||||
|             if re.search('https://www.ldoceonline.com/media/english/ameProns/', href2): | ||||
|                 print(href2) | ||||
|                 r = requests.get(href2, headers=header, stream=True) | ||||
|                 with open('words_mp3_ameProns/'+href[39:]+'.mp3', 'wb') as f: | ||||
|                     for chunk in r.iter_content(chunk_size=32): | ||||
|                         f.write(chunk) | ||||
|             break | ||||
|         print() | ||||
| @@ -0,0 +1,9 @@ | ||||
| # Words in webpage: https://www.guanjihuan.com/archives/4418 | ||||
| # installation: | ||||
| # (1) pip install pygame | ||||
| # (2) pip install --upgrade guan | ||||
|  | ||||
| import guan | ||||
| guan.play_academic_words() | ||||
| # guan.play_academic_words(reverse=1) | ||||
| # guan.play_academic_words(reverse=0, random_on=0, bre_or_ame='ame', show_translation=1, show_link=1, translation_time=2, rest_time=1) | ||||
| @@ -0,0 +1,32 @@ | ||||
| import numpy as np | ||||
| # import os | ||||
| # os.chdir('D:/data')  # 设置文件保存的位置 | ||||
|  | ||||
|  | ||||
| def main(): | ||||
|     x = [4, 3, 5, 7] | ||||
|     y = [6, 1, 3, 2] | ||||
|     value = [3, 1, 10, 2] | ||||
|     Plot_2D_Scatter(x, y, value, title='Plot 2D Scatter') | ||||
|  | ||||
|  | ||||
| def Plot_2D_Scatter(x, y, value, xlabel='x', ylabel='y', title='title', filename='a'): | ||||
|     import matplotlib.pyplot as plt | ||||
|     fig = plt.figure() | ||||
|     ax = fig.add_subplot(111) | ||||
|     plt.subplots_adjust(bottom=0.2, right=0.8, left=0.2)  | ||||
|     for i in range(np.array(x).shape[0]): | ||||
|         ax.scatter(x[i], y[i], marker='o', s=100*value[i], c=[(1,0,0)]) | ||||
|     ax.set_title(title, fontsize=20, fontfamily='Times New Roman') | ||||
|     ax.set_xlabel(xlabel, fontsize=20, fontfamily='Times New Roman')  | ||||
|     ax.set_ylabel(ylabel, fontsize=20, fontfamily='Times New Roman')  | ||||
|     ax.tick_params(labelsize=15)  # 设置刻度值字体大小 | ||||
|     labels = ax.get_xticklabels() + ax.get_yticklabels()  | ||||
|     [label.set_fontname('Times New Roman') for label in labels] # 设置刻度值字体 | ||||
|     # plt.savefig(filename+'.jpg', dpi=300)  | ||||
|     plt.show() | ||||
|     plt.close('all') | ||||
|  | ||||
|  | ||||
| if __name__ == '__main__': | ||||
|     main() | ||||
| @@ -0,0 +1,38 @@ | ||||
| import numpy as np | ||||
| # import os | ||||
| # os.chdir('D:/data')  # 设置文件保存的位置 | ||||
|  | ||||
|  | ||||
| def main(): | ||||
|     x = [1, 3, 5, 7] | ||||
|     y = [2, 4, 6, 8] | ||||
|     z = [2, 8, 6, 1] | ||||
|     value = [3, 1, 10, 2] | ||||
|     Plot_3D_Scatter(x, y, z, value, title='Plot 3D Scatter') | ||||
|  | ||||
|  | ||||
| def Plot_3D_Scatter(x, y, z, value, xlabel='x', ylabel='y', zlabel='z', title='title', filename='a'): | ||||
|     import matplotlib.pyplot as plt | ||||
|     from matplotlib.ticker import LinearLocator | ||||
|     fig = plt.figure() | ||||
|     ax = fig.add_subplot(111, projection='3d') | ||||
|     plt.subplots_adjust(bottom=0.1, right=0.8)  | ||||
|     for i in range(np.array(x).shape[0]): | ||||
|         ax.scatter(x[i], y[i], z[i], marker='o', s=int(100*value[i]), c=[(1,0,0)]) | ||||
|     ax.set_title(title, fontsize=20, fontfamily='Times New Roman') | ||||
|     ax.set_xlabel(xlabel, fontsize=20, fontfamily='Times New Roman')  | ||||
|     ax.set_ylabel(ylabel, fontsize=20, fontfamily='Times New Roman')  | ||||
|     ax.set_zlabel(zlabel, fontsize=20, fontfamily='Times New Roman')  | ||||
|     # ax.set_zlim(0, 20)  | ||||
|     # ax.zaxis.set_major_locator(LinearLocator(6)) # 设置z轴主刻度的个数 | ||||
|     # ax.zaxis.set_major_formatter('{x:.0f}')   # 设置z轴主刻度的格式 | ||||
|     ax.tick_params(labelsize=15)  # 设置刻度值字体大小 | ||||
|     labels = ax.get_xticklabels() + ax.get_yticklabels() + ax.get_zticklabels() | ||||
|     [label.set_fontname('Times New Roman') for label in labels] # 设置刻度值字体 | ||||
|     # plt.savefig(filename+'.jpg', dpi=300)  | ||||
|     plt.show() | ||||
|     plt.close('all') | ||||
|  | ||||
|  | ||||
| if __name__ == '__main__': | ||||
|     main() | ||||
| @@ -0,0 +1,43 @@ | ||||
| import numpy as np | ||||
| # import os | ||||
| # os.chdir('D:/data')  # 设置文件保存的位置 | ||||
|  | ||||
|  | ||||
| def main(): | ||||
|     x = np.arange(-5, 5, 0.25) | ||||
|     y = np.arange(-5, 5, 0.25) | ||||
|     X, Y = np.meshgrid(x, y) | ||||
|     R = np.sqrt(X**2 + Y**2) | ||||
|     Z = np.sin(R) | ||||
|     Plot_3D_Surface(x,y,Z) | ||||
|  | ||||
|  | ||||
| def Plot_3D_Surface(x,y,matrix,filename='a.jpg', titlename='Plot 3D Surface'): | ||||
|     import matplotlib.pyplot as plt | ||||
|     from matplotlib import cm | ||||
|     from matplotlib.ticker import LinearLocator | ||||
|     fig, ax = plt.subplots(subplot_kw={"projection": "3d"}) | ||||
|     plt.subplots_adjust(bottom=0.1, right=0.65)  # 调整位置 | ||||
|     x, y = np.meshgrid(x, y) | ||||
|     surf = ax.plot_surface(x, y, matrix, cmap=cm.coolwarm, linewidth=0, antialiased=False)  # Plot the surface. | ||||
|     ax.set_title(titlename, fontsize=20, fontfamily='Times New Roman') | ||||
|     ax.set_xlabel('x', fontsize=30, fontfamily='Times New Roman') # 坐标标签 | ||||
|     ax.set_ylabel('y', fontsize=30, fontfamily='Times New Roman') # 坐标标签 | ||||
|     ax.set_zlabel('z', fontsize=30, fontfamily='Times New Roman') # 坐标标签 | ||||
|     # ax.set_zlim(-1, 1)  # 设置z轴的范围 | ||||
|     ax.zaxis.set_major_locator(LinearLocator(5)) # 设置z轴主刻度的个数 | ||||
|     ax.zaxis.set_major_formatter('{x:.2f}')   # 设置z轴主刻度的格式 | ||||
|     ax.tick_params(labelsize=15)  # 设置刻度值字体大小 | ||||
|     labels = ax.get_xticklabels() + ax.get_yticklabels() + ax.get_zticklabels() | ||||
|     [label.set_fontname('Times New Roman') for label in labels] # 设置刻度值字体 | ||||
|     cax = plt.axes([0.75, 0.15, 0.05, 0.75]) # color bar的位置 [左,下,宽度, 高度] | ||||
|     cbar = fig.colorbar(surf, cax=cax)  # color bar | ||||
|     cbar.ax.tick_params(labelsize=15) # 设置color bar刻度的字体大小 | ||||
|     [l.set_family('Times New Roman') for l in cbar.ax.yaxis.get_ticklabels()] # 设置color bar刻度的字体 | ||||
|     # plt.savefig(filename, dpi=800) # 保存图片文件 | ||||
|     plt.show() | ||||
|     plt.close('all')  # 关闭所有plt,防止循环画图时占用内存 | ||||
|  | ||||
|  | ||||
| if __name__ == '__main__': | ||||
|     main() | ||||
| @@ -0,0 +1,41 @@ | ||||
| import numpy as np | ||||
| # import os | ||||
| # os.chdir('D:/data')  # 设置文件保存的位置 | ||||
|  | ||||
|  | ||||
| def main(): | ||||
|     x = np.arange(-5, 5, 0.25) | ||||
|     y = np.arange(-5, 5, 0.25) | ||||
|     X, Y = np.meshgrid(x, y) | ||||
|     R = np.sqrt(X**2 + Y**2) | ||||
|     Z = np.sin(R) | ||||
|     Plot_Contour(x,y,Z) | ||||
|  | ||||
|  | ||||
| def Plot_Contour(x,y,matrix,filename='a.jpg', titlename='Plot Contour'): | ||||
|     import matplotlib.pyplot as plt | ||||
|     from matplotlib import cm | ||||
|     from matplotlib.ticker import LinearLocator | ||||
|     fig, ax = plt.subplots() | ||||
|     plt.subplots_adjust(bottom=0.15, right=0.7)  # 调整位置 | ||||
|     x, y = np.meshgrid(x, y) | ||||
|     contour = ax.contourf(x,y,matrix,cmap='jet')  | ||||
|     ax.set_title(titlename, fontsize=20, fontfamily='Times New Roman') | ||||
|     ax.set_xlabel('x', fontsize=30, fontfamily='Times New Roman') # 坐标标签 | ||||
|     ax.set_ylabel('y', fontsize=30, fontfamily='Times New Roman') # 坐标标签 | ||||
|     # plt.xlabel('x') | ||||
|     # plt.ylabel('y')  | ||||
|     ax.tick_params(labelsize=15)  # 设置刻度值字体大小 | ||||
|     labels = ax.get_xticklabels() + ax.get_yticklabels() | ||||
|     [label.set_fontname('Times New Roman') for label in labels] # 设置刻度值字体 | ||||
|     cax = plt.axes([0.75, 0.15, 0.08, 0.73]) # color bar的位置 [左,下,宽度, 高度] | ||||
|     cbar = fig.colorbar(contour, cax=cax)  # color bar | ||||
|     cbar.ax.tick_params(labelsize=15) # 设置color bar刻度的字体大小 | ||||
|     [l.set_family('Times New Roman') for l in cbar.ax.yaxis.get_ticklabels()] # 设置color bar刻度的字体 | ||||
|     # plt.savefig(filename, dpi=800) # 保存图片文件 | ||||
|     plt.show() | ||||
|     plt.close('all')  # 关闭所有plt,防止循环画图时占用内存 | ||||
|  | ||||
|  | ||||
| if __name__ == '__main__': | ||||
|     main() | ||||
| @@ -0,0 +1,30 @@ | ||||
| import numpy as np | ||||
| # import os | ||||
| # os.chdir('D:/data')  # 设置文件保存的位置 | ||||
|  | ||||
|  | ||||
| def main(): | ||||
|     x = np.arange(0.0, 2.0, 0.01) | ||||
|     y = 1 + np.sin(2 * np.pi * x) | ||||
|     Plot_Line(x,y) | ||||
|  | ||||
|  | ||||
| def Plot_Line(x,y,filename='a.jpg', titlename='Plot Line'): | ||||
|     import matplotlib.pyplot as plt | ||||
|     fig, ax = plt.subplots() | ||||
|     plt.subplots_adjust(bottom=0.20, left=0.16)  | ||||
|     ax.plot(x, y, '-o') | ||||
|     ax.grid() | ||||
|     ax.set_title(titlename, fontsize=20, fontfamily='Times New Roman') | ||||
|     ax.set_xlabel('x', fontsize=30, fontfamily='Times New Roman') # 坐标标签 | ||||
|     ax.set_ylabel('y', fontsize=30, fontfamily='Times New Roman') # 坐标标签 | ||||
|     ax.tick_params(labelsize=20)  # 设置刻度值字体大小 | ||||
|     labels = ax.get_xticklabels() + ax.get_yticklabels() | ||||
|     [label.set_fontname('Times New Roman') for label in labels] # 设置刻度值字体 | ||||
|     # plt.savefig(filename, dpi=800) # 保存图片文件 | ||||
|     plt.show() | ||||
|     plt.close('all')  # 关闭所有plt,防止循环画图时占用内存 | ||||
|  | ||||
|  | ||||
| if __name__ == '__main__': | ||||
|     main() | ||||
| @@ -0,0 +1,322 @@ | ||||
| """ | ||||
| This code is supported by the website: https://www.guanjihuan.com | ||||
| The newest version of this code is on the web page: https://www.guanjihuan.com/archives/8734 | ||||
|  | ||||
| 函数调用目录: | ||||
| 1. x, y = read_one_dimensional_data(filename='a') | ||||
| 2. x, y, matrix = read_two_dimensional_data(filename='a') | ||||
| 3. write_one_dimensional_data(x, y, filename='a') | ||||
| 4. write_two_dimensional_data(x, y, matrix, filename='a') | ||||
| 5. plot(x, y, xlabel='x', ylabel='y', title='', filename='a') | ||||
| 6. plot_3d_surface(x, y, matrix, xlabel='x', ylabel='y', zlabel='z', title='', filename='a') | ||||
| 7. plot_contour(x, y, matrix, xlabel='x', ylabel='y', title='', filename='a') | ||||
| 8. plot_2d_scatter(x, y, value, xlabel='x', ylabel='y', title='', filename='a') | ||||
| 9. plot_3d_surface(x, y, z, value, xlabel='x', ylabel='y', zlabel='z', title='', filename='a') | ||||
| 10. creat_animation(image_names, duration_time=0.5, filename='a') | ||||
| 11. eigenvalue_array = calculate_eigenvalue_with_one_paramete(x, matrix) | ||||
| 12. eigenvalue_array = calculate_eigenvalue_with_two_parameters(x, y, matrix) | ||||
|  | ||||
| 函数对应的功能: | ||||
| 1. 读取filename.txt文件中的一维数据y(x) | ||||
| 2. 读取filename.txt文件中的二维数据matrix(x,y) | ||||
| 3. 把一维数据y(x)写入filename.txt文件   | ||||
| 4. 把二维数据matrix(x,y)写入filename.txt文件 | ||||
| 5. 画y(x)图,并保存到filename.jpg文件。具体画图格式可在函数中修改! | ||||
| 6. 画3d_surface图,并保存到filename.jpg文件。具体画图格式可在函数中修改! | ||||
| 7. 画contour图,并保存到filename.jpg文件。具体画图格式可在函数中修改! | ||||
| 8. 画2d_scatter图,并保存到filename.jpg文件。具体画图格式可在函数中修改! | ||||
| 9. 画3d_scatter图,并保存到filename.jpg文件。具体画图格式可在函数中修改! | ||||
| 10. 制作动画 | ||||
| 11. 在参数x下,计算matrix函数的本征值eigenvalue_array[:, index] | ||||
| 12. 在参数(x,y)下,计算matrix函数的本征值eigenvalue_array[:, :, index] | ||||
| """ | ||||
|  | ||||
|  | ||||
| import numpy as np | ||||
| # import os | ||||
| # os.chdir('D:/data') | ||||
|  | ||||
|  | ||||
| def main(): | ||||
|     pass  # 读取数据 + 数据处理 + 保存新数据 | ||||
|  | ||||
|  | ||||
| # 1. 读取filename.txt文件中的一维数据y(x)   | ||||
| def read_one_dimensional_data(filename='a'):  | ||||
|     f = open(filename+'.txt', 'r') | ||||
|     text = f.read() | ||||
|     f.close() | ||||
|     row_list = np.array(text.split('\n'))  | ||||
|     dim_column = np.array(row_list[0].split()).shape[0]  | ||||
|     x = np.array([]) | ||||
|     y = np.array([]) | ||||
|     for row in row_list: | ||||
|         column = np.array(row.split())  | ||||
|         if column.shape[0] != 0:   | ||||
|             x = np.append(x, [float(column[0])], axis=0)   | ||||
|             y_row = np.zeros(dim_column-1) | ||||
|             for dim0 in range(dim_column-1): | ||||
|                 y_row[dim0] = float(column[dim0+1]) | ||||
|             if np.array(y).shape[0] == 0: | ||||
|                 y = [y_row] | ||||
|             else: | ||||
|                 y = np.append(y, [y_row], axis=0) | ||||
|     return x, y | ||||
|  | ||||
|  | ||||
| # 2. 读取filename.txt文件中的二维数据matrix(x,y)   | ||||
| def read_two_dimensional_data(filename='a'):  | ||||
|     f = open(filename+'.txt', 'r') | ||||
|     text = f.read() | ||||
|     f.close() | ||||
|     row_list = np.array(text.split('\n'))  | ||||
|     dim_column = np.array(row_list[0].split()).shape[0]  | ||||
|     x = np.array([]) | ||||
|     y = np.array([]) | ||||
|     matrix = np.array([]) | ||||
|     for i0 in range(row_list.shape[0]): | ||||
|         column = np.array(row_list[i0].split())  | ||||
|         if i0 == 0: | ||||
|             x_str = column[1::]  | ||||
|             x = np.zeros(x_str.shape[0]) | ||||
|             for i00 in range(x_str.shape[0]): | ||||
|                 x[i00] = float(x_str[i00])  | ||||
|         elif column.shape[0] != 0:  | ||||
|             y = np.append(y, [float(column[0])], axis=0)   | ||||
|             matrix_row = np.zeros(dim_column-1) | ||||
|             for dim0 in range(dim_column-1): | ||||
|                 matrix_row[dim0] = float(column[dim0+1]) | ||||
|             if np.array(matrix).shape[0] == 0: | ||||
|                 matrix = [matrix_row] | ||||
|             else: | ||||
|                 matrix = np.append(matrix, [matrix_row], axis=0) | ||||
|     return x, y, matrix | ||||
|  | ||||
|  | ||||
| # 3. 把一维数据y(x)写入filename.txt文件   | ||||
| def write_one_dimensional_data(x, y, filename='a'):  | ||||
|     with open(filename+'.txt', 'w') as f: | ||||
|         i0 = 0 | ||||
|         for x0 in x: | ||||
|             f.write(str(x0)+'   ') | ||||
|             if len(y.shape) == 1: | ||||
|                 f.write(str(y[i0])+'\n') | ||||
|             elif len(y.shape) == 2: | ||||
|                 for j0 in range(y.shape[1]): | ||||
|                     f.write(str(y[i0, j0])+'   ') | ||||
|                 f.write('\n') | ||||
|             i0 += 1 | ||||
|  | ||||
|  | ||||
| # 4. 把二维数据matrix(x,y)写入filename.txt文件   | ||||
| def write_two_dimensional_data(x, y, matrix, filename='a'):  | ||||
|     with open(filename+'.txt', 'w') as f: | ||||
|         f.write('0   ') | ||||
|         for x0 in x: | ||||
|             f.write(str(x0)+'   ') | ||||
|         f.write('\n') | ||||
|         i0 = 0 | ||||
|         for y0 in y: | ||||
|             f.write(str(y0)) | ||||
|             j0 = 0 | ||||
|             for x0 in x: | ||||
|                 f.write('   '+str(matrix[i0, j0])+'   ') | ||||
|                 j0 += 1 | ||||
|             f.write('\n') | ||||
|             i0 += 1    | ||||
|  | ||||
|  | ||||
| # 5. 画y(x)图,并保存到filename.jpg文件。具体画图格式可在函数中修改。 | ||||
| def plot(x, y, xlabel='x', ylabel='y', title='', filename='a', show=1, save=0):  | ||||
|     import matplotlib.pyplot as plt | ||||
|     fig, ax = plt.subplots() | ||||
|     plt.subplots_adjust(bottom=0.20, left=0.18)  | ||||
|     ax.plot(x, y, '-o') | ||||
|     ax.grid() | ||||
|     ax.set_title(title, fontsize=20, fontfamily='Times New Roman') | ||||
|     ax.set_xlabel(xlabel, fontsize=20, fontfamily='Times New Roman')  | ||||
|     ax.set_ylabel(ylabel, fontsize=20, fontfamily='Times New Roman')  | ||||
|     ax.tick_params(labelsize=20)  | ||||
|     labels = ax.get_xticklabels() + ax.get_yticklabels() | ||||
|     [label.set_fontname('Times New Roman') for label in labels] | ||||
|     if save == 1: | ||||
|         plt.savefig(filename+'.jpg', dpi=300)  | ||||
|     if show == 1: | ||||
|         plt.show() | ||||
|     plt.close('all') | ||||
|  | ||||
|  | ||||
|  | ||||
| # 6. 画3d_surface图,并保存到filename.jpg文件。具体画图格式可在函数中修改。 | ||||
| def plot_3d_surface(x, y, matrix, xlabel='x', ylabel='y', zlabel='z', title='', filename='a', show=1, save=0):  | ||||
|     import matplotlib.pyplot as plt | ||||
|     from matplotlib import cm | ||||
|     from matplotlib.ticker import LinearLocator | ||||
|     fig, ax = plt.subplots(subplot_kw={"projection": "3d"}) | ||||
|     plt.subplots_adjust(bottom=0.1, right=0.65)  | ||||
|     x, y = np.meshgrid(x, y) | ||||
|     if len(matrix.shape) == 2: | ||||
|         surf = ax.plot_surface(x, y, matrix, cmap=cm.coolwarm, linewidth=0, antialiased=False)  | ||||
|     elif len(matrix.shape) == 3: | ||||
|         for i0 in range(matrix.shape[2]): | ||||
|             surf = ax.plot_surface(x, y, matrix[:,:,i0], cmap=cm.coolwarm, linewidth=0, antialiased=False)  | ||||
|     ax.set_title(title, fontsize=20, fontfamily='Times New Roman') | ||||
|     ax.set_xlabel(xlabel, fontsize=20, fontfamily='Times New Roman')  | ||||
|     ax.set_ylabel(ylabel, fontsize=20, fontfamily='Times New Roman')  | ||||
|     ax.set_zlabel(zlabel, fontsize=20, fontfamily='Times New Roman')  | ||||
|     ax.zaxis.set_major_locator(LinearLocator(5))  | ||||
|     ax.zaxis.set_major_formatter('{x:.2f}')    | ||||
|     ax.tick_params(labelsize=15)  | ||||
|     labels = ax.get_xticklabels() + ax.get_yticklabels() + ax.get_zticklabels() | ||||
|     [label.set_fontname('Times New Roman') for label in labels]  | ||||
|     cax = plt.axes([0.80, 0.15, 0.05, 0.75])  | ||||
|     cbar = fig.colorbar(surf, cax=cax)   | ||||
|     cbar.ax.tick_params(labelsize=15) | ||||
|     for l in cbar.ax.yaxis.get_ticklabels(): | ||||
|         l.set_family('Times New Roman') | ||||
|     if save == 1: | ||||
|         plt.savefig(filename+'.jpg', dpi=300)  | ||||
|     if show == 1: | ||||
|         plt.show() | ||||
|     plt.close('all') | ||||
|  | ||||
|  | ||||
|  | ||||
| # 7. 画plot_contour图,并保存到filename.jpg文件。具体画图格式可在函数中修改。 | ||||
| def plot_contour(x, y, matrix, xlabel='x', ylabel='y', title='', filename='a', show=1, save=0):   | ||||
|     import matplotlib.pyplot as plt | ||||
|     from matplotlib import cm | ||||
|     from matplotlib.ticker import LinearLocator | ||||
|     fig, ax = plt.subplots() | ||||
|     plt.subplots_adjust(bottom=0.2, right=0.75, left = 0.16)  | ||||
|     x, y = np.meshgrid(x, y) | ||||
|     contour = ax.contourf(x,y,matrix,cmap='jet')  | ||||
|     ax.set_title(title, fontsize=20, fontfamily='Times New Roman') | ||||
|     ax.set_xlabel(xlabel, fontsize=20, fontfamily='Times New Roman')  | ||||
|     ax.set_ylabel(ylabel, fontsize=20, fontfamily='Times New Roman')  | ||||
|     ax.tick_params(labelsize=15)  | ||||
|     labels = ax.get_xticklabels() + ax.get_yticklabels() | ||||
|     [label.set_fontname('Times New Roman') for label in labels]  | ||||
|     cax = plt.axes([0.78, 0.17, 0.08, 0.71]) | ||||
|     cbar = fig.colorbar(contour, cax=cax)  | ||||
|     cbar.ax.tick_params(labelsize=15)  | ||||
|     for l in cbar.ax.yaxis.get_ticklabels(): | ||||
|         l.set_family('Times New Roman') | ||||
|     if save == 1: | ||||
|         plt.savefig(filename+'.jpg', dpi=300)  | ||||
|     if show == 1: | ||||
|         plt.show() | ||||
|     plt.close('all') | ||||
|  | ||||
|  | ||||
| # 8. 画2d_scatter图,并保存到filename.jpg文件。具体画图格式可在函数中修改! | ||||
| def plot_2d_scatter(x, y, value, xlabel='x', ylabel='y', title='', filename='a', show=1, save=0): | ||||
|     import matplotlib.pyplot as plt | ||||
|     from matplotlib.axes._axes import _log as matplotlib_axes_logger | ||||
|     matplotlib_axes_logger.setLevel('ERROR')  | ||||
|     fig = plt.figure() | ||||
|     ax = fig.add_subplot(111) | ||||
|     plt.subplots_adjust(bottom=0.2, right=0.8, left=0.2)  | ||||
|     for i in range(np.array(x).shape[0]): | ||||
|         ax.scatter(x[i], y[i], marker='o', s=100*value[i], c=(1,0,0)) | ||||
|     ax.set_title(title, fontsize=20, fontfamily='Times New Roman') | ||||
|     ax.set_xlabel(xlabel, fontsize=20, fontfamily='Times New Roman')  | ||||
|     ax.set_ylabel(ylabel, fontsize=20, fontfamily='Times New Roman')  | ||||
|     ax.tick_params(labelsize=15) | ||||
|     labels = ax.get_xticklabels() + ax.get_yticklabels()  | ||||
|     [label.set_fontname('Times New Roman') for label in labels] | ||||
|     if save == 1: | ||||
|         plt.savefig(filename+'.jpg', dpi=300)  | ||||
|     if show == 1: | ||||
|         plt.show() | ||||
|     plt.close('all') | ||||
|  | ||||
|  | ||||
| # 9. 画3d_scatter图,并保存到filename.jpg文件。具体画图格式可在函数中修改! | ||||
| def plot_3d_scatter(x, y, z, value, xlabel='x', ylabel='y', zlabel='z', title='', filename='a', show=1, save=0): | ||||
|     import matplotlib.pyplot as plt | ||||
|     from matplotlib.ticker import LinearLocator | ||||
|     from matplotlib.axes._axes import _log as matplotlib_axes_logger | ||||
|     matplotlib_axes_logger.setLevel('ERROR') | ||||
|     fig = plt.figure() | ||||
|     ax = fig.add_subplot(111, projection='3d') | ||||
|     plt.subplots_adjust(bottom=0.1, right=0.8)  | ||||
|     for i in range(np.array(x).shape[0]): | ||||
|         ax.scatter(x[i], y[i], z[i], marker='o', s=int(100*value[i]), c=(1,0,0)) | ||||
|     ax.set_title(title, fontsize=20, fontfamily='Times New Roman') | ||||
|     ax.set_xlabel(xlabel, fontsize=20, fontfamily='Times New Roman')  | ||||
|     ax.set_ylabel(ylabel, fontsize=20, fontfamily='Times New Roman')  | ||||
|     ax.set_zlabel(zlabel, fontsize=20, fontfamily='Times New Roman')  | ||||
|     ax.tick_params(labelsize=15)  | ||||
|     labels = ax.get_xticklabels() + ax.get_yticklabels() + ax.get_zticklabels() | ||||
|     [label.set_fontname('Times New Roman') for label in labels] | ||||
|     if save == 1: | ||||
|         plt.savefig(filename+'.jpg', dpi=300)  | ||||
|     if show == 1: | ||||
|         plt.show() | ||||
|     plt.close('all') | ||||
|  | ||||
|  | ||||
| # 10. 制作动画 | ||||
| def creat_animation(image_names, duration_time=0.5, filename='a'):   | ||||
|     import imageio | ||||
|     images = [] | ||||
|     for name in image_names: | ||||
|         image = name+'.jpg' | ||||
|         im = imageio.imread(image) | ||||
|         images.append(im) | ||||
|     imageio.mimsave(filename+'.gif', images, 'GIF', duration=duration_time)  # durantion是延迟时间 | ||||
|  | ||||
|  | ||||
| # 11. 在参数x下,计算matrix函数的本征值eigenvalue_array[:, index] | ||||
| def calculate_eigenvalue_with_one_parameter(x, matrix): | ||||
|     dim_x = np.array(x).shape[0] | ||||
|     i0 = 0 | ||||
|     if np.array(matrix(0)).shape==(): | ||||
|         eigenvalue_array = np.zeros((dim_x, 1)) | ||||
|         for x0 in x: | ||||
|             matrix0 = matrix(x0) | ||||
|             eigenvalue_array[i0, 0] = np.real(matrix0) | ||||
|             i0 += 1 | ||||
|     else: | ||||
|         dim = np.array(matrix(0)).shape[0] | ||||
|         eigenvalue_array = np.zeros((dim_x, dim)) | ||||
|         for x0 in x: | ||||
|             matrix0 = matrix(x0) | ||||
|             eigenvalue, eigenvector = np.linalg.eig(matrix0) | ||||
|             eigenvalue_array[i0, :] = np.sort(np.real(eigenvalue[:])) | ||||
|             i0 += 1 | ||||
|     return eigenvalue_array | ||||
|  | ||||
|  | ||||
| # 12. 在参数(x,y)下,计算matrix函数的本征值eigenvalue_array[:, :, index] | ||||
| def calculate_eigenvalue_with_two_parameters(x, y, matrix):   | ||||
|     dim_x = np.array(x).shape[0] | ||||
|     dim_y = np.array(y).shape[0] | ||||
|     if np.array(matrix(0,0)).shape==(): | ||||
|         eigenvalue_array = np.zeros((dim_y, dim_x, 1)) | ||||
|         i0 = 0 | ||||
|         for y0 in y: | ||||
|             j0 = 0 | ||||
|             for x0 in x: | ||||
|                 matrix0 = matrix(x0, y0) | ||||
|                 eigenvalue_array[i0, j0, 0] = np.real(matrix0) | ||||
|                 j0 += 1 | ||||
|             i0 += 1 | ||||
|     else: | ||||
|         dim = np.array(matrix(0, 0)).shape[0] | ||||
|         eigenvalue_array = np.zeros((dim_y, dim_x, dim)) | ||||
|         i0 = 0 | ||||
|         for y0 in y: | ||||
|             j0 = 0 | ||||
|             for x0 in x: | ||||
|                 matrix0 = matrix(x0, y0) | ||||
|                 eigenvalue, eigenvector = np.linalg.eig(matrix0) | ||||
|                 eigenvalue_array[i0, j0, :] = np.sort(np.real(eigenvalue[:])) | ||||
|                 j0 += 1 | ||||
|             i0 += 1 | ||||
|     return eigenvalue_array | ||||
|  | ||||
|  | ||||
| if __name__ == "__main__": | ||||
|     main() | ||||
| @@ -0,0 +1,21 @@ | ||||
| import os | ||||
| import time | ||||
|  | ||||
| start = time.time() | ||||
|  | ||||
| print('程序1开始的时间:', time.ctime()) | ||||
| start1 = time.time() | ||||
| os.chdir('D:')  # 代码位置 | ||||
| os.system('python a.py')  # 运行a.py | ||||
| end1 = time.time() | ||||
| print('程序1运行时间(min)=', (end1-start1)/60,'\n') | ||||
|  | ||||
| print('程序2开始的时间:', time.ctime()) | ||||
| start2 = time.time() | ||||
| os.chdir('E:')  # 代码位置 | ||||
| os.system('python b.py')  # 运行b.py | ||||
| end2 = time.time() | ||||
| print('程序2运行时间(min)=', (end2-start2)/60, '\n') | ||||
|  | ||||
| end = time.time() | ||||
| print('总运行时间(min)=', (end-start)/60) | ||||
| @@ -0,0 +1,137 @@ | ||||
| """ | ||||
| This code is supported by the website: https://www.guanjihuan.com | ||||
| The newest version of this code is on the web page: https://www.guanjihuan.com/archives/9129 | ||||
| """ | ||||
|  | ||||
| import os | ||||
| import re  | ||||
| import time | ||||
| import logging  | ||||
| logging.Logger.propagate = False  | ||||
| logging.getLogger().setLevel(logging.ERROR)  # 只显示error级别的通知 | ||||
|  | ||||
|  | ||||
| def main(): | ||||
|     # 参数 | ||||
|     key_word_array = ['photonic', 'Berry phase'] | ||||
|     original_path = 'D:\\文献' | ||||
|      | ||||
|     # 查找所有的PDF文件路径 | ||||
|     pdf_file_all = find_files_pdf(original_path) | ||||
|     print('\n该文件夹下总共有', len(pdf_file_all), '个PDF文件。\n') | ||||
|      | ||||
|     f = open('error.txt','w',encoding='utf-8') | ||||
|     f.close() | ||||
|     for key_word in key_word_array: | ||||
|         f = open(str(key_word)+'.txt','w',encoding='utf-8') | ||||
|         f.write('该文件夹下总共有'+str(len(pdf_file_all))+'个PDF文件。\n') | ||||
|         f.close() | ||||
|  | ||||
|     # 查找包含关键词的PDF文件 | ||||
|     i0 = 1 | ||||
|     begin = time.time() | ||||
|     for pdf_file in pdf_file_all: | ||||
|         print('查找第', i0, '个文件,', end='') | ||||
|         begin0 = time.time() | ||||
|         try:  | ||||
|             content = get_text_from_pdf(pdf_file) | ||||
|             for key_word in key_word_array: | ||||
|                 if re.search(re.compile(key_word),content): | ||||
|                     print('发现文件!关键词', key_word, '对应的文件位置在:\n\n', pdf_file, '\n') | ||||
|                     with open(str(key_word)+'.txt','a',encoding='utf-8') as f: | ||||
|                         f.write('\n查找第'+str(i0)+'个文件时发现文件!位置在:\n'+pdf_file+'\n') | ||||
|         except:  | ||||
|             print('出现异常!位置在:\n\n', pdf_file, '\n') | ||||
|             with open('error.txt','a',encoding='utf-8') as f: | ||||
|                 f.write('\n解析第'+str(i0)+'个文件时出现异常!位置在:\n'+pdf_file+'\n') | ||||
|         end0 = time.time() | ||||
|         print('用时', end0-begin0, '秒') | ||||
|         i0 += 1 | ||||
|     print('\n全部搜索结束!') | ||||
|     end = time.time() | ||||
|     print('\n总共用时:', (end-begin)/60, '分') | ||||
|  | ||||
|  | ||||
| def find_files_pdf(path):  # 查找所有PDF文件 | ||||
|     file_all = find_files(path) | ||||
|     pdf_file_all = [] | ||||
|     for file0 in file_all: | ||||
|         if re.search(re.compile('^fdp.'),file0[::-1]): # 如果文件是以.pdf结尾 | ||||
|             pdf_file_all.append(file0) | ||||
|     return pdf_file_all | ||||
|  | ||||
|  | ||||
| def find_files(path):  # 查找所有文件 | ||||
|     file_all = [] | ||||
|     path_next_loop = [path] | ||||
|     for i in range(10000):  # i为文件在文件夹中的深度 | ||||
|         file_all_in_one_loop, path_next_loop = find_files_loop_module(path_next_loop) | ||||
|         for file_in_one_loop in file_all_in_one_loop: | ||||
|             file_all.append(file_in_one_loop) | ||||
|         if path_next_loop == []: | ||||
|             break | ||||
|     return file_all | ||||
|  | ||||
|  | ||||
| def find_files_loop_module(path_all): # 查找文件的一个循环模块 | ||||
|     file_all_in_one_loop = [] | ||||
|     path_next_loop = [] | ||||
|     for path in path_all: | ||||
|         filenames = os.listdir(path) | ||||
|         for filename in filenames: | ||||
|             filename = os.path.join(path,filename)  | ||||
|             if os.path.isfile(filename): # 如果是文件 | ||||
|                 file_all_in_one_loop.append(filename)  | ||||
|             else:  # 如果是文件夹 | ||||
|                 path_next_loop.append(filename) | ||||
|     return file_all_in_one_loop, path_next_loop | ||||
|  | ||||
|  | ||||
| def get_text_from_pdf(file_path):  # 从PDF中获取文本 | ||||
|     from pdfminer.pdfparser import PDFParser, PDFDocument | ||||
|     from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter | ||||
|     from pdfminer.converter import PDFPageAggregator | ||||
|     from pdfminer.layout import LAParams, LTTextBox | ||||
|     from pdfminer.pdfinterp import PDFTextExtractionNotAllowed | ||||
|  | ||||
|     # 用文件对象来创建一个pdf文档分析器 | ||||
|     praser = PDFParser(open(file_path, 'rb')) | ||||
|     # 创建一个PDF文档 | ||||
|     doc = PDFDocument() | ||||
|     # 连接分析器 与文档对象 | ||||
|     praser.set_document(doc) | ||||
|     doc.set_parser(praser) | ||||
|  | ||||
|     # 提供初始化密码 | ||||
|     # 如果没有密码 就创建一个空的字符串 | ||||
|     doc.initialize() | ||||
|  | ||||
|     # 检测文档是否提供txt转换,不提供就忽略 | ||||
|     if not doc.is_extractable: | ||||
|         raise PDFTextExtractionNotAllowed | ||||
|     else: | ||||
|         # 创建PDf 资源管理器 来管理共享资源 | ||||
|         rsrcmgr = PDFResourceManager() | ||||
|         # 创建一个PDF设备对象 | ||||
|         laparams = LAParams() | ||||
|         device = PDFPageAggregator(rsrcmgr, laparams=laparams) | ||||
|         # 创建一个PDF解释器对象 | ||||
|         interpreter = PDFPageInterpreter(rsrcmgr, device) | ||||
|  | ||||
|         # 循环遍历列表,每次处理一个page的内容 | ||||
|         content = '' | ||||
|         for page in doc.get_pages(): | ||||
|             interpreter.process_page(page)                         | ||||
|             # 接受该页面的LTPage对象 | ||||
|             layout = device.get_result() | ||||
|             # 这里layout是一个LTPage对象,里面存放着这个 page 解析出的各种对象 | ||||
|             # 包括 LTTextBox, LTFigure, LTImage, LTTextBoxHorizontal 等                             | ||||
|             for x in layout: | ||||
|                 if isinstance(x, LTTextBox): | ||||
|                     # print(x.get_text().strip()) | ||||
|                     content  = content + x.get_text().strip() | ||||
|     return content | ||||
|  | ||||
|  | ||||
| if __name__ == "__main__": | ||||
|     main() | ||||
| @@ -0,0 +1,63 @@ | ||||
| import os | ||||
| os.chdir('D:/')  # PDF文件存放的位置 | ||||
| import logging  | ||||
| logging.Logger.propagate = False  | ||||
| logging.getLogger().setLevel(logging.ERROR)  # 只显示error级别的通知 | ||||
|  | ||||
|  | ||||
| def main(): | ||||
|     content = get_text_from_pdf('a') | ||||
|     with open('a.txt', 'w', encoding='utf-8') as f: | ||||
|         f.write(content) | ||||
|  | ||||
|  | ||||
| def get_text_from_pdf(filename): | ||||
|     from pdfminer.pdfparser import PDFParser, PDFDocument | ||||
|     from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter | ||||
|     from pdfminer.converter import PDFPageAggregator | ||||
|     from pdfminer.layout import LAParams, LTTextBox | ||||
|     from pdfminer.pdfinterp import PDFTextExtractionNotAllowed | ||||
|  | ||||
|     path = filename+".pdf" | ||||
|  | ||||
|     # 用文件对象来创建一个pdf文档分析器 | ||||
|     praser = PDFParser(open(path, 'rb')) | ||||
|     # 创建一个PDF文档 | ||||
|     doc = PDFDocument() | ||||
|     # 连接分析器 与文档对象 | ||||
|     praser.set_document(doc) | ||||
|     doc.set_parser(praser) | ||||
|  | ||||
|     # 提供初始化密码 | ||||
|     # 如果没有密码 就创建一个空的字符串 | ||||
|     doc.initialize() | ||||
|  | ||||
|     # 检测文档是否提供txt转换,不提供就忽略 | ||||
|     if not doc.is_extractable: | ||||
|         raise PDFTextExtractionNotAllowed | ||||
|     else: | ||||
|         # 创建PDf 资源管理器 来管理共享资源 | ||||
|         rsrcmgr = PDFResourceManager() | ||||
|         # 创建一个PDF设备对象 | ||||
|         laparams = LAParams() | ||||
|         device = PDFPageAggregator(rsrcmgr, laparams=laparams) | ||||
|         # 创建一个PDF解释器对象 | ||||
|         interpreter = PDFPageInterpreter(rsrcmgr, device) | ||||
|  | ||||
|         # 循环遍历列表,每次处理一个page的内容 | ||||
|         content = '' | ||||
|         for page in doc.get_pages(): | ||||
|             interpreter.process_page(page)                         | ||||
|             # 接受该页面的LTPage对象 | ||||
|             layout = device.get_result() | ||||
|             # 这里layout是一个LTPage对象,里面存放着这个 page 解析出的各种对象 | ||||
|             # 包括 LTTextBox, LTFigure, LTImage, LTTextBoxHorizontal 等                             | ||||
|             for x in layout: | ||||
|                 if isinstance(x, LTTextBox): | ||||
|                     # print(x.get_text().strip()) | ||||
|                     content  = content + x.get_text().strip() | ||||
|     return content | ||||
|  | ||||
|  | ||||
| if __name__ == "__main__": | ||||
|     main() | ||||
| @@ -0,0 +1,33 @@ | ||||
| """ | ||||
| This code is supported by the website: https://www.guanjihuan.com | ||||
| """ | ||||
|  | ||||
| from urllib import response | ||||
| from bs4 import BeautifulSoup | ||||
| import re | ||||
| import requests | ||||
| import urllib.request | ||||
| import os | ||||
| import ssl | ||||
| from urllib.request import urlopen | ||||
|  | ||||
| ssl._create_default_https_context = ssl._create_unverified_context | ||||
| html = urllib.request.urlopen("https://www.guanjihuan.com/archives/10897").read().decode('utf-8') | ||||
| soup = BeautifulSoup(html, features='lxml') | ||||
| all_a_tag = soup.find_all('a', href=True) | ||||
| for a_tag in all_a_tag: | ||||
|     href = a_tag['href'] | ||||
|     if re.search('https://www.merriam-webster.com/dictionary/', href): | ||||
|         print(href[43:]) | ||||
|         exist = os.path.exists('prons/'+href[43:]+'.mp3') | ||||
|         if exist: | ||||
|             continue | ||||
|         header = {'User-Agent':'Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'}   # 头部信息 | ||||
|         html = urlopen(href).read().decode('utf-8') | ||||
|         mp3_file = re.findall('https://media.merriam-webster.com/audio/prons/en/us/mp3/.*.mp3",', html, re.S)[0][:-2] | ||||
|         print(mp3_file[:-2]) | ||||
|         print() | ||||
|         r = requests.get(mp3_file, headers=header, stream=True) | ||||
|         with open('prons/'+href[43:]+'.mp3', 'wb') as f: | ||||
|             for chunk in r.iter_content(chunk_size=32): | ||||
|                 f.write(chunk) | ||||
| @@ -0,0 +1,8 @@ | ||||
| # Words in webpage: https://www.guanjihuan.com/archives/10897 | ||||
| # installation: | ||||
| # (1) pip install pygame | ||||
| # (2) pip install --upgrade guan | ||||
|  | ||||
| import guan | ||||
| guan.play_element_words() | ||||
| # guan.play_element_words(random_on=0, show_translation=1, show_link=1, translation_time=2, rest_time=1) | ||||
| @@ -0,0 +1,29 @@ | ||||
| import numpy as np | ||||
|  | ||||
| # 设置 | ||||
| cpus = 7  # 使用的CPU个数(等于提交任务的个数) | ||||
| parameter_array_all = np.arange(0, 10, 0.1) # 需要计算的参数 | ||||
|  | ||||
|  | ||||
| # 通过.sh脚本文件修改的任务指标。job_index从0开始,最大值为cpus-1 | ||||
| job_index = -1 | ||||
|  | ||||
|  | ||||
| # 预处理 | ||||
| len_of_parameter_all = len(parameter_array_all)  # 需要计算参数的个数 | ||||
| if len_of_parameter_all%cpus == 0: | ||||
| 	len_parameter = int(len_of_parameter_all/cpus) # 一个CPU/任务需要计算参数的个数 | ||||
| 	parameter_array = parameter_array_all[job_index*len_parameter:(job_index+1)*len_parameter] | ||||
| else: | ||||
| 	len_parameter = int(len_of_parameter_all/(cpus-1)) # 一个CPU/任务需要计算参数的个数 | ||||
| 	if job_index != cpus-1: | ||||
| 		parameter_array = parameter_array_all[job_index*len_parameter:(job_index+1)*len_parameter] | ||||
| 	else: | ||||
| 		parameter_array = parameter_array_all[job_index*len_parameter:len_of_parameter_all] | ||||
|  | ||||
|  | ||||
| # 任务 | ||||
| with open('a'+str(job_index)+'.txt', 'w') as f: | ||||
| 	for parameter in parameter_array: | ||||
| 		result = parameter**2 | ||||
| 		f.write(str(parameter)+'            '+str(result)+'\n') | ||||
| @@ -0,0 +1,5 @@ | ||||
| #!/bin/sh | ||||
| #PBS -N task | ||||
| #PBS -l nodes=1:ppn=1 | ||||
| export OMP_NUM_THREADS=1 | ||||
| python a.py | ||||
| @@ -0,0 +1,6 @@ | ||||
| f = open('combine.txt', 'w') | ||||
| for job_index in range(7): | ||||
|     with open('a'+str(job_index)+'.txt', 'r') as f0: | ||||
|         text = f0.read() | ||||
|     f.write(text) | ||||
| f.close() | ||||
| @@ -0,0 +1,14 @@ | ||||
| #!/bin/sh | ||||
|  | ||||
| for ((job_index=0; job_index<7; job_index++)) | ||||
| do | ||||
|  | ||||
| cp a.py a${job_index}.py | ||||
| sed -i "s/job_index = -1/job_index = ${job_index}/" a${job_index}.py | ||||
|  | ||||
|  | ||||
| cp a.sh a${job_index}.sh | ||||
| sed -i "s/python a.py/python a${job_index}.py/" a${job_index}.sh | ||||
| qsub a${job_index}.sh | ||||
|  | ||||
| done | ||||
| @@ -0,0 +1,88 @@ | ||||
| """ | ||||
| This code is supported by the website: https://www.guanjihuan.com | ||||
| The newest version of this code is on the web page: https://www.guanjihuan.com/archives/13623 | ||||
| """ | ||||
|  | ||||
| from bs4 import BeautifulSoup | ||||
| from urllib.request import urlopen | ||||
| import re   | ||||
| from collections import Counter | ||||
| import datetime | ||||
| import random | ||||
| import time | ||||
|  | ||||
|  | ||||
| # time.sleep(random.uniform(0,1800))  # 爬虫简单伪装,在固定时间后0到30分钟后开始运行。调试的时候把该语句注释。 | ||||
| year = datetime.datetime.now().year | ||||
| month = datetime.datetime.now().month | ||||
| day = datetime.datetime.now().day | ||||
|  | ||||
|  | ||||
| # 获取链接 | ||||
| try: | ||||
|     with open('prb_link_list.txt', 'r', encoding='UTF-8') as f:  # 如果文件存在 | ||||
|         link_list = f.read().split('\n')   # 历史已经访问过的链接(数组类型) | ||||
| except: | ||||
|     with open('prb_link_list.txt', 'w', encoding='UTF-8') as f:  # 如果文件不存在 | ||||
|         link_list = []  | ||||
| f = open('prb_link_list.txt', 'a', encoding='UTF-8')  # 打开文件(补充) | ||||
| f.write('\nLink list obtained on '+str(year)+'.'+str(month).rjust(2,'0')+'.'+str(day).rjust(2,'0')+':\n') | ||||
| match_href = []  # 在本次运行中满足条件的链接 | ||||
| for loop in range(3): | ||||
|     if loop == 0: | ||||
|         start_link = "https://journals.aps.org/prb/recent?page=1"  # 看第一页 | ||||
|     elif loop == 1: | ||||
|         start_link = "https://journals.aps.org/prb/recent?page=2"  # 看第二页 | ||||
|     elif loop == 2:  | ||||
|         start_link = "https://journals.aps.org/prb/recent?page=3"  # 看第三页(三页基本上覆盖了当天的所有更新) | ||||
|     html = urlopen(start_link).read().decode('utf-8')  # 打开网页 | ||||
|     soup = BeautifulSoup(html, features='lxml') # 放入soup中 | ||||
|     all_a_tag = soup.find_all('a', href=True)  # 获取超链接标签 | ||||
|     for a_tag in all_a_tag: | ||||
|         href = a_tag['href']  # 超链接字符串 | ||||
|         if re.search('/abstract/', href): # 文章的链接 | ||||
|             if re.search('https://journals.aps.org', href)==None:  # 如果链接不是完整的,那么补充完整 | ||||
|                 href = 'https://journals.aps.org'+ href | ||||
|             if href not in match_href and href not in link_list and re.search('\?', href)==None:  # 链接不重复 | ||||
|                 match_href.append(href) | ||||
|                 f.write(href+'\n') | ||||
| f.close() | ||||
|  | ||||
|  | ||||
|  | ||||
| # 获取摘要 | ||||
| try: | ||||
|     f = open('prb_all.txt', 'a', encoding='UTF-8')  # 全部记录 | ||||
| except: | ||||
|     f = open('prb_all.txt', 'w', encoding='UTF-8')  # 如果文件不存在 | ||||
| try: | ||||
|     f_month = open('prb_'+str(year)+'.'+str(month).rjust(2,'0')+'.txt', 'a', encoding='UTF-8')  # 一个月的记录 | ||||
| except: | ||||
|     f_month = open('prb_'+str(year)+'.'+str(month).rjust(2,'0')+'.txt', 'w', encoding='UTF-8')  # 如果文件不存在 | ||||
| f.write('\n\n['+str(year)+'.'+str(month).rjust(2,'0')+'.'+str(day).rjust(2,'0')+'][total number='+str(len(match_href))+']\n\n\n') | ||||
| f_month.write('\n\n['+str(year)+'.'+str(month).rjust(2,'0')+'.'+str(day).rjust(2,'0')+'][total number='+str(len(match_href))+']\n\n\n') | ||||
| print('total number=', len(match_href))  # 调试的时候显示这个 | ||||
| i00 = 0 | ||||
| for href in match_href:  | ||||
|     i00 += 1 | ||||
|     print('reading number', i00, '...')  # 调试的时候显示这个 | ||||
|     # time.sleep(random.uniform(10,110))  # 爬虫简单伪装,休息一分钟左右。如果链接个数有60个,那么程序运行时间延长60分钟。调试的时候把该语句注释。 | ||||
|     try: | ||||
|         html = urlopen(href).read().decode('utf-8')   # 打开文章链接 | ||||
|         soup = BeautifulSoup(html, features='lxml') # 放入soup中 | ||||
|         title = soup.title   # 文章标题 | ||||
|         f.write(str(title.get_text())+'\n\n')    | ||||
|         f_month.write(str(title.get_text())+'\n\n')  | ||||
|         f.write(str(href)+'\n\n')   # 文章链接 | ||||
|         f_month.write(str(href)+'\n\n')  | ||||
|         abstract = re.findall('"yes"><p>.*</p><div', html, re.S)[0][9:-8]  # 文章摘要 | ||||
|         word_list = abstract.split(' ')  # 划分单词 | ||||
|         for word in word_list: | ||||
|             if re.search('<', word)==None and re.search('>', word)==None:  # 有些内容满足过滤条件,因此信息可能会丢失。 | ||||
|                 f.write(word+' ') | ||||
|                 f_month.write(word+' ') | ||||
|         f.write('\n\n\n') | ||||
|         f_month.write('\n\n\n') | ||||
|     except: | ||||
|         pass | ||||
| f.close() | ||||
| @@ -0,0 +1,88 @@ | ||||
| """ | ||||
| This code is supported by the website: https://www.guanjihuan.com | ||||
| The newest version of this code is on the web page: https://www.guanjihuan.com/archives/13623 | ||||
| """ | ||||
|  | ||||
| from bs4 import BeautifulSoup | ||||
| from urllib.request import urlopen | ||||
| import re   | ||||
| from collections import Counter | ||||
| import datetime | ||||
| import random | ||||
| import time | ||||
|  | ||||
|  | ||||
| # time.sleep(random.uniform(0,1800))  # 爬虫简单伪装,在固定时间后0到30分钟后开始运行。调试的时候把该语句注释。 | ||||
| year = datetime.datetime.now().year | ||||
| month = datetime.datetime.now().month | ||||
| day = datetime.datetime.now().day | ||||
|  | ||||
|  | ||||
| # 获取链接 | ||||
| try: | ||||
|     with open('prl_link_list.txt', 'r', encoding='UTF-8') as f:  # 如果文件存在 | ||||
|         link_list = f.read().split('\n')   # 历史已经访问过的链接(数组类型) | ||||
| except: | ||||
|     with open('prl_link_list.txt', 'w', encoding='UTF-8') as f:  # 如果文件不存在 | ||||
|         link_list = []  | ||||
| f = open('prl_link_list.txt', 'a', encoding='UTF-8')  # 打开文件(补充) | ||||
| f.write('\nLink list obtained on '+str(year)+'.'+str(month).rjust(2,'0')+'.'+str(day).rjust(2,'0')+':\n') | ||||
| match_href = []  # 在本次运行中满足条件的链接 | ||||
| for loop in range(3): | ||||
|     if loop == 0: | ||||
|         start_link = "https://journals.aps.org/prl/recent?page=1"  # 看第一页 | ||||
|     elif loop == 1: | ||||
|         start_link = "https://journals.aps.org/prl/recent?page=2"  # 看第二页 | ||||
|     elif loop == 2:  | ||||
|         start_link = "https://journals.aps.org/prl/recent?page=3"  # 看第三页(三页基本上覆盖了当天的所有更新) | ||||
|     html = urlopen(start_link).read().decode('utf-8')  # 打开网页 | ||||
|     soup = BeautifulSoup(html, features='lxml') # 放入soup中 | ||||
|     all_a_tag = soup.find_all('a', href=True)  # 获取超链接标签 | ||||
|     for a_tag in all_a_tag: | ||||
|         href = a_tag['href']  # 超链接字符串 | ||||
|         if re.search('/abstract/', href): # 文章的链接 | ||||
|             if re.search('https://journals.aps.org', href)==None:  # 如果链接不是完整的,那么补充完整 | ||||
|                 href = 'https://journals.aps.org'+ href | ||||
|             if href not in match_href and href not in link_list and re.search('\?', href)==None:  # 链接不重复 | ||||
|                 match_href.append(href) | ||||
|                 f.write(href+'\n') | ||||
| f.close() | ||||
|  | ||||
|  | ||||
|  | ||||
| # 获取摘要 | ||||
| try: | ||||
|     f = open('prl_all.txt', 'a', encoding='UTF-8')  # 全部记录 | ||||
| except: | ||||
|     f = open('prl_all.txt', 'w', encoding='UTF-8')  # 如果文件不存在 | ||||
| try: | ||||
|     f_month = open('prl_'+str(year)+'.'+str(month).rjust(2,'0')+'.txt', 'a', encoding='UTF-8')  # 一个月的记录 | ||||
| except: | ||||
|     f_month = open('prl_'+str(year)+'.'+str(month).rjust(2,'0')+'.txt', 'w', encoding='UTF-8')  # 如果文件不存在 | ||||
| f.write('\n\n['+str(year)+'.'+str(month).rjust(2,'0')+'.'+str(day).rjust(2,'0')+'][total number='+str(len(match_href))+']\n\n\n') | ||||
| f_month.write('\n\n['+str(year)+'.'+str(month).rjust(2,'0')+'.'+str(day).rjust(2,'0')+'][total number='+str(len(match_href))+']\n\n\n') | ||||
| print('total number=', len(match_href))  # 调试的时候显示这个 | ||||
| i00 = 0 | ||||
| for href in match_href:  | ||||
|     i00 += 1 | ||||
|     print('reading number', i00, '...')  # 调试的时候显示这个 | ||||
|     # time.sleep(random.uniform(10,110))  # 爬虫简单伪装,休息一分钟左右。如果链接个数有60个,那么程序运行时间延长60分钟。调试的时候把该语句注释。 | ||||
|     try: | ||||
|         html = urlopen(href).read().decode('utf-8')   # 打开文章链接 | ||||
|         soup = BeautifulSoup(html, features='lxml') # 放入soup中 | ||||
|         title = soup.title   # 文章标题 | ||||
|         f.write(str(title.get_text())+'\n\n')    | ||||
|         f_month.write(str(title.get_text())+'\n\n')  | ||||
|         f.write(str(href)+'\n\n')   # 文章链接 | ||||
|         f_month.write(str(href)+'\n\n')  | ||||
|         abstract = re.findall('"yes"><p>.*</p><div', html, re.S)[0][9:-8]  # 文章摘要 | ||||
|         word_list = abstract.split(' ')  # 划分单词 | ||||
|         for word in word_list: | ||||
|             if re.search('<', word)==None and re.search('>', word)==None:  # 有些内容满足过滤条件,因此信息可能会丢失。 | ||||
|                 f.write(word+' ') | ||||
|                 f_month.write(word+' ') | ||||
|         f.write('\n\n\n') | ||||
|         f_month.write('\n\n\n') | ||||
|     except: | ||||
|         pass | ||||
| f.close() | ||||
| @@ -0,0 +1,41 @@ | ||||
| """ | ||||
| This code is supported by the website: https://www.guanjihuan.com | ||||
| The newest version of this code is on the web page: https://www.guanjihuan.com/archives/13623 | ||||
| """ | ||||
|  | ||||
| import re   | ||||
| from collections import Counter | ||||
|  | ||||
|  | ||||
| def main(): | ||||
|     file_name = 'prb_all.txt' | ||||
|     with open(file_name, 'r', encoding='UTF-8') as f:  # 打开文件 | ||||
|         paper_list = f.read().split('\n\n\n')  # 通过三个回车划分不同文章 | ||||
|     word_list = []   | ||||
|     ignore = ignore_words()  # 过滤常见单词 | ||||
|     for paper in paper_list: | ||||
|         word_list_in_one_paper = [] | ||||
|         if len(paper)>20:  # 通过字符串长度过滤日期 | ||||
|             content_list = paper.split('\n\n')  # 通过两个回车划分内容 | ||||
|             for content in content_list: | ||||
|                 if re.search('https://', content)==None: # 过滤文章链接 | ||||
|                     words = content.split(' ')  # 通过空格划分单词 | ||||
|                     for word in words: | ||||
|                         if word not in word_list_in_one_paper:  # 一篇文章的某个单词只统计一次 | ||||
|                             if word not in ignore and len(word)>1:  # 过滤词汇 | ||||
|                                 word_list.append(word) | ||||
|                                 word_list_in_one_paper.append(word)        | ||||
|     num = 300 | ||||
|     most_common_words = Counter(word_list).most_common(num)  # 统计出现最多的num个词汇 | ||||
|     print('\n出现频率最高的前', num, '个词汇:') | ||||
|     for word in most_common_words: | ||||
|         print(word) | ||||
|  | ||||
|  | ||||
| def ignore_words(): # 可自行增删 | ||||
|     ignore = ['Phys.', 'the', 'to', 'of', 'in', 'under', 'and', 'by', 'The', 'at', 'with', 'up', 'be', 'above', 'below', 'are', 'is', 'for', 'that', 'as', 'we', '<a', 'abstract', 'abstract"','<span', 'which', 'We', 'such', 'has', 'two', 'these', 'it', 'all', 'results', 'result', 'each', 'have', 'between', 'on', 'an', 'can', 'also', 'from', 'Our', 'our', 'using', 'where', 'These', 'out', 'both', 'due', 'less', 'along', 'but', 'In', 'show', 'into', 'study', 'find', 'provide', 'change','not', 'open', 'this', 'show', 'into', 'study', 'find', 'provide', 'change', 'present', 'Using', 'large', 'This', 'However', 'appear', 'studied', 'obtain', 'been', 'Both', 'they', 'effects', 'effect', 'compute', 'more', 'does', 'shown', 'Based', 'reveal', 'highly', 'number', 'However,', 'was', 'near', 'full', 'based', 'several', 'suggest', 'agreement', 'predicted', 'values', 'work', 'emphasize', 'without', 'or', 'work,', 'studies', 'future', 'identify', 'present.', 'predict', 'presence', 'their', 'were', 'From', 'its', 'By', 'how', 'ground', 'observed', 'recent', 'For', 'other', 'Here', 'test', 'further', 'Its', 'similar', 'however,', 'range', 'within', 'value', 'possible', 'may', 'than', 'low', 'us', 'obtained', 'around', 'consider', 'about', 'very', 'will', 'when', 'played', 'consist', 'consists', 'Here,', 'observe', 'gives', 'It', 'over', 'cannot', 'As', 'whose', 'new', 'some', 'only', 'from', 'yields', 'shows', 'data', 'direct', 'related', 'different', 'evidence', 'role', 'function', 'origin', 'specific', 'set', 'confirm', 'give', 'Moreover', 'develop', 'including', 'could', 'used', 'means', 'allows', 'make', 'e.g.,', 'provides', 'system', 'systems', 'field', 'fields', 'model', 'model,', 'state', 'states', 'states.', 'state.', 'band', 'bands', 'method', 'methods', 'nature', 'rate', 'zero', 'single', 'theory', 'first', 'one', 'complex', 'approach', 'schemes', 'terms', 'even', 'case', 'analysis', 'weight', 'volume', 'evolution', 'well', 'external', 'measured', 'introducing', 'dependence', 'properties', 'demonstrate', 'remains', 'through', 'measurements', 'samples', 'findings', 'respect', 'investigate', 'behavior', 'importance', 'considered', 'experimental', 'increase', 'propose', 'follows', 'increase', 'emerged', 'interesting', 'behaviors', 'influenced', 'paramount', 'indicate', 'Rev.', 'concepts', 'induced', 'zone', 'regions', 'exact', 'contribution', 'behavior', 'formation', 'measurements.', 'utilizing', 'constant', 'regime', 'features', 'strength', 'compare', 'determined', 'combination', 'compare', 'determined', 'At', 'inside', 'ambient', 'then', 'important', 'report', 'Moreover,', 'Despite', 'found', 'because', 'process', 'and,', 'significantly', 'realized', 'much', 'natural', 'since', 'grows', 'any', 'compared', 'while', 'forms.', 'appears', 'indicating', 'coefficient', 'suggested', 'time', 'exhibits', 'calculations.', 'developed', 'array', 'discuss', 'field', 'becomes', 'allowing', 'indicates', 'via', 'introduce', 'considering', 'times.', 'constructed', 'explain', 'form', 'owing', 'parameters.', 'parameter', 'operation', 'probe', 'experiments', 'interest', 'strategies', 'seen', 'emerge', 'generic', 'geometry', 'numbers', 'observation', 'avenue', 'theretically', 'three', 'excellent', 'amount', 'notable', 'example', 'being', 'promising', 'latter', 'little', 'imposed', 'put', 'resource', 'together', 'produce', 'successfully','there', 'enhanced', 'this', 'great', 'dirven', 'increasing','should', 'otherwise', 'Further', 'field,', 'known', 'changes', 'still', 'beyond', 'various', 'center', 'previously', 'way', 'peculiar', 'detailed', 'understanding', 'good', 'years', 'where', 'Me', 'origins', 'years.', 'attributed', 'known,', 'them', 'reported', 'no', 'systems', 'agree', 'examined', 'rise', 'calculate', 'those', 'particular', 'relation', 'defined', 'either', 'again', 'current', 'exhibit', 'calculated', 'here', 'made', 'Further', 'consisting', 'constitutes', 'originated', 'if', 'exceed', 'access'] | ||||
|     return ignore | ||||
|  | ||||
|  | ||||
| if __name__ == '__main__': | ||||
|     main() | ||||
| @@ -0,0 +1,88 @@ | ||||
| """ | ||||
| This code is supported by the website: https://www.guanjihuan.com | ||||
| The newest version of this code is on the web page: https://www.guanjihuan.com/archives/14684 | ||||
| """ | ||||
|  | ||||
| import sympy | ||||
|  | ||||
|  | ||||
| # 定义符号 | ||||
| print() | ||||
| print('定义符号:') | ||||
| x, y, z = sympy.symbols('x y z')  # 使用sympy.symbols | ||||
| print(x) | ||||
| print(y+1) | ||||
| print(z**2) | ||||
| print() | ||||
|  | ||||
|  | ||||
| # 替换(Substitution) | ||||
| print('变量替换:') | ||||
| expression_1 = x**2+1 | ||||
| value_1 = expression_1.subs(x, 3)  # 使用.subs()方法 | ||||
| print(value_1) | ||||
| print() | ||||
|  | ||||
|  | ||||
| # 字符串转成符号表达式 | ||||
| print('字符串转成符号表达式:') | ||||
| expression_string = 'x**3+1' | ||||
| print(expression_string) | ||||
| expression_2 = sympy.sympify(expression_string)  # 使用sympy.sympify() | ||||
| print(expression_2) | ||||
| value_2 = expression_2.subs(x, 2) | ||||
| print(value_2) | ||||
| print() | ||||
|  | ||||
|  | ||||
| # 简化表达式 | ||||
| print('简化表达式:') | ||||
| expression_3 = sympy.simplify(sympy.sin(x)**2 + sympy.cos(x)**2)  # 使用sympy.simplify() | ||||
| print(expression_3) | ||||
| print() | ||||
|  | ||||
|  | ||||
| # 符号矩阵 | ||||
| print('符号矩阵:') | ||||
| a, b = sympy.symbols('a b') | ||||
| matrix = sympy.Matrix([[1, a], [0, b]])   # 使用sympy.Matrix() | ||||
| print(matrix) | ||||
| print() | ||||
|  | ||||
|  | ||||
| sympy.init_printing(use_unicode=True) | ||||
| # 符号矩阵的特征值和特征向量 | ||||
| print('符号矩阵的特征值和特征向量:') | ||||
| eigenvalue = matrix.eigenvals()  # 使用.eigenvals()方法 | ||||
| print('特征值\n', eigenvalue, '\n') | ||||
| sympy.pprint(eigenvalue)               # 使用sympy.pprint()输出 | ||||
| print('\n', sympy.pretty(eigenvalue))  # 使用sympy.pretty()美化后输出 | ||||
| print() | ||||
|  | ||||
| eigenvector = matrix.eigenvects() # 使用.eigenvects()方法 | ||||
| print('特征向量\n', eigenvector, '\n') | ||||
| sympy.pprint(eigenvector) | ||||
| print('\n', sympy.pretty(eigenvector))  | ||||
| print() | ||||
|  | ||||
| P, D = matrix.diagonalize()  # 使用.diagonalize()方法 | ||||
| print('特征值\n', D, '\n') | ||||
| print(sympy.pretty(D), '\n') | ||||
| print('特征向量\n', P, '\n') | ||||
| print(sympy.pretty(P), '\n') | ||||
| print() | ||||
|  | ||||
| print('特征值\n', D.subs(a, -4).subs(b, 2), '\n') | ||||
| print(sympy.pretty(D.subs(a, -4).subs(b, 2)), '\n') | ||||
| print('特征向量\n', P.subs(a, -4).subs(b, 2), '\n') | ||||
| print(sympy.pretty(P.subs(a, -4).subs(b, 2)), '\n') | ||||
| print() | ||||
|  | ||||
| # 和numpy对比 | ||||
| print('和numpy对比:') | ||||
| import numpy as np | ||||
| matrix = np.array([[1, -4], [0, 2]]) | ||||
| eigenvalue, eigenvector = np.linalg.eig(matrix)  | ||||
| print('特征值\n', eigenvalue) | ||||
| print('特征向量\n', eigenvector) | ||||
| print() | ||||
| @@ -0,0 +1,32 @@ | ||||
| """ | ||||
| This code is supported by the website: https://www.guanjihuan.com | ||||
| The newest version of this code is on the web page: https://www.guanjihuan.com/archives/17789 | ||||
| """ | ||||
|  | ||||
| import numpy as np | ||||
|  | ||||
| def hamiltonian(width=2, length=2): | ||||
|     h00 = np.zeros((width*length, width*length)) | ||||
|     for i0 in range(length): | ||||
|         for j0 in range(width-1): | ||||
|             h00[i0*width+j0, i0*width+j0+1] = 1 | ||||
|             h00[i0*width+j0+1, i0*width+j0] = 1 | ||||
|     for i0 in range(length-1): | ||||
|         for j0 in range(width): | ||||
|             h00[i0*width+j0, (i0+1)*width+j0] = 1 | ||||
|             h00[(i0+1)*width+j0, i0*width+j0] = 1 | ||||
|     return h00 | ||||
|  | ||||
| print('矩阵:\n', hamiltonian(), '\n') | ||||
|  | ||||
| eigenvalue, eigenvector = np.linalg.eig(hamiltonian()) | ||||
| print('eig求解特征值:', eigenvalue) | ||||
| print('eig求解特征向量:\n',eigenvector) | ||||
| print('判断特征向量是否正交:\n', np.dot(eigenvector.transpose(), eigenvector)) | ||||
|  | ||||
| print() | ||||
|  | ||||
| eigenvalue, eigenvector = np.linalg.eigh(hamiltonian()) | ||||
| print('eigh求解特征值:', eigenvalue) | ||||
| print('eigh求解特征向量:\n',eigenvector) | ||||
| print('判断特征向量是否正交:\n', np.dot(eigenvector.transpose(), eigenvector)) | ||||
							
								
								
									
										37
									
								
								language_learning/python/2021.11.17_zhihu/nature_physics.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								language_learning/python/2021.11.17_zhihu/nature_physics.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,37 @@ | ||||
| from bs4 import BeautifulSoup | ||||
| from urllib.request import urlopen | ||||
| import re   | ||||
| import datetime | ||||
|  | ||||
|  | ||||
| year = datetime.datetime.now().year | ||||
| month = datetime.datetime.now().month | ||||
| day = datetime.datetime.now().day | ||||
|  | ||||
|  | ||||
| f = open('nature_physics.html', 'w', encoding='UTF-8')  | ||||
| f.write('<meta charset="utf-8"><style type="text/css">a{text-decoration: none;color: #0a5794;}a:hover {text-decoration: underline;color: red; }</style>') | ||||
| f.write('<p>'+str(year)+'.'+str(month).rjust(2,'0')+'.'+str(day).rjust(2,'0')+' 已更新</p>') | ||||
|  | ||||
| match_href = [] | ||||
| start_link = "https://www.nature.com/nphys/research-articles" | ||||
| html = urlopen(start_link).read().decode('utf-8')  # 打开网页 | ||||
| soup = BeautifulSoup(html, features='lxml') # 放入soup中 | ||||
| all_article = soup.find_all('article', {"class":"u-full-height c-card c-card--flush"})  | ||||
| for article in all_article: | ||||
|     all_a_tag = article.find_all('a', href=True)  # 获取超链接标签 | ||||
|     for a_tag in all_a_tag: | ||||
|         href = a_tag['href']  # 超链接字符串 | ||||
|         if re.search('/articles/', href): # 文章的链接 | ||||
|             if re.search('https://www.nature.com', href)==None:  # 如果链接不是完整的,那么补充完整 | ||||
|                 href = 'https://www.nature.com'+ href | ||||
|             if href not in match_href and re.search('\?', href)==None:  # 链接不重复 | ||||
|                 match_href.append(href) | ||||
|                 f.write('<li><a target=\"_blank\" href=\"') | ||||
|                 f.write(href)   # 文章链接 | ||||
|                 f.write('\">') | ||||
|                 f.write(a_tag.get_text()) | ||||
|                 f.write('</a>  ') | ||||
|     time = article.find('time', {"class": "c-meta__item c-meta__item--block-at-lg"}).get_text() | ||||
|     f.write(time+'</li>') | ||||
| f.close() | ||||
| @@ -0,0 +1,36 @@ | ||||
| from bs4 import BeautifulSoup | ||||
| from urllib.request import urlopen | ||||
| import re   | ||||
| import datetime | ||||
|  | ||||
|  | ||||
| year = datetime.datetime.now().year | ||||
| month = datetime.datetime.now().month | ||||
| day = datetime.datetime.now().day | ||||
|  | ||||
| f = open('physics_magazine.html', 'w', encoding='UTF-8')  | ||||
| f.write('<meta charset="utf-8"><style type="text/css">a{text-decoration: none;color: #0a5794;}a:hover {text-decoration: underline;color: red; }</style>') | ||||
| f.write('<p>'+str(year)+'.'+str(month).rjust(2,'0')+'.'+str(day).rjust(2,'0')+' 已更新</p>') | ||||
|  | ||||
| match_href = [] | ||||
| start_link = "https://physics.aps.org/" | ||||
| html = urlopen(start_link).read().decode('utf-8')  # 打开网页 | ||||
| soup = BeautifulSoup(html, features='lxml') # 放入soup中 | ||||
| all_articles = soup.find_all('div', {"class":"feed-item-details"}) | ||||
| for article in all_articles: | ||||
|     all_a_tag = article.find_all('a', href=True)  # 获取超链接标签 | ||||
|     for a_tag in all_a_tag: | ||||
|         href = a_tag['href']  # 超链接字符串 | ||||
|         if re.search('/articles/', href): # 文章的链接 | ||||
|             if re.search('https://physics.aps.org', href)==None:  # 如果链接不是完整的,那么补充完整 | ||||
|                 href = 'https://physics.aps.org'+ href | ||||
|             if href not in match_href: | ||||
|                 match_href.append(href) | ||||
|                 f.write('<li><a target=\"_blank\" href=\"') | ||||
|                 f.write(href)   # 文章链接 | ||||
|                 f.write('\">') | ||||
|                 f.write(a_tag.get_text()) | ||||
|                 f.write('</a>  ') | ||||
|     time = article.find('time', {"class": "feed-item-date"}).get_text() | ||||
|     f.write(time+'</li>') | ||||
| f.close() | ||||
							
								
								
									
										42
									
								
								language_learning/python/2021.11.17_zhihu/prb.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								language_learning/python/2021.11.17_zhihu/prb.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,42 @@ | ||||
| from bs4 import BeautifulSoup | ||||
| from urllib.request import urlopen | ||||
| import re   | ||||
| import datetime | ||||
|  | ||||
|  | ||||
| year = datetime.datetime.now().year | ||||
| month = datetime.datetime.now().month | ||||
| day = datetime.datetime.now().day | ||||
|  | ||||
|  | ||||
| f = open('prb.html', 'w', encoding='UTF-8')  | ||||
| f.write('<meta charset="utf-8"><style type="text/css">a{text-decoration: none;color: #0a5794;}a:hover {text-decoration: underline;color: red; }</style>') | ||||
| f.write('<p>'+str(year)+'.'+str(month).rjust(2,'0')+'.'+str(day).rjust(2,'0')+' 已更新</p>') | ||||
|  | ||||
| match_href = [] | ||||
| for loop in range(1): | ||||
|     if loop == 0: | ||||
|         start_link = "https://journals.aps.org/prb/recent"  # 看第一页 | ||||
|     # elif loop == 1: | ||||
|     #     start_link = "https://journals.aps.org/prb/recent?page=2"  # 看第二页 | ||||
|     html = urlopen(start_link).read().decode('utf-8')  # 打开网页 | ||||
|     soup = BeautifulSoup(html, features='lxml') # 放入soup中 | ||||
|     all_article = soup.find_all('div', {"class":"article panel article-result"})  | ||||
|     for article in all_article: | ||||
|         all_a_tag = article.find_all('a', href=True)  # 获取超链接标签 | ||||
|         for a_tag in all_a_tag: | ||||
|             href = a_tag['href']  # 超链接字符串 | ||||
|             if re.search('/abstract/', href): # 文章的链接 | ||||
|                 if re.search('https://journals.aps.org', href)==None:  # 如果链接不是完整的,那么补充完整 | ||||
|                     href = 'https://journals.aps.org'+ href | ||||
|                 if href not in match_href and re.search('\?', href)==None:  # 链接不重复 | ||||
|                     match_href.append(href) | ||||
|                     f.write('<li><a target=\"_blank\" href=\"') | ||||
|                     f.write(href)   # 文章链接 | ||||
|                     f.write('\">') | ||||
|                     f.write(a_tag.get_text()) | ||||
|                     f.write('</a>  ') | ||||
|         info = article.find('h6', {"class": "pub-info"}).get_text() | ||||
|         f.write(re.findall('– Published .*', info, re.S)[0][12:]+'</li>') | ||||
| f.close() | ||||
|  | ||||
							
								
								
									
										42
									
								
								language_learning/python/2021.11.17_zhihu/prl.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								language_learning/python/2021.11.17_zhihu/prl.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,42 @@ | ||||
| from bs4 import BeautifulSoup | ||||
| from urllib.request import urlopen | ||||
| import re   | ||||
| import datetime | ||||
|  | ||||
|  | ||||
| year = datetime.datetime.now().year | ||||
| month = datetime.datetime.now().month | ||||
| day = datetime.datetime.now().day | ||||
|  | ||||
|  | ||||
| f = open('prl.html', 'w', encoding='UTF-8')  | ||||
| f.write('<meta charset="utf-8"><style type="text/css">a{text-decoration: none;color: #0a5794;}a:hover {text-decoration: underline;color: red; }</style>') | ||||
| f.write('<p>'+str(year)+'.'+str(month).rjust(2,'0')+'.'+str(day).rjust(2,'0')+' 已更新</p>') | ||||
|  | ||||
| match_href = [] | ||||
| for loop in range(1): | ||||
|     if loop == 0: | ||||
|         start_link = "https://journals.aps.org/prl/recent"  # 看第一页 | ||||
|     # elif loop == 1: | ||||
|     #     start_link = "https://journals.aps.org/prl/recent?page=2"  # 看第二页 | ||||
|     html = urlopen(start_link).read().decode('utf-8')  # 打开网页 | ||||
|     soup = BeautifulSoup(html, features='lxml') # 放入soup中 | ||||
|     all_article = soup.find_all('div', {"class":"article panel article-result"}) | ||||
|     for article in all_article: | ||||
|         all_a_tag = article.find_all('a', href=True)  # 获取超链接标签 | ||||
|         for a_tag in all_a_tag: | ||||
|             href = a_tag['href']  # 超链接字符串 | ||||
|             if re.search('/abstract/', href): # 文章的链接 | ||||
|                 if re.search('https://journals.aps.org', href)==None:  # 如果链接不是完整的,那么补充完整 | ||||
|                     href = 'https://journals.aps.org'+ href | ||||
|                 if href not in match_href and re.search('\?', href)==None:  # 链接不重复 | ||||
|                     match_href.append(href) | ||||
|                     f.write('<li><a target=\"_blank\" href=\"') | ||||
|                     f.write(href)   # 文章链接 | ||||
|                     f.write('\">') | ||||
|                     f.write(a_tag.get_text()) | ||||
|                     f.write('</a>  ') | ||||
|         info = article.find('h6', {"class": "pub-info"}).get_text() | ||||
|         f.write(re.findall('– Published.*', info, re.S)[0][12:]+'</li>') | ||||
| f.close() | ||||
|  | ||||
							
								
								
									
										61
									
								
								language_learning/python/2021.11.17_zhihu/zhihu.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										61
									
								
								language_learning/python/2021.11.17_zhihu/zhihu.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,61 @@ | ||||
| """ | ||||
| This code is supported by the website: https://www.guanjihuan.com | ||||
| The newest version of this code is on the web page: https://www.guanjihuan.com/archives/17937 | ||||
| """ | ||||
|  | ||||
| from bs4 import BeautifulSoup | ||||
| from urllib.request import urlopen | ||||
| import re   | ||||
| import datetime | ||||
|  | ||||
| year = datetime.datetime.now().year | ||||
| month = datetime.datetime.now().month | ||||
| day = datetime.datetime.now().day | ||||
|  | ||||
| # 获取链接 | ||||
| match_href = [] | ||||
| # 由于没有模拟登录知乎,因此只能爬取到最新的两篇文章 | ||||
| authors = ["https://www.zhihu.com/people/guanjihuan/posts", # Guan | ||||
| ] | ||||
| for i0 in range(len(authors)): | ||||
|     start_link = authors[i0] | ||||
|     html = urlopen(start_link).read().decode('utf-8')  # 打开网页 | ||||
|     soup = BeautifulSoup(html, features='lxml') # 放入soup中 | ||||
|     all_a_tag = soup.find_all('a', href=True)  # 获取超链接标签 | ||||
|     for a_tag in all_a_tag: | ||||
|         href = a_tag['href']  # 超链接字符串 | ||||
|         if re.search('//zhuanlan.zhihu.com/p/', href) and not re.search('edit', href): # 文章的链接 | ||||
|             if re.search('https:', href)==None:  # 如果链接不是完整的,那么补充完整 | ||||
|                 href = 'https:'+ href | ||||
|             if href not in match_href: | ||||
|                 match_href.append(href) | ||||
| # 对链接进行排序 | ||||
| numbers = [] | ||||
| match_href_new = []  | ||||
| for href in match_href: | ||||
|     numbers.append(int(href[29:])) | ||||
| numbers.sort(reverse = True) | ||||
| for n in numbers: | ||||
|     match_href_new.append('https://zhuanlan.zhihu.com/p/'+str(n)) | ||||
|  | ||||
| # 获取内容并写入文件 | ||||
| f = open('zhihu.html', 'w', encoding='UTF-8')  | ||||
| f.write('<meta charset="utf-8"><style type="text/css">a{text-decoration: none;color: #0a5794;}a:hover {text-decoration: underline;color: red; }</style>') | ||||
| f.write('<p>'+str(year)+'.'+str(month).rjust(2,'0')+'.'+str(day).rjust(2,'0')+' 已更新</p>') | ||||
| for href in match_href_new:  | ||||
|     try: | ||||
|         html = urlopen(href).read().decode('utf-8')   # 打开文章链接 | ||||
|         soup = BeautifulSoup(html, features='lxml') # 放入soup中 | ||||
|         title = soup.title   # 文章标题 | ||||
|         f.write('<li><a target=\"_blank\" href=\"') | ||||
|         f.write(str(href))   # 文章链接 | ||||
|         f.write('\">') | ||||
|         f.write(str(title.get_text()[:-5])) | ||||
|         f.write('</a>  ')  | ||||
|         author = soup.find("span", {"class": "UserLink AuthorInfo-name"}) | ||||
|         f.write(str(author.get_text()+'  ')) | ||||
|         post_time = soup.find("div", {"class" : "ContentItem-time"}) | ||||
|         f.write(str(post_time.get_text()[4:-6])+'</li>') | ||||
|     except: | ||||
|         pass | ||||
| f.close() | ||||
| @@ -0,0 +1,4 @@ | ||||
| import guan | ||||
| guan.change_directory_by_replacement(current_key_word='code', new_key_word='data') | ||||
| with open('data.txt', 'w') as f: # 保存数据 | ||||
|     f.write('Hello world')  | ||||
| @@ -0,0 +1,4 @@ | ||||
| import guan | ||||
| guan.change_directory_by_replacement(current_key_word='working/code', new_key_word='local/data') | ||||
| with open('data.txt', 'w') as f: # 保存数据 | ||||
|     f.write('Hello world')  | ||||
| @@ -0,0 +1,9 @@ | ||||
| import os | ||||
| code_path = os.getcwd() # 当前代码文件的路径 | ||||
| data_path = code_path.replace('\\', '/')  # \改为/,防止路径报错 | ||||
| data_path = data_path.replace('code', 'data') # 把路径中code改为data | ||||
| if os.path.exists(data_path) == False: # 如果文件夹不存在,新建文件夹 | ||||
|     os.makedirs(data_path) | ||||
| os.chdir(data_path) # 转到数据的存放路径 | ||||
| with open('data.txt', 'w') as f: # 保存数据 | ||||
|     f.write('Hello world')  | ||||
							
								
								
									
										42
									
								
								language_learning/python/2022.03.06_numba_time/numba_time.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								language_learning/python/2022.03.06_numba_time/numba_time.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,42 @@ | ||||
| import numpy as np | ||||
| from numba import jit | ||||
| import time | ||||
|  | ||||
| def for_sum(numpy_array): | ||||
|     sum = 0 | ||||
|     for number in numpy_array: | ||||
|         sum += number | ||||
|     return sum | ||||
|  | ||||
| @jit | ||||
| def numba_for_sum(numpy_array): | ||||
|     sum = 0 | ||||
|     for number in numpy_array: | ||||
|         sum += number | ||||
|     return sum | ||||
|  | ||||
| numpy_array = np.arange(0,1e8,1) | ||||
|  | ||||
| start = time.time() | ||||
| result = sum(numpy_array) | ||||
| end = time.time() | ||||
| print('\nresult:', result) | ||||
| print('python中sum()函数求和时间:\n', end - start) | ||||
|  | ||||
| start = time.time() | ||||
| result = np.sum(numpy_array) | ||||
| end = time.time() | ||||
| print('\nresult:', result) | ||||
| print('numpy.sum()函数求和时间:\n', end - start) | ||||
|  | ||||
| start = time.time() | ||||
| result = for_sum(numpy_array) | ||||
| end = time.time() | ||||
| print('\nresult:', result) | ||||
| print('for循环求和numpy数组的时间:\n', end - start) | ||||
|  | ||||
| start = time.time() | ||||
| result = numba_for_sum(numpy_array) | ||||
| end = time.time() | ||||
| print('\nresult:', result) | ||||
| print('numba加速for循环求和numpy数组的时间:\n', end - start, '\n') | ||||
| @@ -0,0 +1,2 @@ | ||||
| import cmath | ||||
| print(cmath.exp(1j*cmath.pi)) | ||||
| @@ -0,0 +1,26 @@ | ||||
| import copy | ||||
| import numpy as np | ||||
|  | ||||
| array_1 = [1, 2, 3] | ||||
| array_2 = array_1 | ||||
| array_1[0] = 100 | ||||
| print('array_1=', array_1) | ||||
| print('array_2=', array_2, '\n') | ||||
|  | ||||
| array_1 = np.array([1, 2, 3]) | ||||
| array_2 = array_1 | ||||
| array_1[0] = 100 | ||||
| print('array_1=', array_1) | ||||
| print('array_2=', array_2, '\n') | ||||
|  | ||||
| array_1 = [1, 2, 3] | ||||
| array_2 = copy.deepcopy(array_1) | ||||
| array_1[0] = 100 | ||||
| print('array_1=', array_1) | ||||
| print('array_2=', array_2, '\n') | ||||
|  | ||||
| array_1 = np.array([1, 2, 3]) | ||||
| array_2 = copy.deepcopy(array_1) | ||||
| array_1[0] = 100 | ||||
| print('array_1=', array_1) | ||||
| print('array_2=', array_2) | ||||
| @@ -0,0 +1,7 @@ | ||||
| import functools | ||||
|  | ||||
| def func(x, y, z): | ||||
|     return x-y+z | ||||
|  | ||||
| partial_func = functools.partial(func, x=5, z=0) | ||||
| print(partial_func(y=2)) | ||||
| @@ -0,0 +1,13 @@ | ||||
| import math | ||||
| print(math.pi) | ||||
| print(math.e) | ||||
| print(math.exp(1)) | ||||
| print(math.cos(math.pi)) | ||||
| print(math.sqrt(2), '\n') | ||||
|  | ||||
| import numpy as np | ||||
| print(np.pi) | ||||
| print(np.e) | ||||
| print(np.exp(1)) | ||||
| print(np.cos(np.pi)) | ||||
| print(np.sqrt(2)) | ||||
| @@ -0,0 +1,8 @@ | ||||
| import matplotlib.pyplot as plt | ||||
| fig = plt.figure() | ||||
| ax = fig.add_subplot(111) | ||||
| ax.plot(range(10), range(10)) | ||||
| ax.set_title('Example', fontsize=20, fontfamily='Times New Roman') | ||||
| ax.set_xlabel('x', fontsize=20, fontfamily='Times New Roman')  | ||||
| ax.set_ylabel('y', fontsize=20, fontfamily='Times New Roman') | ||||
| plt.show() | ||||
| @@ -0,0 +1,23 @@ | ||||
| from multiprocessing import Process | ||||
| import time | ||||
|  | ||||
| def f(name): | ||||
|     time.sleep(5) | ||||
|     print('Hello', name) | ||||
|  | ||||
| if __name__ == '__main__': | ||||
|     start_time = time.time() | ||||
|     p1 = Process(target=f, args=('Bob',)) | ||||
|     p2 = Process(target=f, args=('Alice',)) | ||||
|     p1.start() | ||||
|     p2.start() | ||||
|     p1.join() | ||||
|     p2.join() | ||||
|     end_time = time.time() | ||||
|     print(end_time - start_time, '\n') | ||||
|  | ||||
|     start_time = time.time() | ||||
|     f('Bob') | ||||
|     f('Alice') | ||||
|     end_time = time.time() | ||||
|     print(end_time - start_time) | ||||
| @@ -0,0 +1,21 @@ | ||||
| import numpy as np | ||||
| np.zeros((2, 3)) # 零矩阵 | ||||
| np.identity(3) # 单位矩阵 | ||||
| np.diag([1, 3, 5])  # 对角矩阵 | ||||
| matrix1 = np.array([[3, 5+1j], [2, 7]]) # numpy矩阵 | ||||
| matrix1.shape # 矩阵的维度 | ||||
| matrix1.transpose() # 矩阵转置 | ||||
| matrix1.conj() # 矩阵所有元素共轭 | ||||
| np.conj(matrix1) # 矩阵所有元素共轭(同上) | ||||
| np.arange(1, 5, 1) # 数列(左闭右开) | ||||
| np.linspace(-2, 2, 5) # 数列(左闭右闭) | ||||
| np.random.uniform(-2, 2) # 随机数 | ||||
| np.random.randint(0, 2)  # 随机整数(左闭右开) | ||||
| np.sort([1, 7, 0, 3])  # 排列 | ||||
| np.argsort([1, 7, 0, 3]) # 排列索引 | ||||
| np.linalg.det(matrix1)  # 行列式 | ||||
| matrix2 = np.linalg.inv(matrix1)  # 求逆 | ||||
| np.matmul(matrix1, matrix2) # 矩阵乘积 | ||||
| np.dot(matrix1, matrix2) # 矩阵乘积(同上) | ||||
| eigenvalue, eigenvector = np.linalg.eig(matrix1)  # 求本征值,本征向量 | ||||
| matrix3 = np.append(matrix1, matrix2, axis=0) # 增加数组元素或者矩阵的行 | ||||
| @@ -0,0 +1,6 @@ | ||||
| import os | ||||
| os.getcwd()  # 获取路径 | ||||
| if os.path.exists('new_dir') == False: # 判断路径是否存在 | ||||
|     os.makedirs('new_dir') # 新建文件夹 | ||||
| os.chdir('new_dir')  # 切换到该文件夹 | ||||
| print(os.walk('/'))  # 游走目录 | ||||
| @@ -0,0 +1,5 @@ | ||||
| import time | ||||
| start_time = time.time() | ||||
| time.sleep(5) | ||||
| end_time = time.time() | ||||
| print(end_time-start_time) | ||||
| @@ -0,0 +1,31 @@ | ||||
| """ | ||||
| This code is supported by the website: https://www.guanjihuan.com | ||||
| The newest version of this code is on the web page: https://www.guanjihuan.com/archives/23000 | ||||
| """ | ||||
|  | ||||
|  | ||||
| string_array = ['关', '。', '3', '.'] | ||||
|  | ||||
| for string in string_array: | ||||
|     # 编码 | ||||
|     gb2312 = string.encode(encoding="gb2312") | ||||
|     gbk = string.encode(encoding="gbk") | ||||
|     gb18030 = string.encode(encoding="gb18030") | ||||
|     uft8 = string.encode(encoding="utf-8") | ||||
|  | ||||
|     # 查看 | ||||
|     print('字符串 =', string, ' | 数据类型 =', type(string), ' | 长度 =', len(string)) | ||||
|     print('gb2312编码 =', gb2312, ' | 数据类型 =', type(gb2312), ' | 长度 =', len(gb2312)) | ||||
|     print('gbk编码 =', gbk, ' | 数据类型 =', type(gbk), ' | 长度 =', len(gbk)) | ||||
|     print('gb18030编码 =', gb18030, ' | 数据类型 =', type(gb18030), ' | 长度 =', len(gb18030)) | ||||
|     print('utf8编码 =', uft8, ' | 数据类型 =', type(uft8), ' | 长度 =', len(uft8)) | ||||
|     print() | ||||
|  | ||||
|  | ||||
| # 乱码例子 | ||||
| string = '关关' | ||||
| uft8 = string.encode(encoding="utf-8") | ||||
| new_string_1 = uft8.decode(encoding="utf-8") | ||||
| new_string_2 = uft8.decode(encoding="gbk") | ||||
| print("使用utf-8解码utf-8编码的数据 =", new_string_1) | ||||
| print("使用gbk解码utf-8编码的数据 =", new_string_2) | ||||
| @@ -0,0 +1,41 @@ | ||||
| """ | ||||
| This code is supported by the website: https://www.guanjihuan.com | ||||
| The newest version of this code is on the web page: https://www.guanjihuan.com/archives/25453 | ||||
| """ | ||||
|  | ||||
| import os | ||||
|  | ||||
| # 选取某个目录 | ||||
| directory = 'E:/' | ||||
|  | ||||
| def main(): | ||||
|     for root, dirs, files in os.walk(directory): | ||||
|         for i0 in range(len(files)): | ||||
|             if 'pdf' in files[i0] or 'djvu' in files[i0]: # 满足某个条件的文件  | ||||
|  | ||||
|                 # 显示旧文件名 | ||||
|                 name = files[i0] | ||||
|                 print(name) # 显示旧文件名 | ||||
|  | ||||
|                 # 显示新文件名 | ||||
|                 new_name = modify_name(name) | ||||
|                 print(new_name) | ||||
|                 print() | ||||
|  | ||||
|                 # # 修改文件名。注意:需要检查前面的代码,尤其是modify_name的规则看是否都满足,再运行下面的代码,否则文件名的修改会出现遗漏或混乱。 | ||||
|                 # if new_name != None: | ||||
|                 #     os.rename(root+'/'+name, root+'/'+new_name)  | ||||
|          | ||||
|  | ||||
| def modify_name(name):  # 按某种规则修改文件名 | ||||
|     array = name.split(' - ')  # 通过' - '把这类型的文件名切开 | ||||
|     if len(array) != 3: | ||||
|         print('Miss:', name) | ||||
|         new_name = None  # 如果不满足规则,则不修改 | ||||
|     else: | ||||
|         new_name= array[1]+' - '+array[0]+' - '+array[2] # 做个对调 | ||||
|     return new_name | ||||
|  | ||||
|  | ||||
| if __name__ == '__main__': | ||||
|     main() | ||||
| @@ -0,0 +1,35 @@ | ||||
| """ | ||||
| This code is supported by the website: https://www.guanjihuan.com | ||||
| The newest version of this code is on the web page: https://www.guanjihuan.com/archives/25685 | ||||
| """ | ||||
|  | ||||
| # 注意:这个程序请小心使用,防止误操作把系统文件或个人文件破坏。在选取好directory目录后,请经过再三确认无误后再运行,尤其是directory的层级不能太高。 | ||||
|  | ||||
|  | ||||
| def main(): | ||||
|     # 选取某个目录 | ||||
|     directory = 'E:/test/all_files' | ||||
|     move_all_files_to_root_directory(directory) | ||||
|      | ||||
|     # import guan | ||||
|     # guan.move_all_files_to_root_directory(directory) | ||||
|  | ||||
|  | ||||
| def move_all_files_to_root_directory(directory): | ||||
|     import os | ||||
|     import shutil | ||||
|     for root, dirs, files in os.walk(directory): | ||||
|         for i0 in range(len(files)): | ||||
|             # print(root) # 文件对应目录 | ||||
|             # print(files[i0], '\n') # 文件 | ||||
|             shutil.move(root+'/'+files[i0], directory+'/'+files[i0]) # 移动所有文件至根目录 | ||||
|     for i0 in range(100): # 多次尝试删除层数比较多的空文件夹,例如100层 | ||||
|         for root, dirs, files in os.walk(directory): | ||||
|             try: | ||||
|                 os.rmdir(root) # 删除空文件夹 | ||||
|             except: | ||||
|                 pass | ||||
|  | ||||
|  | ||||
| if __name__ == '__main__': | ||||
|     main() | ||||
| @@ -0,0 +1,116 @@ | ||||
| """ | ||||
| This code is supported by the website: https://www.guanjihuan.com | ||||
| The newest version of this code is on the web page: https://www.guanjihuan.com/archives/25699 | ||||
| """ | ||||
|  | ||||
|  | ||||
| def main(): | ||||
|     directory = 'E:/literature' | ||||
|     write_file_list_in_markdown(directory) | ||||
|  | ||||
|  | ||||
| def write_file_list_in_markdown(directory='./', filename='a', reverse_positive_or_negative=1, starting_from_h1=None, banned_file_format=[], hide_file_format=None, divided_line=None, show_second_number=None, show_third_number=None):  | ||||
|     import os | ||||
|     f = open(filename+'.md', 'w', encoding="utf-8") | ||||
|     filenames1 = os.listdir(directory) | ||||
|     u0 = 0 | ||||
|     for filename1 in filenames1[::reverse_positive_or_negative]: | ||||
|         filename1_with_path = os.path.join(directory,filename1)  | ||||
|         if os.path.isfile(filename1_with_path):  # 文件 | ||||
|             if os.path.splitext(filename1)[1] not in banned_file_format: | ||||
|                 if hide_file_format == None: | ||||
|                     f.write('+ '+str(filename1)+'\n\n') | ||||
|                 else: | ||||
|                     f.write('+ '+str(os.path.splitext(filename1)[0])+'\n\n') | ||||
|         else:  # 文件夹 | ||||
|             u0 += 1 | ||||
|             if divided_line != None and u0 != 1: | ||||
|                 f.write('--------\n\n') | ||||
|             if starting_from_h1 == None: | ||||
|                 f.write('#') | ||||
|             f.write('# '+str(filename1)+'\n\n') | ||||
|  | ||||
|             filenames2 = os.listdir(filename1_with_path)  | ||||
|             i0 = 0      | ||||
|             for filename2 in filenames2[::reverse_positive_or_negative]: | ||||
|                 filename2_with_path = os.path.join(directory, filename1, filename2)  | ||||
|                 if os.path.isfile(filename2_with_path):  # 文件 | ||||
|                     if os.path.splitext(filename2)[1] not in banned_file_format: | ||||
|                         if hide_file_format == None: | ||||
|                             f.write('+ '+str(filename2)+'\n\n') | ||||
|                         else: | ||||
|                             f.write('+ '+str(os.path.splitext(filename2)[0])+'\n\n') | ||||
|                 else:    # 文件夹 | ||||
|                     i0 += 1 | ||||
|                     if starting_from_h1 == None: | ||||
|                         f.write('#') | ||||
|                     if show_second_number != None: | ||||
|                         f.write('## '+str(i0)+'. '+str(filename2)+'\n\n') | ||||
|                     else: | ||||
|                         f.write('## '+str(filename2)+'\n\n') | ||||
|                      | ||||
|                     j0 = 0 | ||||
|                     filenames3 = os.listdir(filename2_with_path) | ||||
|                     for filename3 in filenames3[::reverse_positive_or_negative]: | ||||
|                         filename3_with_path = os.path.join(directory, filename1, filename2, filename3)  | ||||
|                         if os.path.isfile(filename3_with_path):    # 文件 | ||||
|                             if os.path.splitext(filename3)[1] not in banned_file_format: | ||||
|                                 if hide_file_format == None: | ||||
|                                     f.write('+ '+str(filename3)+'\n\n') | ||||
|                                 else: | ||||
|                                     f.write('+ '+str(os.path.splitext(filename3)[0])+'\n\n') | ||||
|                         else:   # 文件夹 | ||||
|                             j0 += 1 | ||||
|                             if starting_from_h1 == None: | ||||
|                                 f.write('#') | ||||
|                             if show_third_number != None: | ||||
|                                 f.write('### ('+str(j0)+') '+str(filename3)+'\n\n') | ||||
|                             else: | ||||
|                                 f.write('### '+str(filename3)+'\n\n') | ||||
|  | ||||
|                             filenames4 = os.listdir(filename3_with_path) | ||||
|                             for filename4 in filenames4[::reverse_positive_or_negative]: | ||||
|                                 filename4_with_path = os.path.join(directory, filename1, filename2, filename3, filename4)  | ||||
|                                 if os.path.isfile(filename4_with_path):   # 文件 | ||||
|                                     if os.path.splitext(filename4)[1] not in banned_file_format: | ||||
|                                         if hide_file_format == None: | ||||
|                                             f.write('+ '+str(filename4)+'\n\n') | ||||
|                                         else: | ||||
|                                             f.write('+ '+str(os.path.splitext(filename4)[0])+'\n\n') | ||||
|                                 else:     # 文件夹 | ||||
|                                     if starting_from_h1 == None: | ||||
|                                         f.write('#') | ||||
|                                     f.write('#### '+str(filename4)+'\n\n') | ||||
|  | ||||
|                                     filenames5 = os.listdir(filename4_with_path) | ||||
|                                     for filename5 in filenames5[::reverse_positive_or_negative]: | ||||
|                                         filename5_with_path = os.path.join(directory, filename1, filename2, filename3, filename4, filename5)  | ||||
|                                         if os.path.isfile(filename5_with_path):    # 文件 | ||||
|                                             if os.path.splitext(filename5)[1] not in banned_file_format: | ||||
|                                                 if hide_file_format == None: | ||||
|                                                     f.write('+ '+str(filename5)+'\n\n') | ||||
|                                                 else: | ||||
|                                                     f.write('+ '+str(os.path.splitext(filename5)[0])+'\n\n') | ||||
|                                         else:   # 文件夹 | ||||
|                                             if starting_from_h1 == None: | ||||
|                                                 f.write('#') | ||||
|                                             f.write('##### '+str(filename5)+'\n\n') | ||||
|  | ||||
|                                             filenames6 = os.listdir(filename5_with_path) | ||||
|                                             for filename6 in filenames6[::reverse_positive_or_negative]: | ||||
|                                                 filename6_with_path = os.path.join(directory, filename1, filename2, filename3, filename4, filename5, filename6)  | ||||
|                                                 if os.path.isfile(filename6_with_path):   # 文件 | ||||
|                                                     if os.path.splitext(filename6)[1] not in banned_file_format: | ||||
|                                                         if hide_file_format == None: | ||||
|                                                             f.write('+ '+str(filename6)+'\n\n') | ||||
|                                                         else: | ||||
|                                                             f.write('+ '+str(os.path.splitext(filename6)[0])+'\n\n') | ||||
|                                                 else:  # 文件夹 | ||||
|                                                     if starting_from_h1 == None: | ||||
|                                                         f.write('#') | ||||
|                                                     f.write('###### '+str(filename6)+'\n\n') | ||||
|     f.close() | ||||
|  | ||||
|  | ||||
| if __name__ == '__main__': | ||||
|     main() | ||||
| @@ -0,0 +1,55 @@ | ||||
| """ | ||||
| This code is supported by the website: https://www.guanjihuan.com | ||||
| The newest version of this code is on the web page: https://www.guanjihuan.com/archives/25943 | ||||
| """ | ||||
|  | ||||
|  | ||||
| def main(): | ||||
|     directory = 'E:/test' | ||||
|     creat_necessary_file(directory) | ||||
|     # delete_file_with_specific_name(directory) | ||||
|  | ||||
|     # import guan | ||||
|     # guan.creat_necessary_file(directory) | ||||
|     # guan.delete_file_with_specific_name(directory) | ||||
|  | ||||
|  | ||||
| def creat_necessary_file(directory, filename='readme', file_format='.md', content='', overwrite=None, ignored_directory_with_words=[]): | ||||
|     import os | ||||
|     directory_with_file = [] | ||||
|     ignored_directory = [] | ||||
|     for root, dirs, files in os.walk(directory): | ||||
|         for i0 in range(len(files)): | ||||
|             if root not in directory_with_file: | ||||
|                 directory_with_file.append(root) | ||||
|             if files[i0] == filename+file_format: | ||||
|                 if root not in ignored_directory: | ||||
|                     ignored_directory.append(root) | ||||
|     if overwrite == None: | ||||
|         for root in ignored_directory: | ||||
|             directory_with_file.remove(root) | ||||
|     ignored_directory_more =[] | ||||
|     for root in directory_with_file:  | ||||
|         for word in ignored_directory_with_words: | ||||
|             if word in root: | ||||
|                 if root not in ignored_directory_more: | ||||
|                     ignored_directory_more.append(root) | ||||
|     for root in ignored_directory_more: | ||||
|         directory_with_file.remove(root)  | ||||
|     for root in directory_with_file: | ||||
|         os.chdir(root) | ||||
|         f = open(filename+file_format, 'w', encoding="utf-8") | ||||
|         f.write(content) | ||||
|         f.close() | ||||
|      | ||||
|  | ||||
| def delete_file_with_specific_name(directory, filename='readme', file_format='.md'): | ||||
|       import os | ||||
|       for root, dirs, files in os.walk(directory): | ||||
|         for i0 in range(len(files)): | ||||
|             if files[i0] == filename+file_format: | ||||
|                 os.remove(root+'/'+files[i0]) | ||||
|  | ||||
|  | ||||
| if __name__ == '__main__': | ||||
|     main() | ||||
| @@ -0,0 +1,45 @@ | ||||
| """ | ||||
| This code is supported by the website: https://www.guanjihuan.com | ||||
| The newest version of this code is on the web page: https://www.guanjihuan.com/archives/26113 | ||||
| """ | ||||
|  | ||||
| # 仅支持文件名判断是否重复,不支持对文件内容的判断。 | ||||
| # 如需对文件名和内容都判断,需要计算文件的哈希值。这里暂时不考虑。 | ||||
|  | ||||
|  | ||||
| def main(): | ||||
|     directory = 'E:/test' | ||||
|     repeated_file = find_repeated_file_with_same_filename(directory) | ||||
|     print(repeated_file) | ||||
|  | ||||
|     # import guan | ||||
|     # repeated_file = guan.find_repeated_file_with_same_filename(directory='./', ignored_directory_with_words=[], ignored_file_with_words=[], num=1000) | ||||
|     # print(repeated_file) | ||||
|  | ||||
|  | ||||
| def find_repeated_file_with_same_filename(directory='./', ignored_directory_with_words=[], ignored_file_with_words=[], num=1000): | ||||
|     import os | ||||
|     from collections import Counter | ||||
|     file_list = [] | ||||
|     for root, dirs, files in os.walk(directory): | ||||
|         for i0 in range(len(files)): | ||||
|             file_list.append(files[i0]) | ||||
|             for word in ignored_directory_with_words: | ||||
|                 if word in root: | ||||
|                     file_list.remove(files[i0])        | ||||
|             for word in ignored_file_with_words: | ||||
|                 if word in files[i0]: | ||||
|                     try: | ||||
|                         file_list.remove(files[i0])    | ||||
|                     except: | ||||
|                         pass  | ||||
|     count_file = Counter(file_list).most_common(num) | ||||
|     repeated_file = [] | ||||
|     for item in count_file: | ||||
|         if item[1]>1: | ||||
|             repeated_file.append(item) | ||||
|     return repeated_file | ||||
|  | ||||
|  | ||||
| if __name__ == '__main__': | ||||
|     main() | ||||
| @@ -0,0 +1,41 @@ | ||||
| """ | ||||
| This code is supported by the website: https://www.guanjihuan.com | ||||
| The newest version of this code is on the web page: https://www.guanjihuan.com/archives/26536 | ||||
| """ | ||||
|  | ||||
|  | ||||
| def main(): | ||||
|     # 如果子文件夹中所有文件的数量小于5,输出路径。 | ||||
|     count_file_in_sub_directory(directory='./', smaller_than_num=5)  | ||||
|  | ||||
|     # import guan | ||||
|     # guan.count_file_in_sub_directory(directory='./', smaller_than_num=5) | ||||
|  | ||||
|  | ||||
| def count_file_in_sub_directory(directory='./', smaller_than_num=None): | ||||
|     import os | ||||
|     from collections import Counter | ||||
|     dirs_list = [] | ||||
|     for root, dirs, files in os.walk(directory): | ||||
|         if dirs != []: | ||||
|             for i0 in range(len(dirs)): | ||||
|                 dirs_list.append(root+'/'+dirs[i0]) | ||||
|     for sub_dir in dirs_list: | ||||
|         file_list = [] | ||||
|         for root, dirs, files in os.walk(sub_dir): | ||||
|             for i0 in range(len(files)): | ||||
|                 file_list.append(files[i0]) | ||||
|         count_file = len(file_list) | ||||
|         if smaller_than_num == None: | ||||
|             print(sub_dir) | ||||
|             print(count_file) | ||||
|             print() | ||||
|         else: | ||||
|             if count_file<smaller_than_num: | ||||
|                 print(sub_dir) | ||||
|                 print(count_file) | ||||
|                 print() | ||||
|  | ||||
|  | ||||
| if __name__ == '__main__': | ||||
|     main() | ||||
| @@ -0,0 +1,48 @@ | ||||
| """ | ||||
| This code is supported by the website: https://www.guanjihuan.com | ||||
| The newest version of this code is on the web page: https://www.guanjihuan.com/archives/29155 | ||||
| """ | ||||
|  | ||||
| import numpy as np | ||||
| import matplotlib.pyplot as plt | ||||
|  | ||||
| def get_data(x_array, y_array): | ||||
|     z_matrix = np.zeros((y_array.shape[0], x_array.shape[0])) | ||||
|     j0 = -1 | ||||
|     for x in x_array: | ||||
|         j0 += 1 | ||||
|         i0 = -1 | ||||
|         for y in y_array: | ||||
|             i0 += 1 | ||||
|             z_matrix[i0, j0] = x**2+y**2 | ||||
|     return z_matrix | ||||
|  | ||||
| x_array = np.linspace(-1, 1, 1000) | ||||
| y_array = x_array | ||||
| z_matrix = get_data(x_array, y_array)  # 举例的数据 | ||||
|  | ||||
| fix_value = 0.5  # 画这个值附近的等高线 | ||||
| precision = 0.01 # 选取该值附近的范围 | ||||
|  | ||||
| # 方法一 | ||||
| x_array_new = [] | ||||
| y_array_new = [] | ||||
| for i0 in range(y_array.shape[0]): | ||||
|     for j0 in range(x_array.shape[0]): | ||||
|         if abs(z_matrix[i0, j0]-fix_value)<precision: | ||||
|             x_array_new.append(x_array[j0]) | ||||
|             y_array_new.append(y_array[i0]) | ||||
| fig, ax = plt.subplots() | ||||
| plt.plot(x_array_new, y_array_new, 'o') | ||||
| ax.set_xlim(min(x_array), max(x_array)) | ||||
| ax.set_ylim(min(y_array), max(y_array)) | ||||
| plt.show() | ||||
|  | ||||
| # 方法二 | ||||
| for i0 in range(y_array.shape[0]): | ||||
|     for j0 in range(x_array.shape[0]): | ||||
|         if abs(z_matrix[i0, j0]-fix_value)>precision: | ||||
|             z_matrix[i0, j0] = None | ||||
| fig, ax = plt.subplots() | ||||
| ax.contourf(x_array,y_array,z_matrix)  | ||||
| plt.show() | ||||
| @@ -0,0 +1,8 @@ | ||||
| import numpy as np | ||||
|  | ||||
| def run(parameter_array): | ||||
|     for parameter in parameter_array: | ||||
|         print('hello world'+' '+str(parameter)) | ||||
|  | ||||
| parameter_array_labeled_for_replacement = [] | ||||
| run(parameter_array_labeled_for_replacement) | ||||
| @@ -0,0 +1,7 @@ | ||||
| #!/bin/sh | ||||
| #PBS -N task | ||||
| #PBS -l nodes=1:ppn=1 | ||||
| #PBS -q bigmem | ||||
| cd $PBS_O_WORKDIR | ||||
| export OMP_NUM_THREADS=1 | ||||
| python a.py | ||||
| @@ -0,0 +1,56 @@ | ||||
| """ | ||||
| This code is supported by the website: https://www.guanjihuan.com | ||||
| The newest version of this code is on the web page: https://www.guanjihuan.com/archives/29200 | ||||
| """ | ||||
|  | ||||
| import os | ||||
|  | ||||
| parameter_str_array = ['np.arange(1, 11, 1)', 'np.arange(11, 21, 1)'] | ||||
|  | ||||
| index = 0 | ||||
| for parameter_str in parameter_str_array: | ||||
|     index += 1 | ||||
|  | ||||
|     # 以下处理代码文件 | ||||
|     old_file = 'a.py' | ||||
|     new_file = 'a'+str(index)+'.py' | ||||
|  | ||||
|     # 说明:linux系统下复制用cp,windows系统下复制用copy | ||||
|     os.system('cp '+old_file+' '+new_file)  # 复制python代码文件 | ||||
|     with open(new_file, 'r') as f:  # 读取 | ||||
|         content  = f.read() | ||||
|      | ||||
|     old_str = 'parameter_array_labeled_for_replacement = []' | ||||
|     new_str = 'parameter_array_labeled_for_replacement = ' + parameter_str | ||||
|     content = content.replace(old_str, new_str) | ||||
|  | ||||
|     # 如果程序需要将数据写入文件,除了需要替代参数,还需要替代文件名,方法和以上相同 | ||||
|  | ||||
|     with open('a'+str(index)+'.py', 'w') as f: # 写入 | ||||
|         f.write(content) | ||||
|  | ||||
|  | ||||
|  | ||||
|     # 以下处理任务上传文件 | ||||
|     old_file = 'a.sh' | ||||
|     new_file = 'a'+str(index)+'.sh' | ||||
|  | ||||
|     os.system('cp '+old_file+' '+new_file)  # 复制任务调度系统的sh上传文件 | ||||
|     with open(new_file, 'r') as f:  # 读取 | ||||
|         content  = f.read() | ||||
|      | ||||
|     old_str = 'python a.py' | ||||
|     new_str = 'python a'+str(index)+'.py' | ||||
|     content = content.replace(old_str, new_str) | ||||
|  | ||||
|     old_str = 'task' | ||||
|     new_str = 'task_'+str(index) | ||||
|     content = content.replace(old_str, new_str) | ||||
|  | ||||
|     with open('a'+str(index)+'.sh', 'w') as f: # 写入 | ||||
|         f.write(content) | ||||
|  | ||||
|  | ||||
|  | ||||
|     # 提交任务 | ||||
|     os.system('qsub '+new_file) | ||||
| @@ -0,0 +1,32 @@ | ||||
| """ | ||||
| This code is supported by the website: https://www.guanjihuan.com | ||||
| The newest version of this code is on the web page: https://www.guanjihuan.com/archives/34649 | ||||
| """ | ||||
|  | ||||
| import PyPDF2 | ||||
|  | ||||
| # 创建一个空的PDF对象 | ||||
| output_pdf = PyPDF2.PdfWriter() | ||||
|  | ||||
| # 打开第一个PDF文件 | ||||
| with open('a.pdf', 'rb') as file1: | ||||
|     pdf1 = PyPDF2.PdfReader(file1) | ||||
|      | ||||
|     # 将第一个PDF文件的所有页面添加到输出PDF对象中 | ||||
|     for page in range(len(pdf1.pages)): | ||||
|         output_pdf.add_page(pdf1.pages[page]) | ||||
|      | ||||
| # 打开第二个PDF文件 | ||||
| with open('b.pdf', 'rb') as file2: | ||||
|     pdf2 = PyPDF2.PdfReader(file2) | ||||
|      | ||||
|     # 将第二个PDF文件的所有页面添加到输出PDF对象中 | ||||
|     for page in range(len(pdf2.pages)): | ||||
|         output_pdf.add_page(pdf2.pages[page]) | ||||
|          | ||||
| # 保存合并后的PDF文件 | ||||
| with open('combined_file.pdf', 'wb') as combined_file: | ||||
|     output_pdf.write(combined_file) | ||||
|  | ||||
| # import guan | ||||
| # guan.combine_two_pdf_files(input_file_1='a.pdf', input_file_2='b.pdf', output_file='combined_file.pdf') | ||||
		Reference in New Issue
	
	Block a user