我写了一个装饰器,试图检查我们是否有Flask POST路线的发布数据:
这是我的装饰者:
def require_post_data(required_fields=None):
def decorator(f):
@wraps(f)
def decorated_function(*args, **kwargs):
for required_field in required_fields:
if not request.form.get(required_field, None):
return jsonify({"error": "Missing %s from post data." %
required_field}), 400
else:
if not request.form:
return jsonify({"error": "No post data, aborting."}), 400
return f(*args, **kwargs)
return decorated_function
return decorator
Run Code Online (Sandbox Code Playgroud)
我有两条路线,一个是URL参数,另一条没有:
from flask import Blueprint, jsonify, request
mod = Blueprint('contacts', __name__, url_prefix='/contacts')
@mod.route('/', methods=['POST'])
@require_post_data(['customer_id', 'some_other_required_field'])
def create_contact():
# Do some business
@mod.route('/<int:contact_id>', methods=['POST'])
@require_post_data
def update_contact(contact_id):
# Do some business
Run Code Online (Sandbox Code Playgroud)
当我运行一个命中的测试时update_contact …
投资组合方差计算如下:
port_var = W'_p * S * W_p
Run Code Online (Sandbox Code Playgroud)
对于具有 N 资产的投资组合,其中
W'_p = transpose of vector of weights of stocks in portfolios
S = sample covariance matrix
W_p = vector of weights of stocks in portfolios
Run Code Online (Sandbox Code Playgroud)
我有以下 numpy 矩阵。
投资组合中股票权重的数组(向量)(有 10 只股票):
weights = np.array(
[[ 0.09],
[ 0.05],
[ 0.15],
[ 0.10],
[ 0.15],
[ 0.15],
[ 0.08],
[ 0.08],
[ 0.1 ],
[ 0.05]])
Run Code Online (Sandbox Code Playgroud)
股票收益的协方差矩阵:
covar = np.array([[ 0.00154474 0.00079555 0.00099691 0.00052596 0.0005363 0.00062005
0.00064031 0.00037494 0.00018826 0.00132809], …
Run Code Online (Sandbox Code Playgroud) 我有一个pandas.DataFrame
包含numpy.nan
花车的.但是,在使用Excel编写器时,应该有numpy.nan
浮点数的字段是空白的.我希望至少有一个字符串表示而不是任何东西.
有关为什么会这样的想法吗?
作家代码如下:
writer=pandas.ExcelWriter('output.xls')
frame.to_excel(writer,'tab name')
writer.save()
Run Code Online (Sandbox Code Playgroud)
在哪里frame
看起来像这样(注意2013年1月1日的NaN):
Series ID Risk Bucket Contract PX Last Contract Value (Local) Currency X Contract Value (USD) Currency
2013-01-01 Future_ES EQ ES1 Index NaN NaN 1 NaN USD Curncy
2013-01-02 Future_ES EQ ES1 Index 1447.16 72362.5 1 72362.5 USD Curncy
2013-01-03 Future_ES EQ ES1 Index 1443.68 72187.5 1 72187.5 USD Curncy
2013-01-04 Future_ES EQ ES1 Index 1447.90 72400.0 1 72400.0 USD Curncy
Run Code Online (Sandbox Code Playgroud)
但是Excel文件有空白(参见附图).
我想bfill
和ffill
一个DataFrame
包含NaN
s(在这种情况下是ImpVol
字段)的多索引使用该interpolate
方法.的A部分DataFrame
可能是这样的:
Expiration OptionType Strike ImpVol
2014-12-26 call 140.0 NaN
145.0 NaN
147.0 NaN
149.0 NaN
150.0 NaN
152.5 NaN
155.0 0.233631
157.5 0.206149
160.0 0.149118
162.5 0.110867
165.0 0.110047
167.5 NaN
170.0 NaN
172.5 NaN
175.0 NaN
177.5 NaN
180.0 NaN
187.5 NaN
192.5 NaN
put 132.0 NaN
135.0 NaN
140.0 NaN
141.0 NaN
142.0 0.541311
143.0 NaN
144.0 0.546672
145.0 0.504691
146.0 0.485586
147.0 0.426898 …
Run Code Online (Sandbox Code Playgroud) 我正在尝试通过 ssh 访问我的 Google Cloud 虚拟机,但它超时了。我的尝试通过腻子和gcloud
命令行界面都失败了。
这是我可以确认的:
default
包含0.0.0.0/0
端口上(所有源)入站防火墙规则的网络tcp:22
gcloud
命令行使用这些说明手动尝试),通过 Compute Engine -> Metadata -> SSH Keys 确认(也在实例本身内创建了密钥)europe-west1-b
,我在亚洲,因此可能存在合法的延迟问题,但我能够通过 Cloud Shell 进行连接关于可能导致这种情况的任何想法?
我有一个简单的.dockerignore
文件,其中包含以下内容:
.git/
.idea/
venv/
Run Code Online (Sandbox Code Playgroud)
我的docker-compose.yml
文件安装了该卷:
version: "3"
services:
frontend:
build: .
command: ["gunicorn", "--bind", "0.0.0.0:8000", "project.app:create_app()"]
env_file:
- .env
volumes:
- .:/frontend
ports:
- "8000:8000"
Run Code Online (Sandbox Code Playgroud)
也许我不理解该.dockerignore
文件的完整语法或意图,但运行后docker-compose up --build
,.git/
,.idea/
和venv/
最终出现在我的容器中。
我已经阅读并看到了这一点,但无法挂载并阻止文件和目录进入容器似乎并不可行。
如何防止这些目录在容器中变得可用?
我修改了PHP中的函数以返回颜色渐变(http://www.herethere.net/~samson/php/color_gradient/color_gradient_generator.php.txt).返回以0结尾的颜色十六进制代码时出现问题.这是功能:
def _get_color(current_step=0, start='000000', end='ffffff', max_steps=16):
'''
Returns the color code for current_step between start and end
'''
start = '{0:#x}'.format(int(start, 16))
end = '{0:#x}'.format(int(end, 16))
if int(max_steps) > 0 & int(max_steps) < 256:
max_steps = max_steps
else:
max_steps = 16
r0 = (int(start, 16) & 0xff0000) >> 16
g0 = (int(start, 16) & 0x00ff00) >> 8
b0 = (int(start, 16) & 0x0000ff) >> 0
r1 = (int(end, 16) & 0xff0000) >> 16
g1 = (int(end, 16) & 0x00ff00) >> …
Run Code Online (Sandbox Code Playgroud) Scipy版本0.10.0
考虑以下:
>>> import math
>>> from scipy.optimize import fsolve
>>> import numpy as np
>>> def p(s, l, k, q):
p = q * np.maximum(s - k, 0.0)
return (p + math.copysign(l, -q)) * math.fabs(q) * 100.0
>>> x0 = fsolve(p, np.arange(33.86, 50.86, 1.0), args=(1.42, 41.0, -1.0), xtol=1e-06, maxfev=500)
Warning (from warnings module):
File "C:\Python27\lib\site-packages\scipy\optimize\minpack.py", line 152
warnings.warn(msg, RuntimeWarning)
RuntimeWarning: The iteration is not making good progress, as measured by the
improvement from the last ten iterations.
>>> print …
Run Code Online (Sandbox Code Playgroud) 真的需要你的帮助来理解,我做错了什么.
我的实验的目的是以编程方式运行spark作业,而不是使用./spark-shell或./spark-submit(这些都适用于我)
环境:我使用./spark-ec2脚本创建了一个包含1个master和1个worker的Spark Cluster
但是,当我尝试运行打包在jar中的代码时,群集看起来很好:
val logFile = "file:///root/spark/bin/README.md"
val conf = new SparkConf()
conf.setAppName("Simple App")
conf.setJars(List("file:///root/spark/bin/hello-apache-spark_2.10-1.0.0-SNAPSHOT.jar"))
conf.setMaster("spark://ec2-54-89-51-36.compute-1.amazonaws.com:7077")
val sc = new SparkContext(conf)
val logData = sc.textFile(logFile, 2).cache()
val numAs = logData.filter(_.contains("a")).count()
val numBs = logData.filter(_.contains("b")).count()
println(s"1. Lines with a: $numAs, Lines with b: $numBs")
Run Code Online (Sandbox Code Playgroud)
我得到一个例外:
*[info] Running com.paycasso.SimpleApp
14/09/05 14:50:29 INFO SecurityManager: Using Spark's default log4j profile: org/apache/spark/log4j-defaults.properties
14/09/05 14:50:29 INFO SecurityManager: Changing view acls to: root
14/09/05 14:50:29 INFO SecurityManager: SecurityManager: authentication disabled; ui acls disabled; users with …
Run Code Online (Sandbox Code Playgroud) 用户按下form
页面上的按钮:
<form id="form">
<input type="button" id="export" value="Export"/>
</form>
Run Code Online (Sandbox Code Playgroud)
单击该按钮后,将进行以下Ajax调用:
ajaxCall('/export', {}, callback_export, 'get');
Run Code Online (Sandbox Code Playgroud)
哪里
function ajaxCall(url, params, callback, type) {
if (validate()) {
var request;
request = $.ajax({
url: url,
type: type,
data: params
});
}
request.done(function (response, textStatus, jqXHR){
callback(response);
});
}
Run Code Online (Sandbox Code Playgroud)
Flask应用程序看起来像这样:
@app.route('/export')
def export():
xl_file= '/absolute/path/to/excel/file.xlsx'
return send_file(xl_file, as_attachment=True, mimetype='application/vnd.ms-excel')
Run Code Online (Sandbox Code Playgroud)
文件的文件内容将返回到浏览器(请参见下图),但不会将文件本身作为附件返回.
问题是,回调需要什么来接受作为文件附件的响应?或者,需要进行哪些修改?
(是的,我搜索并阅读了SE上的许多帖子.大多数讨论使用该form.submit()
方法但不提供详细信息.我希望避免使用,form.submit()
因为其中的其他元素#form
无法提交.)
python ×7
numpy ×3
flask ×2
pandas ×2
ajax ×1
amazon-ec2 ×1
apache-spark ×1
arrays ×1
docker ×1
excel ×1
gcloud ×1
hex ×1
javascript ×1
optimization ×1
portfolio ×1
scala ×1
scipy ×1
ssh ×1
string ×1
variance ×1