如何从UserInput获取URL内容

时间:2019-03-24 22:37:17

标签: java

我正在尝试从用户输入中获取网址内容

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from pykalman import KalmanFilter

if __name__ == "__main__":
    file_name = 'KalmanExample.txt'
    df = pd.read_csv(file_name, index_col = 0)
    prices = df[['ETF', 'ASSET_1', 'ASSET_2']]
    delta = 1e-3
    trans_cov = delta / (1 - delta) * np.eye(3)
    obs_mat = np.vstack( [prices['ASSET_1'], prices['ASSET_2'],  
                          np.ones(prices['ASSET_1'].shape)]).T[:, np.newaxis]
    kf = KalmanFilter(
        n_dim_obs=1,
        n_dim_state=3,
        initial_state_mean=np.zeros(3),
        initial_state_covariance=np.ones((3, 3)),
        transition_matrices=np.eye(3),
        observation_matrices=obs_mat,
        observation_covariance=1.0,
        transition_covariance=trans_cov        
    )

    # state_means, state_covs = kf.em(prices['ETF'].values).smooth(prices['ETF'].values)
    state_means, state_covs = kf.filter(prices['ETF'].values)


    # Re-construct ETF from coefficients and 'ASSET_1' and ASSET_2 values:
    ETF_est = np.array([a.dot(b) for a, b in zip(np.squeeze(obs_mat), state_means)])

    # Draw slope and intercept...
    pd.DataFrame(
        dict(
            slope1=state_means[:, 0],
            slope2=state_means[:, 1],
            intercept=state_means[:, 2],
        ), index=prices.index
    ).plot(subplots=True)
    plt.show()

    # Draw actual y, and estimated y:
    pd.DataFrame(
        dict(
            ETF_est=ETF_est,
            ETF_act=prices['ETF'].values
        ), index=prices.index
    ).plot()
    plt.show()

1 个答案:

答案 0 :(得分:1)

您应该在用户输入URL时验证该URL,而不是在尝试建立连接时进行验证。您的URL方法仅负责一件事,而不验证数据。

相反,您应该创建一种方法,该方法可以验证用户输入的数据。这是一个示例

envsubst '$HOME:$MY_HOME' < ~/.tmpl_redis.conf >  ~/.redis.conf && redis-server ~/.redis.conf
# or
envsubst '$MY_HOME' < ~/.tmpl_redis.conf >  ~/.redis.conf && redis-server !#:5 # 5th argument from the previous command

下面我添加了一个布尔逻辑的工作示例,您可以用来验证扫描仪输入

public static boolean isValidUrl(String url, String regexPattern){
    Pattern patt = Pattern.compile(regexPattern);
    Matcher matcher = patt.matcher(url);
    return matcher.matches();
   }

    public static void main(String[] args) {
        String url = "https://youtube.com";
        String urlRegex =  "\\b(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]";
        System.out.println("Url is valid: " + isValidUrl(url, urlRegex)); //URL is valid, and it will print true
    }