banner
andrewji8

Being towards death

Heed not to the tree-rustling and leaf-lashing rain, Why not stroll along, whistle and sing under its rein. Lighter and better suited than horses are straw sandals and a bamboo staff, Who's afraid? A palm-leaf plaited cape provides enough to misty weather in life sustain. A thorny spring breeze sobers up the spirit, I feel a slight chill, The setting sun over the mountain offers greetings still. Looking back over the bleak passage survived, The return in time Shall not be affected by windswept rain or shine.
telegram
twitter
github

令人震惊的Python自动化脚本,你必须尝试(2)

3、防篡改。这个自动化脚本可以帮助你测试任何文件,并识别它是否被篡改。

# 导入hashlib模块,用于加密哈希功能。
import hashlib
# 导入os模块,用于操作文件和目录。
import os

# 定义函数calculate_sha256,计算文件的SHA-256哈希值。
def calculate_sha256(file_path):
    sha256 = hashlib.sha256()  # 创建一个sha256哈希对象
    # 以二进制读取模式打开文件
    with open(file_path, 'rb') as file:
        # 读取文件内容,每次读取4096字节
        for chunk in iter(lambda: file.read(4096), b''):
            sha256.update(chunk)  # 更新哈希对象的状态
    return sha256.hexdigest()  # 返回哈希值的十六进制表示

# 定义函数check_integrity,验证文件的完整性。
def check_integrity(file_path, expected_checksum):
    actual_checksum = calculate_sha256(file_path)  # 计算文件的实际SHA-256哈希值
    return actual_checksum == expected_checksum  # 比较实际哈希值与预期哈希值是否相同

# 如果此脚本被直接运行。
if __name__ == "__main__":
    file_path = input("Enter the path to the file: ")  # 获取用户输入的文件路径
    expected_checksum = input("Enter the expected SHA-256 checksum: ")  # 获取用户输入的预期SHA-256哈希值
    
    # 检查文件是否存在
    if os.path.isfile(file_path):
        # 调用check_integrity函数验证文件完整性
        if check_integrity(file_path, expected_checksum):
            print("File integrity verified: The file has not been tampered with.")  # 验证成功
        else:
            print("File integrity check failed: The file may have been tampered with.")  # 验证失败
    else:
        print("Error: File not found.")  # 文件不存在错误提示

4、智能交易 这个自动化脚本可以帮助交易者和投资者很好地了解你想投资的任何股票。它使用 Python 的 Prophet 库,基于从雅虎财经获取的历史股票数据来预测近期的股票价格。

# 导入Streamlit库用于创建Web应用
import streamlit as st
# 导入date用于处理日期
from datetime import date
# 导入yfinance库,用于从Yahoo Finance获取股票数据
import yfinance as yf
# 导入Prophet库,用于进行时间序列预测
from prophet import Prophet
# 导入Prophet的Plotly绘图接口和Plotly图形对象
from prophet.plot import plot_plotly
from plotly import graph_objs as go

# 设置预测的起始日期和当前日期
START = "2015-01-01"
TODAY = date.today().strftime("%Y-%m-%d")

# 使用Streamlit创建应用标题
st.title('Stock Forecast App')

# 设置一个选择框,让用户选择股票
stocks = ('MSFT', "TSLA", 'GOOG', 'AAPL', "NVDA")
selected_stock = st.selectbox('Select dataset for prediction', stocks)

# 创建一个滑动条,让用户选择预测的年数,范围从1到4年
n_years = st.slider('Years of prediction:', 1, 4)
period = n_years * 365  # 将年数转换为天数

# 定义一个缓存函数,用于加载股票数据,避免每次会话重新加载
@st.cache
def load_data(ticker):
    data = yf.download(ticker, START, TODAY)  # 从Yahoo Finance下载股票数据
    data.reset_index(inplace=True)  # 重置DataFrame的索引
    return data

# 显示数据加载状态
data_load_state = st.text('Loading data...')
data = load_data(selected_stock)  # 调用函数加载数据
data_load_state.text('Loading data... done!')  # 更新加载状态

# 显示原始数据的最后几行
st.subheader('Raw data')
st.write(data.tail())

# 定义函数,绘制原始数据的开盘和收盘价格
def plot_raw_data():
    fig = go.Figure()
    fig.add_trace(go.Scatter(x=data['Date'], y=data['Open'], name="stock_open"))
    fig.add_trace(go.Scatter(x=data['Date'], y=data['Close'], name="stock_close"))
    fig.layout.update(title_text='Time Series data with Rangeslider', xaxis_rangeslider_visible=True)
    st.plotly_chart(fig)

plot_raw_data()

# 使用Prophet进行预测
df_train = data[['Date', 'Close']]
df_train = df_train.rename(columns={"Date": "ds", "Close": "y"})
m = Prophet()
m.fit(df_train)
future = m.make_future_dataframe(periods=period)
forecast = m.predict(future)

# 显示并绘制预测数据
st.subheader('Forecast data')
st.write(forecast.tail())
st.write(f'Forecast plot for {n_years} years')
fig1 = plot_plotly(m, forecast)
st.plotly_chart(fig1)

# 显示预测组件
st.write("Forecast components")
fig2 = m.plot_components(forecast)
st.write(fig2)
加载中...
此文章数据所有权由区块链加密技术和智能合约保障仅归创作者所有。