文件中的无服务器参考变量不能使用两次

时间:2018-07-20 07:05:57

标签: serverless-framework

嗨,我正在为我的所有功能实现自定义CORS,我使用了一个外部文件来定义CORS对象。

basic
in method (<__main__.A object at 0x00000000029DF320>,)
a:  {'instance_attr': 9}
b:  {'instance_attr': 5}
a:  <bound method A.printf of <__main__.A object at 0x00000000029DF320>>
b:  <bound method A.printf of <__main__.A object at 0x00000000029DF390>>
class dict lookup:  <function A.printf at 0x00000000029F0730>

change class lookup
in function (<__main__.A object at 0x00000000029DF320>,)
a:  {'instance_attr': 9}
b:  {'instance_attr': 5}
a:  <bound method printff of <__main__.A object at 0x00000000029DF320>>
b:  <bound method printff of <__main__.A object at 0x00000000029DF390>>
class dict lookup:  <function printff at 0x00000000029E60D0>

change instance lookup
in function ()
a:  {'instance_attr': 9, 'printf': <function printff at 0x00000000029E60D0>}
b:  {'instance_attr': 5}
a:  <function printff at 0x00000000029E60D0>
b:  <bound method printff of <__main__.A object at 0x00000000029DF390>>
class dict lookup:  <function printff at 0x00000000029E60D0>

revert class lookup
in function ()
a:  {'instance_attr': 9, 'printf': <function printff at 0x00000000029E60D0>}
b:  {'instance_attr': 5}
a:  <function printff at 0x00000000029E60D0>
b:  <bound method A.printf of <__main__.A object at 0x00000000029DF390>>
class dict lookup:  <function A.printf at 0x00000000029F0730>

第一个引用正常,但随后的引用在我使用from sklearn.preprocessing import PolynomialFeatures from sklearn import metrics from sklearn.metrics import mean_squared_error from math import sqrt from sklearn.linear_model import LinearRegression poly_reg = PolynomialFeatures(3) lin_reg_2 = LinearRegression() iv_poly = poly_reg.fit_transform(X_train) poly_reg.fit(iv_poly, y_train) lin_reg_2.fit(iv_poly, y_train) predictValues = lin_reg_2.predict(X_test) print("RMSE is:-") print(np.sqrt(metrics.mean_squared_error(y_test, predictValues))) 时具有 ValueError Traceback (most recent call last) <ipython-input-4-a77f27b571a6> in <module>() 106 107 --> 108 predictValues = lin_reg_2.predict(X_test) 109 print("RMSE is:-") 110 print(np.sqrt(metrics.mean_squared_error(y_test, predictValues))) ~\Anaconda3\lib\site-packages\sklearn\linear_model\base.py in predict(self, X) 254 Returns predicted values. 255 """ --> 256 return self._decision_function(X) 257 258 _preprocess_data = staticmethod(_preprocess_data) ~\Anaconda3\lib\site-packages\sklearn\linear_model\base.py in _decision_function(self, X) 239 X = check_array(X, accept_sparse=['csr', 'csc', 'coo']) 240 return safe_sparse_dot(X, self.coef_.T, --> 241 dense_output=True) + self.intercept_ 242 243 def predict(self, X): ~\Anaconda3\lib\site-packages\sklearn\utils\extmath.py in safe_sparse_dot(a, b, dense_output) 138 return ret 139 else: --> 140 return np.dot(a, b) 141 142 ValueError: shapes (18,17) and (1140,1) not aligned: 17 (dim 1) != 1140 (dim 0)

这导致部署失败

这是一个已知问题吗?我该如何克服?

编辑

这是serverless-cors.yml的内容

service: user

provider:
  name: aws
  runtime: go1.x
  region: us-east-1

functions:
  create:
    runtime: go1.x
    handler: create/bin/main
    package:
      include:
        - ./create/bin/**
    events:
      - http:
          path: user
          method: post
          cors: ${file(../../serverless-cors.yml):custom.cors}
  list:
    runtime: go1.x
    handler: list/bin/main
    package:
      include:
        - ./list/bin/**
    events:
      - http:
          path: user
          method: get
          cors: ${file(../../serverless-cors.yml):custom.cors}
  show:
    runtime: go1.x
    handler: show/bin/main
    package:
      include:
        - ./show/bin/**
    events:
      - http:
          path: user/{user-id}
          method: get
          cors: ${file(../../serverless-cors.yml):custom.cors}
  update:
    runtime: go1.x
    handler: update/bin/main
    package:
      include:
        - ./update/bin/**
    events:
      - http:
          path: user/{user-id}
          method: post
          cors: ${file(../../serverless-cors.yml):custom.cors}
  delete:
    runtime: go1.x
    handler: delete/bin/main
    package:
      include:
        - ./delete/bin/**
    events:
      - http:
          path: user/{user-id}
          method: delete
          cors: ${file(../../serverless-cors.yml):custom.cors}

我还尝试使用单级(sls print)变量,而不是像上面那样没有运气的两个级别

1 个答案:

答案 0 :(得分:0)

尝试从serverless-cors.yml文件所在的目录引用相对于serverless.yml文件。例如:

serverless.yml
path/
├──to/
└──└──serverless-cors.yml

# serverless-cors.yml
cors:
  origin: '*'
  headers:
    - Content-Type
    - X-Amz-Date
    - Authorization
    - X-Api-Key
    - X-Amz-Security-Token
    - X-Amz-User-Agent
    - TZ
  allowCredentials: false

# serverless.yml
...
events:
  - http:
      path: something
      method: post
      cors: ${file(path/to/serverless-cors.yml):cors}